about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEsteban Küber <esteban@kuber.com.ar>2024-12-28 18:53:22 +0000
committerEsteban Küber <esteban@kuber.com.ar>2025-01-18 21:15:42 +0000
commite68a8ce0350892d97ff24323bf98771d65d0ed52 (patch)
treee35a0da7caf44aa6b124ad3bdff5224c06b16e1b
parente7ac2eabd0b51f7beec909794e76db3e991aa08d (diff)
downloadrust-e68a8ce0350892d97ff24323bf98771d65d0ed52.tar.gz
rust-e68a8ce0350892d97ff24323bf98771d65d0ed52.zip
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 */ };
   |                     ++++++++++
```
-rw-r--r--compiler/rustc_hir_typeck/src/errors.rs9
-rw-r--r--compiler/rustc_hir_typeck/src/expr.rs13
-rw-r--r--tests/ui/feature-gates/feature-gate-default-field-values.stderr50
3 files changed, 60 insertions, 12 deletions
diff --git a/compiler/rustc_hir_typeck/src/errors.rs b/compiler/rustc_hir_typeck/src/errors.rs
index 4eed2bc1238..052adaa69b2 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<Span>,
     #[subdiagnostic]
-    pub default_field_values: Option<BaseExpressionDoubleDotEnableDefaultFieldValues>,
+    pub default_field_values_help: Option<BaseExpressionDoubleDotEnableDefaultFieldValues>,
     #[subdiagnostic]
     pub add_expr: Option<BaseExpressionDoubleDotAddExpr>,
     #[subdiagnostic]
diff --git a/compiler/rustc_hir_typeck/src/expr.rs b/compiler/rustc_hir_typeck/src/expr.rs
index 3bb518e7f97..329eb90a1d5 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 d882c322c8e..104d72a3986 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::<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));