Skip to content

Commit 69bf257

Browse files
committed
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 */ }; | ++++++++++ ```
1 parent e08ac11 commit 69bf257

File tree

3 files changed

+60
-12
lines changed

3 files changed

+60
-12
lines changed

compiler/rustc_hir_typeck/src/errors.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,15 @@ use crate::fluent_generated as fluent;
1919
pub(crate) struct BaseExpressionDoubleDot {
2020
#[primary_span]
2121
pub span: Span,
22+
#[suggestion(
23+
hir_typeck_base_expression_double_dot_enable_default_field_values,
24+
code = "#![feature(default_field_values)]\n",
25+
applicability = "machine-applicable",
26+
style = "verbose"
27+
)]
28+
pub default_field_values_suggestion: Option<Span>,
2229
#[subdiagnostic]
23-
pub default_field_values: Option<BaseExpressionDoubleDotEnableDefaultFieldValues>,
30+
pub default_field_values_help: Option<BaseExpressionDoubleDotEnableDefaultFieldValues>,
2431
#[subdiagnostic]
2532
pub add_expr: Option<BaseExpressionDoubleDotAddExpr>,
2633
#[subdiagnostic]

compiler/rustc_hir_typeck/src/expr.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -2152,13 +2152,24 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
21522152
}
21532153
}
21542154
if !self.tcx.features().default_field_values() {
2155+
let sugg = self.tcx.crate_level_attribute_injection_span(expr.hir_id);
21552156
self.dcx().emit_err(BaseExpressionDoubleDot {
21562157
span: span.shrink_to_hi(),
21572158
// We only mention enabling the feature if this is a nightly rustc *and* the
21582159
// expression would make sense with the feature enabled.
2159-
default_field_values: if self.tcx.sess.is_nightly_build()
2160+
default_field_values_suggestion: if self.tcx.sess.is_nightly_build()
21602161
&& missing_mandatory_fields.is_empty()
21612162
&& !missing_optional_fields.is_empty()
2163+
&& sugg.is_some()
2164+
{
2165+
sugg
2166+
} else {
2167+
None
2168+
},
2169+
default_field_values_help: if self.tcx.sess.is_nightly_build()
2170+
&& missing_mandatory_fields.is_empty()
2171+
&& !missing_optional_fields.is_empty()
2172+
&& sugg.is_none()
21622173
{
21632174
Some(BaseExpressionDoubleDotEnableDefaultFieldValues)
21642175
} else {

tests/ui/feature-gates/feature-gate-default-field-values.stderr

+40-10
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,10 @@ error[E0797]: base expression required after `..`
130130
LL | let x = Foo { .. };
131131
| ^
132132
|
133-
= help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields
133+
help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields
134+
|
135+
LL + #![feature(default_field_values)]
136+
|
134137
help: add a base expression here
135138
|
136139
LL | let x = Foo { ../* expr */ };
@@ -142,7 +145,10 @@ error[E0797]: base expression required after `..`
142145
LL | let z = Foo { baz: 1, .. };
143146
| ^
144147
|
145-
= help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields
148+
help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields
149+
|
150+
LL + #![feature(default_field_values)]
151+
|
146152
help: add a base expression here
147153
|
148154
LL | let z = Foo { baz: 1, ../* expr */ };
@@ -154,7 +160,10 @@ error[E0797]: base expression required after `..`
154160
LL | let x = Bar::Foo { .. };
155161
| ^
156162
|
157-
= help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields
163+
help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields
164+
|
165+
LL + #![feature(default_field_values)]
166+
|
158167
help: add a base expression here
159168
|
160169
LL | let x = Bar::Foo { ../* expr */ };
@@ -166,7 +175,10 @@ error[E0797]: base expression required after `..`
166175
LL | let z = Bar::Foo { baz: 1, .. };
167176
| ^
168177
|
169-
= help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields
178+
help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields
179+
|
180+
LL + #![feature(default_field_values)]
181+
|
170182
help: add a base expression here
171183
|
172184
LL | let z = Bar::Foo { baz: 1, ../* expr */ };
@@ -178,7 +190,10 @@ error[E0797]: base expression required after `..`
178190
LL | let x = Qux::<i32, 4> { .. };
179191
| ^
180192
|
181-
= help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields
193+
help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields
194+
|
195+
LL + #![feature(default_field_values)]
196+
|
182197
help: add a base expression here
183198
|
184199
LL | let x = Qux::<i32, 4> { ../* expr */ };
@@ -190,7 +205,10 @@ error[E0797]: base expression required after `..`
190205
LL | assert!(matches!(Qux::<i32, 4> { bar: S, baz: 42, bat: 2, bay: 4, .. }, x));
191206
| ^
192207
|
193-
= help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields
208+
help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields
209+
|
210+
LL + #![feature(default_field_values)]
211+
|
194212
help: add a base expression here
195213
|
196214
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 `..`
202220
LL | let y = Opt { mandatory: None, .. };
203221
| ^
204222
|
205-
= help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields
223+
help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields
224+
|
225+
LL + #![feature(default_field_values)]
226+
|
206227
help: add a base expression here
207228
|
208229
LL | let y = Opt { mandatory: None, ../* expr */ };
@@ -214,7 +235,10 @@ error[E0797]: base expression required after `..`
214235
LL | assert!(matches!(Opt { mandatory: None, .. }, z));
215236
| ^
216237
|
217-
= help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields
238+
help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields
239+
|
240+
LL + #![feature(default_field_values)]
241+
|
218242
help: add a base expression here
219243
|
220244
LL | assert!(matches!(Opt { mandatory: None, ../* expr */ }, z));
@@ -260,7 +284,10 @@ error[E0797]: base expression required after `..`
260284
LL | let y = OptEnum::Variant { mandatory: None, .. };
261285
| ^
262286
|
263-
= help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields
287+
help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields
288+
|
289+
LL + #![feature(default_field_values)]
290+
|
264291
help: add a base expression here
265292
|
266293
LL | let y = OptEnum::Variant { mandatory: None, ../* expr */ };
@@ -272,7 +299,10 @@ error[E0797]: base expression required after `..`
272299
LL | assert!(matches!(OptEnum::Variant { mandatory: None, .. }, z));
273300
| ^
274301
|
275-
= help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields
302+
help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields
303+
|
304+
LL + #![feature(default_field_values)]
305+
|
276306
help: add a base expression here
277307
|
278308
LL | assert!(matches!(OptEnum::Variant { mandatory: None, ../* expr */ }, z));

0 commit comments

Comments
 (0)