From e9c76a30f2ac7b283873e52c81216a591c639d3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sat, 24 Aug 2024 17:22:48 +0000 Subject: [PATCH] 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: Vec = Vec::new(), } S:: { .. } ``` --- compiler/rustc_ast/src/ast.rs | 1 + compiler/rustc_ast/src/mut_visit.rs | 3 +- compiler/rustc_ast/src/visit.rs | 3 +- compiler/rustc_ast_lowering/src/expr.rs | 12 +- compiler/rustc_ast_lowering/src/item.rs | 1 + compiler/rustc_ast_passes/src/feature_gate.rs | 1 + .../src/diagnostics/conflict_errors.rs | 6 +- .../src/deriving/decodable.rs | 2 +- .../src/deriving/default.rs | 67 +++++-- .../src/deriving/generic/mod.rs | 8 +- compiler/rustc_expand/src/placeholders.rs | 1 + compiler/rustc_feature/src/unstable.rs | 5 + compiler/rustc_hir/src/hir.rs | 41 +++- compiler/rustc_hir/src/intravisit.rs | 17 +- compiler/rustc_hir_analysis/src/collect.rs | 7 +- .../rustc_hir_analysis/src/collect/type_of.rs | 4 + compiler/rustc_hir_pretty/src/lib.rs | 12 +- compiler/rustc_hir_typeck/src/expr.rs | 50 ++++- .../rustc_hir_typeck/src/expr_use_visitor.rs | 6 +- .../rustc_hir_typeck/src/fn_ctxt/checks.rs | 2 +- compiler/rustc_hir_typeck/src/pat.rs | 2 +- compiler/rustc_metadata/src/rmeta/decoder.rs | 1 + compiler/rustc_middle/src/thir.rs | 11 +- compiler/rustc_middle/src/thir/visit.rs | 2 +- compiler/rustc_middle/src/ty/adt.rs | 4 +- compiler/rustc_middle/src/ty/mod.rs | 7 +- .../src/build/custom/parse/instruction.rs | 2 +- .../rustc_mir_build/src/build/expr/into.rs | 34 +++- compiler/rustc_mir_build/src/thir/cx/expr.rs | 48 +++-- compiler/rustc_mir_build/src/thir/print.rs | 4 +- compiler/rustc_parse/messages.ftl | 3 - compiler/rustc_parse/src/errors.rs | 8 - compiler/rustc_parse/src/parser/item.rs | 11 +- compiler/rustc_passes/src/liveness.rs | 5 +- compiler/rustc_privacy/src/lib.rs | 13 +- compiler/rustc_resolve/src/late.rs | 17 +- compiler/rustc_span/src/symbol.rs | 1 + src/tools/clippy/clippy_lints/src/default.rs | 4 +- .../src/default_numeric_fallback.rs | 4 +- .../src/inconsistent_struct_constructor.rs | 4 +- .../clippy_lints/src/init_numbered_fields.rs | 4 +- .../clippy_lints/src/loops/never_loop.rs | 4 +- .../clippy_lints/src/needless_update.rs | 4 +- .../clippy/clippy_lints/src/no_effect.rs | 11 +- .../src/single_range_in_vec_init.rs | 4 +- .../src/unnecessary_struct_initialization.rs | 8 +- .../clippy/clippy_lints/src/utils/author.rs | 7 +- src/tools/clippy/clippy_utils/src/higher.rs | 4 +- .../clippy/clippy_utils/src/hir_utils.rs | 11 +- src/tools/clippy/clippy_utils/src/visitors.rs | 4 +- .../feature-gate-default-field-values.rs | 69 +++++++ .../feature-gate-default-field-values.stderr | 180 +++++++++++++++++ ...t-values-and-missing-field-separator.fixed | 35 ---- ...ault-values-and-missing-field-separator.rs | 1 - ...-values-and-missing-field-separator.stderr | 181 ++++++++---------- tests/ui/stats/hir-stats.stderr | 40 ++-- .../structs/default-field-values-failures.rs | 49 +++++ .../default-field-values-failures.stderr | 40 ++++ .../structs/default-field-values-support.rs | 68 +++++++ tests/ui/thir-print/thir-tree-match.stdout | 6 +- 60 files changed, 874 insertions(+), 290 deletions(-) create mode 100644 tests/ui/feature-gates/feature-gate-default-field-values.rs create mode 100644 tests/ui/feature-gates/feature-gate-default-field-values.stderr delete mode 100644 tests/ui/parser/struct-default-values-and-missing-field-separator.fixed create mode 100644 tests/ui/structs/default-field-values-failures.rs create mode 100644 tests/ui/structs/default-field-values-failures.stderr create mode 100644 tests/ui/structs/default-field-values-support.rs diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index 37f429cce44d7..6c1ddf9a1cc0b 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -2995,6 +2995,7 @@ pub struct FieldDef { pub ident: Option, pub ty: P, + pub default: Option, pub is_placeholder: bool, } diff --git a/compiler/rustc_ast/src/mut_visit.rs b/compiler/rustc_ast/src/mut_visit.rs index 104f84f26e0af..2e858f462608c 100644 --- a/compiler/rustc_ast/src/mut_visit.rs +++ b/compiler/rustc_ast/src/mut_visit.rs @@ -1048,12 +1048,13 @@ pub fn walk_flat_map_field_def( visitor: &mut T, mut fd: FieldDef, ) -> SmallVec<[FieldDef; 1]> { - let FieldDef { span, ident, vis, id, ty, attrs, is_placeholder: _ } = &mut fd; + let FieldDef { span, ident, vis, id, ty, attrs, is_placeholder: _, default } = &mut fd; visitor.visit_id(id); visit_attrs(visitor, attrs); visitor.visit_vis(vis); visit_opt(ident, |ident| visitor.visit_ident(ident)); visitor.visit_ty(ty); + visit_opt(default, |default| visitor.visit_anon_const(default)); visitor.visit_span(span); smallvec![fd] } diff --git a/compiler/rustc_ast/src/visit.rs b/compiler/rustc_ast/src/visit.rs index 9f9c3d8c39239..daa9ad2ee5d46 100644 --- a/compiler/rustc_ast/src/visit.rs +++ b/compiler/rustc_ast/src/visit.rs @@ -934,11 +934,12 @@ pub fn walk_struct_def<'a, V: Visitor<'a>>( } pub fn walk_field_def<'a, V: Visitor<'a>>(visitor: &mut V, field: &'a FieldDef) -> V::Result { - let FieldDef { attrs, id: _, span: _, vis, ident, ty, is_placeholder: _ } = field; + let FieldDef { attrs, id: _, span: _, vis, ident, ty, is_placeholder: _, default } = field; walk_list!(visitor, visit_attribute, attrs); try_visit!(visitor.visit_vis(vis)); visit_opt!(visitor, visit_ident, *ident); try_visit!(visitor.visit_ty(ty)); + visit_opt!(visitor, visit_anon_const, &*default); V::Result::output() } diff --git a/compiler/rustc_ast_lowering/src/expr.rs b/compiler/rustc_ast_lowering/src/expr.rs index 52372bbf991c9..b822f8d7e183b 100644 --- a/compiler/rustc_ast_lowering/src/expr.rs +++ b/compiler/rustc_ast_lowering/src/expr.rs @@ -316,12 +316,14 @@ impl<'hir> LoweringContext<'_, 'hir> { ), ExprKind::Struct(se) => { let rest = match &se.rest { - StructRest::Base(e) => Some(self.lower_expr(e)), + StructRest::Base(e) => hir::Rest::Base(self.lower_expr(e)), StructRest::Rest(sp) => { - let guar = self.dcx().emit_err(BaseExpressionDoubleDot { span: *sp }); - Some(&*self.arena.alloc(self.expr_err(*sp, guar))) + if !self.tcx.features().default_field_values { + self.dcx().emit_err(BaseExpressionDoubleDot { span: *sp }); + } + hir::Rest::DefaultFields(*sp) } - StructRest::None => None, + StructRest::None => hir::Rest::None, }; hir::ExprKind::Struct( self.arena.alloc(self.lower_qpath( @@ -1460,7 +1462,7 @@ impl<'hir> LoweringContext<'_, 'hir> { hir::ExprKind::Struct( self.arena.alloc(hir::QPath::LangItem(lang_item, self.lower_span(span))), fields, - None, + hir::Rest::None, ) } diff --git a/compiler/rustc_ast_lowering/src/item.rs b/compiler/rustc_ast_lowering/src/item.rs index 7bb3b2fa29095..2d2880d3e4907 100644 --- a/compiler/rustc_ast_lowering/src/item.rs +++ b/compiler/rustc_ast_lowering/src/item.rs @@ -772,6 +772,7 @@ impl<'hir> LoweringContext<'_, 'hir> { None => Ident::new(sym::integer(index), self.lower_span(f.span)), }, vis_span: self.lower_span(f.vis.span), + default: f.default.as_ref().map(|v| self.lower_anon_const_to_anon_const(v)), ty, } } diff --git a/compiler/rustc_ast_passes/src/feature_gate.rs b/compiler/rustc_ast_passes/src/feature_gate.rs index 83931a8c1a960..4f94a562a2704 100644 --- a/compiler/rustc_ast_passes/src/feature_gate.rs +++ b/compiler/rustc_ast_passes/src/feature_gate.rs @@ -541,6 +541,7 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session, features: &Features) { gate_all!(builtin_syntax, "`builtin #` syntax is unstable"); gate_all!(explicit_tail_calls, "`become` expression is experimental"); gate_all!(generic_const_items, "generic const items are experimental"); + gate_all!(default_field_values, "default values on `struct` fields aren't supported"); gate_all!(unnamed_fields, "unnamed fields are not yet fully implemented"); gate_all!(fn_delegation, "functions delegation is not yet fully implemented"); gate_all!(postfix_match, "postfix match is experimental"); diff --git a/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs b/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs index 60ea0d1edbf4e..e8ed508de1d44 100644 --- a/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs @@ -1146,7 +1146,9 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> { expr: &hir::Expr<'_>, ) { let typeck_results = self.infcx.tcx.typeck(self.mir_def_id()); - let hir::ExprKind::Struct(struct_qpath, fields, Some(base)) = expr.kind else { return }; + let hir::ExprKind::Struct(struct_qpath, fields, hir::Rest::Base(base)) = expr.kind else { + return; + }; let hir::QPath::Resolved(_, path) = struct_qpath else { return }; let hir::def::Res::Def(_, def_id) = path.res else { return }; let Some(expr_ty) = typeck_results.node_type_opt(expr.hir_id) else { return }; @@ -1234,7 +1236,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> { expr: &'tcx hir::Expr<'tcx>, use_spans: Option>, ) { - if let hir::ExprKind::Struct(_, _, Some(_)) = expr.kind { + if let hir::ExprKind::Struct(_, _, hir::Rest::Base(_)) = expr.kind { // We have `S { foo: val, ..base }`. In `check_aggregate_rvalue` we have a single // `Location` that covers both the `S { ... }` literal, all of its fields and the // `base`. If the move happens because of `S { foo: val, bar: base.bar }` the `expr` diff --git a/compiler/rustc_builtin_macros/src/deriving/decodable.rs b/compiler/rustc_builtin_macros/src/deriving/decodable.rs index 686424770fc58..469092e7b1cf0 100644 --- a/compiler/rustc_builtin_macros/src/deriving/decodable.rs +++ b/compiler/rustc_builtin_macros/src/deriving/decodable.rs @@ -205,7 +205,7 @@ where let fields = fields .iter() .enumerate() - .map(|(i, &(ident, span))| { + .map(|(i, &(ident, span, _))| { let arg = getarg(cx, span, ident.name, i); cx.field_imm(span, ident, arg) }) diff --git a/compiler/rustc_builtin_macros/src/deriving/default.rs b/compiler/rustc_builtin_macros/src/deriving/default.rs index 652e6f7740f9f..62b99ffe64f7c 100644 --- a/compiler/rustc_builtin_macros/src/deriving/default.rs +++ b/compiler/rustc_builtin_macros/src/deriving/default.rs @@ -54,26 +54,38 @@ pub(crate) fn expand_deriving_default( trait_def.expand(cx, mitem, item, push) } +fn default_call(cx: &ExtCtxt<'_>, span: Span) -> ast::ptr::P { + // Note that `kw::Default` is "default" and `sym::Default` is "Default"! + let default_ident = cx.std_path(&[kw::Default, sym::Default, kw::Default]); + cx.expr_call_global(span, default_ident, ThinVec::new()) +} + fn default_struct_substructure( cx: &ExtCtxt<'_>, trait_span: Span, substr: &Substructure<'_>, summary: &StaticFields, ) -> BlockOrExpr { - // Note that `kw::Default` is "default" and `sym::Default` is "Default"! - let default_ident = cx.std_path(&[kw::Default, sym::Default, kw::Default]); - let default_call = |span| cx.expr_call_global(span, default_ident.clone(), ThinVec::new()); - let expr = match summary { Unnamed(_, IsTuple::No) => cx.expr_ident(trait_span, substr.type_ident), Unnamed(fields, IsTuple::Yes) => { - let exprs = fields.iter().map(|sp| default_call(*sp)).collect(); + let exprs = fields.iter().map(|sp| default_call(cx, *sp)).collect(); cx.expr_call_ident(trait_span, substr.type_ident, exprs) } Named(fields) => { let default_fields = fields .iter() - .map(|&(ident, span)| cx.field_imm(span, ident, default_call(span))) + .map(|(ident, span, default_val)| { + let value = match default_val { + // We use `Default::default()`. + None => default_call(cx, *span), + // We use the field default const expression. + Some(val) => { + cx.expr(val.value.span, ast::ExprKind::ConstBlock(val.clone())) + } + }; + cx.field_imm(*span, *ident, value) + }) .collect(); cx.expr_struct_ident(trait_span, substr.type_ident, default_fields) } @@ -93,10 +105,38 @@ fn default_enum_substructure( } { Ok(default_variant) => { // We now know there is exactly one unit variant with exactly one `#[default]` attribute. - cx.expr_path(cx.path(default_variant.span, vec![ - Ident::new(kw::SelfUpper, default_variant.span), - default_variant.ident, - ])) + match &default_variant.data { + VariantData::Unit(_) => cx.expr_path(cx.path(default_variant.span, vec![ + Ident::new(kw::SelfUpper, default_variant.span), + default_variant.ident, + ])), + VariantData::Struct { fields, .. } => { + // This only happens if `#![feature(default_field_values)]`. We have validated + // all fields have default values in the definition. + let default_fields = fields + .iter() + .map(|field| { + cx.field_imm(field.span, field.ident.unwrap(), match &field.default { + // We use `Default::default()`. + None => default_call(cx, field.span), + // We use the field default const expression. + Some(val) => { + cx.expr(val.value.span, ast::ExprKind::ConstBlock(val.clone())) + } + }) + }) + .collect(); + let path = cx.path(default_variant.span, vec![ + Ident::new(kw::SelfUpper, default_variant.span), + default_variant.ident, + ]); + cx.expr_struct(default_variant.span, path, default_fields) + } + // Logic error in `extract_default_variant`. + VariantData::Tuple(..) => { + cx.dcx().bug("encountered tuple variant annotated with `#[default]`") + } + } } Err(guar) => DummyResult::raw_expr(trait_span, Some(guar)), }; @@ -156,7 +196,12 @@ fn extract_default_variant<'a>( } }; - if !matches!(variant.data, VariantData::Unit(..)) { + if cx.ecfg.features.default_field_values + && let VariantData::Struct { fields, .. } = &variant.data + && fields.iter().all(|f| f.default.is_some()) + { + // Allowed + } else if !matches!(variant.data, VariantData::Unit(..)) { let guar = cx.dcx().emit_err(errors::NonUnitDefault { span: variant.ident.span }); return Err(guar); } diff --git a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs index 82baaca9a4641..5c889a46303ea 100644 --- a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs +++ b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs @@ -182,8 +182,8 @@ pub(crate) use StaticFields::*; pub(crate) use SubstructureFields::*; use rustc_ast::ptr::P; use rustc_ast::{ - self as ast, BindingMode, ByRef, EnumDef, Expr, GenericArg, GenericParamKind, Generics, - Mutability, PatKind, VariantData, + self as ast, AnonConst, BindingMode, ByRef, EnumDef, Expr, GenericArg, GenericParamKind, + Generics, Mutability, PatKind, VariantData, }; use rustc_attr as attr; use rustc_expand::base::{Annotatable, ExtCtxt}; @@ -296,7 +296,7 @@ pub(crate) enum StaticFields { /// Tuple and unit structs/enum variants like this. Unnamed(Vec, IsTuple), /// Normal structs/struct variants. - Named(Vec<(Ident, Span)>), + Named(Vec<(Ident, Span, Option)>), } /// A summary of the possible sets of fields. @@ -1443,7 +1443,7 @@ impl<'a> TraitDef<'a> { for field in struct_def.fields() { let sp = field.span.with_ctxt(self.span.ctxt()); match field.ident { - Some(ident) => named_idents.push((ident, sp)), + Some(ident) => named_idents.push((ident, sp, field.default.clone())), _ => just_spans.push(sp), } } diff --git a/compiler/rustc_expand/src/placeholders.rs b/compiler/rustc_expand/src/placeholders.rs index 610c69e9d21b4..fd00e44e8c2aa 100644 --- a/compiler/rustc_expand/src/placeholders.rs +++ b/compiler/rustc_expand/src/placeholders.rs @@ -173,6 +173,7 @@ pub(crate) fn placeholder( ty: ty(), vis, is_placeholder: true, + default: None, }]), AstFragmentKind::Variants => AstFragment::Variants(smallvec![ast::Variant { attrs: Default::default(), diff --git a/compiler/rustc_feature/src/unstable.rs b/compiler/rustc_feature/src/unstable.rs index 91b072b56db2b..b8da05ab4c214 100644 --- a/compiler/rustc_feature/src/unstable.rs +++ b/compiler/rustc_feature/src/unstable.rs @@ -424,6 +424,11 @@ declare_features! ( (unstable, custom_test_frameworks, "1.30.0", Some(50297)), /// Allows declarative macros 2.0 (`macro`). (unstable, decl_macro, "1.17.0", Some(39412)), + /// Allows the use of default values on struct definitions and the construction of struct + /// literals with the functional update syntax without a base. + // FIXME: change tracking issue number from RFCS repo to rust repo after RFC is accepted an + // tracking ticket is created. + (unstable, default_field_values, "CURRENT_RUSTC_VERSION", Some(3681)), /// Allows using `#[deprecated_safe]` to deprecate the safeness of a function or trait (unstable, deprecated_safe, "1.61.0", Some(94978)), /// Allows having using `suggestion` in the `#[deprecated]` attribute. diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index 71216023ecc33..00972310aeaab 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -1873,7 +1873,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 { + Rest::Base(init) => init.can_have_side_effects(), + Rest::DefaultFields(_) | Rest::None => false, + }; + fields.iter().map(|field| field.expr).any(|e| e.can_have_side_effects()) + || init_side_effects } ExprKind::Array(args) @@ -1942,20 +1947,28 @@ 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], Rest::None), + ExprKind::Struct(QPath::LangItem(LangItem::RangeTo, _), [val2], Rest::None), ) | ( - ExprKind::Struct(QPath::LangItem(LangItem::RangeToInclusive, _), [val1], None), - ExprKind::Struct(QPath::LangItem(LangItem::RangeToInclusive, _), [val2], None), + ExprKind::Struct( + QPath::LangItem(LangItem::RangeToInclusive, _), + [val1], + Rest::None, + ), + ExprKind::Struct( + QPath::LangItem(LangItem::RangeToInclusive, _), + [val2], + Rest::None, + ), ) | ( - ExprKind::Struct(QPath::LangItem(LangItem::RangeFrom, _), [val1], None), - ExprKind::Struct(QPath::LangItem(LangItem::RangeFrom, _), [val2], None), + ExprKind::Struct(QPath::LangItem(LangItem::RangeFrom, _), [val1], Rest::None), + ExprKind::Struct(QPath::LangItem(LangItem::RangeFrom, _), [val2], Rest::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], Rest::None), + ExprKind::Struct(QPath::LangItem(LangItem::Range, _), [val2, val4], Rest::None), ) => { val1.expr.equivalent_for_indexing(val2.expr) && val3.expr.equivalent_for_indexing(val4.expr) @@ -2112,7 +2125,7 @@ pub enum ExprKind<'hir> { /// /// E.g., `Foo {x: 1, y: 2}`, or `Foo {x: 1, .. base}`, /// where `base` is the `Option`. - Struct(&'hir QPath<'hir>, &'hir [ExprField<'hir>], Option<&'hir Expr<'hir>>), + Struct(&'hir QPath<'hir>, &'hir [ExprField<'hir>], Rest<'hir>), /// An array literal constructed from one repeated element. /// @@ -2127,6 +2140,13 @@ pub enum ExprKind<'hir> { Err(rustc_span::ErrorGuaranteed), } +#[derive(Debug, Clone, Copy, HashStable_Generic)] +pub enum Rest<'hir> { + Base(&'hir Expr<'hir>), + DefaultFields(Span), + None, +} + /// Represents an optionally `Self`-qualified value/type path or associated extension. /// /// To resolve the path to a `DefId`, call [`qpath_res`]. @@ -3181,6 +3201,7 @@ pub struct FieldDef<'hir> { pub hir_id: HirId, pub def_id: LocalDefId, pub ty: &'hir Ty<'hir>, + pub default: Option<&'hir AnonConst>, } impl FieldDef<'_> { diff --git a/compiler/rustc_hir/src/intravisit.rs b/compiler/rustc_hir/src/intravisit.rs index 4da3224578519..8b51408c0c227 100644 --- a/compiler/rustc_hir/src/intravisit.rs +++ b/compiler/rustc_hir/src/intravisit.rs @@ -756,7 +756,10 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>) ExprKind::Struct(ref qpath, fields, ref optional_base) => { try_visit!(visitor.visit_qpath(qpath, expression.hir_id, expression.span)); walk_list!(visitor, visit_expr_field, fields); - visit_opt!(visitor, visit_expr, optional_base); + match optional_base { + Rest::Base(base) => try_visit!(visitor.visit_expr(base)), + Rest::None | Rest::DefaultFields(_) => {} + } } ExprKind::Tup(subexpressions) => { walk_list!(visitor, visit_expr, subexpressions); @@ -1194,10 +1197,14 @@ pub fn walk_struct_def<'v, V: Visitor<'v>>( V::Result::output() } -pub fn walk_field_def<'v, V: Visitor<'v>>(visitor: &mut V, field: &'v FieldDef<'v>) -> V::Result { - try_visit!(visitor.visit_id(field.hir_id)); - try_visit!(visitor.visit_ident(field.ident)); - visitor.visit_ty(field.ty) +pub fn walk_field_def<'v, V: Visitor<'v>>( + visitor: &mut V, + FieldDef { hir_id, ident, ty, default, span: _, vis_span: _, def_id: _ }: &'v FieldDef<'v>, +) -> V::Result { + try_visit!(visitor.visit_id(*hir_id)); + try_visit!(visitor.visit_ident(*ident)); + visit_opt!(visitor, visit_anon_const, default); + visitor.visit_ty(*ty) } pub fn walk_enum_def<'v, V: Visitor<'v>>( diff --git a/compiler/rustc_hir_analysis/src/collect.rs b/compiler/rustc_hir_analysis/src/collect.rs index 93b021be24584..f1afc43ee3a6e 100644 --- a/compiler/rustc_hir_analysis/src/collect.rs +++ b/compiler/rustc_hir_analysis/src/collect.rs @@ -1082,12 +1082,12 @@ impl<'tcx> FieldUniquenessCheckContext<'tcx> { } } -fn lower_variant( - tcx: TyCtxt<'_>, +fn lower_variant<'tcx>( + tcx: TyCtxt<'tcx>, variant_did: Option, ident: Ident, discr: ty::VariantDiscr, - def: &hir::VariantData<'_>, + def: &hir::VariantData<'tcx>, adt_kind: ty::AdtKind, parent_did: LocalDefId, is_anonymous: bool, @@ -1109,6 +1109,7 @@ fn lower_variant( did: f.def_id.to_def_id(), name: f.ident.name, vis: tcx.visibility(f.def_id), + value: f.default.map(|v| v.def_id.to_def_id()), }) .collect(); let recovered = match def { diff --git a/compiler/rustc_hir_analysis/src/collect/type_of.rs b/compiler/rustc_hir_analysis/src/collect/type_of.rs index 48b5e87cbd033..cee778ad93498 100644 --- a/compiler/rustc_hir_analysis/src/collect/type_of.rs +++ b/compiler/rustc_hir_analysis/src/collect/type_of.rs @@ -125,6 +125,10 @@ fn anon_const_type_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> { return ty; } + Node::Field(&hir::FieldDef { default: Some(_), ty, .. }) => { + ItemCtxt::new(tcx, def_id).lower_ty(ty) + } + _ => Ty::new_error_with_message( tcx, span, diff --git a/compiler/rustc_hir_pretty/src/lib.rs b/compiler/rustc_hir_pretty/src/lib.rs index 1c52283d537d3..ae1426478e5ee 100644 --- a/compiler/rustc_hir_pretty/src/lib.rs +++ b/compiler/rustc_hir_pretty/src/lib.rs @@ -1085,12 +1085,12 @@ impl<'a> State<'a> { &mut self, qpath: &hir::QPath<'_>, fields: &[hir::ExprField<'_>], - wth: Option<&hir::Expr<'_>>, + wth: hir::Rest<'_>, ) { self.print_qpath(qpath, true); self.word("{"); self.commasep_cmnt(Consistent, fields, |s, field| s.print_expr_field(field), |f| f.span); - if let Some(expr) = wth { + if let hir::Rest::Base(expr) = wth { self.ibox(INDENT_UNIT); if !fields.is_empty() { self.word(","); @@ -1099,6 +1099,14 @@ impl<'a> State<'a> { self.word(".."); self.print_expr(expr); self.end(); + } else if let hir::Rest::DefaultFields(_) = wth { + self.ibox(INDENT_UNIT); + if !fields.is_empty() { + self.word(","); + self.space(); + } + self.word(".."); + self.end(); } else if !fields.is_empty() { self.word(","); } diff --git a/compiler/rustc_hir_typeck/src/expr.rs b/compiler/rustc_hir_typeck/src/expr.rs index b34ed4640db12..e7c2bd2fc928a 100644 --- a/compiler/rustc_hir_typeck/src/expr.rs +++ b/compiler/rustc_hir_typeck/src/expr.rs @@ -1628,11 +1628,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { fn check_expr_struct( &self, - expr: &hir::Expr<'_>, + expr: &hir::Expr<'tcx>, expected: Expectation<'tcx>, - qpath: &QPath<'tcx>, + qpath: &'tcx QPath<'tcx>, fields: &'tcx [hir::ExprField<'tcx>], - base_expr: &'tcx Option<&'tcx hir::Expr<'tcx>>, + base_expr: &'tcx hir::Rest<'tcx>, ) -> Ty<'tcx> { // Find the relevant variant let (variant, adt_ty) = match self.check_struct_path(qpath, expr.hir_id) { @@ -1672,7 +1672,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { span: Span, variant: &'tcx ty::VariantDef, hir_fields: &'tcx [hir::ExprField<'tcx>], - base_expr: &'tcx Option<&'tcx hir::Expr<'tcx>>, + base_expr: &'tcx hir::Rest<'tcx>, ) { let tcx = self.tcx; @@ -1797,13 +1797,47 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // the fields with the base_expr. This could cause us to hit errors later // when certain fields are assumed to exist that in fact do not. if error_happened { - if let Some(base_expr) = base_expr { + if let hir::Rest::Base(base_expr) = base_expr { self.check_expr(base_expr); } return; } - if let Some(base_expr) = base_expr { + if let hir::Rest::DefaultFields(span) = *base_expr { + for f in &variant.fields { + let ident = self.tcx.adjust_ident(f.ident(self.tcx), variant.def_id); + if let Some(_) = remaining_fields.remove(&ident) + && f.value.is_none() + && self.tcx.features().default_field_values + { + let guar = self + .dcx() + .struct_span_err(span, format!("missing mandatory field `{ident}`")) + .emit(); + self.set_tainted_by_errors(guar); + return; + } + } + let fru_tys = match adt_ty.kind() { + ty::Adt(adt, args) if adt.is_struct() => variant + .fields + .iter() + .map(|f| self.normalize(span, f.ty(self.tcx, args))) + .collect(), + ty::Adt(adt, args) if adt.is_enum() && self.tcx.features().default_field_values => { + variant + .fields + .iter() + .map(|f| self.normalize(span, f.ty(self.tcx, args))) + .collect() + } + _ => { + self.dcx().emit_err(FunctionalRecordUpdateOnNonStruct { span }); + return; + } + }; + self.typeck_results.borrow_mut().fru_field_types_mut().insert(expr.hir_id, fru_tys); + } else if let hir::Rest::Base(base_expr) = base_expr { // FIXME: We are currently creating two branches here in order to maintain // consistency. But they should be merged as much as possible. let fru_tys = if self.tcx.features().type_changing_struct_update { @@ -1935,12 +1969,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { fn check_struct_fields_on_error( &self, fields: &'tcx [hir::ExprField<'tcx>], - base_expr: &'tcx Option<&'tcx hir::Expr<'tcx>>, + base_expr: &'tcx hir::Rest<'tcx>, ) { for field in fields { self.check_expr(field.expr); } - if let Some(base) = *base_expr { + if let hir::Rest::Base(base) = *base_expr { self.check_expr(base); } } diff --git a/compiler/rustc_hir_typeck/src/expr_use_visitor.rs b/compiler/rustc_hir_typeck/src/expr_use_visitor.rs index bb5f351137305..534d66f08757a 100644 --- a/compiler/rustc_hir_typeck/src/expr_use_visitor.rs +++ b/compiler/rustc_hir_typeck/src/expr_use_visitor.rs @@ -687,7 +687,7 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx fn walk_struct_expr<'hir>( &self, fields: &[hir::ExprField<'_>], - opt_with: &Option<&'hir hir::Expr<'_>>, + opt_with: &hir::Rest<'hir>, ) -> Result<(), Cx::Error> { // Consume the expressions supplying values for each field. for field in fields { @@ -703,8 +703,8 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx } let with_expr = match *opt_with { - Some(w) => &*w, - None => { + hir::Rest::Base(w) => &*w, + hir::Rest::DefaultFields(_) | hir::Rest::None => { return Ok(()); } }; diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs index 550c58b5a17c6..fd44598774640 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs @@ -1595,7 +1595,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { pub(crate) fn check_struct_path( &self, - qpath: &QPath<'tcx>, + qpath: &'tcx QPath<'tcx>, hir_id: HirId, ) -> Result<(&'tcx ty::VariantDef, Ty<'tcx>), ErrorGuaranteed> { let path_span = qpath.span(); diff --git a/compiler/rustc_hir_typeck/src/pat.rs b/compiler/rustc_hir_typeck/src/pat.rs index 49c5a7d8a652d..a096d4306b261 100644 --- a/compiler/rustc_hir_typeck/src/pat.rs +++ b/compiler/rustc_hir_typeck/src/pat.rs @@ -976,7 +976,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { fn check_pat_struct( &self, pat: &'tcx Pat<'tcx>, - qpath: &hir::QPath<'tcx>, + qpath: &'tcx hir::QPath<'tcx>, fields: &'tcx [hir::PatField<'tcx>], has_rest_pat: bool, expected: Ty<'tcx>, diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index 4425c93211a01..46c1b2536896f 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -1101,6 +1101,7 @@ impl<'a> CrateMetadataRef<'a> { did, name: self.item_name(did.index), vis: self.get_visibility(did.index), + value: None, }) .collect(), adt_kind, diff --git a/compiler/rustc_middle/src/thir.rs b/compiler/rustc_middle/src/thir.rs index e614d41899a0e..395c647b98c49 100644 --- a/compiler/rustc_middle/src/thir.rs +++ b/compiler/rustc_middle/src/thir.rs @@ -158,8 +158,15 @@ pub struct AdtExpr<'tcx> { pub user_ty: UserTy<'tcx>, pub fields: Box<[FieldExpr]>, - /// The base, e.g. `Foo {x: 1, .. base}`. - pub base: Option>, + /// The base, e.g. `Foo {x: 1, ..base}`. + pub base: Rest<'tcx>, +} + +#[derive(Clone, Debug, HashStable)] +pub enum Rest<'tcx> { + Base(FruInfo<'tcx>), + DefaultFields(Box<[Ty<'tcx>]>), + None, } #[derive(Clone, Debug, HashStable)] diff --git a/compiler/rustc_middle/src/thir/visit.rs b/compiler/rustc_middle/src/thir/visit.rs index 58e2ebaeaf8df..3140d2de76135 100644 --- a/compiler/rustc_middle/src/thir/visit.rs +++ b/compiler/rustc_middle/src/thir/visit.rs @@ -127,7 +127,7 @@ pub fn walk_expr<'thir, 'tcx: 'thir, V: Visitor<'thir, 'tcx>>( for field in &**fields { visitor.visit_expr(&visitor.thir()[field.expr]); } - if let Some(base) = base { + if let crate::thir::Rest::Base(base) = base { visitor.visit_expr(&visitor.thir()[base.base]); } } diff --git a/compiler/rustc_middle/src/ty/adt.rs b/compiler/rustc_middle/src/ty/adt.rs index 8f89f5c25afb4..1e3d7b7dde27f 100644 --- a/compiler/rustc_middle/src/ty/adt.rs +++ b/compiler/rustc_middle/src/ty/adt.rs @@ -251,10 +251,10 @@ impl Into for AdtKind { } } -impl AdtDefData { +impl<'tcx> AdtDefData { /// Creates a new `AdtDefData`. pub(super) fn new( - tcx: TyCtxt<'_>, + tcx: TyCtxt<'tcx>, did: DefId, kind: AdtKind, variants: IndexVec, diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 2b1212a5eb567..3579c1b3dfd20 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -1377,6 +1377,7 @@ pub struct FieldDef { pub did: DefId, pub name: Symbol, pub vis: Visibility, + pub value: Option, } impl PartialEq for FieldDef { @@ -1389,9 +1390,9 @@ impl PartialEq for FieldDef { // of `FieldDef` changes, a compile-error will be produced, reminding // us to revisit this assumption. - let Self { did: lhs_did, name: _, vis: _ } = &self; + let Self { did: lhs_did, name: _, vis: _, value: _ } = &self; - let Self { did: rhs_did, name: _, vis: _ } = other; + let Self { did: rhs_did, name: _, vis: _, value: _ } = other; let res = lhs_did == rhs_did; @@ -1417,7 +1418,7 @@ impl Hash for FieldDef { // of `FieldDef` changes, a compile-error will be produced, reminding // us to revisit this assumption. - let Self { did, name: _, vis: _ } = &self; + let Self { did, name: _, vis: _, value: _ } = &self; did.hash(s) } diff --git a/compiler/rustc_mir_build/src/build/custom/parse/instruction.rs b/compiler/rustc_mir_build/src/build/custom/parse/instruction.rs index 9e3af89105294..046982213dbf1 100644 --- a/compiler/rustc_mir_build/src/build/custom/parse/instruction.rs +++ b/compiler/rustc_mir_build/src/build/custom/parse/instruction.rs @@ -275,7 +275,7 @@ impl<'a, 'tcx> ParseCtxt<'a, 'tcx> { fields.iter().map(|e| self.parse_operand(*e)).collect::>()? )) }, - ExprKind::Adt(box AdtExpr{ adt_def, variant_index, args, fields, .. }) => { + ExprKind::Adt(box AdtExpr { adt_def, variant_index, args, fields, .. }) => { let is_union = adt_def.is_union(); let active_field_index = is_union.then(|| fields[0].name); diff --git a/compiler/rustc_mir_build/src/build/expr/into.rs b/compiler/rustc_mir_build/src/build/expr/into.rs index 86fe447f39973..49c6be51d62f8 100644 --- a/compiler/rustc_mir_build/src/build/expr/into.rs +++ b/compiler/rustc_mir_build/src/build/expr/into.rs @@ -339,9 +339,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { }) .collect(); - let field_names = adt_def.variant(variant_index).fields.indices(); + let variant = adt_def.variant(variant_index); + let field_names = variant.fields.indices(); - let fields = if let Some(FruInfo { base, field_types }) = base { + let fields = if let Rest::Base(FruInfo { base, field_types }) = base { let place_builder = unpack!(block = this.as_place_builder(block, *base)); // MIR does not natively support FRU, so for each @@ -356,6 +357,35 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { } }) .collect() + } else if let Rest::DefaultFields(field_types) = base { + iter::zip(field_names, &**field_types) + .map(|(n, ty)| match fields_map.get(&n) { + Some(v) => v.clone(), + None => match variant.fields[n].value { + Some(def) => { + let ty = this.tcx.type_of(def); + let ty = this.tcx.instantiate_and_normalize_erasing_regions( + args, + this.param_env, + ty, + ); + let value = Const::Unevaluated( + UnevaluatedConst { def, args, promoted: None }, + ty, + ); + let value = value.normalize(this.tcx, this.param_env); + this.literal_operand(expr_span, value) + } + None => { + let name = variant.fields[n].name; + span_bug!( + expr_span, + "missing mandatory field `{name}` of type `{ty}`", + ); + } + }, + }) + .collect() } else { field_names.filter_map(|n| fields_map.get(&n).cloned()).collect() }; diff --git a/compiler/rustc_mir_build/src/thir/cx/expr.rs b/compiler/rustc_mir_build/src/thir/cx/expr.rs index fbd45f59a4fb4..4f64ea06adb55 100644 --- a/compiler/rustc_mir_build/src/thir/cx/expr.rs +++ b/compiler/rustc_mir_build/src/thir/cx/expr.rs @@ -214,7 +214,7 @@ impl<'tcx> Cx<'tcx> { args, fields: Box::new([FieldExpr { name: FieldIdx::from(0u32), expr }]), user_ty: None, - base: None, + base: Rest::None, })); debug!(?kind); @@ -447,7 +447,7 @@ impl<'tcx> Cx<'tcx> { variant_index: index, fields: field_refs, user_ty, - base: None, + base: Rest::None, })) } else { ExprKind::Call { @@ -577,20 +577,33 @@ impl<'tcx> Cx<'tcx> { args, user_ty, fields: self.field_refs(fields), - base: base.map(|base| FruInfo { - base: self.mirror_expr(base), - field_types: self.typeck_results().fru_field_types()[expr.hir_id] - .iter() - .copied() - .collect(), - }), + base: match base { + hir::Rest::Base(base) => Rest::Base(FruInfo { + base: self.mirror_expr(base), + field_types: self.typeck_results().fru_field_types() + [expr.hir_id] + .iter() + .copied() + .collect(), + }), + hir::Rest::DefaultFields(_) => Rest::DefaultFields( + self.typeck_results().fru_field_types()[expr.hir_id] + .iter() + .copied() + .collect(), + ), + hir::Rest::None => Rest::None, + }, })) } AdtKind::Enum => { let res = self.typeck_results().qpath_res(qpath, expr.hir_id); match res { Res::Def(DefKind::Variant, variant_id) => { - assert!(base.is_none()); + assert!(matches!( + base, + hir::Rest::None | hir::Rest::DefaultFields(_) + )); let index = adt.variant_index_with_id(variant_id); let user_provided_types = @@ -604,7 +617,18 @@ impl<'tcx> Cx<'tcx> { args, user_ty, fields: self.field_refs(fields), - base: None, + base: match base { + hir::Rest::DefaultFields(_) => Rest::DefaultFields( + self.typeck_results().fru_field_types()[expr.hir_id] + .iter() + .copied() + .collect(), + ), + hir::Rest::Base(base) => { + span_bug!(base.span, "unexpected res: {:?}", res); + } + hir::Rest::None => Rest::None, + }, })) } _ => { @@ -1013,7 +1037,7 @@ impl<'tcx> Cx<'tcx> { args, user_ty, fields: Box::new([]), - base: None, + base: Rest::None, })), _ => bug!("unexpected ty: {:?}", ty), } diff --git a/compiler/rustc_mir_build/src/thir/print.rs b/compiler/rustc_mir_build/src/thir/print.rs index 61317925d09e5..99e6f8c64bdc5 100644 --- a/compiler/rustc_mir_build/src/thir/print.rs +++ b/compiler/rustc_mir_build/src/thir/print.rs @@ -566,9 +566,11 @@ impl<'a, 'tcx> ThirPrinter<'a, 'tcx> { self.print_expr(field_expr.expr, depth_lvl + 2); } - if let Some(ref base) = adt_expr.base { + if let Rest::Base(ref base) = adt_expr.base { print_indented!(self, "base:", depth_lvl + 1); self.print_fru_info(base, depth_lvl + 2); + } else if let Rest::DefaultFields(_) = adt_expr.base { + print_indented!(self, "defaults", depth_lvl + 1); } else { print_indented!(self, "base: None", depth_lvl + 1); } diff --git a/compiler/rustc_parse/messages.ftl b/compiler/rustc_parse/messages.ftl index 6cb851eb8df6b..6815923f7127b 100644 --- a/compiler/rustc_parse/messages.ftl +++ b/compiler/rustc_parse/messages.ftl @@ -160,9 +160,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 diff --git a/compiler/rustc_parse/src/errors.rs b/compiler/rustc_parse/src/errors.rs index 20bcefd4fe1e7..6b467c63b8666 100644 --- a/compiler/rustc_parse/src/errors.rs +++ b/compiler/rustc_parse/src/errors.rs @@ -2976,14 +2976,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 { diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index 9fc82d84225cc..b8c8b6f36bee3 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -1812,6 +1812,7 @@ impl<'a> Parser<'a> { ident: None, id: DUMMY_NODE_ID, ty, + default: None, attrs, is_placeholder: false, }, @@ -1988,18 +1989,22 @@ 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), vis, id: DUMMY_NODE_ID, ty, + default, attrs, is_placeholder: false, }) diff --git a/compiler/rustc_passes/src/liveness.rs b/compiler/rustc_passes/src/liveness.rs index 366f7dd293c44..7282bbf267358 100644 --- a/compiler/rustc_passes/src/liveness.rs +++ b/compiler/rustc_passes/src/liveness.rs @@ -1006,7 +1006,10 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> { hir::ExprKind::Array(exprs) => self.propagate_through_exprs(exprs, succ), hir::ExprKind::Struct(_, fields, ref with_expr) => { - let succ = self.propagate_through_opt_expr(with_expr.as_deref(), succ); + let succ = match with_expr { + hir::Rest::Base(base) => self.propagate_through_opt_expr(Some(base), succ), + _ => succ, + }; fields .iter() .rev() diff --git a/compiler/rustc_privacy/src/lib.rs b/compiler/rustc_privacy/src/lib.rs index 9094b00fbfb44..6e0b2c8faf86c 100644 --- a/compiler/rustc_privacy/src/lib.rs +++ b/compiler/rustc_privacy/src/lib.rs @@ -989,7 +989,7 @@ impl<'tcx> Visitor<'tcx> for NamePrivacyVisitor<'tcx> { let res = self.typeck_results().qpath_res(qpath, expr.hir_id); let adt = self.typeck_results().expr_ty(expr).ty_adt_def().unwrap(); let variant = adt.variant_of_res(res); - if let Some(base) = *base { + if let hir::Rest::Base(base) = *base { // If the expression uses FRU we need to make sure all the unmentioned fields // are checked for privacy (RFC 736). Rather than computing the set of // unmentioned fields, just check them all. @@ -1003,6 +1003,17 @@ impl<'tcx> Visitor<'tcx> for NamePrivacyVisitor<'tcx> { }; self.check_field(hir_id, use_ctxt, span, adt, variant_field, true); } + } else if let hir::Rest::DefaultFields(span) = *base { + for (vf_index, variant_field) in variant.fields.iter_enumerated() { + let field = fields + .iter() + .find(|f| self.typeck_results().field_index(f.hir_id) == vf_index); + let (hir_id, use_ctxt, span) = match field { + Some(field) => (field.hir_id, field.ident.span, field.span), + None => (expr.hir_id, span, span), + }; + self.check_field(hir_id, use_ctxt, span, adt, variant_field, true); + } } else { for field in fields { let (hir_id, use_ctxt, span) = (field.hir_id, field.ident.span, field.span); diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs index 66c1ff93ce1ce..416d11ee7c127 100644 --- a/compiler/rustc_resolve/src/late.rs +++ b/compiler/rustc_resolve/src/late.rs @@ -13,7 +13,9 @@ use std::collections::hash_map::Entry; use std::mem::{replace, swap, take}; use rustc_ast::ptr::P; -use rustc_ast::visit::{AssocCtxt, BoundKind, FnCtxt, FnKind, Visitor, visit_opt, walk_list}; +use rustc_ast::visit::{ + AssocCtxt, BoundKind, FnCtxt, FnKind, Visitor, try_visit, visit_opt, walk_list, +}; use rustc_ast::*; use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap}; use rustc_errors::codes::*; @@ -749,8 +751,8 @@ impl<'ra: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'_, 'ast, 'r self.resolve_block(block); self.parent_scope.macro_rules = old_macro_rules; } - fn visit_anon_const(&mut self, _constant: &'ast AnonConst) { - bug!("encountered anon const without a manual call to `resolve_anon_const`"); + fn visit_anon_const(&mut self, constant: &'ast AnonConst) { + bug!("encountered anon const without a manual call to `resolve_anon_const` {constant:#?}"); } fn visit_expr(&mut self, expr: &'ast Expr) { self.resolve_expr(expr, None); @@ -1361,7 +1363,14 @@ impl<'ra: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'_, 'ast, 'r fn visit_field_def(&mut self, f: &'ast FieldDef) { self.resolve_doc_links(&f.attrs, MaybeExported::Ok(f.id)); - visit::walk_field_def(self, f) + let FieldDef { attrs, id: _, span: _, vis, ident, ty, is_placeholder: _, default } = f; + walk_list!(self, visit_attribute, attrs); + try_visit!(self.visit_vis(vis)); + visit_opt!(self, visit_ident, *ident); + try_visit!(self.visit_ty(ty)); + if let Some(v) = &default { + self.resolve_anon_const(v, AnonConstKind::ConstArg(IsRepeatExpr::No)); + } } } diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index 8f226b26befda..0a5ca8ca251e1 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -717,6 +717,7 @@ symbols! { declare_lint_pass, decode, default_alloc_error_handler, + default_field_values, default_fn, default_lib_allocator, default_method_body_is_const, diff --git a/src/tools/clippy/clippy_lints/src/default.rs b/src/tools/clippy/clippy_lints/src/default.rs index dc10b64698b4f..60eaab0dfa2e7 100644 --- a/src/tools/clippy/clippy_lints/src/default.rs +++ b/src/tools/clippy/clippy_lints/src/default.rs @@ -5,7 +5,7 @@ use clippy_utils::{contains_name, get_parent_expr, in_automatically_derived, is_ use rustc_data_structures::fx::FxHashSet; use rustc_errors::Applicability; use rustc_hir::def::Res; -use rustc_hir::{Block, Expr, ExprKind, PatKind, QPath, Stmt, StmtKind}; +use rustc_hir::{Block, Expr, ExprKind, PatKind, QPath, Stmt, StmtKind, Rest}; use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::ty; use rustc_middle::ty::print::with_forced_trimmed_paths; @@ -285,7 +285,7 @@ fn field_reassigned_by_stmt<'tcx>(this: &Stmt<'tcx>, binding_name: Symbol) -> Op /// Returns whether `expr` is the update syntax base: `Foo { a: 1, .. base }` fn is_update_syntax_base<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> bool { if let Some(parent) = get_parent_expr(cx, expr) - && let ExprKind::Struct(_, _, Some(base)) = parent.kind + && let ExprKind::Struct(_, _, Rest::Base(base)) = parent.kind { base.hir_id == expr.hir_id } else { diff --git a/src/tools/clippy/clippy_lints/src/default_numeric_fallback.rs b/src/tools/clippy/clippy_lints/src/default_numeric_fallback.rs index a065dc2cf7e52..cfa6b2a33d4f6 100644 --- a/src/tools/clippy/clippy_lints/src/default_numeric_fallback.rs +++ b/src/tools/clippy/clippy_lints/src/default_numeric_fallback.rs @@ -4,7 +4,7 @@ use clippy_utils::source::snippet_opt; use rustc_ast::ast::{LitFloatType, LitIntType, LitKind}; use rustc_errors::Applicability; use rustc_hir::intravisit::{Visitor, walk_expr, walk_stmt}; -use rustc_hir::{Block, Body, ConstContext, Expr, ExprKind, FnRetTy, HirId, Lit, Stmt, StmtKind}; +use rustc_hir::{Block, Body, ConstContext, Expr, ExprKind, FnRetTy, HirId, Lit, Stmt, StmtKind, Rest}; use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_middle::lint::in_external_macro; use rustc_middle::ty::{self, FloatTy, IntTy, PolyFnSig, Ty}; @@ -197,7 +197,7 @@ impl<'a, 'tcx> Visitor<'tcx> for NumericFallbackVisitor<'a, 'tcx> { } // Visit base with no bound. - if let Some(base) = base { + if let Rest::Base(base) = base { self.ty_bounds.push(ExplicitTyBound(false)); self.visit_expr(base); self.ty_bounds.pop(); diff --git a/src/tools/clippy/clippy_lints/src/inconsistent_struct_constructor.rs b/src/tools/clippy/clippy_lints/src/inconsistent_struct_constructor.rs index d386bfca6baa1..0a434c9f40b4a 100644 --- a/src/tools/clippy/clippy_lints/src/inconsistent_struct_constructor.rs +++ b/src/tools/clippy/clippy_lints/src/inconsistent_struct_constructor.rs @@ -3,7 +3,7 @@ use clippy_utils::fulfill_or_allowed; use clippy_utils::source::snippet; use rustc_data_structures::fx::FxHashMap; use rustc_errors::Applicability; -use rustc_hir::{self as hir, ExprKind}; +use rustc_hir::{self as hir, ExprKind, Rest}; use rustc_lint::{LateContext, LateLintPass}; use rustc_session::declare_lint_pass; use rustc_span::symbol::Symbol; @@ -95,7 +95,7 @@ impl<'tcx> LateLintPass<'tcx> for InconsistentStructConstructor { } fields_snippet.push_str(&last_ident.to_string()); - let base_snippet = if let Some(base) = base { + let base_snippet = if let Rest::Base(base) = base { format!(", ..{}", snippet(cx, base.span, "..")) } else { String::new() diff --git a/src/tools/clippy/clippy_lints/src/init_numbered_fields.rs b/src/tools/clippy/clippy_lints/src/init_numbered_fields.rs index 7f183bb601eb4..ae1c8388a70a4 100644 --- a/src/tools/clippy/clippy_lints/src/init_numbered_fields.rs +++ b/src/tools/clippy/clippy_lints/src/init_numbered_fields.rs @@ -2,7 +2,7 @@ use clippy_utils::diagnostics::span_lint_and_then; use clippy_utils::source::{snippet_with_applicability, snippet_with_context}; use rustc_errors::Applicability; use rustc_hir::def::{DefKind, Res}; -use rustc_hir::{Expr, ExprKind}; +use rustc_hir::{Expr, ExprKind, Rest}; use rustc_lint::{LateContext, LateLintPass}; use rustc_session::declare_lint_pass; use rustc_span::SyntaxContext; @@ -43,7 +43,7 @@ declare_lint_pass!(NumberedFields => [INIT_NUMBERED_FIELDS]); impl<'tcx> LateLintPass<'tcx> for NumberedFields { fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) { - if let ExprKind::Struct(path, fields @ [field, ..], None) = e.kind + if let ExprKind::Struct(path, fields @ [field, ..], Rest::None) = e.kind // If the first character of any field is a digit it has to be a tuple. && field.ident.as_str().as_bytes().first().is_some_and(u8::is_ascii_digit) // Type aliases can't be used as functions. diff --git a/src/tools/clippy/clippy_lints/src/loops/never_loop.rs b/src/tools/clippy/clippy_lints/src/loops/never_loop.rs index 1c55e3e22e8c1..c7007e2ba9ef4 100644 --- a/src/tools/clippy/clippy_lints/src/loops/never_loop.rs +++ b/src/tools/clippy/clippy_lints/src/loops/never_loop.rs @@ -5,7 +5,7 @@ use clippy_utils::higher::ForLoop; use clippy_utils::macros::root_macro_call_first_node; use clippy_utils::source::snippet; use rustc_errors::Applicability; -use rustc_hir::{Block, Destination, Expr, ExprKind, HirId, InlineAsmOperand, Pat, Stmt, StmtKind}; +use rustc_hir::{Block, Destination, Expr, ExprKind, HirId, InlineAsmOperand, Pat, Stmt, StmtKind, Rest}; use rustc_lint::LateContext; use rustc_span::{Span, sym}; use std::iter::once; @@ -164,7 +164,7 @@ fn never_loop_expr<'tcx>( }, ExprKind::Struct(_, fields, base) => { let fields = never_loop_expr_all(cx, fields.iter().map(|f| f.expr), local_labels, main_loop_id); - if let Some(base) = base { + if let Rest::Base(base) = base { combine_seq(fields, || never_loop_expr(cx, base, local_labels, main_loop_id)) } else { fields diff --git a/src/tools/clippy/clippy_lints/src/needless_update.rs b/src/tools/clippy/clippy_lints/src/needless_update.rs index 6a2893cefbd5b..62b5a00b24dc5 100644 --- a/src/tools/clippy/clippy_lints/src/needless_update.rs +++ b/src/tools/clippy/clippy_lints/src/needless_update.rs @@ -1,5 +1,5 @@ use clippy_utils::diagnostics::span_lint; -use rustc_hir::{Expr, ExprKind}; +use rustc_hir::{Expr, ExprKind, Rest}; use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::ty; use rustc_session::declare_lint_pass; @@ -51,7 +51,7 @@ declare_lint_pass!(NeedlessUpdate => [NEEDLESS_UPDATE]); impl<'tcx> LateLintPass<'tcx> for NeedlessUpdate { fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { - if let ExprKind::Struct(_, fields, Some(base)) = expr.kind { + if let ExprKind::Struct(_, fields, Rest::Base(base)) = expr.kind { let ty = cx.typeck_results().expr_ty(expr); if let ty::Adt(def, _) = ty.kind() { if fields.len() == def.non_enum_variant().fields.len() diff --git a/src/tools/clippy/clippy_lints/src/no_effect.rs b/src/tools/clippy/clippy_lints/src/no_effect.rs index 392cfcb813e85..1b9b7e1e439eb 100644 --- a/src/tools/clippy/clippy_lints/src/no_effect.rs +++ b/src/tools/clippy/clippy_lints/src/no_effect.rs @@ -8,7 +8,7 @@ use rustc_errors::Applicability; use rustc_hir::def::{DefKind, Res}; use rustc_hir::{ BinOpKind, BlockCheckMode, Expr, ExprKind, HirId, HirIdMap, ItemKind, LocalSource, Node, PatKind, Stmt, StmtKind, - UnsafeSource, is_range_literal, + UnsafeSource, Rest, is_range_literal, }; use rustc_infer::infer::TyCtxtInferExt as _; use rustc_lint::{LateContext, LateLintPass, LintContext}; @@ -234,7 +234,10 @@ fn has_no_effect(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool { ExprKind::Struct(_, fields, ref base) => { !has_drop(cx, cx.typeck_results().expr_ty(expr)) && fields.iter().all(|field| has_no_effect(cx, field.expr)) - && base.as_ref().map_or(true, |base| has_no_effect(cx, base)) + && match &base { + Rest::None | Rest::DefaultFields(_) => true, + Rest::Base(base) => has_no_effect(cx, base), + } }, ExprKind::Call(callee, args) => { if let ExprKind::Path(ref qpath) = callee.kind { @@ -338,6 +341,10 @@ fn reduce_expression<'a>(cx: &LateContext<'_>, expr: &'a Expr<'a>) -> Option Some(base), + Rest::None | Rest::DefaultFields(_) => None, + }; Some(fields.iter().map(|f| &f.expr).chain(base).map(Deref::deref).collect()) } }, diff --git a/src/tools/clippy/clippy_lints/src/single_range_in_vec_init.rs b/src/tools/clippy/clippy_lints/src/single_range_in_vec_init.rs index 44e585953bfb9..93717dc60f95b 100644 --- a/src/tools/clippy/clippy_lints/src/single_range_in_vec_init.rs +++ b/src/tools/clippy/clippy_lints/src/single_range_in_vec_init.rs @@ -6,7 +6,7 @@ use clippy_utils::source::SpanRangeExt; use clippy_utils::ty::implements_trait; use rustc_ast::{LitIntType, LitKind, UintTy}; use rustc_errors::Applicability; -use rustc_hir::{Expr, ExprKind, LangItem, QPath}; +use rustc_hir::{Expr, ExprKind, LangItem, QPath, Rest}; use rustc_lint::{LateContext, LateLintPass}; use rustc_session::declare_lint_pass; use std::fmt::{self, Display, Formatter}; @@ -86,7 +86,7 @@ impl LateLintPass<'_> for SingleRangeInVecInit { return; }; - let ExprKind::Struct(QPath::LangItem(lang_item, ..), [start, end], None) = inner_expr.kind else { + let ExprKind::Struct(QPath::LangItem(lang_item, ..), [start, end], Rest::None) = inner_expr.kind else { return; }; diff --git a/src/tools/clippy/clippy_lints/src/unnecessary_struct_initialization.rs b/src/tools/clippy/clippy_lints/src/unnecessary_struct_initialization.rs index afdd3505cdd16..6cdd7551c8411 100644 --- a/src/tools/clippy/clippy_lints/src/unnecessary_struct_initialization.rs +++ b/src/tools/clippy/clippy_lints/src/unnecessary_struct_initialization.rs @@ -2,7 +2,7 @@ use clippy_utils::diagnostics::span_lint_and_sugg; use clippy_utils::source::snippet; use clippy_utils::ty::is_copy; use clippy_utils::{get_parent_expr, path_to_local}; -use rustc_hir::{BindingMode, Expr, ExprField, ExprKind, Node, PatKind, Path, QPath, UnOp}; +use rustc_hir::{BindingMode, Expr, ExprField, ExprKind, Node, PatKind, Path, QPath, UnOp, Rest}; use rustc_lint::{LateContext, LateLintPass}; use rustc_session::declare_lint_pass; @@ -59,15 +59,15 @@ impl LateLintPass<'_> for UnnecessaryStruct { let field_path = same_path_in_all_fields(cx, expr, fields); let sugg = match (field_path, base) { - (Some(&path), None) => { + (Some(&path), Rest::None | Rest::DefaultFields(_)) => { // all fields match, no base given path.span }, - (Some(path), Some(base)) if base_is_suitable(cx, expr, base) && path_matches_base(path, base) => { + (Some(path), Rest::Base(base)) if base_is_suitable(cx, expr, base) && path_matches_base(path, base) => { // all fields match, has base: ensure that the path of the base matches base.span }, - (None, Some(base)) if fields.is_empty() && base_is_suitable(cx, expr, base) => { + (None, Rest::Base(base)) if fields.is_empty() && base_is_suitable(cx, expr, base) => { // just the base, no explicit fields base.span }, diff --git a/src/tools/clippy/clippy_lints/src/utils/author.rs b/src/tools/clippy/clippy_lints/src/utils/author.rs index 31f9d84f5e466..a1b3e76c9fda4 100644 --- a/src/tools/clippy/clippy_lints/src/utils/author.rs +++ b/src/tools/clippy/clippy_lints/src/utils/author.rs @@ -4,7 +4,7 @@ use rustc_ast::ast::{LitFloatType, LitKind}; use rustc_data_structures::fx::FxHashMap; use rustc_hir::{ self as hir, ArrayLen, BindingMode, CaptureBy, Closure, ClosureKind, ConstArg, ConstArgKind, CoroutineKind, - ExprKind, FnRetTy, HirId, Lit, PatKind, QPath, StmtKind, TyKind, + ExprKind, FnRetTy, HirId, Lit, PatKind, QPath, StmtKind, TyKind, Rest, }; use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_session::declare_lint_pass; @@ -597,7 +597,10 @@ impl<'a, 'tcx> PrintVisitor<'a, 'tcx> { }, ExprKind::Struct(qpath, fields, base) => { bind!(self, qpath, fields); - opt_bind!(self, base); + let base = OptionPat::new(match base { + Rest::Base(base) => Some(self.bind("base", base)), + Rest::None | Rest::DefaultFields(_) => None, + }); kind!("Struct({qpath}, {fields}, {base})"); self.qpath(qpath); self.slice(fields, |field| { diff --git a/src/tools/clippy/clippy_utils/src/higher.rs b/src/tools/clippy/clippy_utils/src/higher.rs index 3175a9a1dd31f..5f8880f45ccdc 100644 --- a/src/tools/clippy/clippy_utils/src/higher.rs +++ b/src/tools/clippy/clippy_utils/src/higher.rs @@ -8,7 +8,7 @@ use crate::ty::is_type_diagnostic_item; use rustc_ast::ast; use rustc_hir as hir; -use rustc_hir::{Arm, Block, Expr, ExprKind, HirId, LoopSource, MatchSource, Node, Pat, QPath}; +use rustc_hir::{Arm, Block, Expr, ExprKind, Rest, HirId, LoopSource, MatchSource, Node, Pat, QPath}; use rustc_lint::LateContext; use rustc_span::{Span, sym, symbol}; @@ -236,7 +236,7 @@ impl<'a> Range<'a> { limits: ast::RangeLimits::Closed, }) }, - ExprKind::Struct(path, fields, None) => match (path, fields) { + ExprKind::Struct(path, fields, Rest::None) => match (path, fields) { (QPath::LangItem(hir::LangItem::RangeFull, ..), []) => Some(Range { start: None, end: None, diff --git a/src/tools/clippy/clippy_utils/src/hir_utils.rs b/src/tools/clippy/clippy_utils/src/hir_utils.rs index 76900379ac787..30af767eb1e3a 100644 --- a/src/tools/clippy/clippy_utils/src/hir_utils.rs +++ b/src/tools/clippy/clippy_utils/src/hir_utils.rs @@ -9,7 +9,7 @@ use rustc_hir::def::Res; use rustc_hir::{ ArrayLen, AssocItemConstraint, BinOpKind, BindingMode, Block, BodyId, Closure, ConstArg, ConstArgKind, Expr, ExprField, ExprKind, FnRetTy, GenericArg, GenericArgs, HirId, HirIdMap, InlineAsmOperand, LetExpr, Lifetime, - LifetimeName, Pat, PatField, PatKind, Path, PathSegment, PrimTy, QPath, Stmt, StmtKind, Ty, TyKind, + LifetimeName, Pat, PatField, PatKind, Path, PathSegment, PrimTy, QPath, Stmt, StmtKind, Ty, TyKind, Rest, }; use rustc_lexer::{TokenKind, tokenize}; use rustc_lint::LateContext; @@ -342,7 +342,12 @@ impl HirEqInterExpr<'_, '_, '_> { (ExprKind::Ret(l), ExprKind::Ret(r)) => both(l, r, |l, r| self.eq_expr(l, r)), (&ExprKind::Struct(l_path, lf, ref lo), &ExprKind::Struct(r_path, rf, ref ro)) => { self.eq_qpath(l_path, r_path) - && both(lo, ro, |l, r| self.eq_expr(l, r)) + && match (lo, ro) { + (Rest::Base(l), Rest::Base(r)) => self.eq_expr(l, r), + (Rest::None, Rest::None) => true, + (Rest::DefaultFields(_), Rest::DefaultFields(_)) => true, + _ => false, + } && over(lf, rf, |l, r| self.eq_expr_field(l, r)) }, (&ExprKind::Tup(l_tup), &ExprKind::Tup(r_tup)) => self.eq_exprs(l_tup, r_tup), @@ -917,7 +922,7 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> { self.hash_expr(f.expr); } - if let Some(e) = *expr { + if let Rest::Base(e) = *expr { self.hash_expr(e); } }, diff --git a/src/tools/clippy/clippy_utils/src/visitors.rs b/src/tools/clippy/clippy_utils/src/visitors.rs index 6d9a85a1181c4..3c207bc1ce7d9 100644 --- a/src/tools/clippy/clippy_utils/src/visitors.rs +++ b/src/tools/clippy/clippy_utils/src/visitors.rs @@ -7,7 +7,7 @@ use rustc_hir::def::{CtorKind, DefKind, Res}; use rustc_hir::intravisit::{self, Visitor, walk_block, walk_expr}; use rustc_hir::{ AnonConst, Arm, Block, BlockCheckMode, Body, BodyId, Expr, ExprKind, HirId, ItemId, ItemKind, LetExpr, Pat, QPath, - Safety, Stmt, UnOp, UnsafeSource, + Safety, Stmt, UnOp, UnsafeSource, Rest, }; use rustc_lint::LateContext; use rustc_middle::hir::nested_filter; @@ -672,7 +672,7 @@ pub fn for_each_unconsumed_temporary<'tcx, B>( for field in fields { helper(typeck, true, field.expr, f)?; } - if let Some(default) = default { + if let Rest::Base(default) = default { helper(typeck, false, default, f)?; } }, diff --git a/tests/ui/feature-gates/feature-gate-default-field-values.rs b/tests/ui/feature-gates/feature-gate-default-field-values.rs new file mode 100644 index 0000000000000..db024ad3e0af8 --- /dev/null +++ b/tests/ui/feature-gates/feature-gate-default-field-values.rs @@ -0,0 +1,69 @@ +#![feature(generic_const_exprs)] +#![allow(unused_variables, dead_code, incomplete_features)] + +pub struct S; + +#[derive(Default)] +pub struct Foo { + pub bar: S = S, //~ ERROR default values on `struct` fields aren't supported + pub baz: i32 = 42 + 3, //~ ERROR default values on `struct` fields aren't supported +} + +#[derive(Default)] +pub enum Bar { + #[default] + Foo { //~ ERROR the `#[default]` attribute may only be used on unit enum variants + bar: S = S, //~ ERROR default values on `struct` fields aren't supported + baz: i32 = 42 + 3, //~ ERROR default values on `struct` fields aren't supported + } +} + +#[derive(Default)] +pub struct Qux { + bar: S = Qux::::S, //~ ERROR default values on `struct` fields aren't supported + baz: i32 = foo(), //~ ERROR default values on `struct` fields aren't supported + bat: i32 = as T>::K, //~ ERROR default values on `struct` fields aren't supported + bay: i32 = C, //~ ERROR default values on `struct` fields aren't supported + bak: Vec = Vec::new(), //~ ERROR default values on `struct` fields aren't supported +} + +impl Qux { + const S: S = S; +} + +trait T { + const K: i32; +} + +impl T for Qux { + const K: i32 = 2; +} + +const fn foo() -> i32 { + 42 +} + +fn main () { + let x = Foo { .. }; //~ ERROR base expression required after `..` + let y = Foo::default(); + let z = Foo { baz: 1, .. }; //~ ERROR base expression required after `..` + + assert_eq!(45, x.baz); + assert_eq!(45, y.baz); + assert_eq!(1, z.baz); + + let x = Bar::Foo { .. }; //~ ERROR base expression required after `..` + //~^ ERROR functional record update syntax requires a struct + let y = Bar::default(); + let z = Bar::Foo { baz: 1, .. }; //~ ERROR base expression required after `..` + //~^ ERROR functional record update syntax requires a struct + + assert!(matches!(Bar::Foo { bar: S, baz: 45 }, x)); + assert!(matches!(Bar::Foo { bar: S, baz: 45 }, y)); + assert!(matches!(Bar::Foo { bar: S, baz: 1 }, z)); + + let x = Qux:: { .. }; //~ ERROR base expression required after `..` + assert!(matches!(Qux:: { bar: S, baz: 42, bat: 2, bay: 4, .. }, x)); + //~^ ERROR base expression required after `..` + assert!(x.bak.is_empty()); +} diff --git a/tests/ui/feature-gates/feature-gate-default-field-values.stderr b/tests/ui/feature-gates/feature-gate-default-field-values.stderr new file mode 100644 index 0000000000000..7d50733b38397 --- /dev/null +++ b/tests/ui/feature-gates/feature-gate-default-field-values.stderr @@ -0,0 +1,180 @@ +error: the `#[default]` attribute may only be used on unit enum variants + --> $DIR/feature-gate-default-field-values.rs:15:5 + | +LL | Foo { + | ^^^ + | + = help: consider a manual implementation of `Default` + +error[E0658]: default values on `struct` fields aren't supported + --> $DIR/feature-gate-default-field-values.rs:8:15 + | +LL | pub bar: S = S, + | ^^^^ + | + = note: see issue #3681 for more information + = help: add `#![feature(default_field_values)]` to the crate attributes to enable + = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date + +error[E0658]: default values on `struct` fields aren't supported + --> $DIR/feature-gate-default-field-values.rs:9:17 + | +LL | pub baz: i32 = 42 + 3, + | ^^^^^^^^^ + | + = note: see issue #3681 for more information + = help: add `#![feature(default_field_values)]` to the crate attributes to enable + = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date + +error[E0658]: default values on `struct` fields aren't supported + --> $DIR/feature-gate-default-field-values.rs:16:15 + | +LL | bar: S = S, + | ^^^^ + | + = note: see issue #3681 for more information + = help: add `#![feature(default_field_values)]` to the crate attributes to enable + = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date + +error[E0658]: default values on `struct` fields aren't supported + --> $DIR/feature-gate-default-field-values.rs:17:17 + | +LL | baz: i32 = 42 + 3, + | ^^^^^^^^^ + | + = note: see issue #3681 for more information + = help: add `#![feature(default_field_values)]` to the crate attributes to enable + = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date + +error[E0658]: default values on `struct` fields aren't supported + --> $DIR/feature-gate-default-field-values.rs:23:11 + | +LL | bar: S = Qux::::S, + | ^^^^^^^^^^^^^^^^^ + | + = note: see issue #3681 for more information + = help: add `#![feature(default_field_values)]` to the crate attributes to enable + = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date + +error[E0658]: default values on `struct` fields aren't supported + --> $DIR/feature-gate-default-field-values.rs:24:13 + | +LL | baz: i32 = foo(), + | ^^^^^^^^ + | + = note: see issue #3681 for more information + = help: add `#![feature(default_field_values)]` to the crate attributes to enable + = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date + +error[E0658]: default values on `struct` fields aren't supported + --> $DIR/feature-gate-default-field-values.rs:25:13 + | +LL | bat: i32 = as T>::K, + | ^^^^^^^^^^^^^^^^^^^^^^ + | + = note: see issue #3681 for more information + = help: add `#![feature(default_field_values)]` to the crate attributes to enable + = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date + +error[E0658]: default values on `struct` fields aren't supported + --> $DIR/feature-gate-default-field-values.rs:26:13 + | +LL | bay: i32 = C, + | ^^^^ + | + = note: see issue #3681 for more information + = help: add `#![feature(default_field_values)]` to the crate attributes to enable + = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date + +error[E0658]: default values on `struct` fields aren't supported + --> $DIR/feature-gate-default-field-values.rs:27:16 + | +LL | bak: Vec = Vec::new(), + | ^^^^^^^^^^^^^ + | + = note: see issue #3681 for more information + = help: add `#![feature(default_field_values)]` to the crate attributes to enable + = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date + +error[E0797]: base expression required after `..` + --> $DIR/feature-gate-default-field-values.rs:47:21 + | +LL | let x = Foo { .. }; + | ^ + | +help: add a base expression here + | +LL | let x = Foo { ../* expr */ }; + | ++++++++++ + +error[E0797]: base expression required after `..` + --> $DIR/feature-gate-default-field-values.rs:49:29 + | +LL | let z = Foo { baz: 1, .. }; + | ^ + | +help: add a base expression here + | +LL | let z = Foo { baz: 1, ../* expr */ }; + | ++++++++++ + +error[E0797]: base expression required after `..` + --> $DIR/feature-gate-default-field-values.rs:55:26 + | +LL | let x = Bar::Foo { .. }; + | ^ + | +help: add a base expression here + | +LL | let x = Bar::Foo { ../* expr */ }; + | ++++++++++ + +error[E0797]: base expression required after `..` + --> $DIR/feature-gate-default-field-values.rs:58:34 + | +LL | let z = Bar::Foo { baz: 1, .. }; + | ^ + | +help: add a base expression here + | +LL | let z = Bar::Foo { baz: 1, ../* expr */ }; + | ++++++++++ + +error[E0797]: base expression required after `..` + --> $DIR/feature-gate-default-field-values.rs:65:31 + | +LL | let x = Qux:: { .. }; + | ^ + | +help: add a base expression here + | +LL | let x = Qux:: { ../* expr */ }; + | ++++++++++ + +error[E0797]: base expression required after `..` + --> $DIR/feature-gate-default-field-values.rs:66:73 + | +LL | assert!(matches!(Qux:: { bar: S, baz: 42, bat: 2, bay: 4, .. }, x)); + | ^ + | +help: add a base expression here + | +LL | assert!(matches!(Qux:: { bar: S, baz: 42, bat: 2, bay: 4, ../* expr */ }, x)); + | ++++++++++ + +error[E0436]: functional record update syntax requires a struct + --> $DIR/feature-gate-default-field-values.rs:55:26 + | +LL | let x = Bar::Foo { .. }; + | ^ + +error[E0436]: functional record update syntax requires a struct + --> $DIR/feature-gate-default-field-values.rs:58:34 + | +LL | let z = Bar::Foo { baz: 1, .. }; + | ^ + +error: aborting due to 18 previous errors + +Some errors have detailed explanations: E0436, E0658, E0797. +For more information about an error, try `rustc --explain E0436`. diff --git a/tests/ui/parser/struct-default-values-and-missing-field-separator.fixed b/tests/ui/parser/struct-default-values-and-missing-field-separator.fixed deleted file mode 100644 index be6ed053c6e3e..0000000000000 --- a/tests/ui/parser/struct-default-values-and-missing-field-separator.fixed +++ /dev/null @@ -1,35 +0,0 @@ -//@ run-rustfix -#![allow(dead_code)] - -enum E { - A, -} - -struct S { - field1: i32, //~ ERROR default values on `struct` fields aren't supported - field2: E, //~ ERROR default values on `struct` fields aren't supported - field3: i32, //~ ERROR default values on `struct` fields aren't supported - field4: i32, //~ ERROR default values on `struct` fields aren't supported - field5: E, //~ ERROR default values on `struct` fields aren't supported - field6: E, //~ ERROR default values on `struct` fields aren't supported -} - -struct S1 { - field1: i32, //~ ERROR expected `,`, or `}`, found `field2` - field2: E, //~ ERROR expected `,`, or `}`, found `field3` - field3: i32, //~ ERROR default values on `struct` fields aren't supported - field4: i32, //~ ERROR default values on `struct` fields aren't supported - field5: E, //~ ERROR default values on `struct` fields aren't supported - field6: E, //~ ERROR default values on `struct` fields aren't supported -} - -struct S2 { - field1 : i32, //~ ERROR expected `:`, found `=` - field2: E, //~ ERROR expected `:`, found `;` -} - -const fn foo(_: i32) -> E { - E::A -} - -fn main() {} diff --git a/tests/ui/parser/struct-default-values-and-missing-field-separator.rs b/tests/ui/parser/struct-default-values-and-missing-field-separator.rs index 7900d397a5ded..8ecf042ad385d 100644 --- a/tests/ui/parser/struct-default-values-and-missing-field-separator.rs +++ b/tests/ui/parser/struct-default-values-and-missing-field-separator.rs @@ -1,4 +1,3 @@ -//@ run-rustfix #![allow(dead_code)] enum E { diff --git a/tests/ui/parser/struct-default-values-and-missing-field-separator.stderr b/tests/ui/parser/struct-default-values-and-missing-field-separator.stderr index 1fb57ab11f9f2..1a7b751142ffe 100644 --- a/tests/ui/parser/struct-default-values-and-missing-field-separator.stderr +++ b/tests/ui/parser/struct-default-values-and-missing-field-separator.stderr @@ -1,152 +1,133 @@ -error: default values on `struct` fields aren't supported - --> $DIR/struct-default-values-and-missing-field-separator.rs:9:16 +error: expected `,`, or `}`, found `field2` + --> $DIR/struct-default-values-and-missing-field-separator.rs:17:16 | -LL | field1: i32 = 42, - | ^^^^^ +LL | field1: i32 + | ^ help: try adding a comma: `,` + +error: expected `,`, or `}`, found `field3` + --> $DIR/struct-default-values-and-missing-field-separator.rs:18:14 + | +LL | field2: E + | ^ help: try adding a comma: `,` + +error: expected `:`, found `=` + --> $DIR/struct-default-values-and-missing-field-separator.rs:26:12 + | +LL | field1 = i32, + | ^ + | | + | expected `:` + | help: field names and their types are separated with `:` + +error: expected `:`, found `;` + --> $DIR/struct-default-values-and-missing-field-separator.rs:27:11 | -help: remove this unsupported default value +LL | field2; E, + | ^ + | | + | expected `:` + | help: field names and their types are separated with `:` + +error[E0658]: default values on `struct` fields aren't supported + --> $DIR/struct-default-values-and-missing-field-separator.rs:8:16 | -LL - field1: i32 = 42, -LL + field1: i32, +LL | field1: i32 = 42, + | ^^^^^ | + = note: see issue #3681 for more information + = help: add `#![feature(default_field_values)]` to the crate attributes to enable + = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date -error: default values on `struct` fields aren't supported - --> $DIR/struct-default-values-and-missing-field-separator.rs:10:14 +error[E0658]: default values on `struct` fields aren't supported + --> $DIR/struct-default-values-and-missing-field-separator.rs:9:14 | LL | field2: E = E::A, | ^^^^^^^ | -help: remove this unsupported default value - | -LL - field2: E = E::A, -LL + field2: E, - | + = note: see issue #3681 for more information + = help: add `#![feature(default_field_values)]` to the crate attributes to enable + = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date -error: default values on `struct` fields aren't supported - --> $DIR/struct-default-values-and-missing-field-separator.rs:11:16 +error[E0658]: default values on `struct` fields aren't supported + --> $DIR/struct-default-values-and-missing-field-separator.rs:10:16 | LL | field3: i32 = 1 + 2, | ^^^^^^^^ | -help: remove this unsupported default value - | -LL - field3: i32 = 1 + 2, -LL + field3: i32, - | + = note: see issue #3681 for more information + = help: add `#![feature(default_field_values)]` to the crate attributes to enable + = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date -error: default values on `struct` fields aren't supported - --> $DIR/struct-default-values-and-missing-field-separator.rs:12:16 +error[E0658]: default values on `struct` fields aren't supported + --> $DIR/struct-default-values-and-missing-field-separator.rs:11:16 | LL | field4: i32 = { 1 + 2 }, | ^^^^^^^^^^^^ | -help: remove this unsupported default value - | -LL - field4: i32 = { 1 + 2 }, -LL + field4: i32, - | + = note: see issue #3681 for more information + = help: add `#![feature(default_field_values)]` to the crate attributes to enable + = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date -error: default values on `struct` fields aren't supported - --> $DIR/struct-default-values-and-missing-field-separator.rs:13:14 +error[E0658]: default values on `struct` fields aren't supported + --> $DIR/struct-default-values-and-missing-field-separator.rs:12:14 | LL | field5: E = foo(42), | ^^^^^^^^^^ | -help: remove this unsupported default value - | -LL - field5: E = foo(42), -LL + field5: E, - | + = note: see issue #3681 for more information + = help: add `#![feature(default_field_values)]` to the crate attributes to enable + = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date -error: default values on `struct` fields aren't supported - --> $DIR/struct-default-values-and-missing-field-separator.rs:14:14 +error[E0658]: default values on `struct` fields aren't supported + --> $DIR/struct-default-values-and-missing-field-separator.rs:13:14 | LL | field6: E = { foo(42) }, | ^^^^^^^^^^^^^^ | -help: remove this unsupported default value - | -LL - field6: E = { foo(42) }, -LL + field6: E, - | - -error: expected `,`, or `}`, found `field2` - --> $DIR/struct-default-values-and-missing-field-separator.rs:18:16 - | -LL | field1: i32 - | ^ help: try adding a comma: `,` - -error: expected `,`, or `}`, found `field3` - --> $DIR/struct-default-values-and-missing-field-separator.rs:19:14 - | -LL | field2: E - | ^ help: try adding a comma: `,` + = note: see issue #3681 for more information + = help: add `#![feature(default_field_values)]` to the crate attributes to enable + = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date -error: default values on `struct` fields aren't supported - --> $DIR/struct-default-values-and-missing-field-separator.rs:20:16 +error[E0658]: default values on `struct` fields aren't supported + --> $DIR/struct-default-values-and-missing-field-separator.rs:19:16 | LL | field3: i32 = 1 + 2, | ^^^^^^^^ | -help: remove this unsupported default value - | -LL - field3: i32 = 1 + 2, -LL + field3: i32, - | + = note: see issue #3681 for more information + = help: add `#![feature(default_field_values)]` to the crate attributes to enable + = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date -error: default values on `struct` fields aren't supported - --> $DIR/struct-default-values-and-missing-field-separator.rs:21:16 +error[E0658]: default values on `struct` fields aren't supported + --> $DIR/struct-default-values-and-missing-field-separator.rs:20:16 | LL | field4: i32 = { 1 + 2 }, | ^^^^^^^^^^^^ | -help: remove this unsupported default value - | -LL - field4: i32 = { 1 + 2 }, -LL + field4: i32, - | + = note: see issue #3681 for more information + = help: add `#![feature(default_field_values)]` to the crate attributes to enable + = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date -error: default values on `struct` fields aren't supported - --> $DIR/struct-default-values-and-missing-field-separator.rs:22:14 +error[E0658]: default values on `struct` fields aren't supported + --> $DIR/struct-default-values-and-missing-field-separator.rs:21:14 | LL | field5: E = foo(42), | ^^^^^^^^^^ | -help: remove this unsupported default value - | -LL - field5: E = foo(42), -LL + field5: E, - | + = note: see issue #3681 for more information + = help: add `#![feature(default_field_values)]` to the crate attributes to enable + = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date -error: default values on `struct` fields aren't supported - --> $DIR/struct-default-values-and-missing-field-separator.rs:23:14 +error[E0658]: default values on `struct` fields aren't supported + --> $DIR/struct-default-values-and-missing-field-separator.rs:22:14 | LL | field6: E = { foo(42) }, | ^^^^^^^^^^^^^^ | -help: remove this unsupported default value - | -LL - field6: E = { foo(42) }, -LL + field6: E, - | - -error: expected `:`, found `=` - --> $DIR/struct-default-values-and-missing-field-separator.rs:27:12 - | -LL | field1 = i32, - | ^ - | | - | expected `:` - | help: field names and their types are separated with `:` - -error: expected `:`, found `;` - --> $DIR/struct-default-values-and-missing-field-separator.rs:28:11 - | -LL | field2; E, - | ^ - | | - | expected `:` - | help: field names and their types are separated with `:` + = note: see issue #3681 for more information + = help: add `#![feature(default_field_values)]` to the crate attributes to enable + = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date error: aborting due to 14 previous errors +For more information about this error, try `rustc --explain E0658`. diff --git a/tests/ui/stats/hir-stats.stderr b/tests/ui/stats/hir-stats.stderr index b27f769ba34f4..583658e6158fa 100644 --- a/tests/ui/stats/hir-stats.stderr +++ b/tests/ui/stats/hir-stats.stderr @@ -15,37 +15,37 @@ ast-stats-1 ForeignItem 88 ( 1.3%) 1 88 ast-stats-1 - Fn 88 ( 1.3%) 1 ast-stats-1 Arm 96 ( 1.4%) 2 48 ast-stats-1 FnDecl 120 ( 1.8%) 5 24 -ast-stats-1 FieldDef 160 ( 2.4%) 2 80 ast-stats-1 Stmt 160 ( 2.4%) 5 32 ast-stats-1 - Let 32 ( 0.5%) 1 ast-stats-1 - MacCall 32 ( 0.5%) 1 ast-stats-1 - Expr 96 ( 1.4%) 3 ast-stats-1 Param 160 ( 2.4%) 4 40 +ast-stats-1 FieldDef 192 ( 2.9%) 2 96 ast-stats-1 Block 192 ( 2.9%) 6 32 ast-stats-1 Variant 208 ( 3.1%) 2 104 ast-stats-1 GenericBound 352 ( 5.3%) 4 88 ast-stats-1 - Trait 352 ( 5.3%) 4 ast-stats-1 AssocItem 352 ( 5.3%) 4 88 -ast-stats-1 - Type 176 ( 2.7%) 2 -ast-stats-1 - Fn 176 ( 2.7%) 2 +ast-stats-1 - Type 176 ( 2.6%) 2 +ast-stats-1 - Fn 176 ( 2.6%) 2 ast-stats-1 GenericParam 480 ( 7.2%) 5 96 ast-stats-1 Pat 504 ( 7.6%) 7 72 ast-stats-1 - Struct 72 ( 1.1%) 1 ast-stats-1 - Wild 72 ( 1.1%) 1 ast-stats-1 - Ident 360 ( 5.4%) 5 -ast-stats-1 Expr 576 ( 8.7%) 8 72 +ast-stats-1 Expr 576 ( 8.6%) 8 72 ast-stats-1 - Path 72 ( 1.1%) 1 ast-stats-1 - Match 72 ( 1.1%) 1 ast-stats-1 - Struct 72 ( 1.1%) 1 ast-stats-1 - Lit 144 ( 2.2%) 2 -ast-stats-1 - Block 216 ( 3.3%) 3 +ast-stats-1 - Block 216 ( 3.2%) 3 ast-stats-1 PathSegment 744 (11.2%) 31 24 -ast-stats-1 Ty 896 (13.5%) 14 64 +ast-stats-1 Ty 896 (13.4%) 14 64 ast-stats-1 - Ptr 64 ( 1.0%) 1 ast-stats-1 - Ref 64 ( 1.0%) 1 ast-stats-1 - ImplicitSelf 128 ( 1.9%) 2 ast-stats-1 - Path 640 ( 9.6%) 10 -ast-stats-1 Item 1_224 (18.4%) 9 136 +ast-stats-1 Item 1_224 (18.3%) 9 136 ast-stats-1 - Trait 136 ( 2.0%) 1 ast-stats-1 - Enum 136 ( 2.0%) 1 ast-stats-1 - ForeignMod 136 ( 2.0%) 1 @@ -53,7 +53,7 @@ ast-stats-1 - Impl 136 ( 2.0%) 1 ast-stats-1 - Fn 272 ( 4.1%) 2 ast-stats-1 - Use 408 ( 6.1%) 3 ast-stats-1 ---------------------------------------------------------------- -ast-stats-1 Total 6_640 +ast-stats-1 Total 6_672 ast-stats-1 ast-stats-2 POST EXPANSION AST STATS ast-stats-2 Name Accumulated Size Count Item Size @@ -70,17 +70,17 @@ ast-stats-2 - Fn 88 ( 1.2%) 1 ast-stats-2 Arm 96 ( 1.3%) 2 48 ast-stats-2 InlineAsm 120 ( 1.6%) 1 120 ast-stats-2 FnDecl 120 ( 1.6%) 5 24 -ast-stats-2 Attribute 128 ( 1.8%) 4 32 +ast-stats-2 Attribute 128 ( 1.7%) 4 32 ast-stats-2 - DocComment 32 ( 0.4%) 1 ast-stats-2 - Normal 96 ( 1.3%) 3 -ast-stats-2 FieldDef 160 ( 2.2%) 2 80 ast-stats-2 Stmt 160 ( 2.2%) 5 32 ast-stats-2 - Let 32 ( 0.4%) 1 ast-stats-2 - Semi 32 ( 0.4%) 1 ast-stats-2 - Expr 96 ( 1.3%) 3 ast-stats-2 Param 160 ( 2.2%) 4 40 +ast-stats-2 FieldDef 192 ( 2.6%) 2 96 ast-stats-2 Block 192 ( 2.6%) 6 32 -ast-stats-2 Variant 208 ( 2.9%) 2 104 +ast-stats-2 Variant 208 ( 2.8%) 2 104 ast-stats-2 GenericBound 352 ( 4.8%) 4 88 ast-stats-2 - Trait 352 ( 4.8%) 4 ast-stats-2 AssocItem 352 ( 4.8%) 4 88 @@ -98,22 +98,22 @@ ast-stats-2 - Struct 72 ( 1.0%) 1 ast-stats-2 - InlineAsm 72 ( 1.0%) 1 ast-stats-2 - Lit 144 ( 2.0%) 2 ast-stats-2 - Block 216 ( 3.0%) 3 -ast-stats-2 PathSegment 864 (11.9%) 36 24 -ast-stats-2 Ty 896 (12.3%) 14 64 +ast-stats-2 PathSegment 864 (11.8%) 36 24 +ast-stats-2 Ty 896 (12.2%) 14 64 ast-stats-2 - Ptr 64 ( 0.9%) 1 ast-stats-2 - Ref 64 ( 0.9%) 1 -ast-stats-2 - ImplicitSelf 128 ( 1.8%) 2 -ast-stats-2 - Path 640 ( 8.8%) 10 -ast-stats-2 Item 1_496 (20.5%) 11 136 +ast-stats-2 - ImplicitSelf 128 ( 1.7%) 2 +ast-stats-2 - Path 640 ( 8.7%) 10 +ast-stats-2 Item 1_496 (20.4%) 11 136 ast-stats-2 - Trait 136 ( 1.9%) 1 ast-stats-2 - Enum 136 ( 1.9%) 1 ast-stats-2 - ExternCrate 136 ( 1.9%) 1 ast-stats-2 - ForeignMod 136 ( 1.9%) 1 ast-stats-2 - Impl 136 ( 1.9%) 1 ast-stats-2 - Fn 272 ( 3.7%) 2 -ast-stats-2 - Use 544 ( 7.5%) 4 +ast-stats-2 - Use 544 ( 7.4%) 4 ast-stats-2 ---------------------------------------------------------------- -ast-stats-2 Total 7_288 +ast-stats-2 Total 7_320 ast-stats-2 hir-stats HIR STATS hir-stats Name Accumulated Size Count Item Size @@ -132,11 +132,11 @@ hir-stats Body 72 ( 0.8%) 3 24 hir-stats InlineAsm 72 ( 0.8%) 1 72 hir-stats ImplItemRef 72 ( 0.8%) 2 36 hir-stats Arm 80 ( 0.9%) 2 40 -hir-stats FieldDef 96 ( 1.1%) 2 48 hir-stats Stmt 96 ( 1.1%) 3 32 hir-stats - Let 32 ( 0.4%) 1 hir-stats - Semi 32 ( 0.4%) 1 hir-stats - Expr 32 ( 0.4%) 1 +hir-stats FieldDef 112 ( 1.2%) 2 56 hir-stats FnDecl 120 ( 1.3%) 3 40 hir-stats Attribute 128 ( 1.4%) 4 32 hir-stats Variant 144 ( 1.6%) 2 72 @@ -174,5 +174,5 @@ hir-stats - Use 352 ( 3.9%) 4 hir-stats Path 1_240 (13.8%) 31 40 hir-stats PathSegment 1_920 (21.4%) 40 48 hir-stats ---------------------------------------------------------------- -hir-stats Total 8_960 +hir-stats Total 8_976 hir-stats diff --git a/tests/ui/structs/default-field-values-failures.rs b/tests/ui/structs/default-field-values-failures.rs new file mode 100644 index 0000000000000..c4de4f5cdade5 --- /dev/null +++ b/tests/ui/structs/default-field-values-failures.rs @@ -0,0 +1,49 @@ +#![feature(default_field_values)] + +#[derive(Debug)] +pub struct S; + +#[derive(Debug, Default)] +pub struct Foo { + pub bar: S = S, + pub baz: i32 = 42 + 3, +} + +#[derive(Debug, Default)] +pub struct Bar { + pub bar: S, //~ ERROR the trait bound `S: Default` is not satisfied + pub baz: i32 = 42 + 3, +} + +#[derive(Default)] +pub struct Qux { + bar: S = Self::S, //~ ERROR generic `Self` types are currently not permitted in anonymous constants + baz: i32 = foo(), + bat: i32 = as T>::K, //~ ERROR generic parameters may not be used in const operations + bay: i32 = C, +} + +impl Qux { + const S: S = S; +} + +trait T { + const K: i32; +} + +impl T for Qux { + const K: i32 = 2; +} + +const fn foo() -> i32 { + 42 +} + +fn main () { + let _ = Foo { .. }; // ok + let _ = Foo::default(); // ok + let _ = Bar { .. }; //~ ERROR mandatory field + let _ = Bar::default(); // silenced + let _ = Bar { bar: S, .. }; // ok + let _ = Qux::<4> { .. }; +} diff --git a/tests/ui/structs/default-field-values-failures.stderr b/tests/ui/structs/default-field-values-failures.stderr new file mode 100644 index 0000000000000..bd96777f86521 --- /dev/null +++ b/tests/ui/structs/default-field-values-failures.stderr @@ -0,0 +1,40 @@ +error: generic parameters may not be used in const operations + --> $DIR/default-field-values-failures.rs:22:23 + | +LL | bat: i32 = as T>::K, + | ^ cannot perform const operation using `C` + | + = help: const parameters may only be used as standalone arguments, i.e. `C` + = help: add `#![feature(generic_const_exprs)]` to allow generic const expressions + +error[E0277]: the trait bound `S: Default` is not satisfied + --> $DIR/default-field-values-failures.rs:14:5 + | +LL | #[derive(Debug, Default)] + | ------- in this derive macro expansion +LL | pub struct Bar { +LL | pub bar: S, + | ^^^^^^^^^^ the trait `Default` is not implemented for `S` + | + = note: this error originates in the derive macro `Default` (in Nightly builds, run with -Z macro-backtrace for more info) +help: consider annotating `S` with `#[derive(Default)]` + | +LL + #[derive(Default)] +LL | pub struct S; + | + +error: missing mandatory field `bar` + --> $DIR/default-field-values-failures.rs:45:21 + | +LL | let _ = Bar { .. }; + | ^ + +error: generic `Self` types are currently not permitted in anonymous constants + --> $DIR/default-field-values-failures.rs:20:14 + | +LL | bar: S = Self::S, + | ^^^^ + +error: aborting due to 4 previous errors + +For more information about this error, try `rustc --explain E0277`. diff --git a/tests/ui/structs/default-field-values-support.rs b/tests/ui/structs/default-field-values-support.rs new file mode 100644 index 0000000000000..bdf21e1272d87 --- /dev/null +++ b/tests/ui/structs/default-field-values-support.rs @@ -0,0 +1,68 @@ +//@ run-pass +#![feature(default_field_values, generic_const_exprs)] +#![allow(unused_variables, dead_code, incomplete_features)] + +pub struct S; + +#[derive(Default)] +pub struct Foo { + pub bar: S = S, + pub baz: i32 = 42 + 3, +} + +#[derive(Default)] +pub enum Bar { + #[default] + Foo { + bar: S = S, + baz: i32 = 42 + 3, + } +} + +#[derive(Default)] +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(), +} + +impl Qux { + const S: S = S; +} + +trait T { + const K: i32; +} + +impl T for Qux { + const K: i32 = 2; +} + +const fn foo() -> i32 { + 42 +} + +fn main () { + let x = Foo { .. }; + let y = Foo::default(); + let z = Foo { baz: 1, .. }; + + assert_eq!(45, x.baz); + assert_eq!(45, y.baz); + assert_eq!(1, z.baz); + + let x = Bar::Foo { .. }; + let y = Bar::default(); + let z = Bar::Foo { baz: 1, .. }; + + assert!(matches!(Bar::Foo { bar: S, baz: 45 }, x)); + 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)); + assert!(x.bak.is_empty()); +} diff --git a/tests/ui/thir-print/thir-tree-match.stdout b/tests/ui/thir-print/thir-tree-match.stdout index b2431698cc645..f8c74106caf97 100644 --- a/tests/ui/thir-print/thir-tree-match.stdout +++ b/tests/ui/thir-print/thir-tree-match.stdout @@ -92,7 +92,7 @@ body: adt_def: AdtDef { did: DefId(0:10 ~ thir_tree_match[fcf8]::Foo) - variants: [VariantDef { def_id: DefId(0:11 ~ thir_tree_match[fcf8]::Foo::FooOne), ctor: Some((Fn, DefId(0:12 ~ thir_tree_match[fcf8]::Foo::FooOne::{constructor#0}))), name: "FooOne", discr: Relative(0), fields: [FieldDef { did: DefId(0:13 ~ thir_tree_match[fcf8]::Foo::FooOne::0), name: "0", vis: Restricted(DefId(0:0 ~ thir_tree_match[fcf8])) }], tainted: None, flags: }, VariantDef { def_id: DefId(0:14 ~ thir_tree_match[fcf8]::Foo::FooTwo), ctor: Some((Const, DefId(0:15 ~ thir_tree_match[fcf8]::Foo::FooTwo::{constructor#0}))), name: "FooTwo", discr: Relative(1), fields: [], tainted: None, flags: }] + variants: [VariantDef { def_id: DefId(0:11 ~ thir_tree_match[fcf8]::Foo::FooOne), ctor: Some((Fn, DefId(0:12 ~ thir_tree_match[fcf8]::Foo::FooOne::{constructor#0}))), name: "FooOne", discr: Relative(0), fields: [FieldDef { did: DefId(0:13 ~ thir_tree_match[fcf8]::Foo::FooOne::0), name: "0", vis: Restricted(DefId(0:0 ~ thir_tree_match[fcf8])), value: None }], tainted: None, flags: }, VariantDef { def_id: DefId(0:14 ~ thir_tree_match[fcf8]::Foo::FooTwo), ctor: Some((Const, DefId(0:15 ~ thir_tree_match[fcf8]::Foo::FooTwo::{constructor#0}))), name: "FooTwo", discr: Relative(1), fields: [], tainted: None, flags: }] flags: IS_ENUM repr: ReprOptions { int: None, align: None, pack: None, flags: , field_shuffle_seed: 3477539199540094892 } args: [] @@ -154,7 +154,7 @@ body: adt_def: AdtDef { did: DefId(0:10 ~ thir_tree_match[fcf8]::Foo) - variants: [VariantDef { def_id: DefId(0:11 ~ thir_tree_match[fcf8]::Foo::FooOne), ctor: Some((Fn, DefId(0:12 ~ thir_tree_match[fcf8]::Foo::FooOne::{constructor#0}))), name: "FooOne", discr: Relative(0), fields: [FieldDef { did: DefId(0:13 ~ thir_tree_match[fcf8]::Foo::FooOne::0), name: "0", vis: Restricted(DefId(0:0 ~ thir_tree_match[fcf8])) }], tainted: None, flags: }, VariantDef { def_id: DefId(0:14 ~ thir_tree_match[fcf8]::Foo::FooTwo), ctor: Some((Const, DefId(0:15 ~ thir_tree_match[fcf8]::Foo::FooTwo::{constructor#0}))), name: "FooTwo", discr: Relative(1), fields: [], tainted: None, flags: }] + variants: [VariantDef { def_id: DefId(0:11 ~ thir_tree_match[fcf8]::Foo::FooOne), ctor: Some((Fn, DefId(0:12 ~ thir_tree_match[fcf8]::Foo::FooOne::{constructor#0}))), name: "FooOne", discr: Relative(0), fields: [FieldDef { did: DefId(0:13 ~ thir_tree_match[fcf8]::Foo::FooOne::0), name: "0", vis: Restricted(DefId(0:0 ~ thir_tree_match[fcf8])), value: None }], tainted: None, flags: }, VariantDef { def_id: DefId(0:14 ~ thir_tree_match[fcf8]::Foo::FooTwo), ctor: Some((Const, DefId(0:15 ~ thir_tree_match[fcf8]::Foo::FooTwo::{constructor#0}))), name: "FooTwo", discr: Relative(1), fields: [], tainted: None, flags: }] flags: IS_ENUM repr: ReprOptions { int: None, align: None, pack: None, flags: , field_shuffle_seed: 3477539199540094892 } args: [] @@ -206,7 +206,7 @@ body: adt_def: AdtDef { did: DefId(0:10 ~ thir_tree_match[fcf8]::Foo) - variants: [VariantDef { def_id: DefId(0:11 ~ thir_tree_match[fcf8]::Foo::FooOne), ctor: Some((Fn, DefId(0:12 ~ thir_tree_match[fcf8]::Foo::FooOne::{constructor#0}))), name: "FooOne", discr: Relative(0), fields: [FieldDef { did: DefId(0:13 ~ thir_tree_match[fcf8]::Foo::FooOne::0), name: "0", vis: Restricted(DefId(0:0 ~ thir_tree_match[fcf8])) }], tainted: None, flags: }, VariantDef { def_id: DefId(0:14 ~ thir_tree_match[fcf8]::Foo::FooTwo), ctor: Some((Const, DefId(0:15 ~ thir_tree_match[fcf8]::Foo::FooTwo::{constructor#0}))), name: "FooTwo", discr: Relative(1), fields: [], tainted: None, flags: }] + variants: [VariantDef { def_id: DefId(0:11 ~ thir_tree_match[fcf8]::Foo::FooOne), ctor: Some((Fn, DefId(0:12 ~ thir_tree_match[fcf8]::Foo::FooOne::{constructor#0}))), name: "FooOne", discr: Relative(0), fields: [FieldDef { did: DefId(0:13 ~ thir_tree_match[fcf8]::Foo::FooOne::0), name: "0", vis: Restricted(DefId(0:0 ~ thir_tree_match[fcf8])), value: None }], tainted: None, flags: }, VariantDef { def_id: DefId(0:14 ~ thir_tree_match[fcf8]::Foo::FooTwo), ctor: Some((Const, DefId(0:15 ~ thir_tree_match[fcf8]::Foo::FooTwo::{constructor#0}))), name: "FooTwo", discr: Relative(1), fields: [], tainted: None, flags: }] flags: IS_ENUM repr: ReprOptions { int: None, align: None, pack: None, flags: , field_shuffle_seed: 3477539199540094892 } args: []