rust-lang / rust-lang/rust-analyzer

Interaction with `mismatched-lifetime-syntaxes` lint

Open
#20,304 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

It's not entirely clear to me whether this is an issue for r-a or whether we should be feeding something to r-a differently from the rustc side, but let's start here.

In Rust 1.89, we're shipping a mismatched-lifetime-syntaxes lint (docs). See:

If the user starts with the following,

#![allow(unused)]
struct W<'a>(&'a ());
fn f(x: &()) -> W { W(x) }

we emit, from rustc, this diagnostic:

warning: hiding a lifetime that's elided elsewhere is confusing
 --> src/main.rs:3:9
  |
3 | fn f(x: &()) -> W { W(x) }
  |         ^^^     - the same lifetime is hidden here
  |         |
  |         the lifetime is elided here
  |
  = help: the same lifetime is referred to in inconsistent ways, making the signature confusing
  = note: `#[warn(mismatched_lifetime_syntaxes)]` on by default
help: use `'_` for type paths
  |
3 | fn f(x: &()) -> W<'_> { W(x) }
  |                  ++++

As above, we want the user to end up with:

fn f(x: &()) -> W<'_> { W(x) }

However, under r-a (latest nightly rustc + r-a current master), the following happens. Going to the first warning puts the cursor here:

fn f(x: &()) -> W { W(x) }
//      ^

With the cursor there, no code suggestion is available to be applied at all. If we move one character further,

fn f(x: &()) -> W { W(x) }
//       ^

then there's one code suggestion available, and it does this,

fn f(x: &'_ ()) -> W<'_> { W(x) }

which isn't what we want.

If we put the cursor here,

fn f(x: &()) -> W { W(x) }
//              ^

we again don't get a relevant code suggestion (we get the ones about wrapping the return type in Option or Result). If we move one character later, to here,

fn f(x: &()) -> W { W(x) }
//               ^

then we do get the suggestion we want first (and the other one we probably don't want second). That will give us,

fn f(x: &()) -> W<'_> { W(x) }

as desired.

But, of course, how would the user know to put the cursor there? That's not where the user is going to put the cursor to try to make this warning go away.

What do we think?

cc @shepmaster @Veykril

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 by reproducing the Rust 1.89 mismatched-lifetime-syntaxes example with the latest nightly rustc and rust-analyzer current master. Compare the diagnostic cursor positions and available code suggestions, especially the unwanted &'_ () edit and the desired W<'_> edit. Done means the warning offers the intended W<'_> suggestion from the user's natural warning location without requiring cursor repositioning.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.