Skip to content

Unterminated char literal complains about lifetimes #101278

Closed

Description

@estebank

Given the following code

let _ = ['1','2,'3'];

the current output is

error: lifetimes cannot start with a number
 --> src/main.rs:2:18
  |
2 |     let _ = ['1','2,'3'];
  |                  ^^

error: expected `while`, `for`, `loop` or `{` after a label
 --> src/main.rs:2:20
  |
2 |     let _ = ['1','2,'3'];
  |                    ^ expected `while`, `for`, `loop` or `{` after a label

It should instead recognize that a char literal was intended:

error: unterminated char literal
 --> src/main.rs:2:18
  |
2 |     let _ = ['1','2,'3'];
  |                  ^^
help: add a closing `'`
  |
2 |     let _ = ['1','2','3'];
  |                    +

https://internals.rust-lang.org/t/error-message-for-vec-1-2-3/17309

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    A-parserArea: The lexing & parsing of Rust source code to an AST
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`
    D-papercutDiagnostics: An error or lint that needs small tweaks.
    D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.
    on Sep 1, 2022
  2. steffahn commented on Sep 1, 2022

    @steffahn
    Member

    Note that users might deliberately try naming lifetimes '1 or '2, too, in other cases; especially given that some error messages introduce lifetimes with such stand-in names, e.g.

    [snip]
    3 | fn f(x: Cell<(&i32, &i32)>) {
      |               -     - let's call the lifetime of this reference `'1`
      |               |
      |               let's call the lifetime of this reference `'2`
    4 |     g(x)
      |     ^^^^ argument requires that `'1` must outlive `'2`
    
  3. estebank commented on Sep 1, 2022

    @estebank
    ContributorAuthor

    Yes, the error recovery should be constricted to places where a char literal would be completely valid.

  4. compiler-errors commented on Sep 1, 2022

    @compiler-errors
    Contributor

    I think we can do this with a stashed diagnostic.

  5. estebank commented on Sep 1, 2022

    @estebank
    ContributorAuthor

    I think a limited lookahead (and maybe some passed down info) that checks whether a literal is acceptable there.

  6. ChayimFriedman2 commented on Sep 1, 2022

    @ChayimFriedman2
    Contributor

    Are they even accepted in the same positions? Lifetimes are only in generic parameter lists and chars are only in expressions, no?

    Edit: Oh, but labels.

  7. compiler-errors commented on Sep 1, 2022

    @compiler-errors
    Contributor

    Lifetimes can also be labels that start blocks

  8. ChayimFriedman2 commented on Sep 1, 2022

    @ChayimFriedman2
    Contributor

    So "if not followed by colon" can be a pretty good estimation. We can also restrict even more to "colon then brace or loop or while or for".

  9. added a commit that references this issue on Oct 23, 2022
    e029c1f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-parserArea: The lexing & parsing of Rust source code to an ASTA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.D-papercutDiagnostics: An error or lint that needs small tweaks.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