summaryrefslogtreecommitdiffstats
path: root/tests/ui/uninhabited
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:19:03 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:19:03 +0000
commit64d98f8ee037282c35007b64c2649055c56af1db (patch)
tree5492bcf97fce41ee1c0b1cc2add283f3e66cdab0 /tests/ui/uninhabited
parentAdding debian version 1.67.1+dfsg1-1. (diff)
downloadrustc-64d98f8ee037282c35007b64c2649055c56af1db.tar.xz
rustc-64d98f8ee037282c35007b64c2649055c56af1db.zip
Merging upstream version 1.68.2+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/ui/uninhabited')
-rw-r--r--tests/ui/uninhabited/exhaustive-wo-nevertype-issue-51221.rs9
-rw-r--r--tests/ui/uninhabited/privately-uninhabited-dead-code.rs20
-rw-r--r--tests/ui/uninhabited/privately-uninhabited-mir-call.rs29
-rw-r--r--tests/ui/uninhabited/privately-uninhabited-mir-call.stderr17
-rw-r--r--tests/ui/uninhabited/uninhabited-enum-cast.rs9
-rw-r--r--tests/ui/uninhabited/uninhabited-irrefutable.rs30
-rw-r--r--tests/ui/uninhabited/uninhabited-irrefutable.stderr24
-rw-r--r--tests/ui/uninhabited/uninhabited-matches-feature-gated.rs39
-rw-r--r--tests/ui/uninhabited/uninhabited-matches-feature-gated.stderr115
-rw-r--r--tests/ui/uninhabited/uninhabited-patterns.rs47
-rw-r--r--tests/ui/uninhabited/uninhabited-patterns.stderr38
11 files changed, 377 insertions, 0 deletions
diff --git a/tests/ui/uninhabited/exhaustive-wo-nevertype-issue-51221.rs b/tests/ui/uninhabited/exhaustive-wo-nevertype-issue-51221.rs
new file mode 100644
index 000000000..b59432078
--- /dev/null
+++ b/tests/ui/uninhabited/exhaustive-wo-nevertype-issue-51221.rs
@@ -0,0 +1,9 @@
+// check-pass
+
+#![feature(exhaustive_patterns)]
+
+enum Void {}
+fn main() {
+ let a: Option<Void> = None;
+ let None = a;
+}
diff --git a/tests/ui/uninhabited/privately-uninhabited-dead-code.rs b/tests/ui/uninhabited/privately-uninhabited-dead-code.rs
new file mode 100644
index 000000000..f476704cd
--- /dev/null
+++ b/tests/ui/uninhabited/privately-uninhabited-dead-code.rs
@@ -0,0 +1,20 @@
+// build-pass (FIXME(62277): could be check-pass?)
+
+#![deny(unused_variables)]
+
+mod foo {
+ enum Bar {}
+
+ #[allow(dead_code)]
+ pub struct Foo {
+ value: Bar, // "privately" uninhabited
+ }
+
+ pub fn give_foo() -> Foo { panic!() }
+}
+
+fn main() {
+ let a = 42;
+ foo::give_foo();
+ println!("Hello, {}", a); // ok: we can't tell that this code is dead
+}
diff --git a/tests/ui/uninhabited/privately-uninhabited-mir-call.rs b/tests/ui/uninhabited/privately-uninhabited-mir-call.rs
new file mode 100644
index 000000000..2764bb563
--- /dev/null
+++ b/tests/ui/uninhabited/privately-uninhabited-mir-call.rs
@@ -0,0 +1,29 @@
+// Verifies that MIR building for a call expression respects
+// privacy when checking if a call return type is uninhabited.
+
+pub mod widget {
+ enum Unimplemented {}
+ pub struct Widget(Unimplemented);
+
+ impl Widget {
+ pub fn new() -> Widget {
+ todo!();
+ }
+ }
+
+ pub fn f() {
+ let x: &mut u32;
+ Widget::new();
+ // Ok. Widget type returned from new is known to be uninhabited
+ // and the following code is considered unreachable.
+ *x = 1;
+ }
+}
+
+fn main() {
+ let y: &mut u32;
+ widget::Widget::new();
+ // Error. Widget type is not known to be uninhabited here,
+ // so the following code is considered reachable.
+ *y = 2; //~ ERROR E0381
+}
diff --git a/tests/ui/uninhabited/privately-uninhabited-mir-call.stderr b/tests/ui/uninhabited/privately-uninhabited-mir-call.stderr
new file mode 100644
index 000000000..0dfd22a30
--- /dev/null
+++ b/tests/ui/uninhabited/privately-uninhabited-mir-call.stderr
@@ -0,0 +1,17 @@
+error[E0381]: used binding `y` isn't initialized
+ --> $DIR/privately-uninhabited-mir-call.rs:28:5
+ |
+LL | let y: &mut u32;
+ | - binding declared here but left uninitialized
+...
+LL | *y = 2;
+ | ^^^^^^ `y` used here but it isn't initialized
+ |
+help: consider assigning a value
+ |
+LL | let y: &mut u32 = todo!();
+ | +++++++++
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0381`.
diff --git a/tests/ui/uninhabited/uninhabited-enum-cast.rs b/tests/ui/uninhabited/uninhabited-enum-cast.rs
new file mode 100644
index 000000000..5a75c94c4
--- /dev/null
+++ b/tests/ui/uninhabited/uninhabited-enum-cast.rs
@@ -0,0 +1,9 @@
+// check-pass
+
+enum E {}
+
+fn f(e: E) {
+ println!("{}", (e as isize).to_string());
+}
+
+fn main() {}
diff --git a/tests/ui/uninhabited/uninhabited-irrefutable.rs b/tests/ui/uninhabited/uninhabited-irrefutable.rs
new file mode 100644
index 000000000..4b001aca2
--- /dev/null
+++ b/tests/ui/uninhabited/uninhabited-irrefutable.rs
@@ -0,0 +1,30 @@
+#![feature(never_type)]
+#![feature(exhaustive_patterns)]
+
+mod foo {
+ pub struct SecretlyEmpty {
+ _priv: !,
+ }
+
+ pub struct NotSoSecretlyEmpty {
+ pub _pub: !,
+ }
+}
+
+struct NotSoSecretlyEmpty {
+ _priv: !,
+}
+
+enum Foo {
+ A(foo::SecretlyEmpty),
+ B(foo::NotSoSecretlyEmpty),
+ C(NotSoSecretlyEmpty),
+ D(u32, u32),
+}
+
+fn main() {
+ let x: Foo = Foo::D(123, 456);
+ let Foo::D(_y, _z) = x;
+ //~^ ERROR refutable pattern in local binding
+ //~| `Foo::A(_)` not covered
+}
diff --git a/tests/ui/uninhabited/uninhabited-irrefutable.stderr b/tests/ui/uninhabited/uninhabited-irrefutable.stderr
new file mode 100644
index 000000000..8cafea555
--- /dev/null
+++ b/tests/ui/uninhabited/uninhabited-irrefutable.stderr
@@ -0,0 +1,24 @@
+error[E0005]: refutable pattern in local binding
+ --> $DIR/uninhabited-irrefutable.rs:27:9
+ |
+LL | let Foo::D(_y, _z) = x;
+ | ^^^^^^^^^^^^^^ pattern `Foo::A(_)` not covered
+ |
+ = note: `let` bindings require an "irrefutable pattern", like a `struct` or an `enum` with only one variant
+ = note: for more information, visit https://doc.rust-lang.org/book/ch18-02-refutability.html
+note: `Foo` defined here
+ --> $DIR/uninhabited-irrefutable.rs:18:6
+ |
+LL | enum Foo {
+ | ^^^
+LL | A(foo::SecretlyEmpty),
+ | - not covered
+ = note: the matched value is of type `Foo`
+help: you might want to use `let else` to handle the variant that isn't matched
+ |
+LL | let Foo::D(_y, _z) = x else { todo!() };
+ | ++++++++++++++++
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0005`.
diff --git a/tests/ui/uninhabited/uninhabited-matches-feature-gated.rs b/tests/ui/uninhabited/uninhabited-matches-feature-gated.rs
new file mode 100644
index 000000000..e804afcf9
--- /dev/null
+++ b/tests/ui/uninhabited/uninhabited-matches-feature-gated.rs
@@ -0,0 +1,39 @@
+use std::mem::zeroed;
+enum Void {}
+
+fn main() {
+ let x: Result<u32, &'static Void> = Ok(23);
+ let _ = match x { //~ ERROR non-exhaustive
+ Ok(n) => n,
+ };
+
+ // This is pretty much instant UB. However, we have no choice -- we need to
+ // test matching on a reference to `&Void`; we cannot do anything other than
+ // just accept the fact that this is UB if `main` did run, but it doesn't;
+ // this test only checks that these are feature-gated.
+ let x: &Void = unsafe { zeroed() };
+ let _ = match x {}; //~ ERROR non-exhaustive
+
+ let x: (Void,) = unsafe { zeroed() };
+ let _ = match x {}; //~ ERROR non-exhaustive
+
+ let x: [Void; 1] = unsafe { zeroed() };
+ let _ = match x {}; //~ ERROR non-exhaustive
+
+ let x: &[Void] = unsafe { zeroed() };
+ let _ = match x { //~ ERROR non-exhaustive
+ &[] => (),
+ };
+
+ let x: Void = unsafe { zeroed() };
+ let _ = match x {}; // okay
+
+ let x: Result<u32, Void> = Ok(23);
+ let _ = match x { //~ ERROR non-exhaustive
+ Ok(x) => x,
+ };
+
+ let x: Result<u32, Void> = Ok(23);
+ let Ok(x) = x;
+ //~^ ERROR refutable
+}
diff --git a/tests/ui/uninhabited/uninhabited-matches-feature-gated.stderr b/tests/ui/uninhabited/uninhabited-matches-feature-gated.stderr
new file mode 100644
index 000000000..466d7f2ea
--- /dev/null
+++ b/tests/ui/uninhabited/uninhabited-matches-feature-gated.stderr
@@ -0,0 +1,115 @@
+error[E0004]: non-exhaustive patterns: `Err(_)` not covered
+ --> $DIR/uninhabited-matches-feature-gated.rs:6:19
+ |
+LL | let _ = match x {
+ | ^ pattern `Err(_)` not covered
+ |
+note: `Result<u32, &Void>` defined here
+ --> $SRC_DIR/core/src/result.rs:LL:COL
+ ::: $SRC_DIR/core/src/result.rs:LL:COL
+ |
+ = note: not covered
+ = note: the matched value is of type `Result<u32, &Void>`
+help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
+ |
+LL ~ Ok(n) => n,
+LL ~ Err(_) => todo!(),
+ |
+
+error[E0004]: non-exhaustive patterns: type `&Void` is non-empty
+ --> $DIR/uninhabited-matches-feature-gated.rs:15:19
+ |
+LL | let _ = match x {};
+ | ^
+ |
+note: `Void` defined here
+ --> $DIR/uninhabited-matches-feature-gated.rs:2:6
+ |
+LL | enum Void {}
+ | ^^^^
+ = note: the matched value is of type `&Void`
+ = note: references are always considered inhabited
+help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern as shown
+ |
+LL ~ let _ = match x {
+LL + _ => todo!(),
+LL ~ };
+ |
+
+error[E0004]: non-exhaustive patterns: type `(Void,)` is non-empty
+ --> $DIR/uninhabited-matches-feature-gated.rs:18:19
+ |
+LL | let _ = match x {};
+ | ^
+ |
+ = note: the matched value is of type `(Void,)`
+help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern as shown
+ |
+LL ~ let _ = match x {
+LL + _ => todo!(),
+LL ~ };
+ |
+
+error[E0004]: non-exhaustive patterns: type `[Void; 1]` is non-empty
+ --> $DIR/uninhabited-matches-feature-gated.rs:21:19
+ |
+LL | let _ = match x {};
+ | ^
+ |
+ = note: the matched value is of type `[Void; 1]`
+help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern as shown
+ |
+LL ~ let _ = match x {
+LL + _ => todo!(),
+LL ~ };
+ |
+
+error[E0004]: non-exhaustive patterns: `&[_, ..]` not covered
+ --> $DIR/uninhabited-matches-feature-gated.rs:24:19
+ |
+LL | let _ = match x {
+ | ^ pattern `&[_, ..]` not covered
+ |
+ = note: the matched value is of type `&[Void]`
+help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
+ |
+LL ~ &[] => (),
+LL ~ &[_, ..] => todo!(),
+ |
+
+error[E0004]: non-exhaustive patterns: `Err(_)` not covered
+ --> $DIR/uninhabited-matches-feature-gated.rs:32:19
+ |
+LL | let _ = match x {
+ | ^ pattern `Err(_)` not covered
+ |
+note: `Result<u32, Void>` defined here
+ --> $SRC_DIR/core/src/result.rs:LL:COL
+ ::: $SRC_DIR/core/src/result.rs:LL:COL
+ |
+ = note: not covered
+ = note: the matched value is of type `Result<u32, Void>`
+help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
+ |
+LL ~ Ok(x) => x,
+LL ~ Err(_) => todo!(),
+ |
+
+error[E0005]: refutable pattern in local binding
+ --> $DIR/uninhabited-matches-feature-gated.rs:37:9
+ |
+LL | let Ok(x) = x;
+ | ^^^^^ pattern `Err(_)` not covered
+ |
+ = note: `let` bindings require an "irrefutable pattern", like a `struct` or an `enum` with only one variant
+ = note: for more information, visit https://doc.rust-lang.org/book/ch18-02-refutability.html
+ = note: the matched value is of type `Result<u32, Void>`
+help: you might want to use `let else` to handle the variant that isn't matched
+ |
+LL | let Ok(x) = x else { todo!() };
+ | ++++++++++++++++
+
+error: aborting due to 7 previous errors
+
+Some errors have detailed explanations: E0004, E0005.
+For more information about an error, try `rustc --explain E0004`.
diff --git a/tests/ui/uninhabited/uninhabited-patterns.rs b/tests/ui/uninhabited/uninhabited-patterns.rs
new file mode 100644
index 000000000..f1573b6ad
--- /dev/null
+++ b/tests/ui/uninhabited/uninhabited-patterns.rs
@@ -0,0 +1,47 @@
+#![feature(box_patterns)]
+#![feature(never_type)]
+#![feature(exhaustive_patterns)]
+
+
+#![deny(unreachable_patterns)]
+
+mod foo {
+ pub struct SecretlyEmpty {
+ _priv: !,
+ }
+}
+
+struct NotSoSecretlyEmpty {
+ _priv: !,
+}
+
+fn foo() -> Option<NotSoSecretlyEmpty> {
+ None
+}
+
+fn main() {
+ let x: &[!] = &[];
+
+ match x {
+ &[] => (),
+ &[..] => (), //~ ERROR unreachable pattern
+ };
+
+ let x: Result<Box<NotSoSecretlyEmpty>, &[Result<!, !>]> = Err(&[]);
+ match x {
+ Ok(box _) => (), //~ ERROR unreachable pattern
+ Err(&[]) => (),
+ Err(&[..]) => (), //~ ERROR unreachable pattern
+ }
+
+ let x: Result<foo::SecretlyEmpty, Result<NotSoSecretlyEmpty, u32>> = Err(Err(123));
+ match x {
+ Ok(_y) => (),
+ Err(Err(_y)) => (),
+ Err(Ok(_y)) => (), //~ ERROR unreachable pattern
+ }
+
+ while let Some(_y) = foo() {
+ //~^ ERROR unreachable pattern
+ }
+}
diff --git a/tests/ui/uninhabited/uninhabited-patterns.stderr b/tests/ui/uninhabited/uninhabited-patterns.stderr
new file mode 100644
index 000000000..655569ad6
--- /dev/null
+++ b/tests/ui/uninhabited/uninhabited-patterns.stderr
@@ -0,0 +1,38 @@
+error: unreachable pattern
+ --> $DIR/uninhabited-patterns.rs:27:9
+ |
+LL | &[..] => (),
+ | ^^^^^
+ |
+note: the lint level is defined here
+ --> $DIR/uninhabited-patterns.rs:6:9
+ |
+LL | #![deny(unreachable_patterns)]
+ | ^^^^^^^^^^^^^^^^^^^^
+
+error: unreachable pattern
+ --> $DIR/uninhabited-patterns.rs:32:9
+ |
+LL | Ok(box _) => (),
+ | ^^^^^^^^^
+
+error: unreachable pattern
+ --> $DIR/uninhabited-patterns.rs:34:9
+ |
+LL | Err(&[..]) => (),
+ | ^^^^^^^^^^
+
+error: unreachable pattern
+ --> $DIR/uninhabited-patterns.rs:41:9
+ |
+LL | Err(Ok(_y)) => (),
+ | ^^^^^^^^^^^
+
+error: unreachable pattern
+ --> $DIR/uninhabited-patterns.rs:44:15
+ |
+LL | while let Some(_y) = foo() {
+ | ^^^^^^^^
+
+error: aborting due to 5 previous errors
+