Skip to content

Commit 1ff6e44

Browse files
authored
Rollup merge of #143444 - lukas-code:gvn-test, r=RalfJung
clean up GVN TypeId test addresses #142789 (comment) 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``
2 parents e0dd7ec + 71176a2 commit 1ff6e44

6 files changed

+95
-34
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
- // MIR for `no_optimize` before GVN
2+
+ // MIR for `no_optimize` after GVN
3+
4+
fn no_optimize() -> bool {
5+
let mut _0: bool;
6+
7+
bb0: {
8+
_0 = Eq(const no_optimize::<T>::{constant#0}, const no_optimize::<T>::{constant#1});
9+
return;
10+
}
11+
}
12+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
- // MIR for `optimize_false` before GVN
2+
+ // MIR for `optimize_false` after GVN
3+
4+
fn optimize_false() -> bool {
5+
let mut _0: bool;
6+
7+
bb0: {
8+
- _0 = Eq(const optimize_false::<T>::{constant#0}, const optimize_false::<T>::{constant#1});
9+
+ _0 = const false;
10+
return;
11+
}
12+
}
13+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
- // MIR for `optimize_true` before GVN
2+
+ // MIR for `optimize_true` after GVN
3+
4+
fn optimize_true() -> bool {
5+
let mut _0: bool;
6+
7+
bb0: {
8+
- _0 = Eq(const optimize_true::<T>::{constant#0}, const optimize_true::<T>::{constant#1});
9+
+ _0 = const true;
10+
return;
11+
}
12+
}
13+
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//@ test-mir-pass: GVN
2+
//@ compile-flags: --crate-type lib
3+
4+
//! Regressions test for a mis-optimization where some functions
5+
//! (`type_id` / `type_name` / `needs_drop`) could be evaluated in
6+
//! a generic context, even though their value depends on some type
7+
//! parameter `T`.
8+
//!
9+
//! In particular, `type_name_of_val(&generic::<T>)` was incorrectly
10+
//! evaluated to the string "crate_name::generic::<T>", and
11+
//! `no_optimize` was incorrectly optimized to `false`.
12+
13+
#![feature(const_type_name)]
14+
15+
fn generic<T>() {}
16+
17+
const fn type_name_contains_i32<T>(_: &T) -> bool {
18+
let pattern = b"i32";
19+
let name = std::any::type_name::<T>().as_bytes();
20+
let mut i = 0;
21+
'outer: while i < name.len() - pattern.len() + 1 {
22+
let mut j = 0;
23+
while j < pattern.len() {
24+
if name[i + j] != pattern[j] {
25+
i += 1;
26+
continue 'outer;
27+
}
28+
j += 1;
29+
}
30+
return true;
31+
}
32+
false
33+
}
34+
35+
// EMIT_MIR gvn_const_eval_polymorphic.optimize_true.GVN.diff
36+
fn optimize_true<T>() -> bool {
37+
// CHECK-LABEL: fn optimize_true(
38+
// CHECK: _0 = const true;
39+
// CHECK-NEXT: return;
40+
(const { type_name_contains_i32(&generic::<i32>) }) == const { true }
41+
}
42+
43+
// EMIT_MIR gvn_const_eval_polymorphic.optimize_false.GVN.diff
44+
fn optimize_false<T>() -> bool {
45+
// CHECK-LABEL: fn optimize_false(
46+
// CHECK: _0 = const false;
47+
// CHECK-NEXT: return;
48+
(const { type_name_contains_i32(&generic::<i64>) }) == const { true }
49+
}
50+
51+
// EMIT_MIR gvn_const_eval_polymorphic.no_optimize.GVN.diff
52+
fn no_optimize<T>() -> bool {
53+
// CHECK-LABEL: fn no_optimize(
54+
// CHECK: _0 = Eq(const no_optimize::<T>::{constant#0}, const no_optimize::<T>::{constant#1});
55+
// CHECK-NEXT: return;
56+
(const { type_name_contains_i32(&generic::<T>) }) == const { true }
57+
}

tests/mir-opt/gvn_type_id_polymorphic.cursed_is_i32.GVN.diff

Lines changed: 0 additions & 12 deletions
This file was deleted.

tests/mir-opt/gvn_type_id_polymorphic.rs

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)