Skip to content
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

Can't resolve SyntaxNodePtr when extracting struct from enum variant #16328

Closed
shepmaster opened this issue Jan 9, 2024 · 4 comments · Fixed by #16396
Closed

Can't resolve SyntaxNodePtr when extracting struct from enum variant #16328

shepmaster opened this issue Jan 9, 2024 · 4 comments · Fixed by #16396
Labels

Comments

@shepmaster
Copy link
Member

enum TheEnum {
    TheVariant { the_field: u8 },
}

fn main() {
    vec![TheEnum::TheVariant { the_field: 42 }];
}

Place the cursor on TheVariant on line 2. Invoke "Extract struct from enum variant".

Panic context:
> 
version: 0.0.0 (12e7aa3132 2024-01-08)
request: codeAction/resolve CodeAction {
    title: "Extract struct from enum variant",
    group: None,
    kind: Some(
        CodeActionKind(
            "refactor.rewrite",
        ),
    ),
    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: "/private/tmp/reduce/src/main.rs",
                        query: None,
                        fragment: None,
                    },
                },
                range: Range {
                    start: Position {
                        line: 1,
                        character: 4,
                    },
                    end: Position {
                        line: 1,
                        character: 4,
                    },
                },
                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,
                },
            },
            id: "extract_struct_from_enum_variant:RefactorRewrite:1",
        },
    ),
}

thread 'Worker' panicked at /Users/shep/.cargo/registry/src/index.crates.io-6f17d22bba15001f/rowan-0.15.15/src/ast.rs:73:50:
can't resolve SyntaxNodePtr { kind: PATH_SEGMENT, range: 64..74 } with SOURCE_FILE@0..115
stack backtrace:
   0: rust_begin_unwind
             at /rustc/75c68cfd2b9870f2953b62d250bd7d0564a7b56d/library/std/src/panicking.rs:645:5
   1: core::panicking::panic_fmt
             at /rustc/75c68cfd2b9870f2953b62d250bd7d0564a7b56d/library/core/src/panicking.rs:72:14
   2: rowan::ast::SyntaxNodePtr<L>::to_node
   3: ide_db::source_change::TreeMutator::make_syntax_mut
   4: ide_db::source_change::SourceChangeBuilder::make_mut
   5: core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &mut F>::call_once
   6: alloc::vec::in_place_collect::<impl alloc::vec::spec_from_iter::SpecFromIter<T,I> for alloc::vec::Vec<T>>::from_iter
   7: ide_assists::assist_context::Assists::add::{{closure}}
   8: ide_assists::assist_context::Assists::add_impl
   9: ide_assists::handlers::extract_struct_from_enum_variant::extract_struct_from_enum_variant
  10: ide_assists::assists
  11: std::panicking::try
  12: ide::Analysis::with_db
  13: ide::Analysis::assists_with_fixes
  14: rust_analyzer::handlers::request::handle_code_action_resolve
  15: std::panicking::try
  16: core::ops::function::FnOnce::call_once{{vtable.shim}}
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

rust-analyzer version: rust-analyzer 0.0.0 (12e7aa3132 2024-01-08)

rustc version: rustc 1.75.0 (82e1608df 2023-12-21)

relevant settings: I'm using this through emacs.

@shepmaster shepmaster added the C-bug Category: bug label Jan 9, 2024
@Young-Flash
Copy link
Member

    #[test]
    fn test_with_marco() {
        check_assist(
            extract_struct_from_enum_variant,
r#"
enum TheEnum {
    TheVariant$0 { the_field: u8 },
}

fn main() {
    vec![TheEnum::TheVariant { the_field: 42 }];
}
"#,
r#"
struct TheVariant{ the_field: u8 }

enum TheEnum {
    TheVariant(TheVariant),
}

fn main() {
    vec![TheEnum::TheVariant { the_field: 42 }];
}
"#,
        );
    }

can't reproduce in the above test(am I missing something?) on 12e7aa3132 , but the panic does happen in the editor.

image

@Veykril
Copy link
Member

Veykril commented Jan 10, 2024

Test fixtures don't have access to the std library so the vec macro call is unresolved in your test

@Young-Flash
Copy link
Member

Test fixtures don't have access to the std library so the vec macro call is unresolved in your test

can minicore help with accessing vec?

@lnicola
Copy link
Member

lnicola commented Jan 10, 2024

You probably don't need it:

macro_rules! foo {
    ($x:expr) => {
        $x
    };
}

enum TheEnum {
    TheVariant { the_field: u8 },
}

fn main() {
    foo![TheEnum::TheVariant { the_field: 42 }];
}

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

Successfully merging a pull request may close this issue.

4 participants