Skip to content

Commit 2746e12

Browse files
committed
check_binding_alt_eq_ty: improve precision wrt. if let.
1 parent d3c7934 commit 2746e12

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

src/librustc_typeck/check/pat.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -559,8 +559,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
559559
let var_ty = self.resolve_vars_with_obligations(var_ty);
560560
let msg = format!("first introduced with type `{}` here", var_ty);
561561
err.span_label(hir.span(var_id), msg);
562-
let in_arm = hir.parent_iter(var_id).any(|(_, n)| matches!(n, hir::Node::Arm(..)));
563-
let pre = if in_arm { "in the same arm, " } else { "" };
562+
let in_match = hir.parent_iter(var_id).any(|(_, n)| {
563+
matches!(
564+
n,
565+
hir::Node::Expr(hir::Expr {
566+
kind: hir::ExprKind::Match(.., hir::MatchSource::Normal),
567+
..
568+
})
569+
)
570+
});
571+
let pre = if in_match { "in the same arm, " } else { "" };
564572
err.note(&format!("{}a binding must have the same type in all alternatives", pre));
565573
err.emit();
566574
}

src/test/ui/or-patterns/or-patterns-binding-type-mismatch.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ LL | if let Blah::A(_, x, y) | Blah::B(x, y) = Blah::A(1, 1, 2) {
101101
| | expected `usize`, found `isize`
102102
| first introduced with type `usize` here
103103
|
104-
= note: in the same arm, a binding must have the same type in all alternatives
104+
= note: a binding must have the same type in all alternatives
105105

106106
error[E0308]: mismatched types
107107
--> $DIR/or-patterns-binding-type-mismatch.rs:38:47
@@ -112,7 +112,7 @@ LL | if let Some(Blah::A(_, x, y) | Blah::B(x, y)) = Some(Blah::A(1, 1, 2))
112112
| | expected `usize`, found `isize`
113113
| first introduced with type `usize` here
114114
|
115-
= note: in the same arm, a binding must have the same type in all alternatives
115+
= note: a binding must have the same type in all alternatives
116116

117117
error[E0308]: mismatched types
118118
--> $DIR/or-patterns-binding-type-mismatch.rs:42:22
@@ -123,7 +123,7 @@ LL | if let (x, y) | (y, x) = (0u8, 1u16) {
123123
| | expected `u16`, found `u8`
124124
| first introduced with type `u16` here
125125
|
126-
= note: in the same arm, a binding must have the same type in all alternatives
126+
= note: a binding must have the same type in all alternatives
127127

128128
error[E0308]: mismatched types
129129
--> $DIR/or-patterns-binding-type-mismatch.rs:42:25
@@ -134,7 +134,7 @@ LL | if let (x, y) | (y, x) = (0u8, 1u16) {
134134
| | expected `u8`, found `u16`
135135
| first introduced with type `u8` here
136136
|
137-
= note: in the same arm, a binding must have the same type in all alternatives
137+
= note: a binding must have the same type in all alternatives
138138

139139
error[E0308]: mismatched types
140140
--> $DIR/or-patterns-binding-type-mismatch.rs:47:44
@@ -147,7 +147,7 @@ LL | if let Some((x, Some((y, z)))) | Some((y, Some((x, z) | (z, x))))
147147
LL | = Some((0u8, Some((1u16, 2u32))))
148148
| ------------------------------- this expression has type `std::option::Option<(u8, std::option::Option<(u16, u32)>)>`
149149
|
150-
= note: in the same arm, a binding must have the same type in all alternatives
150+
= note: a binding must have the same type in all alternatives
151151

152152
error[E0308]: mismatched types
153153
--> $DIR/or-patterns-binding-type-mismatch.rs:47:53
@@ -160,7 +160,7 @@ LL | if let Some((x, Some((y, z)))) | Some((y, Some((x, z) | (z, x))))
160160
LL | = Some((0u8, Some((1u16, 2u32))))
161161
| ------------------------------- this expression has type `std::option::Option<(u8, std::option::Option<(u16, u32)>)>`
162162
|
163-
= note: in the same arm, a binding must have the same type in all alternatives
163+
= note: a binding must have the same type in all alternatives
164164

165165
error[E0308]: mismatched types
166166
--> $DIR/or-patterns-binding-type-mismatch.rs:47:62
@@ -173,7 +173,7 @@ LL | if let Some((x, Some((y, z)))) | Some((y, Some((x, z) | (z, x))))
173173
LL | = Some((0u8, Some((1u16, 2u32))))
174174
| ------------------------------- this expression has type `std::option::Option<(u8, std::option::Option<(u16, u32)>)>`
175175
|
176-
= note: in the same arm, a binding must have the same type in all alternatives
176+
= note: a binding must have the same type in all alternatives
177177

178178
error[E0308]: mismatched types
179179
--> $DIR/or-patterns-binding-type-mismatch.rs:47:65
@@ -184,7 +184,7 @@ LL | if let Some((x, Some((y, z)))) | Some((y, Some((x, z) | (z, x))))
184184
LL | = Some((0u8, Some((1u16, 2u32))))
185185
| ------------------------------- this expression has type `std::option::Option<(u8, std::option::Option<(u16, u32)>)>`
186186
|
187-
= note: in the same arm, a binding must have the same type in all alternatives
187+
= note: a binding must have the same type in all alternatives
188188

189189
error[E0308]: mismatched types
190190
--> $DIR/or-patterns-binding-type-mismatch.rs:55:39

0 commit comments

Comments
 (0)