Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exercise const trait interaction with default fields #134136

Merged
merged 2 commits into from
Dec 11, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 30 additions & 9 deletions tests/ui/structs/default-field-values-support.rs
jieyouxu marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -22,25 +22,35 @@ pub enum Bar {
}
}

#[derive(Default)]
pub struct Qux<A, const C: i32> {
bar: S = Qux::<A, C>::S,
#[const_trait] pub trait ConstDefault {
fn value() -> Self;
}

impl const ConstDefault for i32 {
fn value() -> i32 {
101
}
}

pub struct Qux<A, const C: i32, X: const ConstDefault> {
bar: S = Qux::<A, C, X>::S,
baz: i32 = foo(),
bat: i32 = <Qux<A, C> as T>::K,
bat: i32 = <Qux<A, C, X> as T>::K,
baq: i32 = Self::K,
bay: i32 = C,
bak: Vec<A> = Vec::new(),
ban: X = X::value(),
}

impl<A, const C: i32> Qux<A, C> {
impl<A, const C: i32, X: const ConstDefault> Qux<A, C, X> {
const S: S = S;
}

trait T {
const K: i32;
}

impl<A, const C: i32> T for Qux<A, C> {
impl<A, const C: i32, X: const ConstDefault> T for Qux<A, C, X> {
const K: i32 = 2;
}

Expand All @@ -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::<i32, 4> { .. };
assert!(matches!(Qux::<i32, 4> { bar: S, baz: 42, bat: 2, baq: 2, bay: 4, .. }, x));
let x = Qux::<i32, 4, i32> { .. };
assert!(matches!(
Qux::<i32, 4, i32> {
bar: S,
baz: 42,
bat: 2,
baq: 2,
bay: 4,
ban: 101,
..
},
x,
));
assert!(x.bak.is_empty());

let x = xc::A { .. };
Expand Down
Loading