rust-lang / rust-lang/rust

Confusing error when using non-`XID_Start` characters in lifetime names

Open
#141,081 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics A-lexer D-confusing T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
fn bad_lifetime_name<'πŸ›>(_: &'πŸ› ()) {}
Current output
error: character literal may only contain one codepoint
 --> src/lib.rs:1:22
  |
1 | fn bad_lifetime_name<'πŸ›>(_: &'πŸ› ()) {}
  |                      ^^^^^^^^^^
  |
help: if you meant to write a string literal, use double quotes
  |
1 - fn bad_lifetime_name<'πŸ›>(_: &'πŸ› ()) {}
1 + fn bad_lifetime_name<"πŸ›>(_: &"πŸ› ()) {}
  |

error: unexpected closing delimiter: `)`
 --> src/lib.rs:1:35
  |
1 | fn bad_lifetime_name<'πŸ›>(_: &'πŸ› ()) {}
  |                                     ^ unexpected closing delimiter
Desired output
error: a lifetime name cannot begin with a character that is neither `_` nor a `XID_Start` character

 --> src/lib.rs:1:22
  |
1 | fn bad_lifetime_name<'πŸ›>(_: &'πŸ› ()) {}
  |                       ^--
  |                        help: `\u{1F41B}` is neither `_` nor in `XID_Start`
Rationale and extra context

See also https://doc.rust-lang.org/reference/identifiers.html.
Initially reported at #general > Compiler isn't helpful with invalid lifetimes.

Looks like this recovery path(?) is hit when starting a lifetime name (not including the ') with a non-XID_Start character fails both is_id_start check and ASCII digit check hitting the !can_be_a_lifetime codepath.

https://github.com/rust-lang/rust/blob/c79bbfab78dcb0a72aa3b2bc35c00334b58bfe2e/compiler/rustc_lexer/src/lib.rs#L824-L842

Other cases

Marginally related is loop labels, this complains about "unterminated character literal".

fn main() {
    'πŸ›: {
        todo!();  
    };
}
Rust Version
$ rustc --version --verbose
rustc 1.87.0 (17067e9ac 2025-05-09)
binary: rustc
commit-hash: 17067e9ac6d7ecb70e50f92c1944e545188d2359
commit-date: 2025-05-09
host: aarch64-unknown-linux-gnu
release: 1.87.0
LLVM version: 20.1.1
Anything else?

No response

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 cited recovery path in compiler/rustc_lexer/src/lib.rs around lines 824–842 and reproduce the supplied lifetime-name example. Compare the current and desired diagnostics, then verify that invalid lifetime names report the XID_Start guidance without producing the misleading follow-up errors; the related loop-label case is also noted for consideration.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.