diff --git a/compiler/rustc_hir_typeck/src/errors.rs b/compiler/rustc_hir_typeck/src/errors.rs index 4eed2bc12388d..052adaa69b2b9 100644 --- a/compiler/rustc_hir_typeck/src/errors.rs +++ b/compiler/rustc_hir_typeck/src/errors.rs @@ -19,8 +19,15 @@ use crate::fluent_generated as fluent; pub(crate) struct BaseExpressionDoubleDot { #[primary_span] pub span: Span, + #[suggestion( + hir_typeck_base_expression_double_dot_enable_default_field_values, + code = "#![feature(default_field_values)]\n", + applicability = "machine-applicable", + style = "verbose" + )] + pub default_field_values_suggestion: Option, #[subdiagnostic] - pub default_field_values: Option, + pub default_field_values_help: Option, #[subdiagnostic] pub add_expr: Option, #[subdiagnostic] diff --git a/compiler/rustc_hir_typeck/src/expr.rs b/compiler/rustc_hir_typeck/src/expr.rs index 3bb518e7f9711..329eb90a1d59a 100644 --- a/compiler/rustc_hir_typeck/src/expr.rs +++ b/compiler/rustc_hir_typeck/src/expr.rs @@ -2138,13 +2138,24 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } } if !self.tcx.features().default_field_values() { + let sugg = self.tcx.crate_level_attribute_injection_span(expr.hir_id); self.dcx().emit_err(BaseExpressionDoubleDot { span: span.shrink_to_hi(), // We only mention enabling the feature if this is a nightly rustc *and* the // expression would make sense with the feature enabled. - default_field_values: if self.tcx.sess.is_nightly_build() + default_field_values_suggestion: if self.tcx.sess.is_nightly_build() && missing_mandatory_fields.is_empty() && !missing_optional_fields.is_empty() + && sugg.is_some() + { + sugg + } else { + None + }, + default_field_values_help: if self.tcx.sess.is_nightly_build() + && missing_mandatory_fields.is_empty() + && !missing_optional_fields.is_empty() + && sugg.is_none() { Some(BaseExpressionDoubleDotEnableDefaultFieldValues) } else { diff --git a/tests/ui/feature-gates/feature-gate-default-field-values.stderr b/tests/ui/feature-gates/feature-gate-default-field-values.stderr index d882c322c8eda..104d72a39861d 100644 --- a/tests/ui/feature-gates/feature-gate-default-field-values.stderr +++ b/tests/ui/feature-gates/feature-gate-default-field-values.stderr @@ -130,7 +130,10 @@ error[E0797]: base expression required after `..` LL | let x = Foo { .. }; | ^ | - = help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields +help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields + | +LL + #![feature(default_field_values)] + | help: add a base expression here | LL | let x = Foo { ../* expr */ }; @@ -142,7 +145,10 @@ error[E0797]: base expression required after `..` LL | let z = Foo { baz: 1, .. }; | ^ | - = help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields +help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields + | +LL + #![feature(default_field_values)] + | help: add a base expression here | LL | let z = Foo { baz: 1, ../* expr */ }; @@ -154,7 +160,10 @@ error[E0797]: base expression required after `..` LL | let x = Bar::Foo { .. }; | ^ | - = help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields +help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields + | +LL + #![feature(default_field_values)] + | help: add a base expression here | LL | let x = Bar::Foo { ../* expr */ }; @@ -166,7 +175,10 @@ error[E0797]: base expression required after `..` LL | let z = Bar::Foo { baz: 1, .. }; | ^ | - = help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields +help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields + | +LL + #![feature(default_field_values)] + | help: add a base expression here | LL | let z = Bar::Foo { baz: 1, ../* expr */ }; @@ -178,7 +190,10 @@ error[E0797]: base expression required after `..` LL | let x = Qux:: { .. }; | ^ | - = help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields +help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields + | +LL + #![feature(default_field_values)] + | help: add a base expression here | LL | let x = Qux:: { ../* expr */ }; @@ -190,7 +205,10 @@ error[E0797]: base expression required after `..` LL | assert!(matches!(Qux:: { bar: S, baz: 42, bat: 2, bay: 4, .. }, x)); | ^ | - = help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields +help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields + | +LL + #![feature(default_field_values)] + | help: add a base expression here | LL | assert!(matches!(Qux:: { bar: S, baz: 42, bat: 2, bay: 4, ../* expr */ }, x)); @@ -202,7 +220,10 @@ error[E0797]: base expression required after `..` LL | let y = Opt { mandatory: None, .. }; | ^ | - = help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields +help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields + | +LL + #![feature(default_field_values)] + | help: add a base expression here | LL | let y = Opt { mandatory: None, ../* expr */ }; @@ -214,7 +235,10 @@ error[E0797]: base expression required after `..` LL | assert!(matches!(Opt { mandatory: None, .. }, z)); | ^ | - = help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields +help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields + | +LL + #![feature(default_field_values)] + | help: add a base expression here | LL | assert!(matches!(Opt { mandatory: None, ../* expr */ }, z)); @@ -260,7 +284,10 @@ error[E0797]: base expression required after `..` LL | let y = OptEnum::Variant { mandatory: None, .. }; | ^ | - = help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields +help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields + | +LL + #![feature(default_field_values)] + | help: add a base expression here | LL | let y = OptEnum::Variant { mandatory: None, ../* expr */ }; @@ -272,7 +299,10 @@ error[E0797]: base expression required after `..` LL | assert!(matches!(OptEnum::Variant { mandatory: None, .. }, z)); | ^ | - = help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields +help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields + | +LL + #![feature(default_field_values)] + | help: add a base expression here | LL | assert!(matches!(OptEnum::Variant { mandatory: None, ../* expr */ }, z));