about summary refs log tree commit diff
path: root/tests/ui/const_prop
diff options
context:
space:
mode:
authorAlbert Larsan <74931857+albertlarsan68@users.noreply.github.com>2023-01-05 09:13:28 +0100
committerAlbert Larsan <74931857+albertlarsan68@users.noreply.github.com>2023-01-11 09:32:08 +0000
commitcf2dff2b1e3fa55fa5415d524200070d0d7aacfe (patch)
tree40a88d9a46aaf3e8870676eb2538378b75a263eb /tests/ui/const_prop
parentca855e6e42787ecd062d81d53336fe6788ef51a9 (diff)
downloadrust-cf2dff2b1e3fa55fa5415d524200070d0d7aacfe.tar.gz
rust-cf2dff2b1e3fa55fa5415d524200070d0d7aacfe.zip
Move /src/test to /tests
Diffstat (limited to 'tests/ui/const_prop')
-rw-r--r--tests/ui/const_prop/ice-assert-fail-div-by-zero.rs14
-rw-r--r--tests/ui/const_prop/ice-assert-fail-div-by-zero.stderr14
-rw-r--r--tests/ui/const_prop/inline_spans.rs15
-rw-r--r--tests/ui/const_prop/inline_spans_lint_attribute.rs15
-rw-r--r--tests/ui/const_prop/issue-102553.rs24
5 files changed, 82 insertions, 0 deletions
diff --git a/tests/ui/const_prop/ice-assert-fail-div-by-zero.rs b/tests/ui/const_prop/ice-assert-fail-div-by-zero.rs
new file mode 100644
index 00000000000..2afbf3432fb
--- /dev/null
+++ b/tests/ui/const_prop/ice-assert-fail-div-by-zero.rs
@@ -0,0 +1,14 @@
+// check-pass
+
+// need to emit MIR, because const prop (which emits `unconditional_panic`) only runs if
+// the `optimized_mir` query is run, which it isn't in check-only mode.
+// compile-flags: --crate-type lib --emit=mir,link
+
+#![warn(unconditional_panic)]
+
+pub struct Fixed64(i64);
+
+// HACK: this test passes only because this is a const fn that is written to metadata
+pub const fn div(f: Fixed64) {
+    f.0 / 0; //~ WARN will panic at runtime
+}
diff --git a/tests/ui/const_prop/ice-assert-fail-div-by-zero.stderr b/tests/ui/const_prop/ice-assert-fail-div-by-zero.stderr
new file mode 100644
index 00000000000..865c69c3c89
--- /dev/null
+++ b/tests/ui/const_prop/ice-assert-fail-div-by-zero.stderr
@@ -0,0 +1,14 @@
+warning: this operation will panic at runtime
+  --> $DIR/ice-assert-fail-div-by-zero.rs:13:5
+   |
+LL |     f.0 / 0;
+   |     ^^^^^^^ attempt to divide `_` by zero
+   |
+note: the lint level is defined here
+  --> $DIR/ice-assert-fail-div-by-zero.rs:7:9
+   |
+LL | #![warn(unconditional_panic)]
+   |         ^^^^^^^^^^^^^^^^^^^
+
+warning: 1 warning emitted
+
diff --git a/tests/ui/const_prop/inline_spans.rs b/tests/ui/const_prop/inline_spans.rs
new file mode 100644
index 00000000000..504f2781156
--- /dev/null
+++ b/tests/ui/const_prop/inline_spans.rs
@@ -0,0 +1,15 @@
+// build-pass
+// compile-flags: -Zmir-opt-level=3
+// Overflow can't be detected by const prop
+// could only be detected after optimizations
+
+#![deny(warnings)]
+
+fn main() {
+    let _ = add(u8::MAX, 1);
+}
+
+#[inline(always)]
+fn add(x: u8, y: u8) -> u8 {
+    x + y
+}
diff --git a/tests/ui/const_prop/inline_spans_lint_attribute.rs b/tests/ui/const_prop/inline_spans_lint_attribute.rs
new file mode 100644
index 00000000000..1db53d77193
--- /dev/null
+++ b/tests/ui/const_prop/inline_spans_lint_attribute.rs
@@ -0,0 +1,15 @@
+// Must be build-pass, because check-pass will not run const prop and thus not emit the lint anyway.
+// build-pass
+// compile-flags: -Zmir-opt-level=3
+
+#![deny(warnings)]
+
+fn main() {
+    #[allow(arithmetic_overflow)]
+    let _ = add(u8::MAX, 1);
+}
+
+#[inline(always)]
+fn add(x: u8, y: u8) -> u8 {
+    x + y
+}
diff --git a/tests/ui/const_prop/issue-102553.rs b/tests/ui/const_prop/issue-102553.rs
new file mode 100644
index 00000000000..523a9d7ac72
--- /dev/null
+++ b/tests/ui/const_prop/issue-102553.rs
@@ -0,0 +1,24 @@
+// compile-flags: --crate-type=lib
+// check-pass
+
+pub trait Widget<E> {
+    fn boxed<'w>(self) -> Box<dyn WidgetDyn<E> + 'w>
+    where
+        Self: Sized + 'w;
+}
+
+pub trait WidgetDyn<E> {}
+
+impl<T, E> WidgetDyn<E> for T where T: Widget<E> {}
+
+impl<E> Widget<E> for dyn WidgetDyn<E> + '_ {
+    fn boxed<'w>(self) -> Box<dyn WidgetDyn<E> + 'w>
+    where
+        Self: Sized + 'w,
+    {
+        // Even though this is illegal to const evaluate, this should never
+        // trigger an ICE because it can never be called from actual code
+        // (due to the trivially false where-clause predicate).
+        Box::new(self)
+    }
+}