Skip to content

Commit 269827c

Browse files
committed
Auto merge of #46956 - estebank:incompatible-arm-span-label, r=Zoxc
"incompatible arm" diagnostic span tweak Use span label instead of span note for single line spans in "incompatible arm" diagnostic.
2 parents 0efdfa1 + fed4fcb commit 269827c

File tree

6 files changed

+23
-22
lines changed

6 files changed

+23
-22
lines changed

src/librustc/infer/error_reporting/mod.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -437,10 +437,20 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
437437
match cause.code {
438438
ObligationCauseCode::MatchExpressionArm { arm_span, source } => match source {
439439
hir::MatchSource::IfLetDesugar {..} => {
440-
err.span_note(arm_span, "`if let` arm with an incompatible type");
440+
let msg = "`if let` arm with an incompatible type";
441+
if self.tcx.sess.codemap().is_multiline(arm_span) {
442+
err.span_note(arm_span, msg);
443+
} else {
444+
err.span_label(arm_span, msg);
445+
}
441446
}
442447
_ => {
443-
err.span_note(arm_span, "match arm with an incompatible type");
448+
let msg = "match arm with an incompatible type";
449+
if self.tcx.sess.codemap().is_multiline(arm_span) {
450+
err.span_note(arm_span, msg);
451+
} else {
452+
err.span_label(arm_span, msg);
453+
}
444454
}
445455
},
446456
_ => ()

src/libsyntax/codemap.rs

+6
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,12 @@ impl CodeMap {
446446
.expect("CodeMap::span_to_unmapped_path called for imported FileMap?")
447447
}
448448

449+
pub fn is_multiline(&self, sp: Span) -> bool {
450+
let lo = self.lookup_char_pos(sp.lo());
451+
let hi = self.lookup_char_pos(sp.hi());
452+
lo.line != hi.line
453+
}
454+
449455
pub fn span_to_lines(&self, sp: Span) -> FileLinesResult {
450456
debug!("span_to_lines(sp={:?})", sp);
451457

src/test/ui/issue-11319.stderr

+2-5
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,14 @@ error[E0308]: match arms have incompatible types
66
14 | | //~| expected type `bool`
77
15 | | //~| found type `()`
88
... |
9+
19 | | None => (),
10+
| | -- match arm with an incompatible type
911
20 | | _ => true
1012
21 | | }
1113
| |_____^ expected bool, found ()
1214
|
1315
= note: expected type `bool`
1416
found type `()`
15-
note: match arm with an incompatible type
16-
--> $DIR/issue-11319.rs:19:20
17-
|
18-
19 | None => (),
19-
| ^^
2017

2118
error: aborting due to previous error
2219

src/test/ui/issue-24036.stderr

+1-5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ error[E0308]: match arms have incompatible types
1717
19 | | //~^ ERROR match arms have incompatible types
1818
20 | | 1 => |c| c + 1,
1919
21 | | 2 => |c| c - 1,
20+
| | --------- match arm with an incompatible type
2021
22 | | _ => |c| c - 1
2122
23 | | };
2223
| |_____^ expected closure, found a different closure
@@ -25,11 +26,6 @@ error[E0308]: match arms have incompatible types
2526
found type `[closure@$DIR/issue-24036.rs:21:14: 21:23]`
2627
= note: no two closures, even if identical, have the same type
2728
= help: consider boxing your closure and/or using it as a trait object
28-
note: match arm with an incompatible type
29-
--> $DIR/issue-24036.rs:21:14
30-
|
31-
21 | 2 => |c| c - 1,
32-
| ^^^^^^^^^
3329

3430
error: aborting due to 2 previous errors
3531

src/test/ui/lub-glb/old-lub-glb-hr.stderr

+1-5
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,14 @@ error[E0308]: match arms have incompatible types
55
| _____________^
66
19 | | 0 => x,
77
20 | | _ => y,
8+
| | - match arm with an incompatible type
89
21 | | };
910
| |_____^ expected bound lifetime parameter, found concrete lifetime
1011
|
1112
= note: expected type `for<'r, 's> fn(&'r u8, &'s u8)`
1213
found type `for<'a> fn(&'a u8, &'a u8)`
1314
= note: this was previously accepted by the compiler but has been phased out
1415
= note: for more information, see https://github.com/rust-lang/rust/issues/45852
15-
note: match arm with an incompatible type
16-
--> $DIR/old-lub-glb-hr.rs:20:14
17-
|
18-
20 | _ => y,
19-
| ^
2016

2117
error: aborting due to previous error
2218

src/test/ui/lub-glb/old-lub-glb-object.stderr

+1-5
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,14 @@ error[E0308]: match arms have incompatible types
55
| _____________^
66
21 | | 0 => x,
77
22 | | _ => y,
8+
| | - match arm with an incompatible type
89
23 | | };
910
| |_____^ expected bound lifetime parameter 'a, found concrete lifetime
1011
|
1112
= note: expected type `&for<'a, 'b> Foo<&'a u8, &'b u8>`
1213
found type `&for<'a> Foo<&'a u8, &'a u8>`
1314
= note: this was previously accepted by the compiler but has been phased out
1415
= note: for more information, see https://github.com/rust-lang/rust/issues/45852
15-
note: match arm with an incompatible type
16-
--> $DIR/old-lub-glb-object.rs:22:14
17-
|
18-
22 | _ => y,
19-
| ^
2016

2117
error: aborting due to previous error
2218

0 commit comments

Comments
 (0)