Skip to content

Commit 4fb260b

Browse files
committed
Guard against non-monomorphized type_id intrinsic call
1 parent d7f9451 commit 4fb260b

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

src/librustc_mir/interpret/intrinsics.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_middle::mir::{
1212
};
1313
use rustc_middle::ty;
1414
use rustc_middle::ty::subst::SubstsRef;
15-
use rustc_middle::ty::{Ty, TyCtxt};
15+
use rustc_middle::ty::{Ty, TyCtxt, TypeFoldable};
1616
use rustc_span::symbol::{sym, Symbol};
1717
use rustc_target::abi::{Abi, LayoutOf as _, Primitive, Size};
1818

@@ -54,6 +54,9 @@ crate fn eval_nullary_intrinsic<'tcx>(
5454
let name = tcx.item_name(def_id);
5555
Ok(match name {
5656
sym::type_name => {
57+
if tp_ty.needs_subst() {
58+
throw_inval!(TooGeneric);
59+
}
5760
let alloc = type_name::alloc_type_name(tcx, tp_ty);
5861
ConstValue::Slice { data: alloc, start: 0, end: alloc.len() }
5962
}
@@ -68,7 +71,12 @@ crate fn eval_nullary_intrinsic<'tcx>(
6871
};
6972
ConstValue::from_machine_usize(n, &tcx)
7073
}
71-
sym::type_id => ConstValue::from_u64(tcx.type_id_hash(tp_ty)),
74+
sym::type_id => {
75+
if tp_ty.needs_subst() {
76+
throw_inval!(TooGeneric);
77+
}
78+
ConstValue::from_u64(tcx.type_id_hash(tp_ty))
79+
}
7280
sym::variant_count => {
7381
if let ty::Adt(ref adt, _) = tp_ty.kind {
7482
ConstValue::from_machine_usize(adt.variants.len() as u64, &tcx)

src/test/ui/consts/issue-73976.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// This test is from #73976. We previously did not check if a type is monomorphized
2+
// before calculating its type id, which leads to the bizzare behaviour below that
3+
// TypeId of a generic type does not match itself.
4+
//
5+
// This test case should either run-pass or be rejected at compile time.
6+
// Currently we just disallow this usage and require pattern is monomorphic.
7+
8+
#![feature(const_type_id)]
9+
10+
use std::any::TypeId;
11+
12+
pub struct GetTypeId<T>(T);
13+
14+
impl<T: 'static> GetTypeId<T> {
15+
pub const VALUE: TypeId = TypeId::of::<T>();
16+
}
17+
18+
const fn check_type_id<T: 'static>() -> bool {
19+
matches!(GetTypeId::<T>::VALUE, GetTypeId::<T>::VALUE)
20+
//~^ ERROR could not evaluate constant pattern
21+
//~| ERROR could not evaluate constant pattern
22+
}
23+
24+
fn main() {
25+
assert!(check_type_id::<usize>());
26+
}

src/test/ui/consts/issue-73976.stderr

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: could not evaluate constant pattern
2+
--> $DIR/issue-73976.rs:19:37
3+
|
4+
LL | matches!(GetTypeId::<T>::VALUE, GetTypeId::<T>::VALUE)
5+
| ^^^^^^^^^^^^^^^^^^^^^
6+
7+
error: could not evaluate constant pattern
8+
--> $DIR/issue-73976.rs:19:37
9+
|
10+
LL | matches!(GetTypeId::<T>::VALUE, GetTypeId::<T>::VALUE)
11+
| ^^^^^^^^^^^^^^^^^^^^^
12+
13+
error: aborting due to 2 previous errors
14+

0 commit comments

Comments
 (0)