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
This commit is contained in:
AprilNEA 2025-12-27 22:35:40 +08:00
parent 0850949213
commit f29c5168b2
No known key found for this signature in database
GPG key ID: 80384D1232DF8242
2 changed files with 45 additions and 0 deletions

View file

@ -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 <https://github.com/rust-lang/rust/issues/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<impl Into<!>, u32>) {
| ^^^^^^^ required by this bound in `bar`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0277`.

View file

@ -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<impl Into<!>, u32>) {
unimplemented!()
}
fn foo(x: Result<!, u32>) {
bar(try { x? });
//[e2021]~^ ERROR the trait bound `!: From<()>` is not satisfied
}
fn main() {
}