Skip to content

'r#_ gives two confusingly similar errors #143152

@sodiboo

Description

@sodiboo

Code

fn f<'r#_>(){}

Current output

error: `_` cannot be a raw lifetime
 --> src/lib.rs:1:6
  |
1 | fn f<'r#_>(){}
  |      ^^^^

error[E0637]: `'_` cannot be used here
 --> src/lib.rs:1:6
  |
1 | fn f<'r#_>(){}
  |      ^^^^ `'_` is a reserved lifetime name

Desired output

error: `_` cannot be a raw lifetime
 --> src/lib.rs:1:6
  |
1 | fn f<'r#_>(){}
  |      ^^^^

Rationale and extra context

I believe the first diagnostic is from the lexer, and the second diagnostic occurs in parsing.

These diagnostics technically don't communicate the same information:

  • the first diagnostic states that there is something wrong with the token's shape
  • the second diagnostic states that if 'r#_ was a valid token (in which case it would be a synonym of '_, similar to 'r#static), it would not be allowed in this position.

But both boil down to "this isn't allowed" without any suggestion for how to change it. And the second diagnostic is really about a hypothetical synonym to 'r#_, but because that token is invalid, it has no synonyms.

So, i think the best alternative would be to only display the error saying that "`_` cannot be a raw lifetime"

Rust Version

rustc 1.90.0-nightly (bdaba05a9 2025-06-27)
binary: rustc
commit-hash: bdaba05a953eb5abeba0011cdda2560d157aed2e
commit-date: 2025-06-27
host: x86_64-unknown-linux-gnu
release: 1.90.0-nightly
LLVM version: 20.1.7

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-lifetimesArea: Lifetimes / regionsD-verboseDiagnostics: Too much output caused by a single piece of incorrect code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions