Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unterminated char literal complains about lifetimes #101278

Closed
estebank opened this issue Sep 1, 2022 · 7 comments · Fixed by #101293
Closed

Unterminated char literal complains about lifetimes #101278

estebank opened this issue Sep 1, 2022 · 7 comments · Fixed by #101293
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@estebank
Copy link
Contributor

estebank commented Sep 1, 2022

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

@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-papercut Diagnostics: An error or lint that needs small tweaks. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. labels Sep 1, 2022
@steffahn
Copy link
Member

steffahn commented Sep 1, 2022

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`

@estebank
Copy link
Contributor Author

estebank commented Sep 1, 2022

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

@compiler-errors
Copy link
Member

I think we can do this with a stashed diagnostic.

@compiler-errors compiler-errors self-assigned this Sep 1, 2022
@estebank
Copy link
Contributor Author

estebank commented Sep 1, 2022

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

@ChayimFriedman2
Copy link
Contributor

ChayimFriedman2 commented Sep 1, 2022

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.

@compiler-errors
Copy link
Member

compiler-errors commented Sep 1, 2022

Lifetimes can also be labels that start blocks

@ChayimFriedman2
Copy link
Contributor

ChayimFriedman2 commented Sep 1, 2022

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".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants