about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2024-03-17 19:26:21 +0100
committerGitHub <noreply@github.com>2024-03-17 19:26:21 +0100
commit3fbe203cc121f1af6690b4cef2ec67be6ac81e6d (patch)
tree4ea824035b78779f9ddb5fa9db68ad4ec5b64ba2
parent5c761d8cfe76e2a5a1840193d545b5e415c621c2 (diff)
parentfefd06dc02eea5c5bd96aedce17a5e58c8645f9a (diff)
downloadrust-3fbe203cc121f1af6690b4cef2ec67be6ac81e6d.tar.gz
rust-3fbe203cc121f1af6690b4cef2ec67be6ac81e6d.zip
Rollup merge of #122572 - the8472:test-const-deadness, r=RalfJung
add test for #122301 to cover behavior that's on stable

If this ought to be broken it should at least happen intentionally

See #122301
-rw-r--r--tests/ui/consts/control-flow/dead_branches_dont_eval.rs46
1 files changed, 46 insertions, 0 deletions
diff --git a/tests/ui/consts/control-flow/dead_branches_dont_eval.rs b/tests/ui/consts/control-flow/dead_branches_dont_eval.rs
new file mode 100644
index 00000000000..374349732f9
--- /dev/null
+++ b/tests/ui/consts/control-flow/dead_branches_dont_eval.rs
@@ -0,0 +1,46 @@
+//@ build-pass
+
+// issue 122301 - currently the only way to supress
+// const eval and codegen of code conditional on some other const
+
+struct Foo<T, const N: usize>(T);
+
+impl<T, const N: usize> Foo<T, N> {
+    const BAR: () = if N == 0 {
+        panic!()
+    };
+}
+
+struct Invoke<T, const N: usize>(T);
+
+impl<T, const N: usize> Invoke<T, N> {
+    const FUN: fn() = if N != 0 {
+        || Foo::<T, N>::BAR
+    } else {
+        || {}
+    };
+}
+
+// without closures
+
+struct S<T>(T);
+impl<T> S<T> {
+    const C: () = panic!();
+}
+
+const fn bar<T>() { S::<T>::C }
+
+struct ConstIf<T, const N: usize>(T);
+
+impl<T, const N: usize> ConstIf<T, N> {
+    const VAL: () = if N != 0 {
+        bar::<T>() // not called for N == 0, and hence not monomorphized
+    } else {
+        ()
+    };
+}
+
+fn main() {
+    let _val = Invoke::<(), 0>::FUN();
+    let _val = ConstIf::<(), 0>::VAL;
+}