最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

rust - Passing file name dynamically to a procedural macro as argument - Stack Overflow

programmeradmin4浏览0评论

I am working on a Rust macro that needs to accept the file name as a argument. Based on the file name I want to parse and do some operations and generate some code. I believe proc_macro in Rust should help me achieve my requirements. However, I am facing issues passing file!() as argument to the procedural macro. The idea is to get the file name via the file!() macro and pass it as argument to my procedural macro. This is my current implementation:

// lib.rs - proc_macro crate

#[proc_macro]
pub fn my_macro(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as LitStr);

    // perform operation on input string


    let expanded = quote! {
        #output_str
    };

    TokenStream::from(expanded)
}
fn main() {
    let dir_name =  my_macro!(file!());
    println!("Dir name: {:?}", dir_name);
}

With this approach, I am getting compiler error:

error: expected string literal
  --> src/main.rs
   |
12 |     let dir_name =  my_macro!(file!());
   |                               ^^^^

If I pass in the static strings, it works, for example:

let dir_name =  my_macro!("my_string");

I am not sure how to pass the file name dynamically to procedural macros. Is there any other way to achieve this? Any help/reference is appreciated.

I am working on a Rust macro that needs to accept the file name as a argument. Based on the file name I want to parse and do some operations and generate some code. I believe proc_macro in Rust should help me achieve my requirements. However, I am facing issues passing file!() as argument to the procedural macro. The idea is to get the file name via the file!() macro and pass it as argument to my procedural macro. This is my current implementation:

// lib.rs - proc_macro crate

#[proc_macro]
pub fn my_macro(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as LitStr);

    // perform operation on input string


    let expanded = quote! {
        #output_str
    };

    TokenStream::from(expanded)
}
fn main() {
    let dir_name =  my_macro!(file!());
    println!("Dir name: {:?}", dir_name);
}

With this approach, I am getting compiler error:

error: expected string literal
  --> src/main.rs
   |
12 |     let dir_name =  my_macro!(file!());
   |                               ^^^^

If I pass in the static strings, it works, for example:

let dir_name =  my_macro!("my_string");

I am not sure how to pass the file name dynamically to procedural macros. Is there any other way to achieve this? Any help/reference is appreciated.

Share Improve this question asked Mar 13 at 11:11 Abhijit NathwaniAbhijit Nathwani 4341 gold badge7 silver badges18 bronze badges 1
  • There is the macro-string crate that is designed to eagerly evaluate standard macro built strings. However, it does not appear to support file!(). – kmdreko Commented Mar 13 at 16:43
Add a comment  | 

1 Answer 1

Reset to default 1

You can't pass the result of a macro to another macro, because macros get evaluated outside-in.

However, if you just need to get the file in which your macro was invoked, you can just use proc_macro2::Span::source_file, taking Span from any of your input tokens.

发布评论

评论列表(0)

  1. 暂无评论