Skip to content

Commit c717721

Browse files
authored
Rollup merge of rust-lang#69599 - Centril:typeck-tweak-wording, r=davidtwco
check_binding_alt_eq_ty: improve precision wrt. `if let` Follow up to rust-lang#69452 -- this tweaks the `check_binding_alt_eq_ty` logic wrt. wording so that `if let` doesn't include "in this arm" (because there can only ever be one arm). r? @estebank
2 parents d4860fc + 2746e12 commit c717721

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

src/librustc_typeck/check/pat.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -577,8 +577,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
577577
let var_ty = self.resolve_vars_with_obligations(var_ty);
578578
let msg = format!("first introduced with type `{}` here", var_ty);
579579
err.span_label(hir.span(var_id), msg);
580-
let in_arm = hir.parent_iter(var_id).any(|(_, n)| matches!(n, hir::Node::Arm(..)));
581-
let pre = if in_arm { "in the same arm, " } else { "" };
580+
let in_match = hir.parent_iter(var_id).any(|(_, n)| {
581+
matches!(
582+
n,
583+
hir::Node::Expr(hir::Expr {
584+
kind: hir::ExprKind::Match(.., hir::MatchSource::Normal),
585+
..
586+
})
587+
)
588+
});
589+
let pre = if in_match { "in the same arm, " } else { "" };
582590
err.note(&format!("{}a binding must have the same type in all alternatives", pre));
583591
err.emit();
584592
}

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

+8-8
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)