From f29c5168b205bfa51316ab79d04c143f1937edef Mon Sep 17 00:00:00 2001 From: AprilNEA Date: Sat, 27 Dec 2025 22:35:40 +0800 Subject: [PATCH] Add test for never type fallback in try blocks with Into Closes #125364 This test verifies that the never type fallback behavior in try blocks with `Into` bounds works correctly across different editions: - Edition 2021: Fallback to `()` causes type error - Edition 2024: Fallback to `!` compiles successfully --- ...try-block-never-type-fallback.e2021.stderr | 20 +++++++++++++++ .../try-block-never-type-fallback.rs | 25 +++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 tests/ui/never_type/try-block-never-type-fallback.e2021.stderr create mode 100644 tests/ui/never_type/try-block-never-type-fallback.rs diff --git a/tests/ui/never_type/try-block-never-type-fallback.e2021.stderr b/tests/ui/never_type/try-block-never-type-fallback.e2021.stderr new file mode 100644 index 000000000000..7e23c86bcbcc --- /dev/null +++ b/tests/ui/never_type/try-block-never-type-fallback.e2021.stderr @@ -0,0 +1,20 @@ +error[E0277]: the trait bound `!: From<()>` is not satisfied + --> $DIR/try-block-never-type-fallback.rs:20:9 + | +LL | bar(try { x? }); + | --- ^^^^^^^^^^ the trait `From<()>` is not implemented for `!` + | | + | required by a bound introduced by this call + | + = note: this error might have been caused by changes to Rust's type-inference algorithm (see issue #148922 for more information) + = help: you might have intended to use the type `()` here instead + = note: required for `()` to implement `Into` +note: required by a bound in `bar` + --> $DIR/try-block-never-type-fallback.rs:15:23 + | +LL | fn bar(_: Result, u32>) { + | ^^^^^^^ required by this bound in `bar` + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0277`. diff --git a/tests/ui/never_type/try-block-never-type-fallback.rs b/tests/ui/never_type/try-block-never-type-fallback.rs new file mode 100644 index 000000000000..94107a0ff103 --- /dev/null +++ b/tests/ui/never_type/try-block-never-type-fallback.rs @@ -0,0 +1,25 @@ +//@ revisions: e2021 e2024 +//@[e2021] edition: 2021 +//@[e2024] edition: 2024 +//@[e2024] check-pass + +// Issue #125364: Bad interaction between never_type, try_blocks, and From/Into +// +// In edition 2021, the never type in try blocks falls back to (), +// causing a type error (since (): Into does not hold). +// In edition 2024, it falls back to !, allowing the code to compile correctly. + +#![feature(never_type)] +#![feature(try_blocks)] + +fn bar(_: Result, u32>) { + unimplemented!() +} + +fn foo(x: Result) { + bar(try { x? }); + //[e2021]~^ ERROR the trait bound `!: From<()>` is not satisfied +} + +fn main() { +}