Skip to content

Commit 26dbd93

Browse files
authored
Rollup merge of rust-lang#74538 - nbdd0121:issue-73976, r=lcnr
Guard against non-monomorphized type_id intrinsic call This PR checks whether the type is sufficient monomorphized when calling type_id or type_name intrinsics. If the type is not sufficiently monomorphized, e.g. used in a pattern, the code will be rejected. Fixes rust-lang#73976
2 parents 83e5b5e + b3340b5 commit 26dbd93

File tree

4 files changed

+112
-2
lines changed

4 files changed

+112
-2
lines changed

src/librustc_mir/interpret/intrinsics.rs

+10-2
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)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// check-pass
2+
//
3+
// This test is complement to the test in issue-73976-polymorphic.rs.
4+
// In that test we ensure that polymorphic use of type_id and type_name in patterns
5+
// will be properly rejected. This test will ensure that monomorphic use of these
6+
// would not be wrongly rejected in patterns.
7+
8+
#![feature(const_type_id)]
9+
#![feature(const_type_name)]
10+
11+
use std::any::{self, TypeId};
12+
13+
pub struct GetTypeId<T>(T);
14+
15+
impl<T: 'static> GetTypeId<T> {
16+
pub const VALUE: TypeId = TypeId::of::<T>();
17+
}
18+
19+
const fn check_type_id<T: 'static>() -> bool {
20+
matches!(GetTypeId::<T>::VALUE, GetTypeId::<usize>::VALUE)
21+
}
22+
23+
pub struct GetTypeNameLen<T>(T);
24+
25+
impl<T: 'static> GetTypeNameLen<T> {
26+
pub const VALUE: usize = any::type_name::<T>().len();
27+
}
28+
29+
const fn check_type_name_len<T: 'static>() -> bool {
30+
matches!(GetTypeNameLen::<T>::VALUE, GetTypeNameLen::<usize>::VALUE)
31+
}
32+
33+
fn main() {
34+
assert!(check_type_id::<usize>());
35+
assert!(check_type_name_len::<usize>());
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
#![feature(const_type_name)]
10+
11+
use std::any::{self, TypeId};
12+
13+
pub struct GetTypeId<T>(T);
14+
15+
impl<T: 'static> GetTypeId<T> {
16+
pub const VALUE: TypeId = TypeId::of::<T>();
17+
}
18+
19+
const fn check_type_id<T: 'static>() -> bool {
20+
matches!(GetTypeId::<T>::VALUE, GetTypeId::<T>::VALUE)
21+
//~^ ERROR could not evaluate constant pattern
22+
//~| ERROR could not evaluate constant pattern
23+
}
24+
25+
pub struct GetTypeNameLen<T>(T);
26+
27+
impl<T: 'static> GetTypeNameLen<T> {
28+
pub const VALUE: usize = any::type_name::<T>().len();
29+
}
30+
31+
const fn check_type_name_len<T: 'static>() -> bool {
32+
matches!(GetTypeNameLen::<T>::VALUE, GetTypeNameLen::<T>::VALUE)
33+
//~^ ERROR could not evaluate constant pattern
34+
//~| ERROR could not evaluate constant pattern
35+
}
36+
37+
fn main() {
38+
assert!(check_type_id::<usize>());
39+
assert!(check_type_name_len::<usize>());
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error: could not evaluate constant pattern
2+
--> $DIR/issue-73976-polymorphic.rs:20:37
3+
|
4+
LL | matches!(GetTypeId::<T>::VALUE, GetTypeId::<T>::VALUE)
5+
| ^^^^^^^^^^^^^^^^^^^^^
6+
7+
error: could not evaluate constant pattern
8+
--> $DIR/issue-73976-polymorphic.rs:32:42
9+
|
10+
LL | matches!(GetTypeNameLen::<T>::VALUE, GetTypeNameLen::<T>::VALUE)
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
12+
13+
error: could not evaluate constant pattern
14+
--> $DIR/issue-73976-polymorphic.rs:20:37
15+
|
16+
LL | matches!(GetTypeId::<T>::VALUE, GetTypeId::<T>::VALUE)
17+
| ^^^^^^^^^^^^^^^^^^^^^
18+
19+
error: could not evaluate constant pattern
20+
--> $DIR/issue-73976-polymorphic.rs:32:42
21+
|
22+
LL | matches!(GetTypeNameLen::<T>::VALUE, GetTypeNameLen::<T>::VALUE)
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
24+
25+
error: aborting due to 4 previous errors
26+

0 commit comments

Comments
 (0)