]> git.lizzy.rs Git - rust.git/commitdiff
Account for `match` expr in single line
authorEsteban Küber <esteban@kuber.com.ar>
Tue, 6 Dec 2022 03:42:21 +0000 (19:42 -0800)
committerEsteban Küber <esteban@kuber.com.ar>
Wed, 28 Dec 2022 00:45:55 +0000 (16:45 -0800)
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
  ```

compiler/rustc_infer/src/infer/error_reporting/mod.rs
src/test/ui/match/single-line.rs [new file with mode: 0644]
src/test/ui/match/single-line.stderr [new file with mode: 0644]

index 96a976fb89e0fe8f15fdf680787893d676315771..e5823660e3fbd27f3cc769567dcf0822f3fa7ca8 100644 (file)
@@ -729,7 +729,7 @@ fn note_error_origin(
                             format!("this and all prior arms are found to be of type `{}`", t),
                         );
                     }
-                    let outer_error_span = if any_multiline_arm {
+                    let outer = if any_multiline_arm || !source_map.is_multiline(cause.span) {
                         // Cover just `match` and the scrutinee expression, not
                         // the entire match body, to reduce diagram noise.
                         cause.span.shrink_to_lo().to(scrut_span)
@@ -737,7 +737,7 @@ fn note_error_origin(
                         cause.span
                     };
                     let msg = "`match` arms have incompatible types";
-                    err.span_label(outer_error_span, msg);
+                    err.span_label(outer, msg);
                     self.suggest_remove_semi_or_return_binding(
                         err,
                         prior_arm_block_id,
diff --git a/src/test/ui/match/single-line.rs b/src/test/ui/match/single-line.rs
new file mode 100644 (file)
index 0000000..0f69d08
--- /dev/null
@@ -0,0 +1,3 @@
+fn main() {
+    let _ = match Some(42) { Some(x) => x, None => "" }; //~ ERROR E0308
+}
diff --git a/src/test/ui/match/single-line.stderr b/src/test/ui/match/single-line.stderr
new file mode 100644 (file)
index 0000000..ec3b76e
--- /dev/null
@@ -0,0 +1,12 @@
+error[E0308]: `match` arms have incompatible types
+  --> $DIR/single-line.rs:2:52
+   |
+LL |     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
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0308`.