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:
@@ -1857,7 +1857,12 @@ impl Expr<'_> {
|
||||
base.can_have_side_effects()
|
||||
}
|
||||
ExprKind::Struct(_, fields, init) => {
|
||||
fields.iter().map(|field| field.expr).chain(init).any(|e| e.can_have_side_effects())
|
||||
let init_side_effects = match init {
|
||||
StructTailExpr::Base(init) => init.can_have_side_effects(),
|
||||
StructTailExpr::DefaultFields(_) | StructTailExpr::None => false,
|
||||
};
|
||||
fields.iter().map(|field| field.expr).any(|e| e.can_have_side_effects())
|
||||
|| init_side_effects
|
||||
}
|
||||
|
||||
ExprKind::Array(args)
|
||||
@@ -1926,20 +1931,52 @@ impl Expr<'_> {
|
||||
ExprKind::Path(QPath::Resolved(None, path2)),
|
||||
) => path1.res == path2.res,
|
||||
(
|
||||
ExprKind::Struct(QPath::LangItem(LangItem::RangeTo, _), [val1], None),
|
||||
ExprKind::Struct(QPath::LangItem(LangItem::RangeTo, _), [val2], None),
|
||||
ExprKind::Struct(
|
||||
QPath::LangItem(LangItem::RangeTo, _),
|
||||
[val1],
|
||||
StructTailExpr::None,
|
||||
),
|
||||
ExprKind::Struct(
|
||||
QPath::LangItem(LangItem::RangeTo, _),
|
||||
[val2],
|
||||
StructTailExpr::None,
|
||||
),
|
||||
)
|
||||
| (
|
||||
ExprKind::Struct(QPath::LangItem(LangItem::RangeToInclusive, _), [val1], None),
|
||||
ExprKind::Struct(QPath::LangItem(LangItem::RangeToInclusive, _), [val2], None),
|
||||
ExprKind::Struct(
|
||||
QPath::LangItem(LangItem::RangeToInclusive, _),
|
||||
[val1],
|
||||
StructTailExpr::None,
|
||||
),
|
||||
ExprKind::Struct(
|
||||
QPath::LangItem(LangItem::RangeToInclusive, _),
|
||||
[val2],
|
||||
StructTailExpr::None,
|
||||
),
|
||||
)
|
||||
| (
|
||||
ExprKind::Struct(QPath::LangItem(LangItem::RangeFrom, _), [val1], None),
|
||||
ExprKind::Struct(QPath::LangItem(LangItem::RangeFrom, _), [val2], None),
|
||||
ExprKind::Struct(
|
||||
QPath::LangItem(LangItem::RangeFrom, _),
|
||||
[val1],
|
||||
StructTailExpr::None,
|
||||
),
|
||||
ExprKind::Struct(
|
||||
QPath::LangItem(LangItem::RangeFrom, _),
|
||||
[val2],
|
||||
StructTailExpr::None,
|
||||
),
|
||||
) => val1.expr.equivalent_for_indexing(val2.expr),
|
||||
(
|
||||
ExprKind::Struct(QPath::LangItem(LangItem::Range, _), [val1, val3], None),
|
||||
ExprKind::Struct(QPath::LangItem(LangItem::Range, _), [val2, val4], None),
|
||||
ExprKind::Struct(
|
||||
QPath::LangItem(LangItem::Range, _),
|
||||
[val1, val3],
|
||||
StructTailExpr::None,
|
||||
),
|
||||
ExprKind::Struct(
|
||||
QPath::LangItem(LangItem::Range, _),
|
||||
[val2, val4],
|
||||
StructTailExpr::None,
|
||||
),
|
||||
) => {
|
||||
val1.expr.equivalent_for_indexing(val2.expr)
|
||||
&& val3.expr.equivalent_for_indexing(val4.expr)
|
||||
@@ -2096,7 +2133,7 @@ pub enum ExprKind<'hir> {
|
||||
///
|
||||
/// E.g., `Foo {x: 1, y: 2}`, or `Foo {x: 1, .. base}`,
|
||||
/// where `base` is the `Option<Expr>`.
|
||||
Struct(&'hir QPath<'hir>, &'hir [ExprField<'hir>], Option<&'hir Expr<'hir>>),
|
||||
Struct(&'hir QPath<'hir>, &'hir [ExprField<'hir>], StructTailExpr<'hir>),
|
||||
|
||||
/// An array literal constructed from one repeated element.
|
||||
///
|
||||
@@ -2111,6 +2148,19 @@ pub enum ExprKind<'hir> {
|
||||
Err(rustc_span::ErrorGuaranteed),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, HashStable_Generic)]
|
||||
pub enum StructTailExpr<'hir> {
|
||||
/// A struct expression where all the fields are explicitly enumerated: `Foo { a, b }`.
|
||||
None,
|
||||
/// A struct expression with a "base", an expression of the same type as the outer struct that
|
||||
/// will be used to populate any fields not explicitly mentioned: `Foo { ..base }`
|
||||
Base(&'hir Expr<'hir>),
|
||||
/// A struct expression with a `..` tail but no "base" expression. The values from the struct
|
||||
/// fields' default values will be used to populate any fields not explicitly mentioned:
|
||||
/// `Foo { .. }`.
|
||||
DefaultFields(Span),
|
||||
}
|
||||
|
||||
/// Represents an optionally `Self`-qualified value/type path or associated extension.
|
||||
///
|
||||
/// To resolve the path to a `DefId`, call [`qpath_res`].
|
||||
@@ -3172,6 +3222,7 @@ pub struct FieldDef<'hir> {
|
||||
pub def_id: LocalDefId,
|
||||
pub ty: &'hir Ty<'hir>,
|
||||
pub safety: Safety,
|
||||
pub default: Option<&'hir AnonConst>,
|
||||
}
|
||||
|
||||
impl FieldDef<'_> {
|
||||
|
||||
Reference in New Issue
Block a user