Skip to content

Can't unit test procedural macros #110247

Closed
Closed
@TadaHrd

Description

@TadaHrd

I have this test:

mod tests {
    use crate::str2code;
    #[test]
    fn test() {
        str2code!(r#"println!("Hello, world!");"#);
    }
}

The str2code macro is a procedural macro that I made.

#[proc_macro]
pub fn str2code(input: TokenStream) -> TokenStream

It gives this error:

error: can't use a procedural macro from the same crate that defines it
  --> src\lib.rs:31:9
   |
31 |         str2code!(r#"println!("Hello, world!");"#);
   |         ^^^^^^^^

Why does the compiler not let me test my macros and why does this exist?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-libtestArea: `#[test]` / the `test` libraryA-proc-macrosArea: Procedural macros

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions