Skip to content

Commit b3340b5

Browse files
committedJul 21, 2020
Expand test to cover type_name and monomorphic use
1 parent 4fb260b commit b3340b5

4 files changed

+77
-15
lines changed
 
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+
}

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

+15-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
// Currently we just disallow this usage and require pattern is monomorphic.
77

88
#![feature(const_type_id)]
9+
#![feature(const_type_name)]
910

10-
use std::any::TypeId;
11+
use std::any::{self, TypeId};
1112

1213
pub struct GetTypeId<T>(T);
1314

@@ -21,6 +22,19 @@ const fn check_type_id<T: 'static>() -> bool {
2122
//~| ERROR could not evaluate constant pattern
2223
}
2324

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+
2437
fn main() {
2538
assert!(check_type_id::<usize>());
39+
assert!(check_type_name_len::<usize>());
2640
}
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+

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

-14
This file was deleted.

0 commit comments

Comments
 (0)