Use span_label as it looks better when we show pattern missing binding in order

This commit is contained in:
Esteban Küber 2024-12-15 22:58:16 +00:00
parent 8c8e8d35bc
commit 733fd03f0f
3 changed files with 10 additions and 39 deletions

View file

@ -1129,7 +1129,7 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
) { ) {
let [segment] = path else { return }; let [segment] = path else { return };
let None = following_seg 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 { for (def_id, spans) in &rib.patterns_with_skipped_bindings {
if let Some(fields) = self.r.field_idents(*def_id) { if let Some(fields) = self.r.field_idents(*def_id) {
for field in fields { 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(); spans.iter().map(|(s, _)| *s).collect::<Vec<_>>().into();
err.span_note( err.span_note(
multispan, multispan,
"this pattern had a recovered parse error which likely \ "this pattern had a recovered parse error which likely lost \
lost the expected fields", the expected fields",
); );
err.downgrade_to_delayed_bug(); 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); let ty = self.r.tcx.item_name(*def_id);
multispan.push_span_label(def_span, String::new()); for (span, _) in spans {
multispan.push_span_label(field.span, "defined here".to_string()); err.span_label(
for (span, _) in spans.iter().filter(|(_, had_err)| had_err.is_ok()) {
multispan.push_span_label(
*span, *span,
format!( format!(
"this pattern doesn't include `{field}`, which is \ "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;
} }
} }
} }

View file

@ -1,6 +1,6 @@
struct Website { struct Website {
url: String, url: String,
title: Option<String> ,//~ NOTE defined here title: Option<String>,
} }
fn main() { fn main() {
@ -14,8 +14,7 @@ fn main() {
println!("[{}]({})", title, url); // we hide the errors for `title` and `url` println!("[{}]({})", title, url); // we hide the errors for `title` and `url`
} }
if let Website { url, .. } = website { //~ NOTE `Website` has a field `title` if let Website { url, .. } = website { //~ NOTE this pattern
//~^ NOTE this pattern
println!("[{}]({})", title, url); //~ ERROR cannot find value `title` in this scope println!("[{}]({})", title, url); //~ ERROR cannot find value `title` in this scope
//~^ NOTE not found in this scope //~^ NOTE not found in this scope
} }

View file

@ -7,23 +7,12 @@ LL | if let Website { url, Some(title) } = website {
| while parsing the fields for this pattern | while parsing the fields for this pattern
error[E0425]: cannot find value `title` in this scope 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); LL | println!("[{}]({})", title, url);
| ^^^^^ not found in this scope | ^^^^^ 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 error: aborting due to 2 previous errors