Skip to content

Commit

Permalink
Provide suggestion for #![feature(default_field_values)]
Browse files Browse the repository at this point in the history
```
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 */ };
   |                     ++++++++++
```
  • Loading branch information
estebank committed Dec 31, 2024
1 parent e08ac11 commit 69bf257
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 12 deletions.
9 changes: 8 additions & 1 deletion compiler/rustc_hir_typeck/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
13 changes: 12 additions & 1 deletion compiler/rustc_hir_typeck/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2152,13 +2152,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 {
Expand Down
50 changes: 40 additions & 10 deletions tests/ui/feature-gates/feature-gate-default-field-values.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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 */ };
Expand All @@ -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 */ };
Expand All @@ -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 */ };
Expand All @@ -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 */ };
Expand All @@ -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 */ };
Expand All @@ -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));
Expand All @@ -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 */ };
Expand All @@ -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));
Expand Down Expand Up @@ -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 */ };
Expand All @@ -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));
Expand Down

0 comments on commit 69bf257

Please sign in to comment.