Skip to content
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

fix ensure_monomorphic_enough #136839

Merged
merged 2 commits into from
Feb 11, 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
44 changes: 3 additions & 41 deletions compiler/rustc_const_eval/src/interpret/util.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
use std::ops::ControlFlow;

use rustc_hir::def_id::LocalDefId;
use rustc_middle::mir;
use rustc_middle::mir::interpret::{AllocInit, Allocation, InterpResult, Pointer};
use rustc_middle::ty::layout::TyAndLayout;
use rustc_middle::ty::{
self, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor,
};
use rustc_middle::ty::{TyCtxt, TypeVisitable, TypeVisitableExt};
use tracing::debug;

use super::{InterpCx, MPlaceTy, MemoryKind, interp_ok, throw_inval};
Expand All @@ -20,44 +16,10 @@ where
T: TypeVisitable<TyCtxt<'tcx>>,
{
debug!("ensure_monomorphic_enough: ty={:?}", ty);
if !ty.has_param() {
return interp_ok(());
}

struct FoundParam;
struct UsedParamsNeedInstantiationVisitor {}

impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for UsedParamsNeedInstantiationVisitor {
type Result = ControlFlow<FoundParam>;

fn visit_ty(&mut self, ty: Ty<'tcx>) -> Self::Result {
if !ty.has_param() {
return ControlFlow::Continue(());
}

match *ty.kind() {
ty::Param(_) => ControlFlow::Break(FoundParam),
ty::Closure(..) | ty::CoroutineClosure(..) | ty::Coroutine(..) | ty::FnDef(..) => {
ControlFlow::Continue(())
}
_ => ty.super_visit_with(self),
}
}

fn visit_const(&mut self, c: ty::Const<'tcx>) -> Self::Result {
match c.kind() {
ty::ConstKind::Param(..) => ControlFlow::Break(FoundParam),
_ => c.super_visit_with(self),
}
}
}

let mut vis = UsedParamsNeedInstantiationVisitor {};
if matches!(ty.visit_with(&mut vis), ControlFlow::Break(FoundParam)) {
if ty.has_param() {
throw_inval!(TooGeneric);
} else {
interp_ok(())
}
interp_ok(())
}

impl<'tcx> InterpretationResult<'tcx> for mir::interpret::ConstAllocation<'tcx> {
Expand Down
12 changes: 12 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,12 @@
- // 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;
}
}

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 = 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>());
}
Loading