Skip to content

Commit

Permalink
Rollup merge of rust-lang#69599 - Centril:typeck-tweak-wording, r=dav…
Browse files Browse the repository at this point in the history
…idtwco

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
  • Loading branch information
Centril authored Mar 8, 2020
2 parents 12920f4 + 2746e12 commit 7920d7b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
12 changes: 10 additions & 2 deletions src/librustc_typeck/check/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,8 +577,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let var_ty = self.resolve_vars_with_obligations(var_ty);
let msg = format!("first introduced with type `{}` here", var_ty);
err.span_label(hir.span(var_id), msg);
let in_arm = hir.parent_iter(var_id).any(|(_, n)| matches!(n, hir::Node::Arm(..)));
let pre = if in_arm { "in the same arm, " } else { "" };
let in_match = hir.parent_iter(var_id).any(|(_, n)| {
matches!(
n,
hir::Node::Expr(hir::Expr {
kind: hir::ExprKind::Match(.., hir::MatchSource::Normal),
..
})
)
});
let pre = if in_match { "in the same arm, " } else { "" };
err.note(&format!("{}a binding must have the same type in all alternatives", pre));
err.emit();
}
Expand Down
16 changes: 8 additions & 8 deletions src/test/ui/or-patterns/or-patterns-binding-type-mismatch.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ LL | if let Blah::A(_, x, y) | Blah::B(x, y) = Blah::A(1, 1, 2) {
| | expected `usize`, found `isize`
| first introduced with type `usize` here
|
= note: in the same arm, a binding must have the same type in all alternatives
= note: a binding must have the same type in all alternatives

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

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

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

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

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

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

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

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

0 comments on commit 7920d7b

Please sign in to comment.