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

Provide a better error when code is put outside of a function #92615

Open
meithecatte opened this issue Jan 6, 2022 · 4 comments · May be fixed by #125388
Open

Provide a better error when code is put outside of a function #92615

meithecatte opened this issue Jan 6, 2022 · 4 comments · May be fixed by #125388
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

@meithecatte
Copy link
Contributor

meithecatte commented Jan 6, 2022

Examples in the documentation often provide excerpts instead of full programs. A beginner might try compiling that code directly, and be confused that it doesn't work. Currently, the error messages for that case don't provide any hints as to what might be wrong:

error: expected item, found keyword `let`
 --> src/lib.rs:1:1
  |
1 | let data = [4, 8, 15, 16, 23, 42];
  | ^^^ expected item
error: expected one of `!` or `::`, found `(`
 --> src/lib.rs:2:10
  |
2 | fs::write("foo.txt", "Hello!").unwrap();
  |          ^ expected one of `!` or `::`
error: macro expansion ignores token `{` and any following
   |
  ::: src/lib.rs:1:1
   |
1  |   println!("Hello, world!");
   |   ------------------------- caused by the macro expansion here
   |
   = note: the usage of `println!` is likely invalid in item context

Ideally the output should look like:

error: `let` is invalid outside of a function
 --> src/lib.rs:1:1
  |
1 | let data = [4, 8, 15, 16, 23, 42];
  | ^^^ not valid outside of a function
help: put your code in a `main` function:
  | fn main() {
  |     let data = [4, 8, 15, 16, 23, 42];
  |     for n in data {
  |         dbg!(n);
  |     }
  | }
@meithecatte meithecatte 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 Jan 6, 2022
@chordtoll
Copy link
Contributor

@rustbot claim

@chordtoll
Copy link
Contributor

After investigating, it looks like this functionality might not be possible without significant changes- the above errors occur in the parse stage, before we have the information needed to decide if other functions exist, or if the user did in fact make the mistake of trying to directly run a snippet.

@chordtoll
Copy link
Contributor

I'm going to release this ticket so a contributor with more experience in the parser can hopefully give it a look

@chordtoll
Copy link
Contributor

@rustbot release-assignment

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