-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Error expanding macro recursively at caret: $crate
cannot come from SyntaxContextId::ROOT
#18658
Comments
See #18163 for possibly more context. |
Is there a backtrace in the logs? |
Found it in the Output pane for the Rust Analyzer LSP:
Want me to get a full backtrace? Seems, though, it started in frame 3 or 4. |
@ChayimFriedman2 This is because there's a bug in expanding recursive attribute macro, breaking rust-analyzer/crates/ide/src/expand_macro.rs Lines 155 to 187 in 9b2e72c
When A bit more about my debugging process: I found that the rust-analyzer/crates/hir-expand/src/prettify_macro_expansion_.rs Lines 12 to 25 in 9b2e72c
rust-analyzer/crates/syntax-bridge/src/prettify_macro_expansion.rs Lines 61 to 64 in 9b2e72c
// in snippet 1
let mut it = span_map.iter();
// in snippet 2
assert_eq!(token.text_range().end(), it.next().unwrap().0) Adding a sanity check here reveals that the Here's a minimal reproducing snippet: // macros
#[proc_macro_attribute]
pub fn foo(args: TokenStream, input: TokenStream) -> TokenStream {
let func = parse_macro_input!(input as syn::ItemFn);
let msg = parse_macro_input!(args as syn::LitStr);
let ident = &func.sig.ident;
quote! {
#[bar]
fn #ident() {
log::info!(#msg);
log::info!(#msg);
log::info!(#msg);
log::info!(#msg);
}
}.into()
}
#[proc_macro_attribute]
pub fn bar(_args: TokenStream, input: TokenStream) -> TokenStream {
let func = parse_macro_input!(input as syn::ItemFn);
let ident = &func.sig.ident;
quote! {
fn #ident() {
panic!("bar");
panic!("bar");
panic!("bar");
panic!("bar");
panic!("bar");
panic!("bar");
}
}.into()
}
// invocation
extern crate log;
use reproduce::{foo, bar};
#[foo("foo")]
fn wrong_crate() {}
#[foo("fooooooooooooooooooooooooooooooooooooooooooooooo")]
fn would_panic() {}
// Recursive expansion of foo macro
// =================================
fn wrong_crate() {
{
core::panicking::panic_fmt(core::const_format_args!("bar"));
};
{
core::panicking::panic_fmt(core::const_format_args!("bar"));
};
{
core::panicking::panic_fmt(core::const_format_args!("bar"));
};
{
core::panicking::panic_fmt(core::const_format_args!("bar"));
};
{
core::panicking::panic_fmt(core::const_format_args!("bar"));
};
{
log::panicking::panic_fmt(log::const_format_args!("bar")); // <- `$crate` becomes `log`
};
}
|
When trying to expand a proc_macro, most often the rust-analyzer errs with:
What's odd is that it doesn't always happen. Sometimes I can rebuild dependencies in our workspace, restart the LSP server, and it'll work. I also don't use
$crate
myself, though#[tokio::test]
,assert_eq!()
, and some other macros may that end up getting expanded recursively.This most often happens when I make a change to my crate e.g., minor bug fix or small behavior change as I tweak what it does for this in-development feature, and then try to expand the macro. rust-analyzer is rebuilding dependencies, but I've suspected it's a cached build output issue somehow.
rust-analyzer version: (eg. output of "rust-analyzer: Show RA Version" command, accessible in VSCode via Ctrl/⌘+Shift+P)
0.3.2212-standalone
rustc version: (eg. output of
rustc -V
)rustc 1.80.0 (051478957 2024-07-21) (pinned via
rust-toolchain.toml
)editor or extension: (eg. VSCode, Vim, Emacs, etc. For VSCode users, specify your extension version; for users of other editors, provide the distribution if applicable)
VSCode rust-lang.rust-analyzer extension version v0.3.2212
relevant settings: (eg. client settings, or environment variables like
CARGO
,RUSTC
,RUSTUP_HOME
orCARGO_HOME
)Only a few relative things come to mind:
rust-toolchain.toml
pins channel to1.80.0
.vscode/settings.json
sets"rust-analyzer.cargo.features": "all"
and"rust-analyzer.check.command": "clippy"
.repository link (if public, optional): (eg. rust-analyzer)
code snippet to reproduce:
See links directly above.
The text was updated successfully, but these errors were encountered: