]> git.lizzy.rs Git - rust.git/blob - tests/ui/consts/issue-73976-polymorphic.rs
Auto merge of #106711 - albertlarsan68:use-ci-llvm-when-lld, r=jyn514
[rust.git] / tests / ui / consts / issue-73976-polymorphic.rs
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 bizarre 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 constant pattern depends on a generic parameter
22     //~| ERROR constant pattern depends on a generic parameter
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 constant pattern depends on a generic parameter
34     //~| ERROR constant pattern depends on a generic parameter
35 }
36
37 fn main() {
38     assert!(check_type_id::<usize>());
39     assert!(check_type_name_len::<usize>());
40 }