]> git.proxmox.com Git - rustc.git/blob - tests/ui/rfc-2632-const-trait-impl/specialization/non-const-default-const-specialized.rs
New upstream version 1.68.2+dfsg1
[rustc.git] / tests / ui / rfc-2632-const-trait-impl / specialization / non-const-default-const-specialized.rs
1 // Tests that a non-const default impl can be specialized by a const trait impl,
2 // but that the default impl cannot be used in a const context.
3
4 // run-pass
5
6 #![feature(const_trait_impl)]
7 #![feature(min_specialization)]
8
9 #[const_trait]
10 trait Value {
11 fn value() -> u32;
12 }
13
14 const fn get_value<T: ~const Value>() -> u32 {
15 T::value()
16 }
17
18 impl<T> Value for T {
19 default fn value() -> u32 {
20 println!("You can't do that (constly)");
21 0
22 }
23 }
24
25 struct FortyTwo;
26
27 impl const Value for FortyTwo {
28 fn value() -> u32 {
29 42
30 }
31 }
32
33 fn main() {
34 let zero = get_value::<()>();
35 assert_eq!(zero, 0);
36
37 const FORTY_TWO: u32 = get_value::<FortyTwo>();
38 assert_eq!(FORTY_TWO, 42);
39 }