rust-lang / rust-lang/rust-analyzer

Extract into function; Option::unwrap()

Open
#12,065 9 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-assists C-bug I-panic S-actionable
Dominant language
Rust
Stars
16.9k
Forks
2.2k
Avg merge
1d 12h
Merged PRs (30d)
72

Description

Hi there

I was doing Extract Function in vs code and then this happened.
It does happen continuously.

Just in case:
The code is valid and can be compiled.
cargo clean din't help.

Panic context:
> 
version: db2a7087b 2021-12-13 stable
request: codeAction/resolve CodeAction {
    title: "Extract into function",
    group: None,
    kind: Some(
        CodeActionKind(
            "refactor.extract",
        ),
    ),
    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/maxim/projects/tabled/papergrid/src/lib.rs",
                        query: None,
                        fragment: None,
                    },
                },
                range: Range {
                    start: Position {
                        line: 2389,
                        character: 4,
                    },
                    end: Position {
                        line: 2453,
                        character: 5,
                    },
                },
                context: CodeActionContext {
                    diagnostics: [],
                    only: None,
                },
                work_done_progress_params: WorkDoneProgressParams {
                    work_done_token: None,
                },
                partial_result_params: PartialResultParams {
                    partial_result_token: None,
                },
            },
            id: "extract_function:RefactorExtract:0",
        },
    ),
}

thread '<unnamed>' panicked at 'called `Option::unwrap()` on a `None` value', crates/syntax/src/ted.rs:137:41
stack backtrace:
   0: rust_begin_unwind
             at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/std/src/panicking.rs:517:5
   1: core::panicking::panic_fmt
             at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/core/src/panicking.rs:100:14
   2: core::panicking::panic
             at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/core/src/panicking.rs:50:5
   3: syntax::ted::replace_all
   4: syntax::ted::replace_with_many
   5: ide_assists::handlers::extract_function::rewrite_body_segment
   6: <alloc::vec::Vec<T> as alloc::vec::spec_from_iter::SpecFromIter<T,I>>::from_iter
   7: ide_assists::handlers::extract_function::format_function
   8: ide_assists::assist_context::Assists::add::{{closure}}
   9: ide_assists::assist_context::Assists::add_impl
  10: ide_assists::handlers::extract_function::extract_function
  11: ide_assists::assists
  12: ide::Analysis::assists_with_fixes::{{closure}}
  13: std::panicking::try
  14: rust_analyzer::handlers::handle_code_action_resolve
  15: std::panicking::try
  16: <F as threadpool::FnBox>::call_box
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

rust-analyzer version: (eg. output of "Rust Analyzer: Show RA Version" command)

rust-analyzer version: db2a7087b 2021-12-13 stable

rustc version: (eg. output of rustc -V)

rustc 1.59.0 (9d1b2106e 2022-02-23)

Take care

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the panic at crates/syntax/src/ted.rs:137 and trace how replace_all and replace_with_many are called from ide_assists::handlers::extract_function::rewrite_body_segment. Reproduce the Extract into function code action from the reported range in papergrid/src/lib.rs, then verify that resolving it no longer panics on the supplied rust-analyzer and rustc versions.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.