about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <476013+matthiaskrgr@users.noreply.github.com>2025-07-05 00:12:12 +0200
committerGitHub <noreply@github.com>2025-07-05 00:12:12 +0200
commit1ff6e4478529ed4bf096476cd68b877099fc09f0 (patch)
treeeed8e7a7980453b05c75a153eb21a3a7e23bb839
parente0dd7ecac11363cbdd6cb2fc9c33b983e9f7fe31 (diff)
parent71176a28e698d04b23a3e20d8a8d93de4d467609 (diff)
downloadrust-1ff6e4478529ed4bf096476cd68b877099fc09f0.tar.gz
rust-1ff6e4478529ed4bf096476cd68b877099fc09f0.zip
Rollup merge of #143444 - lukas-code:gvn-test, r=RalfJung
clean up GVN TypeId test

addresses https://github.com/rust-lang/rust/pull/142789#discussion_r2184897992

This is an attempt to clarify what this test is actually supposed to test and make it less dependent on `TypeId` internals (it now depends on the output of `type_name` instead).

I verified that this version still miscompiles on `nightly-2025-02-11`.

r? ``@oli-obk`` ``@RalfJung``
-rw-r--r--tests/mir-opt/gvn_const_eval_polymorphic.no_optimize.GVN.diff12
-rw-r--r--tests/mir-opt/gvn_const_eval_polymorphic.optimize_false.GVN.diff13
-rw-r--r--tests/mir-opt/gvn_const_eval_polymorphic.optimize_true.GVN.diff13
-rw-r--r--tests/mir-opt/gvn_const_eval_polymorphic.rs57
-rw-r--r--tests/mir-opt/gvn_type_id_polymorphic.cursed_is_i32.GVN.diff12
-rw-r--r--tests/mir-opt/gvn_type_id_polymorphic.rs22
6 files changed, 95 insertions, 34 deletions
diff --git a/tests/mir-opt/gvn_const_eval_polymorphic.no_optimize.GVN.diff b/tests/mir-opt/gvn_const_eval_polymorphic.no_optimize.GVN.diff
new file mode 100644
index 00000000000..a91561ba304
--- /dev/null
+++ b/tests/mir-opt/gvn_const_eval_polymorphic.no_optimize.GVN.diff
@@ -0,0 +1,12 @@
+- // MIR for `no_optimize` before GVN
++ // MIR for `no_optimize` after GVN
+  
+  fn no_optimize() -> bool {
+      let mut _0: bool;
+  
+      bb0: {
+          _0 = Eq(const no_optimize::<T>::{constant#0}, const no_optimize::<T>::{constant#1});
+          return;
+      }
+  }
+  
diff --git a/tests/mir-opt/gvn_const_eval_polymorphic.optimize_false.GVN.diff b/tests/mir-opt/gvn_const_eval_polymorphic.optimize_false.GVN.diff
new file mode 100644
index 00000000000..bdfa2987b23
--- /dev/null
+++ b/tests/mir-opt/gvn_const_eval_polymorphic.optimize_false.GVN.diff
@@ -0,0 +1,13 @@
+- // MIR for `optimize_false` before GVN
++ // MIR for `optimize_false` after GVN
+  
+  fn optimize_false() -> bool {
+      let mut _0: bool;
+  
+      bb0: {
+-         _0 = Eq(const optimize_false::<T>::{constant#0}, const optimize_false::<T>::{constant#1});
++         _0 = const false;
+          return;
+      }
+  }
+  
diff --git a/tests/mir-opt/gvn_const_eval_polymorphic.optimize_true.GVN.diff b/tests/mir-opt/gvn_const_eval_polymorphic.optimize_true.GVN.diff
new file mode 100644
index 00000000000..dc337d43fb0
--- /dev/null
+++ b/tests/mir-opt/gvn_const_eval_polymorphic.optimize_true.GVN.diff
@@ -0,0 +1,13 @@
+- // MIR for `optimize_true` before GVN
++ // MIR for `optimize_true` after GVN
+  
+  fn optimize_true() -> bool {
+      let mut _0: bool;
+  
+      bb0: {
+-         _0 = Eq(const optimize_true::<T>::{constant#0}, const optimize_true::<T>::{constant#1});
++         _0 = const true;
+          return;
+      }
+  }
+  
diff --git a/tests/mir-opt/gvn_const_eval_polymorphic.rs b/tests/mir-opt/gvn_const_eval_polymorphic.rs
new file mode 100644
index 00000000000..7320ad947ff
--- /dev/null
+++ b/tests/mir-opt/gvn_const_eval_polymorphic.rs
@@ -0,0 +1,57 @@
+//@ test-mir-pass: GVN
+//@ compile-flags: --crate-type lib
+
+//! Regressions test for a mis-optimization where some functions
+//! (`type_id` / `type_name` / `needs_drop`) could be evaluated in
+//! a generic context, even though their value depends on some type
+//! parameter `T`.
+//!
+//! In particular, `type_name_of_val(&generic::<T>)` was incorrectly
+//! evaluated to the string "crate_name::generic::<T>", and
+//! `no_optimize` was incorrectly optimized to `false`.
+
+#![feature(const_type_name)]
+
+fn generic<T>() {}
+
+const fn type_name_contains_i32<T>(_: &T) -> bool {
+    let pattern = b"i32";
+    let name = std::any::type_name::<T>().as_bytes();
+    let mut i = 0;
+    'outer: while i < name.len() - pattern.len() + 1 {
+        let mut j = 0;
+        while j < pattern.len() {
+            if name[i + j] != pattern[j] {
+                i += 1;
+                continue 'outer;
+            }
+            j += 1;
+        }
+        return true;
+    }
+    false
+}
+
+// EMIT_MIR gvn_const_eval_polymorphic.optimize_true.GVN.diff
+fn optimize_true<T>() -> bool {
+    // CHECK-LABEL: fn optimize_true(
+    // CHECK: _0 = const true;
+    // CHECK-NEXT: return;
+    (const { type_name_contains_i32(&generic::<i32>) }) == const { true }
+}
+
+// EMIT_MIR gvn_const_eval_polymorphic.optimize_false.GVN.diff
+fn optimize_false<T>() -> bool {
+    // CHECK-LABEL: fn optimize_false(
+    // CHECK: _0 = const false;
+    // CHECK-NEXT: return;
+    (const { type_name_contains_i32(&generic::<i64>) }) == const { true }
+}
+
+// EMIT_MIR gvn_const_eval_polymorphic.no_optimize.GVN.diff
+fn no_optimize<T>() -> bool {
+    // CHECK-LABEL: fn no_optimize(
+    // CHECK: _0 = Eq(const no_optimize::<T>::{constant#0}, const no_optimize::<T>::{constant#1});
+    // CHECK-NEXT: return;
+    (const { type_name_contains_i32(&generic::<T>) }) == const { true }
+}
diff --git a/tests/mir-opt/gvn_type_id_polymorphic.cursed_is_i32.GVN.diff b/tests/mir-opt/gvn_type_id_polymorphic.cursed_is_i32.GVN.diff
deleted file mode 100644
index 2f83f54d2af..00000000000
--- a/tests/mir-opt/gvn_type_id_polymorphic.cursed_is_i32.GVN.diff
+++ /dev/null
@@ -1,12 +0,0 @@
-- // MIR for `cursed_is_i32` before GVN
-+ // MIR for `cursed_is_i32` after GVN
-  
-  fn cursed_is_i32() -> bool {
-      let mut _0: bool;
-  
-      bb0: {
-          _0 = Eq(const cursed_is_i32::<T>::{constant#0}, const cursed_is_i32::<T>::{constant#1});
-          return;
-      }
-  }
-  
diff --git a/tests/mir-opt/gvn_type_id_polymorphic.rs b/tests/mir-opt/gvn_type_id_polymorphic.rs
deleted file mode 100644
index 39bc5c24ecc..00000000000
--- a/tests/mir-opt/gvn_type_id_polymorphic.rs
+++ /dev/null
@@ -1,22 +0,0 @@
-//@ test-mir-pass: GVN
-//@ compile-flags: -C opt-level=2
-
-#![feature(core_intrinsics)]
-
-fn generic<T>() {}
-
-const fn type_id_of_val<T: 'static>(_: &T) -> u128 {
-    std::intrinsics::type_id::<T>()
-}
-
-// EMIT_MIR gvn_type_id_polymorphic.cursed_is_i32.GVN.diff
-fn cursed_is_i32<T: 'static>() -> bool {
-    // CHECK-LABEL: fn cursed_is_i32(
-    // CHECK: _0 = Eq(const cursed_is_i32::<T>::{constant#0}, const cursed_is_i32::<T>::{constant#1});
-    // CHECK-NEXT: return;
-    (const { type_id_of_val(&generic::<T>) } == const { type_id_of_val(&generic::<i32>) })
-}
-
-fn main() {
-    dbg!(cursed_is_i32::<i32>());
-}