Skip to content

Commit 05e8ba1

Browse files
committed
Account for match expr in single line
When encountering `match Some(42) { Some(x) => x, None => "" };`, output ``` error[E0308]: `match` arms have incompatible types --> f53.rs:2:52 | 2 | let _ = match Some(42) { Some(x) => x, None => "" }; | -------------- - ^^ expected integer, found `&str` | | | | | this is found to be of type `{integer}` | `match` arms have incompatible types ```
1 parent 92c1937 commit 05e8ba1

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

Diff for: compiler/rustc_infer/src/infer/error_reporting/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -729,15 +729,15 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
729729
format!("this and all prior arms are found to be of type `{}`", t),
730730
);
731731
}
732-
let outer_error_span = if any_multiline_arm {
732+
let outer = if any_multiline_arm || !source_map.is_multiline(cause.span) {
733733
// Cover just `match` and the scrutinee expression, not
734734
// the entire match body, to reduce diagram noise.
735735
cause.span.shrink_to_lo().to(scrut_span)
736736
} else {
737737
cause.span
738738
};
739739
let msg = "`match` arms have incompatible types";
740-
err.span_label(outer_error_span, msg);
740+
err.span_label(outer, msg);
741741
self.suggest_remove_semi_or_return_binding(
742742
err,
743743
prior_arm_block_id,

Diff for: src/test/ui/match/single-line.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
let _ = match Some(42) { Some(x) => x, None => "" }; //~ ERROR E0308
3+
}

Diff for: src/test/ui/match/single-line.stderr

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0308]: `match` arms have incompatible types
2+
--> $DIR/single-line.rs:2:52
3+
|
4+
LL | let _ = match Some(42) { Some(x) => x, None => "" };
5+
| -------------- - ^^ expected integer, found `&str`
6+
| | |
7+
| | this is found to be of type `{integer}`
8+
| `match` arms have incompatible types
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)