Introduce default_field_values
feature
Initial implementation of `#[feature(default_field_values]`, proposed in https://github.com/rust-lang/rfcs/pull/3681. Support default fields in enum struct variant Allow default values in an enum struct variant definition: ```rust pub enum Bar { Foo { bar: S = S, baz: i32 = 42 + 3, } } ``` Allow using `..` without a base on an enum struct variant ```rust Bar::Foo { .. } ``` `#[derive(Default)]` doesn't account for these as it is still gating `#[default]` only being allowed on unit variants. Support `#[derive(Default)]` on enum struct variants with all defaulted fields ```rust pub enum Bar { #[default] Foo { bar: S = S, baz: i32 = 42 + 3, } } ``` Check for missing fields in typeck instead of mir_build. Expand test with `const` param case (needs `generic_const_exprs` enabled). Properly instantiate MIR const The following works: ```rust struct S<A> { a: Vec<A> = Vec::new(), } S::<i32> { .. } ``` Add lint for default fields that will always fail const-eval We *allow* this to happen for API writers that might want to rely on users' getting a compile error when using the default field, different to the error that they would get when the field isn't default. We could change this to *always* error instead of being a lint, if we wanted. This will *not* catch errors for partially evaluated consts, like when the expression relies on a const parameter. Suggestions when encountering `Foo { .. }` without `#[feature(default_field_values)]`: - Suggest adding a base expression if there are missing fields. - Suggest enabling the feature if all the missing fields have optional values. - Suggest removing `..` if there are no missing fields.
This commit is contained in:
parent
f6cb952dc1
commit
9ac95c10c0
70 changed files with 1469 additions and 392 deletions
|
@ -169,9 +169,6 @@ parse_enum_struct_mutually_exclusive = `enum` and `struct` are mutually exclusiv
|
|||
parse_eq_field_init = expected `:`, found `=`
|
||||
.suggestion = replace equals symbol with a colon
|
||||
|
||||
parse_equals_struct_default = default values on `struct` fields aren't supported
|
||||
.suggestion = remove this unsupported default value
|
||||
|
||||
parse_escape_only_char = {$byte ->
|
||||
[true] byte
|
||||
*[false] character
|
||||
|
|
|
@ -3066,14 +3066,6 @@ pub(crate) struct SingleColonStructType {
|
|||
pub span: Span,
|
||||
}
|
||||
|
||||
#[derive(Diagnostic)]
|
||||
#[diag(parse_equals_struct_default)]
|
||||
pub(crate) struct EqualsStructDefault {
|
||||
#[primary_span]
|
||||
#[suggestion(code = "", applicability = "machine-applicable", style = "verbose")]
|
||||
pub span: Span,
|
||||
}
|
||||
|
||||
#[derive(Diagnostic)]
|
||||
#[diag(parse_macro_rules_missing_bang)]
|
||||
pub(crate) struct MacroRulesMissingBang {
|
||||
|
|
|
@ -3533,7 +3533,7 @@ impl<'a> Parser<'a> {
|
|||
let exp_span = self.prev_token.span;
|
||||
// We permit `.. }` on the left-hand side of a destructuring assignment.
|
||||
if self.check(&token::CloseDelim(close_delim)) {
|
||||
base = ast::StructRest::Rest(self.prev_token.span.shrink_to_hi());
|
||||
base = ast::StructRest::Rest(self.prev_token.span);
|
||||
break;
|
||||
}
|
||||
match self.parse_expr() {
|
||||
|
|
|
@ -1845,6 +1845,7 @@ impl<'a> Parser<'a> {
|
|||
ident: None,
|
||||
id: DUMMY_NODE_ID,
|
||||
ty,
|
||||
default: None,
|
||||
attrs,
|
||||
is_placeholder: false,
|
||||
},
|
||||
|
@ -2024,12 +2025,15 @@ impl<'a> Parser<'a> {
|
|||
if self.token == token::Colon && self.look_ahead(1, |t| *t != token::Colon) {
|
||||
self.dcx().emit_err(errors::SingleColonStructType { span: self.token.span });
|
||||
}
|
||||
if self.token == token::Eq {
|
||||
let default = if self.token == token::Eq {
|
||||
self.bump();
|
||||
let const_expr = self.parse_expr_anon_const()?;
|
||||
let sp = ty.span.shrink_to_hi().to(const_expr.value.span);
|
||||
self.dcx().emit_err(errors::EqualsStructDefault { span: sp });
|
||||
}
|
||||
self.psess.gated_spans.gate(sym::default_field_values, sp);
|
||||
Some(const_expr)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
Ok(FieldDef {
|
||||
span: lo.to(self.prev_token.span),
|
||||
ident: Some(name),
|
||||
|
@ -2037,6 +2041,7 @@ impl<'a> Parser<'a> {
|
|||
safety,
|
||||
id: DUMMY_NODE_ID,
|
||||
ty,
|
||||
default,
|
||||
attrs,
|
||||
is_placeholder: false,
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue