Skip to content

Commit 2e25c23

Browse files
Note pattern mismatch coming from for-loop desugaring
1 parent 9d1aeae commit 2e25c23

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,14 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
609609
if !matches!(ty.kind(), ty::Infer(ty::InferTy::TyVar(_) | ty::InferTy::FreshTy(_)))
610610
{
611611
// don't show type `_`
612-
err.span_label(span, format!("this expression has type `{}`", ty));
612+
if span.desugaring_kind() == Some(DesugaringKind::ForLoop)
613+
&& let ty::Adt(def, substs) = ty.kind()
614+
&& Some(def.did()) == self.tcx.get_diagnostic_item(sym::Option)
615+
{
616+
err.span_label(span, format!("this is an iterator with items of type `{}`", substs.type_at(0)));
617+
} else {
618+
err.span_label(span, format!("this expression has type `{}`", ty));
619+
}
613620
}
614621
if let Some(ty::error::ExpectedFound { found, .. }) = exp_found
615622
&& ty.is_box() && ty.boxed_ty() == found
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
struct Qux(i32);
2+
3+
fn bad() {
4+
let mut map = std::collections::HashMap::new();
5+
map.insert(('a', 'b'), ('c', 'd'));
6+
7+
for ((_, _), (&mut c, _)) in &mut map {
8+
//~^ ERROR mismatched types
9+
if c == 'e' {}
10+
}
11+
}
12+
13+
fn bad2() {
14+
for Some(Qux(_)) | None in [Some(""), None] {
15+
//~^ ERROR mismatched types
16+
todo!();
17+
}
18+
}
19+
20+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/for-loop-bad-item.rs:7:19
3+
|
4+
LL | for ((_, _), (&mut c, _)) in &mut map {
5+
| ^^^^^^ -------- this is an iterator with items of type `(&(char, char), &mut (char, char))`
6+
| |
7+
| expected `char`, found `&mut _`
8+
| help: you can probably remove the explicit borrow: `c`
9+
|
10+
= note: expected type `char`
11+
found mutable reference `&mut _`
12+
13+
error[E0308]: mismatched types
14+
--> $DIR/for-loop-bad-item.rs:14:14
15+
|
16+
LL | for Some(Qux(_)) | None in [Some(""), None] {
17+
| ^^^^^^ ---------------- this is an iterator with items of type `Option<&str>`
18+
| |
19+
| expected `str`, found struct `Qux`
20+
21+
error: aborting due to 2 previous errors
22+
23+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)