Skip to content

Invalid macro call causes syntax_bridge.rs to unwrap() on None #16529

Closed
@konnorandrews

Description

@konnorandrews

The following code (when placed at the root of a crate) causes rust analyzer to fully crash. All functionally of rust analyzer is lost until the macro call is fixed or removed. In which case rust analyzer immediately begins functioning again (most of the time).

I have no idea why this particular setup is what causes the crash. I found this while developing a macro in the wild and scaled it down to this. I have also had multiple people confirm this happens on other instances of rust analyzer and not just my machine.

mod any {
    #[macro_export]
    macro_rules! nameable {
        {
            struct $name:ident[$a:lifetime]
        } => {
            $crate::any::nameable! {
                struct $name[$a]
                a
            }
        };
        {
            struct $name:ident[$a:lifetime]
            a
        } => {};
    }
    pub use nameable;

    nameable! {
        Name['a]
    }
}

The above code does not compile, but rust analyzer should be reporting that the macro failed to be called like rustc does when run separately.
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=ed43d742a8607e61aa113fe6491a6fc6

The panic is (The top part is the context reported by neovim):

Panic context> 
version: 1.76.0 (07dca48 2024-02-04)
request: textDocument/semanticTokens/full/delta SemanticTokensDeltaParams {
    work_done_progress_params: WorkDoneProgressParams {
        work_done_token: None,
    },
    partial_result_params: PartialResultParams {
        partial_result_token: None,
    },
    text_document: TextDocumentIdentifier {
        uri: Url {
            scheme: "file",
            cannot_be_a_base: false,
            username: "",
            password: None,
            host: None,
            port: None,
            path: "/data/ra_crash/src/lib.rs",
            query: None,
            fragment: None,
        },
    },
    previous_result_id: "1",
}

thread 'Worker' panicked at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/src/tools/rust-analyzer/crates/mbe/src/syntax_bridge.rs:673:89:
called `Option::unwrap()` on a `None` value
stack backtrace:
    0: rust_begin_unwind
        at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/panicking.rs:645:5
    1: core::panicking::panic_fmt
        at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/panicking.rs:72:14
    2: core::panicking::panic
        at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/panicking.rs:144:5
    3: <mbe::syntax_bridge::SynToken<tt::SpanData<base_db::span::SpanAnchor, base_db::span::SyntaxContextId>> as mbe::syntax_bridge::SrcToken<mbe::syntax_bridge::Converter<mbe::token_map::SpanMap<tt::SpanData<base_db::span::SpanAnchor, base_db::span::SyntaxContextId>>, tt::SpanData<base_db::span::SpanAnchor, base_db::span::SyntaxContextId>>, tt::SpanData<base_db::span::SpanAnchor, base_db::span::SyntaxContextId>>>::kind
    4: mbe::syntax_bridge::convert_tokens::<tt::SpanData<base_db::span::SpanAnchor, base_db::span::SyntaxContextId>, mbe::syntax_bridge::Converter<hir_expand::span::SpanMapRef, tt::SpanData<base_db::span::SpanAnchor, base_db::span::SyntaxContextId>>>
    5: hir_expand::db::macro_arg
    ...

rust-analyzer version: 1.76.0 (07dca48 2024-02-04)

rustc version: rustc 1.76.0 (07dca489a 2024-02-04)

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions