Skip to content

clean up GVN TypeId test #143444

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions tests/mir-opt/gvn_const_eval_polymorphic.no_optimize.GVN.diff
Original file line number Diff line number Diff line change
@@ -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;
}
}

13 changes: 13 additions & 0 deletions tests/mir-opt/gvn_const_eval_polymorphic.optimize_false.GVN.diff
Original file line number Diff line number Diff line change
@@ -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;
}
}

13 changes: 13 additions & 0 deletions tests/mir-opt/gvn_const_eval_polymorphic.optimize_true.GVN.diff
Original file line number Diff line number Diff line change
@@ -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;
}
}

57 changes: 57 additions & 0 deletions tests/mir-opt/gvn_const_eval_polymorphic.rs
Original file line number Diff line number Diff line change
@@ -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 }
}
12 changes: 0 additions & 12 deletions tests/mir-opt/gvn_type_id_polymorphic.cursed_is_i32.GVN.diff

This file was deleted.

22 changes: 0 additions & 22 deletions tests/mir-opt/gvn_type_id_polymorphic.rs

This file was deleted.

Loading