Skip to content

Commit 733fd03

Browse files
committed
Use span_label as it looks better when we show pattern missing binding in order
1 parent 8c8e8d3 commit 733fd03

File tree

3 files changed

+10
-39
lines changed

3 files changed

+10
-39
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

+5-22
Original file line numberDiff line numberDiff line change
@@ -1129,7 +1129,7 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
11291129
) {
11301130
let [segment] = path else { return };
11311131
let None = following_seg else { return };
1132-
'outer: for rib in self.ribs[ValueNS].iter().rev() {
1132+
for rib in self.ribs[ValueNS].iter().rev() {
11331133
for (def_id, spans) in &rib.patterns_with_skipped_bindings {
11341134
if let Some(fields) = self.r.field_idents(*def_id) {
11351135
for field in fields {
@@ -1141,38 +1141,21 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
11411141
spans.iter().map(|(s, _)| *s).collect::<Vec<_>>().into();
11421142
err.span_note(
11431143
multispan,
1144-
"this pattern had a recovered parse error which likely \
1145-
lost the expected fields",
1144+
"this pattern had a recovered parse error which likely lost \
1145+
the expected fields",
11461146
);
11471147
err.downgrade_to_delayed_bug();
11481148
}
1149-
let mut multispan: MultiSpan = spans
1150-
.iter()
1151-
.filter(|(_, had_error)| had_error.is_ok())
1152-
.map(|(sp, _)| *sp)
1153-
.collect::<Vec<_>>()
1154-
.into();
1155-
let def_span = self.r.def_span(*def_id);
11561149
let ty = self.r.tcx.item_name(*def_id);
1157-
multispan.push_span_label(def_span, String::new());
1158-
multispan.push_span_label(field.span, "defined here".to_string());
1159-
for (span, _) in spans.iter().filter(|(_, had_err)| had_err.is_ok()) {
1160-
multispan.push_span_label(
1150+
for (span, _) in spans {
1151+
err.span_label(
11611152
*span,
11621153
format!(
11631154
"this pattern doesn't include `{field}`, which is \
11641155
available in `{ty}`",
11651156
),
11661157
);
11671158
}
1168-
err.span_note(
1169-
multispan,
1170-
format!(
1171-
"`{ty}` has a field `{field}` which could have been included \
1172-
in this pattern, but it wasn't",
1173-
),
1174-
);
1175-
break 'outer;
11761159
}
11771160
}
11781161
}

tests/ui/pattern/struct-pattern-with-missing-fields-resolve-error.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
struct Website {
22
url: String,
3-
title: Option<String> ,//~ NOTE defined here
3+
title: Option<String>,
44
}
55

66
fn main() {
@@ -14,8 +14,7 @@ fn main() {
1414
println!("[{}]({})", title, url); // we hide the errors for `title` and `url`
1515
}
1616

17-
if let Website { url, .. } = website { //~ NOTE `Website` has a field `title`
18-
//~^ NOTE this pattern
17+
if let Website { url, .. } = website { //~ NOTE this pattern
1918
println!("[{}]({})", title, url); //~ ERROR cannot find value `title` in this scope
2019
//~^ NOTE not found in this scope
2120
}

tests/ui/pattern/struct-pattern-with-missing-fields-resolve-error.stderr

+3-14
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,12 @@ LL | if let Website { url, Some(title) } = website {
77
| while parsing the fields for this pattern
88

99
error[E0425]: cannot find value `title` in this scope
10-
--> $DIR/struct-pattern-with-missing-fields-resolve-error.rs:19:30
10+
--> $DIR/struct-pattern-with-missing-fields-resolve-error.rs:18:30
1111
|
12+
LL | if let Website { url, .. } = website {
13+
| ------------------- this pattern doesn't include `title`, which is available in `Website`
1214
LL | println!("[{}]({})", title, url);
1315
| ^^^^^ not found in this scope
14-
|
15-
note: `Website` has a field `title` which could have been included in this pattern, but it wasn't
16-
--> $DIR/struct-pattern-with-missing-fields-resolve-error.rs:17:12
17-
|
18-
LL | / struct Website {
19-
LL | | url: String,
20-
LL | | title: Option<String> ,
21-
| | ----- defined here
22-
LL | | }
23-
| |_-
24-
...
25-
LL | if let Website { url, .. } = website {
26-
| ^^^^^^^^^^^^^^^^^^^ this pattern doesn't include `title`, which is available in `Website`
2716

2817
error: aborting due to 2 previous errors
2918

0 commit comments

Comments
 (0)