Skip to content

Commit 2130310

Browse files
authored
Rollup merge of #136839 - lukas-code:actually-monomorphic-enough, r=compiler-errors
fix ensure_monomorphic_enough When polymorphization was still a thing, the visitor was used to only recurse into *used generic parameters* of function/closure/coroutine types and allow unused parameters (i.e. the polymorphized parameters) to remain generic. When polymorphization got removed, this got changed to always treat all parameters as polymorphic and never recurse into them: https://github.com/rust-lang/rust/pull/133883/files#diff-210c59e321070d0ca4625c04e9fb064bf43ddc34082e7e33a7ee8a6c577e95afL44-L62 This is clearly wrong and can cause MIR opts to misbehave, for example this currently prints "false" in release mode: ```rust #![feature(core_intrinsics)] fn generic<T>() {} const fn type_id_of_val<T: 'static>(_: &T) -> u128 { std::intrinsics::type_id::<T>() } fn cursed_is_i32<T: 'static>() -> bool { (const { type_id_of_val(&generic::<T>) } == const { type_id_of_val(&generic::<i32>) }) } fn main() { dbg!(cursed_is_i32::<i32>()); } ``` This PR reverts to the old behavior of always treating all types that contain type parameters as too generic, like we used to do without `-Zpolymorphize` before. ~~I'm not including the above as a test case here, because I think there is little value in testing code paths that have been removed and this seems unlikely to regress in a way that would be caught by a regression test, but let me know if you disagree and want me to add a test anyway.~~
2 parents 4ae214b + c1da4f1 commit 2130310

File tree

3 files changed

+37
-41
lines changed

3 files changed

+37
-41
lines changed

compiler/rustc_const_eval/src/interpret/util.rs

+3-41
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
use std::ops::ControlFlow;
2-
31
use rustc_hir::def_id::LocalDefId;
42
use rustc_middle::mir;
53
use rustc_middle::mir::interpret::{AllocInit, Allocation, InterpResult, Pointer};
64
use rustc_middle::ty::layout::TyAndLayout;
7-
use rustc_middle::ty::{
8-
self, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor,
9-
};
5+
use rustc_middle::ty::{TyCtxt, TypeVisitable, TypeVisitableExt};
106
use tracing::debug;
117

128
use super::{InterpCx, MPlaceTy, MemoryKind, interp_ok, throw_inval};
@@ -20,44 +16,10 @@ where
2016
T: TypeVisitable<TyCtxt<'tcx>>,
2117
{
2218
debug!("ensure_monomorphic_enough: ty={:?}", ty);
23-
if !ty.has_param() {
24-
return interp_ok(());
25-
}
26-
27-
struct FoundParam;
28-
struct UsedParamsNeedInstantiationVisitor {}
29-
30-
impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for UsedParamsNeedInstantiationVisitor {
31-
type Result = ControlFlow<FoundParam>;
32-
33-
fn visit_ty(&mut self, ty: Ty<'tcx>) -> Self::Result {
34-
if !ty.has_param() {
35-
return ControlFlow::Continue(());
36-
}
37-
38-
match *ty.kind() {
39-
ty::Param(_) => ControlFlow::Break(FoundParam),
40-
ty::Closure(..) | ty::CoroutineClosure(..) | ty::Coroutine(..) | ty::FnDef(..) => {
41-
ControlFlow::Continue(())
42-
}
43-
_ => ty.super_visit_with(self),
44-
}
45-
}
46-
47-
fn visit_const(&mut self, c: ty::Const<'tcx>) -> Self::Result {
48-
match c.kind() {
49-
ty::ConstKind::Param(..) => ControlFlow::Break(FoundParam),
50-
_ => c.super_visit_with(self),
51-
}
52-
}
53-
}
54-
55-
let mut vis = UsedParamsNeedInstantiationVisitor {};
56-
if matches!(ty.visit_with(&mut vis), ControlFlow::Break(FoundParam)) {
19+
if ty.has_param() {
5720
throw_inval!(TooGeneric);
58-
} else {
59-
interp_ok(())
6021
}
22+
interp_ok(())
6123
}
6224

6325
impl<'tcx> InterpretationResult<'tcx> for mir::interpret::ConstAllocation<'tcx> {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
- // MIR for `cursed_is_i32` before GVN
2+
+ // MIR for `cursed_is_i32` after GVN
3+
4+
fn cursed_is_i32() -> bool {
5+
let mut _0: bool;
6+
7+
bb0: {
8+
_0 = Eq(const cursed_is_i32::<T>::{constant#0}, const cursed_is_i32::<T>::{constant#1});
9+
return;
10+
}
11+
}
12+
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//@ test-mir-pass: GVN
2+
//@ compile-flags: -C opt-level=2
3+
4+
#![feature(core_intrinsics)]
5+
6+
fn generic<T>() {}
7+
8+
const fn type_id_of_val<T: 'static>(_: &T) -> u128 {
9+
std::intrinsics::type_id::<T>()
10+
}
11+
12+
// EMIT_MIR gvn_type_id_polymorphic.cursed_is_i32.GVN.diff
13+
fn cursed_is_i32<T: 'static>() -> bool {
14+
// CHECK-LABEL: fn cursed_is_i32(
15+
// CHECK: _0 = Eq(const cursed_is_i32::<T>::{constant#0}, const cursed_is_i32::<T>::{constant#1});
16+
// CHECK-NEXT: return;
17+
(const { type_id_of_val(&generic::<T>) } == const { type_id_of_val(&generic::<i32>) })
18+
}
19+
20+
fn main() {
21+
dbg!(cursed_is_i32::<i32>());
22+
}

0 commit comments

Comments
 (0)