Skip to content

Re-exported concat macro causes "failed to resolve macro" error #10300

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

Closed
NicholasGorski opened this issue Sep 21, 2021 · 6 comments · Fixed by #15959
Closed

Re-exported concat macro causes "failed to resolve macro" error #10300

NicholasGorski opened this issue Sep 21, 2021 · 6 comments · Fixed by #15959
Assignees
Labels
A-macro macro expansion A-nameres name, path and module resolution C-bug Category: bug S-actionable Someone could pick this issue up and work on it right now

Comments

@NicholasGorski
Copy link

Minimal example, reduced from a larger macro:

pub mod private {
    pub use core::concat;
}

macro_rules! demo {
    ($fmt:expr $(, $($args:tt)*)?) => {{
        // The use of `$crate::private::concat` here causes an error,
        // but unqualified `concat!` does not. They ought to be identical?
        __impl_demo!($crate::private::concat!(": ", $fmt), $($($args)*)?)
    }};
}

macro_rules! __impl_demo {
    ($fmt:expr $(, $($args:tt)*)?) => {{
        // This one doesn't care about which concat is used.
        //
        // However, the use of panic! seems to be necessary; for example,
        // using `println!` works as expected. Weirder, if instead one does
        // `assert!(true, ...)` then *both* expansions below give the error.
        panic!(concat!("prefix", $fmt), $($($args)*)?);
    }};
}

pub fn foo(i: u32) {
    if i == 0 {
        demo!("{}", 1); // A
    }

    if i == 1 {
        demo!(""); // B
    }
}

As is, this is valid code that compiles. However, rust analyzer emits "failed to resolve macro" at expansion B.

Furthermore, as noted one can use println! and neither expansion has an error, or switch to assert! and get an error at both expansions. In all cases there is no actual error during rustc compilation.

(The use of re-exported concat in the private module is to avoid call-site environment pollution in the macro expansion.)

Let me know if this is unclear or if I've made an error along the way. Thanks!

NicholasGorski added a commit to NicholasGorski/assume that referenced this issue Sep 21, 2021
Mostly this is to avoid rust-lang/rust-analyzer#10300, though the result is overall better anyway.
@Veykril Veykril added the A-macro macro expansion label Sep 21, 2021
@jonas-schievink jonas-schievink added the A-nameres name, path and module resolution label Sep 21, 2021
@lnicola lnicola added C-bug Category: bug S-actionable Someone could pick this issue up and work on it right now labels Sep 21, 2021
@jonas-schievink
Copy link
Contributor

Reduces to:

mod private {
    pub use core::concat;
}

macro_rules! m {
    () => {
        panic!(concat!($crate::private::concat!("")));
    };
}

fn f() {
    m!();
}

Might be caused by a limitation in our $crate handling (perhaps we're losing information about the origin crate after passing through panic?)

@xffxff
Copy link
Contributor

xffxff commented Nov 15, 2021

Can't reproduce it.
RA version: 73668334f05c3446b04116ccc3156240d2d8ab19 with cargo xtask install
image

@NicholasGorski
Copy link
Author

Hm, strange. I am using v0.2.817 (~1 hour ago at time of writing) and still reproducing the issue. Am I incorrectly updated?

@jonas-schievink
Copy link
Contributor

No, I can still repro with both snippets in this thread

@jonas-schievink

This comment has been minimized.

@jonas-schievink

This comment has been minimized.

bors bot added a commit that referenced this issue Jan 27, 2022
11354: fix: More correct `$crate` handling in eager macros r=jonas-schievink a=jonas-schievink

Fixes a few of the additional bugs in #10300, but not yet that issue itself.

bors r+

Co-authored-by: Jonas Schievink <[email protected]>
@Veykril Veykril self-assigned this Nov 24, 2023
@bors bors closed this as completed in e91fdf7 Dec 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-macro macro expansion A-nameres name, path and module resolution C-bug Category: bug S-actionable Someone could pick this issue up and work on it right now
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants