-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Never pattern in function arguments diverges
- Loading branch information
Showing
8 changed files
with
236 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
tests/ui/rfcs/rfc-0000-never_patterns/diverge-causes-unreachable-code.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#![feature(never_patterns)] | ||
#![allow(incomplete_features)] | ||
#![deny(unreachable_patterns)] | ||
#![deny(unreachable_code)] | ||
|
||
fn main() {} | ||
|
||
enum Void {} | ||
|
||
fn never_arg(!: Void) -> u32 { | ||
println!(); | ||
//~^ ERROR unreachable statement | ||
} | ||
|
||
fn ref_never_arg(&!: &Void) -> u32 { | ||
println!(); | ||
//~^ ERROR unreachable statement | ||
} | ||
|
||
//fn never_let() -> u32 { | ||
// let ptr: *const Void = std::ptr::null(); | ||
// unsafe { | ||
// let ! = *ptr; | ||
// } | ||
// println!(); | ||
//} | ||
|
||
fn never_match() -> u32 { | ||
let ptr: *const Void = std::ptr::null(); | ||
unsafe { | ||
match *ptr { ! }; | ||
} | ||
println!(); | ||
//~^ ERROR unreachable statement | ||
} |
38 changes: 38 additions & 0 deletions
38
tests/ui/rfcs/rfc-0000-never_patterns/diverge-causes-unreachable-code.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
error: unreachable statement | ||
--> $DIR/diverge-causes-unreachable-code.rs:11:5 | ||
| | ||
LL | fn never_arg(!: Void) -> u32 { | ||
| - any code following a never pattern is unreachable | ||
LL | println!(); | ||
| ^^^^^^^^^^ unreachable statement | ||
| | ||
note: the lint level is defined here | ||
--> $DIR/diverge-causes-unreachable-code.rs:4:9 | ||
| | ||
LL | #![deny(unreachable_code)] | ||
| ^^^^^^^^^^^^^^^^ | ||
= 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:16:5 | ||
| | ||
LL | fn ref_never_arg(&!: &Void) -> u32 { | ||
| -- any code following a never pattern is unreachable | ||
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:33:5 | ||
| | ||
LL | match *ptr { ! }; | ||
| ---------------- any code following this `match` expression is unreachable, as all arms diverge | ||
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: aborting due to 3 previous errors | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#![feature(never_patterns)] | ||
#![feature(let_chains)] | ||
#![allow(incomplete_features)] | ||
#![deny(unreachable_patterns)] | ||
|
||
fn main() {} | ||
|
||
enum Void {} | ||
|
||
// Contrast with `./diverges.rs`: merely having an empty type around isn't enough to diverge. | ||
|
||
fn wild_void(_: Void) -> u32 {} | ||
//~^ ERROR: mismatched types | ||
|
||
fn wild_let() -> u32 { | ||
let ptr: *const Void = std::ptr::null(); | ||
unsafe { | ||
//~^ ERROR: mismatched types | ||
let _ = *ptr; | ||
} | ||
} | ||
|
||
fn wild_match() -> u32 { | ||
let ptr: *const Void = std::ptr::null(); | ||
unsafe { | ||
match *ptr { | ||
_ => {} //~ ERROR: mismatched types | ||
} | ||
} | ||
} | ||
|
||
fn binding_void(_x: Void) -> u32 {} | ||
//~^ ERROR: mismatched types | ||
|
||
fn binding_let() -> u32 { | ||
let ptr: *const Void = std::ptr::null(); | ||
unsafe { | ||
//~^ ERROR: mismatched types | ||
let _x = *ptr; | ||
} | ||
} | ||
|
||
fn binding_match() -> u32 { | ||
let ptr: *const Void = std::ptr::null(); | ||
unsafe { | ||
match *ptr { | ||
_x => {} //~ ERROR: mismatched types | ||
} | ||
} | ||
} | ||
|
||
// Don't confuse this with a `let !` statement. | ||
fn let_chain(x: Void) -> u32 { | ||
if let true = true && let ! = x {} | ||
//~^ ERROR: mismatched types | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/diverges-not.rs:12:26 | ||
| | ||
LL | fn wild_void(_: Void) -> u32 {} | ||
| --------- ^^^ expected `u32`, found `()` | ||
| | | ||
| implicitly returns `()` as its body has no tail or `return` expression | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/diverges-not.rs:17:5 | ||
| | ||
LL | / unsafe { | ||
LL | | | ||
LL | | let _ = *ptr; | ||
LL | | } | ||
| |_____^ expected `u32`, found `()` | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/diverges-not.rs:27:18 | ||
| | ||
LL | _ => {} | ||
| ^^ expected `u32`, found `()` | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/diverges-not.rs:32:30 | ||
| | ||
LL | fn binding_void(_x: Void) -> u32 {} | ||
| ------------ ^^^ expected `u32`, found `()` | ||
| | | ||
| implicitly returns `()` as its body has no tail or `return` expression | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/diverges-not.rs:37:5 | ||
| | ||
LL | / unsafe { | ||
LL | | | ||
LL | | let _x = *ptr; | ||
LL | | } | ||
| |_____^ expected `u32`, found `()` | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/diverges-not.rs:47:19 | ||
| | ||
LL | _x => {} | ||
| ^^ expected `u32`, found `()` | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/diverges-not.rs:54:37 | ||
| | ||
LL | if let true = true && let ! = x {} | ||
| ^^ expected `u32`, found `()` | ||
|
||
error: aborting due to 7 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0308`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// check-pass | ||
#![feature(never_patterns)] | ||
#![allow(incomplete_features)] | ||
#![deny(unreachable_patterns)] | ||
|
||
fn main() {} | ||
|
||
enum Void {} | ||
|
||
// A never pattern alone diverges. | ||
|
||
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_match() -> u32 { | ||
let ptr: *const Void = std::ptr::null(); | ||
unsafe { | ||
match *ptr { ! }; | ||
} | ||
println!(); // Ensures this typechecks because of divergence. | ||
} |