Skip to content

Commit

Permalink
Use span_label as it looks better when we show pattern missing bind…
Browse files Browse the repository at this point in the history
…ing in order
  • Loading branch information
estebank committed Dec 15, 2024
1 parent 8c8e8d3 commit 733fd03
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 39 deletions.
27 changes: 5 additions & 22 deletions compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -1141,38 +1141,21 @@ 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 \
available in `{ty}`",
),
);
}
err.span_note(
multispan,
format!(
"`{ty}` has a field `{field}` which could have been included \
in this pattern, but it wasn't",
),
);
break 'outer;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
struct Website {
url: String,
title: Option<String> ,//~ NOTE defined here
title: Option<String>,
}

fn main() {
Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 733fd03

Please sign in to comment.