Skip to content

rust-analyzer panics when applying "Implement missing members" quick fix: Can't find [email protected] in AstIdMap #17321

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

Closed
antonilol opened this issue May 31, 2024 · 1 comment · Fixed by #18555
Labels

Comments

@antonilol
Copy link

rust-analyzer version: rust-analyzer version: 0.4.1979-standalone

rustc version: rustc 1.80.0-nightly (9cdfe285c 2024-05-22)

editor or extension: code oss

relevant settings: None

code snippet to reproduce:

fn main() {}

mod other_file_1 {
    pub const SOME_CONSTANT: usize = 8;
}

mod other_file_2 {
    use crate::other_file_1::SOME_CONSTANT;

    pub trait Trait {
        type Iter: Iterator<Item = [u8; SOME_CONSTANT]>;
    }
}

pub struct MyStruct;

impl other_file_2::Trait for MyStruct {}
panic log
Panic context:
> 
version: 0.4.1979-standalone
request: codeAction/resolve CodeAction {
    title: "Implement missing members",
    group: None,
    kind: Some(
        CodeActionKind(
            "quickfix",
        ),
    ),
    command: None,
    edit: None,
    is_preferred: None,
    data: Some(
        CodeActionData {
            code_action_params: CodeActionParams {
                text_document: TextDocumentIdentifier {
                    uri: Url {
                        scheme: "file",
                        cannot_be_a_base: false,
                        username: "",
                        password: None,
                        host: None,
                        port: None,
                        path: "/home/antoni/code/rust-playground/src/main.rs",
                        query: None,
                        fragment: None,
                    },
                },
                range: Range {
                    start: Position {
                        line: 16,
                        character: 0,
                    },
                    end: Position {
                        line: 16,
                        character: 37,
                    },
                },
                context: CodeActionContext {
                    diagnostics: [
                        Diagnostic {
                            range: Range {
                                start: Position {
                                    line: 16,
                                    character: 5,
                                },
                                end: Position {
                                    line: 16,
                                    character: 24,
                                },
                            },
                            severity: Some(
                                Error,
                            ),
                            code: Some(
                                String(
                                    "E0046",
                                ),
                            ),
                            code_description: Some(
                                CodeDescription {
                                    href: Url {
                                        scheme: "https",
                                        cannot_be_a_base: false,
                                        username: "",
                                        password: None,
                                        host: Some(
                                            Domain(
                                                "doc.rust-lang.org",
                                            ),
                                        ),
                                        port: None,
                                        path: "/stable/error_codes/E0046.html",
                                        query: None,
                                        fragment: None,
                                    },
                                },
                            ),
                            source: Some(
                                "rust-analyzer",
                            ),
                            message: "not all trait items implemented, missing: `type Iter`",
                            related_information: None,
                            tags: None,
                            data: None,
                        },
                        Diagnostic {
                            range: Range {
                                start: Position {
                                    line: 16,
                                    character: 0,
                                },
                                end: Position {
                                    line: 16,
                                    character: 37,
                                },
                            },
                            severity: Some(
                                Error,
                            ),
                            code: Some(
                                String(
                                    "Click for full compiler diagnostic",
                                ),
                            ),
                            code_description: Some(
                                CodeDescription {
                                    href: Url {
                                        scheme: "rust-analyzer-diagnostics-view",
                                        cannot_be_a_base: false,
                                        username: "",
                                        password: None,
                                        host: None,
                                        port: None,
                                        path: "/diagnostic%20message%20%5B1%5D",
                                        query: Some(
                                            "1",
                                        ),
                                        fragment: Some(
                                            "file%3A%2F%2F%2Fhome%2Fantoni%2Fcode%2Frust-playground%2Fsrc%2Fmain.rs",
                                        ),
                                    },
                                },
                            ),
                            source: Some(
                                "rustc",
                            ),
                            message: "not all trait items implemented, missing: `Iter`\nmissing `Iter` in implementation",
                            related_information: Some(
                                [
                                    DiagnosticRelatedInformation {
                                        location: Location {
                                            uri: Url {
                                                scheme: "file",
                                                cannot_be_a_base: false,
                                                username: "",
                                                password: None,
                                                host: None,
                                                port: None,
                                                path: "/home/antoni/code/rust-playground/src/main.rs",
                                                query: None,
                                                fragment: None,
                                            },
                                            range: Range {
                                                start: Position {
                                                    line: 10,
                                                    character: 8,
                                                },
                                                end: Position {
                                                    line: 10,
                                                    character: 55,
                                                },
                                            },
                                        },
                                        message: "`Iter` from trait",
                                    },
                                    DiagnosticRelatedInformation {
                                        location: Location {
                                            uri: Url {
                                                scheme: "file",
                                                cannot_be_a_base: false,
                                                username: "",
                                                password: None,
                                                host: None,
                                                port: None,
                                                path: "/home/antoni/code/rust-playground/src/main.rs",
                                                query: None,
                                                fragment: None,
                                            },
                                            range: Range {
                                                start: Position {
                                                    line: 16,
                                                    character: 39,
                                                },
                                                end: Position {
                                                    line: 16,
                                                    character: 39,
                                                },
                                            },
                                        },
                                        message: "implement the missing item: `type Iter = /* Type */;`: `type Iter = /* Type */;\n`",
                                    },
                                ],
                            ),
                            tags: None,
                            data: Some(
                                Object {
                                    "rendered": String("\u{1b}[0m\u{1b}[1m\u{1b}[38;5;9merror[E0046]\u{1b}[0m\u{1b}[0m\u{1b}[1m: not all trait items implemented, missing: `Iter`\u{1b}[0m\n\u{1b}[0m  \u{1b}[0m\u{1b}[0m\u{1b}[1m\u{1b}[38;5;12m--> \u{1b}[0m\u{1b}[0msrc/main.rs:17:1\u{1b}[0m\n\u{1b}[0m   \u{1b}[0m\u{1b}[0m\u{1b}[1m\u{1b}[38;5;12m|\u{1b}[0m\n\u{1b}[0m\u{1b}[1m\u{1b}[38;5;12m11\u{1b}[0m\u{1b}[0m \u{1b}[0m\u{1b}[0m\u{1b}[1m\u{1b}[38;5;12m|\u{1b}[0m\u{1b}[0m \u{1b}[0m\u{1b}[0m        type Iter: Iterator<Item = [u8; SOME_CONSTANT]>;\u{1b}[0m\n\u{1b}[0m   \u{1b}[0m\u{1b}[0m\u{1b}[1m\u{1b}[38;5;12m|\u{1b}[0m\u{1b}[0m         \u{1b}[0m\u{1b}[0m\u{1b}[1m\u{1b}[38;5;12m-----------------------------------------------\u{1b}[0m\u{1b}[0m \u{1b}[0m\u{1b}[0m\u{1b}[1m\u{1b}[38;5;12m`Iter` from trait\u{1b}[0m\n\u{1b}[0m\u{1b}[1m\u{1b}[38;5;12m...\u{1b}[0m\n\u{1b}[0m\u{1b}[1m\u{1b}[38;5;12m17\u{1b}[0m\u{1b}[0m \u{1b}[0m\u{1b}[0m\u{1b}[1m\u{1b}[38;5;12m|\u{1b}[0m\u{1b}[0m \u{1b}[0m\u{1b}[0mimpl other_file_2::Trait for MyStruct {}\u{1b}[0m\n\u{1b}[0m   \u{1b}[0m\u{1b}[0m\u{1b}[1m\u{1b}[38;5;12m|\u{1b}[0m\u{1b}[0m \u{1b}[0m\u{1b}[0m\u{1b}[1m\u{1b}[38;5;9m^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\u{1b}[0m\u{1b}[0m \u{1b}[0m\u{1b}[0m\u{1b}[1m\u{1b}[38;5;9mmissing `Iter` in implementation\u{1b}[0m\n\n"),
                                },
                            ),
                        },
                    ],
                    only: Some(
                        [
                            CodeActionKind(
                                "quickfix",
                            ),
                        ],
                    ),
                    trigger_kind: Some(
                        Invoked,
                    ),
                },
                work_done_progress_params: WorkDoneProgressParams {
                    work_done_token: None,
                },
                partial_result_params: PartialResultParams {
                    partial_result_token: None,
                },
            },
            id: "add_impl_missing_members:QuickFix:0",
            version: Some(
                1,
            ),
        },
    ),
}

