Skip to content

Commit

Permalink
add test for const type_id misoptimization
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukas Markeffsky committed Feb 11, 2025
1 parent 4bb6ec0 commit 4898753
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
13 changes: 13 additions & 0 deletions tests/mir-opt/gvn_type_id_polymorphic.cursed_is_i32.GVN.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
- // 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});
+ _0 = const false;
return;
}
}

22 changes: 22 additions & 0 deletions tests/mir-opt/gvn_type_id_polymorphic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//@ 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 = const false;
// CHECK-NEXT: return;
(const { type_id_of_val(&generic::<T>) } == const { type_id_of_val(&generic::<i32>) })
}

fn main() {
dbg!(cursed_is_i32::<i32>());
}

0 comments on commit 4898753

Please sign in to comment.