Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2fbf703

Browse files
authoredDec 11, 2024
Unrolled build for rust-lang#134136
Rollup merge of rust-lang#134136 - estebank:const-trait-default-field-test, r=jieyouxu Exercise const trait interaction with default fields Add a test case for using the result of a fn call of an associated function of a `const` trait in a struct default field. ```rust struct X; trait Trait { fn value() -> Self; } impl const Trait for X { fn value() -> Self { X } } struct S<T: const Trait> { a: T = T::value(), } ```
2 parents 5a6036a + 979eb4e commit 2fbf703

File tree

1 file changed

+39
-13
lines changed

1 file changed

+39
-13
lines changed
 

‎tests/ui/structs/default-field-values-support.rs

+39-13
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
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.
14
//@ run-pass
25
//@ 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)]
47
#![allow(unused_variables, dead_code, incomplete_features)]
58

69
extern crate struct_field_default as xc;
710

811
pub struct S;
912

13+
// Basic expressions and `Default` expansion
1014
#[derive(Default)]
1115
pub struct Foo {
1216
pub bar: S = S,
1317
pub baz: i32 = 42 + 3,
1418
}
1519

20+
// Enum support for deriving `Default` when all fields have default values
1621
#[derive(Default)]
1722
pub enum Bar {
1823
#[default]
@@ -22,25 +27,35 @@ pub enum Bar {
2227
}
2328
}
2429

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
3348
}
3449

35-
impl<A, const C: i32> Qux<A, C> {
50+
impl<A, const C: i32, X: const ConstDefault> Qux<A, C, X> {
3651
const S: S = S;
3752
}
3853

3954
trait T {
4055
const K: i32;
4156
}
4257

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> {
4459
const K: i32 = 2;
4560
}
4661

@@ -65,8 +80,19 @@ fn main () {
6580
assert!(matches!(Bar::Foo { bar: S, baz: 45 }, y));
6681
assert!(matches!(Bar::Foo { bar: S, baz: 1 }, z));
6782

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+
));
7096
assert!(x.bak.is_empty());
7197

7298
let x = xc::A { .. };

0 commit comments

Comments
 (0)
Failed to load comments.