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

Confusing parser error when writing expression in item context #119161

Open
knutwalker opened this issue Dec 20, 2023 · 6 comments
Open

Confusing parser error when writing expression in item context #119161

knutwalker opened this issue Dec 20, 2023 · 6 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Comments

@knutwalker
Copy link

Playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=2241d9411a77aa3a465dd34e997bd74d

I tried using the documented example for std::panic::set_hook in a clean project (see playground)

use std::panic;

panic::set_hook(Box::new(|_| {
    println!("Custom panic hook");
}));

panic!("Normal panic");

I expected to see this happen: It compiles

Instead, this happened:

   Compiling playground v0.0.1 (/playground)
error: expected one of `!` or `::`, found `(`
 --> src/lib.rs:3:16
  |
3 | panic::set_hook(Box::new(|_| {
  |                ^ expected one of `!` or `::`

error: could not compile `playground` (lib) due to previous error

I tried this with 1.74.1, 1.75.0-beta.7 (2023-12-16 b216e7b0e7a2bdf11300), and 1.76.0-nightly (2023-12-17 6a62871320e262661bb1) from the playground as well as locally with

rustc 1.76.0-nightly (3f28fe133 2023-12-18)
binary: rustc
commit-hash: 3f28fe133475ec5faf3413b556bf3cfb0d51336c
commit-date: 2023-12-18
host: aarch64-apple-darwin
release: 1.76.0-nightly
LLVM version: 17.0.6
rustc 1.75.0-beta.7 (b216e7b0e 2023-12-16)
binary: rustc
commit-hash: b216e7b0e7a2bdf11300a21a614dac6be3e99c5b
commit-date: 2023-12-16
host: aarch64-apple-darwin
release: 1.75.0-beta.7
LLVM version: 17.0.6
rustc 1.74.1 (a28077b28 2023-12-04)
binary: rustc
commit-hash: a28077b28a02b92985b3a3faecf92813155f1ea1
commit-date: 2023-12-04
host: aarch64-apple-darwin
release: 1.74.1
LLVM version: 17.0.4
rustc 1.73.0 (cc66ad468 2023-10-03)
binary: rustc
commit-hash: cc66ad468955717ab92600c770da8c1601a4ff33
commit-date: 2023-10-03
host: aarch64-apple-darwin
release: 1.73.0
LLVM version: 17.0.2
rustc 1.71.0 (8ede3aae2 2023-07-12)
binary: rustc
commit-hash: 8ede3aae28fe6e4d52b38157d7bfe0d3bceef225
commit-date: 2023-07-12
host: aarch64-apple-darwin
release: 1.71.0
LLVM version: 16.0.5
rustc 1.69.0 (84c898d65 2023-04-16)
binary: rustc
commit-hash: 84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc
commit-date: 2023-04-16
host: aarch64-apple-darwin
release: 1.69.0
LLVM version: 15.0.7
rustc 1.65.0 (897e37553 2022-11-02)
binary: rustc
commit-hash: 897e37553bba8b42751c67658967889d11ecd120
commit-date: 2022-11-02
host: aarch64-apple-darwin
release: 1.65.0
LLVM version: 15.0.0
rustc 1.63.0 (4b91a6ea7 2022-08-08)
binary: rustc
commit-hash: 4b91a6ea7258a947e59c6522cd5898e7c0a6a88f
commit-date: 2022-08-08
host: aarch64-apple-darwin
release: 1.63.0
LLVM version: 14.0.5

None of them worked, it's the same error in all cases.

I also tried various forms of using set_hook:

use std::panic::set_hook as sh;
sh(...);
use ::std::panic::set_hook as sh;
sh(...);
use std::panic as p;
p::set_hook(...);
use ::std::panic as p;
p::set_hook(...);
std::panic::set_hook(...);
::std::panic::set_hook(...);

And they all produce the same error.

@knutwalker knutwalker added the C-bug Category: This is a bug. label Dec 20, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Dec 20, 2023
@ChrisDenton ChrisDenton added A-diagnostics Area: Messages for errors, warnings, and lints T-libs Relevant to the library team, which will review and decide on the PR/issue. and removed C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Dec 20, 2023
@ChrisDenton
Copy link
Member

ChrisDenton commented Dec 20, 2023

set_hook needs to be called within a function body, not at the top-level. If you click the "run" button from the example, it will load a working example in the playground, where it adds fn main() { } around the code.

@knutwalker
Copy link
Author

Ah, thanks. Makes sense, actually, when I think about it. Maybe it can be mentioned in the docs or the diagnostics can be improved to mention that function calls cannot happed at the top-level?

@ChrisDenton
Copy link
Member

cc @estebank maybe? Can the diagnostics be improved here?

In any case, it does make sense to improve the docs. I think just showing the fn main in the example would be improvement. Currently it's implicit because for many examples it's just line noise.

@estebank
Copy link
Contributor

@ChrisDenton I've had a personal backlog item to "fallback to parsing expression in item context" (and in pattern context) for better diagnostics here, but haven't had the time to do so. If someone wants to take that on sooner than I will, I would be more than happy to help them.

@estebank estebank changed the title panic::set_hook cannot be used, produces parser errors instead Confusing parser error when writing expression in item context Dec 20, 2023
@estebank estebank added A-parser Area: The parsing of Rust source code to an AST D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. labels Dec 20, 2023
@chenyukang
Copy link
Member

There are too many code examples following this style(especially in library), fix this specific doc may not help, maybe we need a more general solution...

@chenyukang
Copy link
Member

@estebank I don't know how to fallback to get a better diagnostics here, any more hint?

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 D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants