1
+ // Exercise the `default_field_values` feature to confirm it interacts correctly with other nightly
2
+ // features. In particular, we want to verify that interaction with consts coming from different
3
+ // contexts are usable as a default field value.
1
4
//@ run-pass
2
5
//@ aux-build:struct_field_default.rs
3
- #![ feature( default_field_values, generic_const_exprs) ]
6
+ #![ feature( const_trait_impl , default_field_values, generic_const_exprs) ]
4
7
#![ allow( unused_variables, dead_code, incomplete_features) ]
5
8
6
9
extern crate struct_field_default as xc;
7
10
8
11
pub struct S ;
9
12
13
+ // Basic expressions and `Default` expansion
10
14
#[ derive( Default ) ]
11
15
pub struct Foo {
12
16
pub bar : S = S ,
13
17
pub baz : i32 = 42 + 3 ,
14
18
}
15
19
20
+ // Enum support for deriving `Default` when all fields have default values
16
21
#[ derive( Default ) ]
17
22
pub enum Bar {
18
23
#[ default]
@@ -22,25 +27,35 @@ pub enum Bar {
22
27
}
23
28
}
24
29
25
- #[ derive( Default ) ]
26
- pub struct Qux < A , const C : i32 > {
27
- bar : S = Qux :: < A , C > :: S ,
28
- baz : i32 = foo ( ) ,
29
- bat : i32 = <Qux < A , C > as T >:: K ,
30
- baq : i32 = Self :: K ,
31
- bay : i32 = C ,
32
- bak : Vec < A > = Vec :: new ( ) ,
30
+ #[ const_trait] pub trait ConstDefault {
31
+ fn value ( ) -> Self ;
32
+ }
33
+
34
+ impl const ConstDefault for i32 {
35
+ fn value ( ) -> i32 {
36
+ 101
37
+ }
38
+ }
39
+
40
+ pub struct Qux < A , const C : i32 , X : const ConstDefault > {
41
+ bar : S = Qux :: < A , C , X > :: S , // Associated constant from inherent impl
42
+ baz : i32 = foo ( ) , // Constant function
43
+ bat : i32 = <Qux < A , C , X > as T >:: K , // Associated constant from explicit trait
44
+ baq : i32 = Self :: K , // Associated constant from implicit trait
45
+ bay : i32 = C , // `const` parameter
46
+ bak : Vec < A > = Vec :: new ( ) , // Associated constant function
47
+ ban : X = X :: value ( ) , // Associated constant function from `const` trait parameter
33
48
}
34
49
35
- impl < A , const C : i32 > Qux < A , C > {
50
+ impl < A , const C : i32 , X : const ConstDefault > Qux < A , C , X > {
36
51
const S : S = S ;
37
52
}
38
53
39
54
trait T {
40
55
const K : i32 ;
41
56
}
42
57
43
- impl < A , const C : i32 > T for Qux < A , C > {
58
+ impl < A , const C : i32 , X : const ConstDefault > T for Qux < A , C , X > {
44
59
const K : i32 = 2 ;
45
60
}
46
61
@@ -65,8 +80,19 @@ fn main () {
65
80
assert ! ( matches!( Bar :: Foo { bar: S , baz: 45 } , y) ) ;
66
81
assert ! ( matches!( Bar :: Foo { bar: S , baz: 1 } , z) ) ;
67
82
68
- let x = Qux :: < i32 , 4 > { .. } ;
69
- assert ! ( matches!( Qux :: <i32 , 4 > { bar: S , baz: 42 , bat: 2 , baq: 2 , bay: 4 , .. } , x) ) ;
83
+ let x = Qux :: < i32 , 4 , i32 > { .. } ;
84
+ assert ! ( matches!(
85
+ Qux :: <i32 , 4 , i32 > {
86
+ bar: S ,
87
+ baz: 42 ,
88
+ bat: 2 ,
89
+ baq: 2 ,
90
+ bay: 4 ,
91
+ ban: 101 ,
92
+ ..
93
+ } ,
94
+ x,
95
+ ) ) ;
70
96
assert ! ( x. bak. is_empty( ) ) ;
71
97
72
98
let x = xc:: A { .. } ;
0 commit comments