summaryrefslogtreecommitdiffstats
path: root/tests/ui/lint/dead-code
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-18 02:49:50 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-18 02:49:50 +0000
commit9835e2ae736235810b4ea1c162ca5e65c547e770 (patch)
tree3fcebf40ed70e581d776a8a4c65923e8ec20e026 /tests/ui/lint/dead-code
parentReleasing progress-linux version 1.70.0+dfsg2-1~progress7.99u1. (diff)
downloadrustc-9835e2ae736235810b4ea1c162ca5e65c547e770.tar.xz
rustc-9835e2ae736235810b4ea1c162ca5e65c547e770.zip
Merging upstream version 1.71.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/ui/lint/dead-code')
-rw-r--r--tests/ui/lint/dead-code/closure-bang.rs4
-rw-r--r--tests/ui/lint/dead-code/closure-bang.stderr17
-rw-r--r--tests/ui/lint/dead-code/offset-of-correct-param-env.rs42
-rw-r--r--tests/ui/lint/dead-code/offset-of.rs44
-rw-r--r--tests/ui/lint/dead-code/offset-of.stderr50
5 files changed, 154 insertions, 3 deletions
diff --git a/tests/ui/lint/dead-code/closure-bang.rs b/tests/ui/lint/dead-code/closure-bang.rs
index 8e8636b11..bca131a15 100644
--- a/tests/ui/lint/dead-code/closure-bang.rs
+++ b/tests/ui/lint/dead-code/closure-bang.rs
@@ -1,9 +1,7 @@
-// ignore-test FIXME(#20574)
-
#![deny(unreachable_code)]
fn main() {
- let x = || panic!();
+ let x = || -> ! { panic!() };
x();
println!("Foo bar"); //~ ERROR: unreachable statement
}
diff --git a/tests/ui/lint/dead-code/closure-bang.stderr b/tests/ui/lint/dead-code/closure-bang.stderr
new file mode 100644
index 000000000..119ce11e3
--- /dev/null
+++ b/tests/ui/lint/dead-code/closure-bang.stderr
@@ -0,0 +1,17 @@
+error: unreachable statement
+ --> $DIR/closure-bang.rs:6:5
+ |
+LL | x();
+ | --- any code following this expression is unreachable
+LL | println!("Foo bar");
+ | ^^^^^^^^^^^^^^^^^^^ unreachable statement
+ |
+note: the lint level is defined here
+ --> $DIR/closure-bang.rs:1:9
+ |
+LL | #![deny(unreachable_code)]
+ | ^^^^^^^^^^^^^^^^
+ = note: this error originates in the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
+
+error: aborting due to previous error
+
diff --git a/tests/ui/lint/dead-code/offset-of-correct-param-env.rs b/tests/ui/lint/dead-code/offset-of-correct-param-env.rs
new file mode 100644
index 000000000..2c6fcef25
--- /dev/null
+++ b/tests/ui/lint/dead-code/offset-of-correct-param-env.rs
@@ -0,0 +1,42 @@
+// check-pass
+
+#![feature(offset_of)]
+#![deny(dead_code)]
+
+// This struct contains a projection that can only be normalized after getting the field type.
+struct A<T: Project> {
+ a: <T as Project>::EquateParamTo,
+}
+
+// This is the inner struct that we want to get.
+struct MyFieldIsNotDead {
+ not_dead: u8,
+}
+
+// These are some helpers.
+// Inside the param env of `test`, we want to make it so that it considers T=MyFieldIsNotDead.
+struct GenericIsEqual<T>(T);
+trait Project {
+ type EquateParamTo;
+}
+impl<T> Project for GenericIsEqual<T> {
+ type EquateParamTo = T;
+}
+
+fn test<T>() -> usize
+where
+ GenericIsEqual<T>: Project<EquateParamTo = MyFieldIsNotDead>,
+{
+ // The first field of the A that we construct here is
+ // `<GenericIsEqual<T>> as Project>::EquateParamTo`.
+ // Typeck normalizes this and figures that the not_dead field is totally fine and accessible.
+ // But importantly, the normalization ends up with T, which, as we've declared in our param
+ // env is MyFieldDead. When we're in the param env of the `a` field, the where bound above
+ // is not in scope, so we don't know what T is - it's generic.
+ // If we use the wrong param env, the lint will ICE.
+ std::mem::offset_of!(A<GenericIsEqual<T>>, a.not_dead)
+}
+
+fn main() {
+ test::<MyFieldIsNotDead>();
+}
diff --git a/tests/ui/lint/dead-code/offset-of.rs b/tests/ui/lint/dead-code/offset-of.rs
new file mode 100644
index 000000000..da91de386
--- /dev/null
+++ b/tests/ui/lint/dead-code/offset-of.rs
@@ -0,0 +1,44 @@
+#![feature(offset_of)]
+#![deny(dead_code)]
+
+use std::mem::offset_of;
+
+struct Alpha {
+ a: (),
+ b: (), //~ ERROR field `b` is never read
+ c: Beta,
+}
+
+struct Beta {
+ a: (), //~ ERROR field `a` is never read
+ b: (),
+}
+
+struct Gamma {
+ a: (), //~ ERROR field `a` is never read
+ b: (),
+}
+
+struct Delta {
+ a: (),
+ b: (), //~ ERROR field `b` is never read
+}
+
+trait Trait {
+ type Assoc;
+}
+impl Trait for () {
+ type Assoc = Delta;
+}
+
+struct Project<T: Trait> {
+ a: u8, //~ ERROR field `a` is never read
+ b: <T as Trait>::Assoc,
+}
+
+fn main() {
+ offset_of!(Alpha, a);
+ offset_of!(Alpha, c.b);
+ offset_of!((Gamma,), 0.b);
+ offset_of!(Project::<()>, b.a);
+}
diff --git a/tests/ui/lint/dead-code/offset-of.stderr b/tests/ui/lint/dead-code/offset-of.stderr
new file mode 100644
index 000000000..ed2916461
--- /dev/null
+++ b/tests/ui/lint/dead-code/offset-of.stderr
@@ -0,0 +1,50 @@
+error: field `b` is never read
+ --> $DIR/offset-of.rs:8:5
+ |
+LL | struct Alpha {
+ | ----- field in this struct
+LL | a: (),
+LL | b: (),
+ | ^
+ |
+note: the lint level is defined here
+ --> $DIR/offset-of.rs:2:9
+ |
+LL | #![deny(dead_code)]
+ | ^^^^^^^^^
+
+error: field `a` is never read
+ --> $DIR/offset-of.rs:13:5
+ |
+LL | struct Beta {
+ | ---- field in this struct
+LL | a: (),
+ | ^
+
+error: field `a` is never read
+ --> $DIR/offset-of.rs:18:5
+ |
+LL | struct Gamma {
+ | ----- field in this struct
+LL | a: (),
+ | ^
+
+error: field `b` is never read
+ --> $DIR/offset-of.rs:24:5
+ |
+LL | struct Delta {
+ | ----- field in this struct
+LL | a: (),
+LL | b: (),
+ | ^
+
+error: field `a` is never read
+ --> $DIR/offset-of.rs:35:5
+ |
+LL | struct Project<T: Trait> {
+ | ------- field in this struct
+LL | a: u8,
+ | ^
+
+error: aborting due to 5 previous errors
+