Skip to content

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

@Artikae

Description

@Artikae

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 [email protected] in this Semantics.
Make sure to use only query nodes, derived from this instance of Semantics.
root node:   [email protected]
known nodes: [email protected]


  Code: -32603

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions