Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
cwfitzgerald opened this issue Aug 11, 2019 · 7 comments · Fixed by #63717
Closed
Labels
A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-resolve Area: Name/path resolution done by `rustc_resolve` specifically C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@cwfitzgerald
Copy link

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

@jonas-schievink jonas-schievink added A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-resolve Area: Name/path resolution done by `rustc_resolve` specifically C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 11, 2019
@nrc
Copy link
Member

nrc commented Aug 12, 2019

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

@tesuji
Copy link
Contributor

tesuji commented Aug 18, 2019

cc @petrochenkov for the hygiene macro in #63535

@tesuji

This comment has been minimized.

@petrochenkov
Copy link
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).

@tesuji
Copy link
Contributor

tesuji commented Aug 18, 2019

Thanks, I filed it in #63685

@petrochenkov
Copy link
Contributor

Fixed in #63717.

Centril added a commit to Centril/rust that referenced this issue Aug 21, 2019
Fix nested eager expansions in arguments of `format_args`

Fixes rust-lang#63460
Fixes rust-lang#63685 (regression from making `format_args` opaque - rust-lang#63114)

r? @matthewjasper
@cwfitzgerald
Copy link
Author

Thanks for your work on this!

petrochenkov added a commit to petrochenkov/rust that referenced this issue Aug 24, 2019
pietroalbini pushed a commit to pietroalbini/rust that referenced this issue Sep 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-resolve Area: Name/path resolution done by `rustc_resolve` specifically C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants