Skip to content

Panic: used an incorrect AttrId during completion in expand_speculative #21248

@sjrsjz

Description

@sjrsjz

Problem
I encountered a panic in rust-analyzer while triggering code completion (textDocument/completion).
The error message is used an incorrect AttrId; crate=Crate(Id(4c3a)), attr_id=AttrId { id: 0 }.

It seems to crash inside hir_expand::attrs::AttrId::find_attr_range_with_source, likely when speculative expansion involves certain macros or attributes.

I tried to create a minimal reproduction, but failed to isolate the issue because it seems to depend on the specific state of my project (likely related to incremental compilation cache or complex macro nesting).

Rust Analyzer version
0.3.2711-standalone (5e3e9c4e61 2025-12-07)

Platform
Windows

Editor
VS Code

Log / Stack Trace

thread 'Worker0' (39892) panicked at crates\hir-expand\src\attrs.rs:583:17:
used an incorrect `AttrId`; crate=Crate(Id(4c3a)), attr_id=AttrId { id: 0 }
stack backtrace:
   0: std::panicking::panic_handler
             at /rustc/ed61e7d7e242494fb7057f2657300d9e77bb4fcb/library\std\src\panicking.rs:698
   1: core::panicking::panic_fmt
             at /rustc/ed61e7d7e242494fb7057f2657300d9e77bb4fcb/library\core\src\panicking.rs:75
   2: hir_expand::attrs::AttrId::find_attr_range_with_source
   3: hir_expand::db::expand_speculative
   4: hir::semantics::SemanticsImpl::speculative_expand_attr_macro
   5: ide_completion::context::analysis::expand_and_analyze
   6: ide_completion::context::analysis::expand_maybe_stop
   7: ide_completion::context::analysis::expand_and_analyze
   8: ide_completion::completions
   9: salsa::cancelled::Cancelled::catch
  10: std::thread::local::LocalKey<T>::with
  11: ide::Analysis::completions
  12: rust_analyzer::handlers::request::handle_completion
  13: <[T] as core::fmt::Debug>::fmt

Additional Context
The request failed with:

[Error - 21:30:21] Request textDocument/completion failed.
  Message: request handler panicked: used an incorrect `AttrId`; crate=Crate(Id(4c3a)), attr_id=AttrId { id: 0 }
  Code: -32603 

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions