Skip to content

Confusing parser error when writing expression in item context #119161

Open
@knutwalker

Description

@knutwalker

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-parserArea: The lexing & parsing of Rust source code to an ASTD-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.T-libsRelevant to the library 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