rust-lang / rust-lang/rust-analyzer

Extract type as type alias produces invalid code with trait object lifetime inference

Open
#20,107 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

rust-analyzer version: (eg. output of "rust-analyzer: Show RA Version" command, accessible in VSCode via Ctrl/⌘+Shift+P)

rust-analyzer version: 0.3.2509-standalone [/home/caj/.vscode-server/extensions/rust-lang.rust-analyzer-0.3.2509-linux-x64/server/rust-analyzer]

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

rustc 1.87.0 (17067e9ac 2025-05-09)

editor or extension: (eg. VSCode, Vim, Emacs, etc. For VSCode users, specify your extension version; for users of other editors, provide the distribution if applicable)

VSCode

Consider the following code.

fn main() {
    let step = 3;
    let funcs: Box<dyn Fn(i64) -> i64> = Box::new(|i| i + step);

    println!("Hello, world! {}", funcs(2));
}

Select the type of 'funcs' and select 'Extract type as type alias'. This produces:

type Type = Box<dyn Fn(i64) -> i64>;

fn main() {
    let step = 3;
    let funcs: Type = Box::new(|i| i + step);

    println!("Hello, world! {}", funcs(2));
}

Which doesn't compile with error:

error[E0597]: `step` does not live long enough
 --> src/main.rs:5:40
  |
4 |     let step = 3;
  |         ---- binding `step` declared here
5 |     let funcs: Type = Box::new(|i| i + step);
  |                ----            ---     ^^^^ borrowed value does not live long enough
  |                |               |
  |                |               value captured here
  |                type annotation requires that `step` is borrowed for `'static`
...
8 | }
  | - `step` dropped here while still borrowed

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

Reproduce the issue from the provided Rust snippet using the “Extract type as type alias” action. Start by locating the rust-analyzer assist entry point and its existing tests for this extraction, then add a regression case for the inferred trait-object lifetime. Done means the extracted code compiles without requiring the captured local to live for 'static.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.