Skip to content

Recursive macro call inside concat macro fails to find macros referred to by $crate #63460

Closed
@cwfitzgerald

Description

@cwfitzgerald
#[macro_export]
macro_rules! separator {
    () => { "/" };
}

#[macro_export]
macro_rules! concat_separator {
    ( $e:literal, $($other:literal),+ ) => {
        concat!($e, $crate::separator!(), $crate::concat_separator!($($other),+))
    };
    ( $e:literal ) => {
        $e
    }
}

fn main() {
    println!("{}", concat_separator!(2, 3, 4))
}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
error[E0433]: failed to resolve: could not find `separator` in `$crate`
  --> src/main.rs:9:29
   |
9  |         concat!($e, $crate::separator!(), $crate::concat_separator!($($other),+))
   |                             ^^^^^^^^^ could not find `separator` in `$crate`
...
17 |     println!("{}", concat_separator!(2, 3, 4))
   |                    -------------------------- in this macro invocation

error[E0433]: failed to resolve: could not find `concat_separator` in `$crate`
  --> src/main.rs:9:51
   |
9  |         concat!($e, $crate::separator!(), $crate::concat_separator!($($other),+))
   |                                                   ^^^^^^^^^^^^^^^^ could not find `concat_separator` in `$crate`
...
17 |     println!("{}", concat_separator!(2, 3, 4))
   |                    -------------------------- in this macro invocation

If either the recursion (playground) or the concat! is removed (playground), this compiles exactly as expected. Additionally, if I explicitly name my crate, it works as expected, but would be vulnerable to crate renames in user code (can't reproduce in playground because it doesn't build a library).

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)A-resolveArea: Name/path resolution done by `rustc_resolve` specificallyC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions