Skip to content

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

Closed

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).

Activity

  1. added
    A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)
    A-resolveArea: Name/path resolution done by `rustc_resolve` specifically
    C-bugCategory: This is a bug.
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Aug 11, 2019
  2. nrc commented on Aug 12, 2019

    @nrc
    Member

    If you try and build Rustup, you get a similar failure, but without recursion or $crate. Seems that just using another macro and concat is enough in some circumstances

  3. tesuji commented on Aug 18, 2019

    @tesuji
    Contributor

    cc @petrochenkov for the hygiene macro in #63535

  4. tesuji commented on Aug 18, 2019

    @tesuji
  5. petrochenkov commented on Aug 18, 2019

    @petrochenkov
    Contributor

    @lzutao
    Please, file a separate issue for the regression, I'll prioritize it over pre-existing issues (which the original issue seems to be).

  6. tesuji commented on Aug 18, 2019

    @tesuji
    Contributor

    Thanks, I filed it in #63685

  7. petrochenkov commented on Aug 21, 2019

    @petrochenkov
    Contributor

    Fixed in #63717.

  8. added a commit that references this issue on Aug 21, 2019
  9. cwfitzgerald commented on Aug 21, 2019

    @cwfitzgerald
    Author

    Thanks for your work on this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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