Skip to content

Commit 9af125d

Browse files
authored
Rollup merge of rust-lang#53371 - estebank:tuple, r=nikomatsakis
Do not emit E0277 on incorrect tuple destructured binding Fix rust-lang#50333.
2 parents f2302da + 63cd81c commit 9af125d

File tree

3 files changed

+54
-4
lines changed

3 files changed

+54
-4
lines changed

src/librustc_typeck/check/_match.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,11 +298,21 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
298298
TypeVariableOrigin::TypeInference(pat.span)));
299299
let element_tys = tcx.mk_type_list(element_tys_iter);
300300
let pat_ty = tcx.mk_ty(ty::Tuple(element_tys));
301-
self.demand_eqtype(pat.span, expected, pat_ty);
302-
for (i, elem) in elements.iter().enumerate_and_adjust(max_len, ddpos) {
303-
self.check_pat_walk(elem, &element_tys[i], def_bm, true);
301+
if let Some(mut err) = self.demand_eqtype_diag(pat.span, expected, pat_ty) {
302+
err.emit();
303+
// Walk subpatterns with an expected type of `err` in this case to silence
304+
// further errors being emitted when using the bindings. #50333
305+
let element_tys_iter = (0..max_len).map(|_| tcx.types.err);
306+
for (_, elem) in elements.iter().enumerate_and_adjust(max_len, ddpos) {
307+
self.check_pat_walk(elem, &tcx.types.err, def_bm, true);
308+
}
309+
tcx.mk_tup(element_tys_iter)
310+
} else {
311+
for (i, elem) in elements.iter().enumerate_and_adjust(max_len, ddpos) {
312+
self.check_pat_walk(elem, &element_tys[i], def_bm, true);
313+
}
314+
pat_ty
304315
}
305-
pat_ty
306316
}
307317
PatKind::Box(ref inner) => {
308318
let inner_ty = self.next_ty_var(TypeVariableOrigin::TypeInference(inner.span));
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Hide irrelevant E0277 errors (#50333)
12+
13+
trait T {}
14+
15+
struct A;
16+
impl T for A {}
17+
impl A {
18+
fn new() -> Self {
19+
Self {}
20+
}
21+
}
22+
23+
fn main() {
24+
let (a, b, c) = (A::new(), A::new()); // This tuple is 2 elements, should be three
25+
//~^ ERROR mismatched types
26+
let ts: Vec<&T> = vec![&a, &b, &c];
27+
// There is no E0277 error above, as `a`, `b` and `c` are `TyErr`
28+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/elide-errors-on-mismatched-tuple.rs:24:9
3+
|
4+
LL | let (a, b, c) = (A::new(), A::new()); // This tuple is 2 elements, should be three
5+
| ^^^^^^^^^ expected a tuple with 2 elements, found one with 3 elements
6+
|
7+
= note: expected type `(A, A)`
8+
found type `(_, _, _)`
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)