]> git.lizzy.rs Git - rust.git/commitdiff
Do not emit E0277 on incorrect tuple destructured binding
authorEsteban Küber <esteban@kuber.com.ar>
Tue, 14 Aug 2018 23:46:28 +0000 (16:46 -0700)
committerEsteban Küber <esteban@kuber.com.ar>
Wed, 12 Sep 2018 00:09:22 +0000 (17:09 -0700)
src/librustc_typeck/check/_match.rs
src/librustc_typeck/check/demand.rs
src/test/ui/elide-errors-on-mismatched-tuple.rs [new file with mode: 0644]
src/test/ui/elide-errors-on-mismatched-tuple.stderr [new file with mode: 0644]

index 2a8ee4bd8df0eeeb9feb5e19bbecb337641271c0..ca2b892018b5f4ee8f8b1f00ebfc619ca326eb5c 100644 (file)
@@ -299,10 +299,19 @@ pub fn check_pat_walk(
                 let element_tys = tcx.mk_type_list(element_tys_iter);
                 let pat_ty = tcx.mk_ty(ty::Tuple(element_tys));
                 self.demand_eqtype(pat.span, expected, pat_ty);
-                for (i, elem) in elements.iter().enumerate_and_adjust(max_len, ddpos) {
-                    self.check_pat_walk(elem, &element_tys[i], def_bm, true);
+                if self.has_errors.get() {
+                    let element_tys_iter = (0..max_len).map(|_| tcx.types.err);
+                    let element_tys = tcx.mk_type_list(element_tys_iter);
+                    for (_, elem) in elements.iter().enumerate_and_adjust(max_len, ddpos) {
+                        self.check_pat_walk(elem, &tcx.types.err, def_bm, true);
+                    }
+                    tcx.mk_ty(ty::TyTuple(element_tys))
+                } else {
+                    for (i, elem) in elements.iter().enumerate_and_adjust(max_len, ddpos) {
+                        self.check_pat_walk(elem, &element_tys[i], def_bm, true);
+                    }
+                    pat_ty
                 }
-                pat_ty
             }
             PatKind::Box(ref inner) => {
                 let inner_ty = self.next_ty_var(TypeVariableOrigin::TypeInference(inner.span));
index 4e22ead8db987a0514b885a992a43eac35332084..47bbc95aa496ba9098a4a6493e08d43f99cee314 100644 (file)
@@ -50,6 +50,7 @@ pub fn demand_suptype_diag(&self,
 
     pub fn demand_eqtype(&self, sp: Span, expected: Ty<'tcx>, actual: Ty<'tcx>) {
         if let Some(mut err) = self.demand_eqtype_diag(sp, expected, actual) {
+            self.has_errors.set(true);
             err.emit();
         }
     }
diff --git a/src/test/ui/elide-errors-on-mismatched-tuple.rs b/src/test/ui/elide-errors-on-mismatched-tuple.rs
new file mode 100644 (file)
index 0000000..68e5797
--- /dev/null
@@ -0,0 +1,28 @@
+// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// Hide irrelevant E0277 errors (#50333)
+
+trait T {}
+
+struct A;
+impl T for A {}
+impl A {
+    fn new() -> Self {
+        Self {}
+    }
+}
+
+fn main() {
+    let (a, b, c) = (A::new(), A::new()); // This tuple is 2 elements, should be three
+    //~^ ERROR mismatched types
+    let ts: Vec<&T> = vec![&a, &b, &c];
+    // There is no E0277 error above, as `a`, `b` and `c` are `TyErr`
+}
diff --git a/src/test/ui/elide-errors-on-mismatched-tuple.stderr b/src/test/ui/elide-errors-on-mismatched-tuple.stderr
new file mode 100644 (file)
index 0000000..b901175
--- /dev/null
@@ -0,0 +1,12 @@
+error[E0308]: mismatched types
+  --> $DIR/elide-errors-on-mismatched-tuple.rs:24:9
+   |
+LL |     let (a, b, c) = (A::new(), A::new()); // This tuple is 2 elements, should be three
+   |         ^^^^^^^^^ expected a tuple with 2 elements, found one with 3 elements
+   |
+   = note: expected type `(A, A)`
+              found type `(_, _, _)`
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0308`.