thread 'Worker' panicked at crates/span/src/ast_id.rs:203:21:
Can't find [email protected] in AstIdMap:
[SyntaxNodePtr { kind: SOURCE_FILE, range: 0..291 }, SyntaxNodePtr { kind: FN, range: 0..12 }, SyntaxNodePtr { kind: MODULE, range: 14..74 }, SyntaxNodePtr { kind: MODULE, range: 76..226 }, SyntaxNodePtr { kind: STRUCT, range: 228..248 }, SyntaxNodePtr { kind: IMPL, range: 250..290 }, SyntaxNodePtr { kind: BLOCK_EXPR, range: 10..12 }, SyntaxNodePtr { kind: CONST, range: 37..72 }, SyntaxNodePtr { kind: USE, range: 99..138 }, SyntaxNodePtr { kind: TRAIT, range: 144..224 }, SyntaxNodePtr { kind: TYPE_ALIAS, range: 170..218 }, SyntaxNodePtr { kind: CONST_ARG, range: 202..215 }]
stack backtrace:
   0: rust_begin_unwind
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/panicking.rs:645:5
   1: core::panicking::panic_fmt
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/panicking.rs:72:14
   2: span::ast_id::AstIdMap::erased_ast_id
   3: hir_def::hir::type_ref::ConstRef::from_const_arg
   4: hir_def::hir::type_ref::TypeRef::from_ast
   5: hir_def::path::lower::lower_generic_args
   6: hir_def::path::lower::lower_path
   7: hir::semantics::SemanticsScope::speculative_resolve
   8: ide_db::path_transform::Ctx::transform_path
   9: ide_db::path_transform::Ctx::apply
  10: ide_db::path_transform::PathTransform::apply
  11: ide_assists::utils::add_trait_assoc_items_to_impl
  12: ide_assists::assist_context::Assists::add::{{closure}}
  13: ide_assists::assist_context::Assists::add_impl
  14: ide_assists::assist_context::Assists::add
  15: ide_assists::handlers::add_missing_impl_members::add_missing_impl_members_inner
  16: ide_assists::assists
  17: std::panicking::try
  18: ide::Analysis::assists_with_fixes
  19: rust_analyzer::handlers::request::handle_code_action_resolve
  20: std::panicking::try
  21: core::ops::function::FnOnce::call_once{{vtable.shim}}
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

useful info that might help debugging:

  • when importing SOME_CONSTANT (use other_file_1::SOME_CONSTANT) before doing the quick fix everything works as expected
  • the panic message starts with Can't find [email protected] in AstIdMap, what it tries to find has a length of 27, just like other_file_1::SOME_CONSTANT (including other_file_1::), the start position, 202, is where SOME_CONSTANT starts on line 11
@antonilol antonilol added the C-bug Category: bug label May 31, 2024
@DropDemBits
Copy link
Contributor

Culprit seems to be that PathTransform::apply (specifically ide_db::path_transform::Ctx::transform_path) looks up resolution information based on the mutable versions of the syntax nodes instead of the original syntax nodes, so after transform_path it'll be looking up a CONST_ARG with the wrong span.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants