Skip to content

RA sometimes panics and raises an error incorrectly for closure -> fn pointer coercion #22497

@ElliottjPierce

Description

@ElliottjPierce

I'm encountering an issue where RA falsely claims that closure to fn pointer coercion is not allowed.
Ex: "expected unsafe fn(ErasedUninitRef<'_>), found {closure#591559}".
Further, RA appears to be panicking when this happens.

Other issues may be related, but I couldn't find one.
If I missed one, hopefully another data point helps.

Here's the log:

thread 'Worker6' (20744104) panicked at /Users/runner/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/rowan-0.15.18/src/cursor.rs:755:9:
Bad offset: range 17053..17071 offset 17036
stack backtrace:
   0: __rustc::rust_begin_unwind
   1: core::panicking::panic_fmt
   2: rowan::cursor::SyntaxNode::token_at_offset
   3: ide_assists::handlers::extract_variable::extract_token_range_of
   4: ide_assists::handlers::extract_variable::extract_variable
   5: ide_assists::assists
   6: salsa::cancelled::Cancelled::catch
   7: std::thread::local::LocalKey<T>::with
   8: ide::Analysis::assists_with_fixes
   9: rust_analyzer::handlers::request::handle_code_action
  10: core::ops::function::FnOnce::call_once{{vtable.shim}}
  11: <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
additional context:
   0:
version: 0.3.2913-standalone (de5824b6d0 2026-05-24)
request: textDocument/codeAction CodeActionParams {
    text_document: TextDocumentIdentifier {
        uri: Url {
            scheme: "file",
            cannot_be_a_base: false,
            username: "",
            password: None,
            host: None,
            port: None,
            path: "path/to/lib/macros/src/lib.rs",
            query: None,
            fragment: None,
        },
    },
    range: Range {
        start: Position {
            line: 385,
            character: 64,
        },
        end: Position {
            line: 385,
            character: 79,
        },
    },
    context: CodeActionContext {
        diagnostics: [],
        only: None,
        trigger_kind: None,
    },
    work_done_progress_params: WorkDoneProgressParams {
        work_done_token: None,
    },
    partial_result_params: PartialResultParams {
        partial_result_token: None,
    },
}

For additional context, I've had this issue with rust 1.95 and 1.96. The issue may have existed before then; it only breaks when I open the offending file, and this is just when I noticed it. I am using the Zed editor primarily, but this also happens in VS Code. Both cargo check and cargo build across various profiles, etc work. It's only RA that fails here.

The offending code looks something like this:

pub enum Foo {
    A,
    B,
}

pub trait Initializer {
    const INITIALIZER: unsafe fn(*mut u8);
}

impl Initializer for Foo {
    const INITIALIZER: unsafe fn(*mut u8) = const {
        let func: fn(*mut u8) = |dst| unsafe {
            dst.cast::<Self>().write(Self::A);
        };
        func
    };
}

Interestingly, RA doesn't break with just this code, but does in my case, the only major difference being that the implementation is coming from a derive macro. I was not able to reproduce the whole error in a separate example (and not for lack of trying), but I was able to verify that the panic was coming from the derive macro file. Why RA reports it as an error is still a mystery to me. I suspect it has something to do with how the closure captures the Self type. This is allowed, (It's captured variables that aren't.) but maybe RA doesn't see that when it's coming from complex derive macros?

Here's a zip of the min reproduction I made for the panic: reproduce_ra_false_positive.zip. If there's anything you'd like me to try to help reproduce it or investigate, I'm happy to help. Thanks for looking into this!

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: bug

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions