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

Poor recovery from pub let x = ... #101622

Open
kpreid opened this issue Sep 9, 2022 · 2 comments · May be fixed by #125388
Open

Poor recovery from pub let x = ... #101622

kpreid opened this issue Sep 9, 2022 · 2 comments · May be fixed by #125388
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@kpreid
Copy link
Contributor

kpreid commented Sep 9, 2022

Given the following code:

fn main() {
    pub let x = 123;
}

The current output is:

error: visibility `pub` is not followed by an item
 --> src/main.rs:2:5
  |
2 |     pub let x = 123;
  |     ^^^ the visibility
  |
  = help: you likely meant to define an item, e.g., `pub fn foo() {}`

error: expected expression, found statement (`let`)
 --> src/main.rs:2:9
  |
2 |     pub let x = 123;
  |         ^^^^^^^^^^^
  |
  = note: variable declaration using `let` is a statement

error[E0658]: `let` expressions in this position are unstable
 --> src/main.rs:2:9
  |
2 |     pub let x = 123;
  |         ^^^^^^^^^^^
  |
  = note: see issue #53667 <https://github.com/rust-lang/rust/issues/53667> for more information

The latter two errors are redundant and confusing given the first. It seems like the compiler is trying to recover by parsing the tokens following pub as an expression, thus disallowing let, but there's no obvious reason to do that. whereas parsing them as a statement would be more useful and more analogous to the item syntax.

I propose one or both of the following improvements:

  • After encountering pub not followed by an item, try recovery by parsing as a statement, as if the pub token was absent. (This should be sufficient to produce only one, meaningful, error message in this situation.)
  • Detect pub let specifically, and suggest either let or pub static/const. (This might be part of a general improvement to handling confusion between items and statements — Provide a better error when code is put outside of a function #92615.)

Possible output would be:

error: visibility does not apply to `let` statements
 --> src/main.rs:2:5
  |
2 |     pub let x = 123;
  |     ^^^ the visibility
  |
  = help: remove the `pub`

rustc version: 1.63.0 stable. On nightly, a fourth error related to the let-expression interpretation is emitted.

@kpreid kpreid added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 9, 2022
@obeis obeis removed their assignment Jan 14, 2024
@dev-ardi
Copy link
Contributor

@rustbot claim

@dev-ardi dev-ardi linked a pull request May 21, 2024 that will close this issue
@dev-ardi
Copy link
Contributor

Another point not covered in this issue is that pub inside of a function doesn't make sense. That should likely be a lint though.

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

3 participants