Skip to content

Commit ae8d3db

Browse files
committed
Be more accurate when mentioning type of found match arms
1 parent 20cdd65 commit ae8d3db

File tree

3 files changed

+51
-28
lines changed

3 files changed

+51
-28
lines changed

src/librustc/infer/error_reporting/mod.rs

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -662,19 +662,22 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
662662
}
663663
}
664664
_ => {
665+
// `last_ty` can be `!`, `expected` will have better info when present.
666+
let t = self.resolve_vars_if_possible(&match exp_found {
667+
Some(ty::error::ExpectedFound { expected, .. }) => expected,
668+
_ => last_ty,
669+
});
665670
let msg = "`match` arms have incompatible types";
666671
err.span_label(cause.span, msg);
667672
if prior_arms.len() <= 4 {
668673
for sp in prior_arms {
669-
err.span_label(*sp, format!(
670-
"this is found to be of type `{}`",
671-
self.resolve_vars_if_possible(&last_ty),
672-
));
674+
err.span_label( *sp, format!("this is found to be of type `{}`", t));
673675
}
674676
} else if let Some(sp) = prior_arms.last() {
675-
err.span_label(*sp, format!(
676-
"this and all prior arms are found to be of type `{}`", last_ty,
677-
));
677+
err.span_label(
678+
*sp,
679+
format!("this and all prior arms are found to be of type `{}`", t),
680+
);
678681
}
679682
}
680683
},
@@ -1143,27 +1146,6 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
11431146
}
11441147
(_, false, _) => {
11451148
if let Some(exp_found) = exp_found {
1146-
let (def_id, ret_ty) = match exp_found.found.sty {
1147-
ty::FnDef(def, _) => {
1148-
(Some(def), Some(self.tcx.fn_sig(def).output()))
1149-
}
1150-
_ => (None, None),
1151-
};
1152-
1153-
let exp_is_struct = match exp_found.expected.sty {
1154-
ty::Adt(def, _) => def.is_struct(),
1155-
_ => false,
1156-
};
1157-
1158-
if let (Some(def_id), Some(ret_ty)) = (def_id, ret_ty) {
1159-
if exp_is_struct && &exp_found.expected == ret_ty.skip_binder() {
1160-
let message = format!(
1161-
"did you mean `{}(/* fields */)`?",
1162-
self.tcx.def_path_str(def_id)
1163-
);
1164-
diag.span_label(span, message);
1165-
}
1166-
}
11671149
self.suggest_as_ref_where_appropriate(span, &exp_found, diag);
11681150
}
11691151

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
enum E {
2+
A,
3+
B,
4+
C,
5+
D,
6+
E,
7+
F,
8+
}
9+
10+
fn main() {
11+
match E::F {
12+
E::A => 1,
13+
E::B => 2,
14+
E::C => 3,
15+
E::D => 4,
16+
E::E => unimplemented!(""),
17+
E::F => "", //~ ERROR match arms have incompatible types
18+
};
19+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error[E0308]: match arms have incompatible types
2+
--> $DIR/match-arm-resolving-to-never.rs:17:17
3+
|
4+
LL | / match E::F {
5+
LL | | E::A => 1,
6+
LL | | E::B => 2,
7+
LL | | E::C => 3,
8+
LL | | E::D => 4,
9+
LL | | E::E => unimplemented!(""),
10+
| | ------------------ this and all prior arms are found to be of type `{integer}`
11+
LL | | E::F => "",
12+
| | ^^ expected integer, found reference
13+
LL | | };
14+
| |_____- `match` arms have incompatible types
15+
|
16+
= note: expected type `{integer}`
17+
found type `&'static str`
18+
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
19+
20+
error: aborting due to previous error
21+
22+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)