Provide suggestion for #![feature(default_field_values)]

```
error[E0797]: base expression required after `..`
  --> $DIR/feature-gate-default-field-values.rs:62:21
   |
LL |     let x = Foo { .. };
   |                     ^
   |
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 */ };
   |                     ++++++++++
```
This commit is contained in:
Esteban Küber 2024-12-28 18:53:22 +00:00
parent e7ac2eabd0
commit e68a8ce035
3 changed files with 60 additions and 12 deletions

View File

@ -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<Span>,
#[subdiagnostic]
pub default_field_values: Option<BaseExpressionDoubleDotEnableDefaultFieldValues>,
pub default_field_values_help: Option<BaseExpressionDoubleDotEnableDefaultFieldValues>,
#[subdiagnostic]
pub add_expr: Option<BaseExpressionDoubleDotAddExpr>,
#[subdiagnostic]

View File

@ -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 {

View File

@ -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::<i32, 4> { .. };
| ^
|
= 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::<i32, 4> { ../* expr */ };
@ -190,7 +205,10 @@ error[E0797]: base expression required after `..`
LL | assert!(matches!(Qux::<i32, 4> { 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::<i32, 4> { 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));