Never pattern in let statement diverges

This commit is contained in:
Nadrieril 2024-01-18 18:22:08 +01:00
parent a9ea07d17c
commit d1f1075931
4 changed files with 33 additions and 15 deletions

View File

@ -1471,6 +1471,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
/// Type check a `let` statement.
pub fn check_decl_local(&self, local: &'tcx hir::Local<'tcx>) {
self.check_decl(local.into());
if local.pat.is_never_pattern() {
self.diverges.set(Diverges::Always {
span: local.pat.span,
custom_note: Some("any code following a never pattern is unreachable"),
});
}
}
pub fn check_stmt(&self, stmt: &'tcx hir::Stmt<'tcx>) {

View File

@ -17,13 +17,14 @@ fn ref_never_arg(&!: &Void) -> u32 {
//~^ ERROR unreachable statement
}
//fn never_let() -> u32 {
// let ptr: *const Void = std::ptr::null();
// unsafe {
// let ! = *ptr;
// }
// println!();
//}
fn never_let() -> u32 {
let ptr: *const Void = std::ptr::null();
unsafe {
let ! = *ptr;
}
println!();
//~^ ERROR unreachable statement
}
fn never_match() -> u32 {
let ptr: *const Void = std::ptr::null();

View File

@ -24,7 +24,18 @@ LL | println!();
= note: this error originates in the macro `$crate::print` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
error: unreachable statement
--> $DIR/diverge-causes-unreachable-code.rs:33:5
--> $DIR/diverge-causes-unreachable-code.rs:25:5
|
LL | let ! = *ptr;
| - any code following a never pattern is unreachable
LL | }
LL | println!();
| ^^^^^^^^^^ unreachable statement
|
= note: this error originates in the macro `$crate::print` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
error: unreachable statement
--> $DIR/diverge-causes-unreachable-code.rs:34:5
|
LL | match *ptr { ! };
| ---------------- any code following this `match` expression is unreachable, as all arms diverge
@ -34,5 +45,5 @@ LL | println!();
|
= note: this error originates in the macro `$crate::print` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to 3 previous errors
error: aborting due to 4 previous errors

View File

@ -13,12 +13,12 @@ fn never_arg(!: Void) -> u32 {}
fn ref_never_arg(&!: &Void) -> u32 {}
// fn never_let() -> u32 {
// let ptr: *const Void = std::ptr::null();
// unsafe {
// let ! = *ptr;
// }
// }
fn never_let() -> u32 {
let ptr: *const Void = std::ptr::null();
unsafe {
let ! = *ptr;
}
}
fn never_match() -> u32 {
let ptr: *const Void = std::ptr::null();