]> git.lizzy.rs Git - rust.git/blob - src/test/ui/consts/issue-73976-polymorphic.rs
Rollup merge of #75038 - rust-lang:Havvy-patch-1, r=steveklabnik
[rust.git] / src / test / 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_name)]
9
10 use std::any::{self, 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 constant pattern depends on a generic parameter
21     //~| ERROR constant pattern depends on a generic parameter
22 }
23
24 pub struct GetTypeNameLen<T>(T);
25
26 impl<T: 'static> GetTypeNameLen<T> {
27     pub const VALUE: usize = any::type_name::<T>().len();
28 }
29
30 const fn check_type_name_len<T: 'static>() -> bool {
31     matches!(GetTypeNameLen::<T>::VALUE, GetTypeNameLen::<T>::VALUE)
32     //~^ ERROR constant pattern depends on a generic parameter
33     //~| ERROR constant pattern depends on a generic parameter
34 }
35
36 fn main() {
37     assert!(check_type_id::<usize>());
38     assert!(check_type_name_len::<usize>());
39 }