From 5eac9c00561751c78cc3f9dd914600c1ddff827b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Mon, 9 Dec 2024 23:40:54 +0000 Subject: [PATCH 1/2] Excercise 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 { a: T = T::value(), } ``` --- .../structs/default-field-values-support.rs | 39 ++++++++++++++----- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/tests/ui/structs/default-field-values-support.rs b/tests/ui/structs/default-field-values-support.rs index da0379af94b8f..9efba3ca2ce15 100644 --- a/tests/ui/structs/default-field-values-support.rs +++ b/tests/ui/structs/default-field-values-support.rs @@ -1,6 +1,6 @@ //@ run-pass //@ aux-build:struct_field_default.rs -#![feature(default_field_values, generic_const_exprs)] +#![feature(const_trait_impl, default_field_values, generic_const_exprs)] #![allow(unused_variables, dead_code, incomplete_features)] extern crate struct_field_default as xc; @@ -22,17 +22,27 @@ pub enum Bar { } } -#[derive(Default)] -pub struct Qux { - bar: S = Qux::::S, +#[const_trait] pub trait ConstDefault { + fn value() -> Self; +} + +impl const ConstDefault for i32 { + fn value() -> i32 { + 101 + } +} + +pub struct Qux { + bar: S = Qux::::S, baz: i32 = foo(), - bat: i32 = as T>::K, + bat: i32 = as T>::K, baq: i32 = Self::K, bay: i32 = C, bak: Vec = Vec::new(), + ban: X = X::value(), } -impl Qux { +impl Qux { const S: S = S; } @@ -40,7 +50,7 @@ trait T { const K: i32; } -impl T for Qux { +impl T for Qux { const K: i32 = 2; } @@ -65,8 +75,19 @@ fn main () { assert!(matches!(Bar::Foo { bar: S, baz: 45 }, y)); assert!(matches!(Bar::Foo { bar: S, baz: 1 }, z)); - let x = Qux:: { .. }; - assert!(matches!(Qux:: { bar: S, baz: 42, bat: 2, baq: 2, bay: 4, .. }, x)); + let x = Qux:: { .. }; + assert!(matches!( + Qux:: { + bar: S, + baz: 42, + bat: 2, + baq: 2, + bay: 4, + ban: 101, + .. + }, + x, + )); assert!(x.bak.is_empty()); let x = xc::A { .. }; From 979eb4e98eac8b9888fedd8ebd0a0e354b82fe21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Tue, 10 Dec 2024 19:21:07 +0000 Subject: [PATCH 2/2] Further document default field test --- .../structs/default-field-values-support.rs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/tests/ui/structs/default-field-values-support.rs b/tests/ui/structs/default-field-values-support.rs index 9efba3ca2ce15..8209d6dd4a093 100644 --- a/tests/ui/structs/default-field-values-support.rs +++ b/tests/ui/structs/default-field-values-support.rs @@ -1,3 +1,6 @@ +// Exercise the `default_field_values` feature to confirm it interacts correctly with other nightly +// features. In particular, we want to verify that interaction with consts coming from different +// contexts are usable as a default field value. //@ run-pass //@ aux-build:struct_field_default.rs #![feature(const_trait_impl, default_field_values, generic_const_exprs)] @@ -7,12 +10,14 @@ extern crate struct_field_default as xc; pub struct S; +// Basic expressions and `Default` expansion #[derive(Default)] pub struct Foo { pub bar: S = S, pub baz: i32 = 42 + 3, } +// Enum support for deriving `Default` when all fields have default values #[derive(Default)] pub enum Bar { #[default] @@ -33,13 +38,13 @@ impl const ConstDefault for i32 { } pub struct Qux { - bar: S = Qux::::S, - baz: i32 = foo(), - bat: i32 = as T>::K, - baq: i32 = Self::K, - bay: i32 = C, - bak: Vec = Vec::new(), - ban: X = X::value(), + bar: S = Qux::::S, // Associated constant from inherent impl + baz: i32 = foo(), // Constant function + bat: i32 = as T>::K, // Associated constant from explicit trait + baq: i32 = Self::K, // Associated constant from implicit trait + bay: i32 = C, // `const` parameter + bak: Vec = Vec::new(), // Associated constant function + ban: X = X::value(), // Associated constant function from `const` trait parameter } impl Qux {