about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEsteban Küber <esteban@kuber.com.ar>2024-12-15 22:58:16 +0000
committerEsteban Küber <esteban@kuber.com.ar>2024-12-15 22:58:16 +0000
commit733fd03f0f9d5c8ad595f7b7cde17d3c1f33a19e (patch)
tree76019f347ad1f93c992f49328d0d1ac2aaa5ef1b
parent8c8e8d35bc207353977d44344506eda40bf502f5 (diff)
downloadrust-733fd03f0f9d5c8ad595f7b7cde17d3c1f33a19e.tar.gz
rust-733fd03f0f9d5c8ad595f7b7cde17d3c1f33a19e.zip
Use `span_label` as it looks better when we show pattern missing binding in order
-rw-r--r--compiler/rustc_resolve/src/late/diagnostics.rs27
-rw-r--r--tests/ui/pattern/struct-pattern-with-missing-fields-resolve-error.rs5
-rw-r--r--tests/ui/pattern/struct-pattern-with-missing-fields-resolve-error.stderr17
3 files changed, 10 insertions, 39 deletions
diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs
index bd4d166be0d..85ea6a74d3c 100644
--- a/compiler/rustc_resolve/src/late/diagnostics.rs
+++ b/compiler/rustc_resolve/src/late/diagnostics.rs
@@ -1129,7 +1129,7 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
     ) {
         let [segment] = path else { return };
         let None = following_seg else { return };
-        'outer: for rib in self.ribs[ValueNS].iter().rev() {
+        for rib in self.ribs[ValueNS].iter().rev() {
             for (def_id, spans) in &rib.patterns_with_skipped_bindings {
                 if let Some(fields) = self.r.field_idents(*def_id) {
                     for field in fields {
@@ -1141,23 +1141,14 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
                                     spans.iter().map(|(s, _)| *s).collect::<Vec<_>>().into();
                                 err.span_note(
                                     multispan,
-                                    "this pattern had a recovered parse error which likely \
-                                        lost the expected fields",
+                                    "this pattern had a recovered parse error which likely lost \
+                                     the expected fields",
                                 );
                                 err.downgrade_to_delayed_bug();
                             }
-                            let mut multispan: MultiSpan = spans
-                                .iter()
-                                .filter(|(_, had_error)| had_error.is_ok())
-                                .map(|(sp, _)| *sp)
-                                .collect::<Vec<_>>()
-                                .into();
-                            let def_span = self.r.def_span(*def_id);
                             let ty = self.r.tcx.item_name(*def_id);
-                            multispan.push_span_label(def_span, String::new());
-                            multispan.push_span_label(field.span, "defined here".to_string());
-                            for (span, _) in spans.iter().filter(|(_, had_err)| had_err.is_ok()) {
-                                multispan.push_span_label(
+                            for (span, _) in spans {
+                                err.span_label(
                                     *span,
                                     format!(
                                         "this pattern doesn't include `{field}`, which is \
@@ -1165,14 +1156,6 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
                                     ),
                                 );
                             }
-                            err.span_note(
-                                multispan,
-                                format!(
-                                    "`{ty}` has a field `{field}` which could have been included \
-                                     in this pattern, but it wasn't",
-                                ),
-                            );
-                            break 'outer;
                         }
                     }
                 }
diff --git a/tests/ui/pattern/struct-pattern-with-missing-fields-resolve-error.rs b/tests/ui/pattern/struct-pattern-with-missing-fields-resolve-error.rs
index 7238a0a7bb7..225891e390f 100644
--- a/tests/ui/pattern/struct-pattern-with-missing-fields-resolve-error.rs
+++ b/tests/ui/pattern/struct-pattern-with-missing-fields-resolve-error.rs
@@ -1,6 +1,6 @@
 struct Website {
     url: String,
-    title: Option<String> ,//~ NOTE defined here
+    title: Option<String>,
 }
 
 fn main() {
@@ -14,8 +14,7 @@ fn main() {
         println!("[{}]({})", title, url); // we hide the errors for `title` and `url`
     }
 
-    if let Website { url, .. } = website { //~ NOTE `Website` has a field `title`
-        //~^ NOTE this pattern
+    if let Website { url, .. } = website { //~ NOTE this pattern
         println!("[{}]({})", title, url); //~ ERROR cannot find value `title` in this scope
         //~^ NOTE not found in this scope
     }
diff --git a/tests/ui/pattern/struct-pattern-with-missing-fields-resolve-error.stderr b/tests/ui/pattern/struct-pattern-with-missing-fields-resolve-error.stderr
index 9d2fca8eab9..80fcd714400 100644
--- a/tests/ui/pattern/struct-pattern-with-missing-fields-resolve-error.stderr
+++ b/tests/ui/pattern/struct-pattern-with-missing-fields-resolve-error.stderr
@@ -7,23 +7,12 @@ LL |     if let Website { url, Some(title) } = website {
    |            while parsing the fields for this pattern
 
 error[E0425]: cannot find value `title` in this scope
-  --> $DIR/struct-pattern-with-missing-fields-resolve-error.rs:19:30
+  --> $DIR/struct-pattern-with-missing-fields-resolve-error.rs:18:30
    |
+LL |     if let Website { url, .. } = website {
+   |            ------------------- this pattern doesn't include `title`, which is available in `Website`
 LL |         println!("[{}]({})", title, url);
    |                              ^^^^^ not found in this scope
-   |
-note: `Website` has a field `title` which could have been included in this pattern, but it wasn't
-  --> $DIR/struct-pattern-with-missing-fields-resolve-error.rs:17:12
-   |
-LL | / struct Website {
-LL | |     url: String,
-LL | |     title: Option<String> ,
-   | |     ----- defined here
-LL | | }
-   | |_-
-...
-LL |       if let Website { url, .. } = website {
-   |              ^^^^^^^^^^^^^^^^^^^ this pattern doesn't include `title`, which is available in `Website`
 
 error: aborting due to 2 previous errors