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

Procedural macro which expands into declarative macro causes a 'textDocument/documentHighlight' panic. #17592

Closed
Artikae opened this issue Jul 14, 2024 · 2 comments · Fixed by #18940
Labels
A-ide general IDE features C-bug Category: bug I-panic

Comments

@Artikae
Copy link

Artikae commented Jul 14, 2024

rust-analyzer version: 0.3.2029-standalone (a5b21ea0a 2024-07-07)

rustc version: rustc 1.77.0 (aedd173a2 2024-03-17)

editor or extension: VSCode. (extension version: v0.3.2029)

code snippet to reproduce:
Define a proc macro that expands into a declarative macro.

// Proc Macro Crate
use proc_macro::TokenStream;
use quote::quote;
use proc_macro2::TokenStream as TokenStream2;
#[proc_macro]
pub fn define_macro(input: TokenStream) -> TokenStream {
    let mut tokens = TokenStream2::from(input).into_iter();
    let Some(name) = tokens.next() else {panic!("no ident")};
    let result = quote!(
        macro_rules! #name {
            () => {()}
        }
    );
    result.into()
}
// Main Crate
use macro_hack::define_macro;
fn main() {
    define_macro!(macro_name); // not this one
    macro_name!(); // only here, and only the identifier, not the parentheses or the '!'
}

Moving the text cursor into the second macro_name causes a panic. The panic doesn't happen if the text cursor is moved directly from the first macro_name to the second.

The panic output:

request: textDocument/documentHighlight DocumentHighlightParams {
    text_document_position_params: TextDocumentPositionParams {
        text_document: TextDocumentIdentifier {
            uri: Url {
                scheme: "file",
                cannot_be_a_base: false,
                username: "",
                password: None,
                host: None,
                port: None,
                path: "/c%3A/Users/user/[...]/rust-analyzer-bug/src/main.rs",
                query: None,
                fragment: None,
            },
        },
        position: Position {
            line: 3,
            character: 4,
        },
    },
    work_done_progress_params: WorkDoneProgressParams {
        work_done_token: None,
    },
    partial_result_params: PartialResultParams {
        partial_result_token: None,
    },
}

thread 'Worker' panicked at crates\hir\src\semantics.rs:1554:13:
[...]
stack backtrace:
   0: std::panicking::begin_panic_handler
             at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library\std\src\panicking.rs:652
   1: core::panicking::panic_fmt
             at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library\core\src\panicking.rs:72
   2: hir::semantics::SemanticsImpl::find_file
   3: ide_db::defs::NameClass::classify
   4: ide_db::search::FindUsages::search
   5: hashbrown::raw::RawIterRange<T>::fold_impl
   6: <core::iter::adapters::map::Map<I,F> as core::iter::traits::iterator::Iterator>::fold
   7: <hashbrown::map::HashMap<K,V,S,A> as core::iter::traits::collect::Extend<(K,V)>>::extend
   8: std::panicking::try
   9: rust_analyzer::handlers::request::handle_document_highlight
  10: std::panicking::try
  11: core::ops::function::FnOnce::call_once{{vtable.shim}}
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
[Error - 8:43:12 PM] Request textDocument/documentHighlight failed.
  Message: request handler panicked: 

Failed to lookup MACRO_RULES@0..32 in this Semantics.
Make sure to use only query nodes, derived from this instance of Semantics.
root node:   MACRO_STMTS@0..32
known nodes: SOURCE_FILE@0..94


  Code: -32603
@Artikae Artikae added the C-bug Category: bug label Jul 14, 2024
@Veykril Veykril added A-ide general IDE features I-panic labels Jul 14, 2024
@Veykril
Copy link
Member

Veykril commented Jul 14, 2024

Sounds like we are mixing up file ids again, goes to show #17367

@ChayimFriedman2
Copy link
Contributor

Should be fixed by #18117.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ide general IDE features C-bug Category: bug I-panic
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants