Skip to content

Commit

Permalink
Never pattern in let statement diverges
Browse files Browse the repository at this point in the history
  • Loading branch information
Nadrieril committed Jan 18, 2024
1 parent 6ff1daf commit 02e95c9
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 15 deletions.
6 changes: 6 additions & 0 deletions compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1473,6 +1473,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>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

12 changes: 6 additions & 6 deletions tests/ui/rfcs/rfc-0000-never_patterns/diverges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 02e95c9

Please sign in to comment.