From d43cd2e3a9c19909b94d6866a8c91507a856faa7 Mon Sep 17 00:00:00 2001
From: Ariel Ben-Yehuda <ariel.byd@gmail.com>
Date: Fri, 4 Mar 2016 01:05:31 +0200
Subject: [PATCH 1/7] truncate i8-s to i1-s when loading constants

Fixes #30891
---
 src/librustc_trans/trans/consts.rs | 26 +++++++++++++++++---------
 src/test/run-pass/issue-30891.rs   | 19 +++++++++++++++++++
 2 files changed, 36 insertions(+), 9 deletions(-)
 create mode 100644 src/test/run-pass/issue-30891.rs

diff --git a/src/librustc_trans/trans/consts.rs b/src/librustc_trans/trans/consts.rs
index 79c73a83ee7e0..20da15834966e 100644
--- a/src/librustc_trans/trans/consts.rs
+++ b/src/librustc_trans/trans/consts.rs
@@ -161,13 +161,17 @@ pub fn addr_of(ccx: &CrateContext,
     gv
 }
 
-fn const_deref_ptr(cx: &CrateContext, v: ValueRef) -> ValueRef {
+/// Deref a constant pointer
+fn load_const(cx: &CrateContext, v: ValueRef, t: Ty) -> ValueRef {
     let v = match cx.const_unsized().borrow().get(&v) {
         Some(&v) => v,
         None => v
     };
-    unsafe {
-        llvm::LLVMGetInitializer(v)
+    let d = unsafe { llvm::LLVMGetInitializer(v) };
+    if t.is_bool() {
+        unsafe { llvm::LLVMConstTrunc(d, Type::i1(cx).to_ref()) }
+    } else {
+        d
     }
 }
 
@@ -178,7 +182,7 @@ fn const_deref<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
     match ty.builtin_deref(true, ty::NoPreference) {
         Some(mt) => {
             if type_is_sized(cx.tcx(), mt.ty) {
-                (const_deref_ptr(cx, v), mt.ty)
+                (load_const(cx, v, mt.ty), mt.ty)
             } else {
                 // Derefing a fat pointer does not change the representation,
                 // just the type to the unsized contents.
@@ -588,7 +592,10 @@ fn const_expr_unadjusted<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
             let is_float = ty.is_fp();
             let signed = ty.is_signed();
 
-            let (te2, _) = try!(const_expr(cx, &e2, param_substs, fn_args, trueconst));
+            let (te2, ty2) = try!(const_expr(cx, &e2, param_substs, fn_args, trueconst));
+            debug!("const_expr_unadjusted: te2={}, ty={:?}",
+                   cx.tn().val_to_string(te2),
+                   ty2);
 
             try!(check_binary_expr_validity(cx, e, ty, te1, te2, trueconst));
 
@@ -671,13 +678,13 @@ fn const_expr_unadjusted<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
             };
             let (arr, len) = match bt.sty {
                 ty::TyArray(_, u) => (bv, C_uint(cx, u)),
-                ty::TySlice(_) | ty::TyStr => {
+                ty::TySlice(..) | ty::TyStr => {
                     let e1 = const_get_elt(cx, bv, &[0]);
-                    (const_deref_ptr(cx, e1), const_get_elt(cx, bv, &[1]))
+                    (load_const(cx, e1, bt), const_get_elt(cx, bv, &[1]))
                 },
                 ty::TyRef(_, mt) => match mt.ty.sty {
                     ty::TyArray(_, u) => {
-                        (const_deref_ptr(cx, bv), C_uint(cx, u))
+                        (load_const(cx, bv, mt.ty), C_uint(cx, u))
                     },
                     _ => cx.sess().span_bug(base.span,
                                             &format!("index-expr base must be a vector \
@@ -891,7 +898,8 @@ fn const_expr_unadjusted<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
                     expr::trans_def_fn_unadjusted(cx, e, def, param_substs).val
                 }
                 Def::Const(def_id) | Def::AssociatedConst(def_id) => {
-                    const_deref_ptr(cx, try!(get_const_val(cx, def_id, e, param_substs)))
+                    load_const(cx, try!(get_const_val(cx, def_id, e, param_substs)),
+                               ety)
                 }
                 Def::Variant(enum_did, variant_did) => {
                     let vinfo = cx.tcx().lookup_adt_def(enum_did).variant_with_id(variant_did);
diff --git a/src/test/run-pass/issue-30891.rs b/src/test/run-pass/issue-30891.rs
new file mode 100644
index 0000000000000..622e5fa544ec9
--- /dev/null
+++ b/src/test/run-pass/issue-30891.rs
@@ -0,0 +1,19 @@
+// Copyright 2016 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.
+
+const ERROR_CONST: bool = true;
+
+fn get() -> bool {
+    false || ERROR_CONST
+}
+
+pub fn main() {
+    assert_eq!(get(), true);
+}

From 14ba07010dbe3476af0f6042db22d7979f60ed0a Mon Sep 17 00:00:00 2001
From: Ariel Ben-Yehuda <ariel.byd@gmail.com>
Date: Fri, 4 Mar 2016 02:26:42 +0200
Subject: [PATCH 2/7] clean up check_pat_enum

---
 src/librustc_typeck/check/_match.rs | 74 +++++++++++------------------
 1 file changed, 28 insertions(+), 46 deletions(-)

diff --git a/src/librustc_typeck/check/_match.rs b/src/librustc_typeck/check/_match.rs
index bd2c7b3915363..8d0ae0f880d09 100644
--- a/src/librustc_typeck/check/_match.rs
+++ b/src/librustc_typeck/check/_match.rs
@@ -599,12 +599,12 @@ fn bad_struct_kind_err(sess: &Session, pat: &hir::Pat, path: &hir::Path, lint: b
     }
 }
 
-pub fn check_pat_enum<'a, 'tcx>(pcx: &pat_ctxt<'a, 'tcx>,
-                                pat: &hir::Pat,
-                                path: &hir::Path,
-                                subpats: Option<&'tcx [P<hir::Pat>]>,
-                                expected: Ty<'tcx>,
-                                is_tuple_struct_pat: bool)
+fn check_pat_enum<'a, 'tcx>(pcx: &pat_ctxt<'a, 'tcx>,
+                            pat: &hir::Pat,
+                            path: &hir::Path,
+                            subpats: Option<&'tcx [P<hir::Pat>]>,
+                            expected: Ty<'tcx>,
+                            is_tuple_struct_pat: bool)
 {
     // Typecheck the path.
     let fcx = pcx.fcx;
@@ -687,46 +687,14 @@ pub fn check_pat_enum<'a, 'tcx>(pcx: &pat_ctxt<'a, 'tcx>,
     demand::eqtype(fcx, pat.span, expected, pat_ty);
 
     let real_path_ty = fcx.node_ty(pat.id);
-    let (arg_tys, kind_name): (Vec<_>, &'static str) = match real_path_ty.sty {
+    let (kind_name, variant, expected_substs) = match real_path_ty.sty {
         ty::TyEnum(enum_def, expected_substs) => {
             let variant = enum_def.variant_of_def(def);
-            if variant.kind() == ty::VariantKind::Struct {
-                report_bad_struct_kind(false);
-                return;
-            }
-            if is_tuple_struct_pat && variant.kind() != ty::VariantKind::Tuple {
-                // Matching unit variants with tuple variant patterns (`UnitVariant(..)`)
-                // is allowed for backward compatibility.
-                let is_special_case = variant.kind() == ty::VariantKind::Unit;
-                report_bad_struct_kind(is_special_case);
-                if !is_special_case {
-                    return
-                }
-            }
-            (variant.fields
-                    .iter()
-                    .map(|f| fcx.instantiate_type_scheme(pat.span,
-                                                         expected_substs,
-                                                         &f.unsubst_ty()))
-                    .collect(),
-             "variant")
+            ("variant", variant, expected_substs)
         }
         ty::TyStruct(struct_def, expected_substs) => {
             let variant = struct_def.struct_variant();
-            if is_tuple_struct_pat && variant.kind() != ty::VariantKind::Tuple {
-                // Matching unit structs with tuple variant patterns (`UnitVariant(..)`)
-                // is allowed for backward compatibility.
-                let is_special_case = variant.kind() == ty::VariantKind::Unit;
-                report_bad_struct_kind(is_special_case);
-                return;
-            }
-            (variant.fields
-                    .iter()
-                    .map(|f| fcx.instantiate_type_scheme(pat.span,
-                                                         expected_substs,
-                                                         &f.unsubst_ty()))
-                    .collect(),
-             "struct")
+            ("struct", variant, expected_substs)
         }
         _ => {
             report_bad_struct_kind(false);
@@ -734,12 +702,26 @@ pub fn check_pat_enum<'a, 'tcx>(pcx: &pat_ctxt<'a, 'tcx>,
         }
     };
 
+    match (is_tuple_struct_pat, variant.kind()) {
+        (true, ty::VariantKind::Unit) => {
+            // Matching unit structs with tuple variant patterns (`UnitVariant(..)`)
+            // is allowed for backward compatibility.
+            report_bad_struct_kind(true);
+        }
+        (_, ty::VariantKind::Struct) => {
+            report_bad_struct_kind(false);
+            return
+        }
+        _ => {}
+    }
+
     if let Some(subpats) = subpats {
-        if subpats.len() == arg_tys.len() {
-            for (subpat, arg_ty) in subpats.iter().zip(arg_tys) {
-                check_pat(pcx, &subpat, arg_ty);
+        if subpats.len() == variant.fields.len() {
+            for (subpat, field) in subpats.iter().zip(&variant.fields) {
+                let field_ty = fcx.field_ty(subpat.span, field, expected_substs);
+                check_pat(pcx, &subpat, field_ty);
             }
-        } else if arg_tys.is_empty() {
+        } else if variant.fields.is_empty() {
             span_err!(tcx.sess, pat.span, E0024,
                       "this pattern has {} field{}, but the corresponding {} has no fields",
                       subpats.len(), if subpats.len() == 1 {""} else {"s"}, kind_name);
@@ -752,7 +734,7 @@ pub fn check_pat_enum<'a, 'tcx>(pcx: &pat_ctxt<'a, 'tcx>,
                       "this pattern has {} field{}, but the corresponding {} has {} field{}",
                       subpats.len(), if subpats.len() == 1 {""} else {"s"},
                       kind_name,
-                      arg_tys.len(), if arg_tys.len() == 1 {""} else {"s"});
+                      variant.fields.len(), if variant.fields.len() == 1 {""} else {"s"});
 
             for pat in subpats {
                 check_pat(pcx, &pat, tcx.types.err);

From 0e02ed77fe20758361a51708f7ac2c9c32d13ba8 Mon Sep 17 00:00:00 2001
From: Ariel Ben-Yehuda <ariel.byd@gmail.com>
Date: Fri, 4 Mar 2016 15:34:28 +0200
Subject: [PATCH 3/7] don't treat unit patterns as wildcard patterns

Fixes #32004
---
 src/librustc_typeck/check/_match.rs  |  2 +-
 src/test/compile-fail/issue-32004.rs | 29 ++++++++++++++++++++++++++++
 2 files changed, 30 insertions(+), 1 deletion(-)
 create mode 100644 src/test/compile-fail/issue-32004.rs

diff --git a/src/librustc_typeck/check/_match.rs b/src/librustc_typeck/check/_match.rs
index 8d0ae0f880d09..93d983fae12e7 100644
--- a/src/librustc_typeck/check/_match.rs
+++ b/src/librustc_typeck/check/_match.rs
@@ -204,7 +204,7 @@ pub fn check_pat<'a, 'tcx>(pcx: &pat_ctxt<'a, 'tcx>,
             check_pat_enum(pcx, pat, path, subpats.as_ref().map(|v| &v[..]), expected, true);
         }
         PatKind::Path(ref path) => {
-            check_pat_enum(pcx, pat, path, None, expected, false);
+            check_pat_enum(pcx, pat, path, Some(&[]), expected, false);
         }
         PatKind::QPath(ref qself, ref path) => {
             let self_ty = fcx.to_ty(&qself.ty);
diff --git a/src/test/compile-fail/issue-32004.rs b/src/test/compile-fail/issue-32004.rs
new file mode 100644
index 0000000000000..0227a80fd75d3
--- /dev/null
+++ b/src/test/compile-fail/issue-32004.rs
@@ -0,0 +1,29 @@
+// Copyright 2016 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.
+
+enum Foo {
+    Bar(i32),
+    Baz
+}
+
+struct S;
+
+fn main() {
+    match Foo::Baz {
+        Foo::Bar => {}
+        //~^ ERROR this pattern has 0 fields, but the corresponding variant
+        _ => {}
+    }
+
+    match S {
+        S(()) => {}
+        //~^ ERROR this pattern has 1 field, but the corresponding struct
+    }
+}

From d66fabbb399ff6f06dbbe40cd098b5d91f9de115 Mon Sep 17 00:00:00 2001
From: Ariel Ben-Yehuda <ariel.byd@gmail.com>
Date: Fri, 4 Mar 2016 16:10:22 +0200
Subject: [PATCH 4/7] remove wrong assert in check_match

the assert was invalidated by PR #31020

Fixes #31561
---
 src/librustc/middle/check_match.rs   |  5 +----
 src/test/compile-fail/issue-31561.rs | 20 ++++++++++++++++++++
 2 files changed, 21 insertions(+), 4 deletions(-)
 create mode 100644 src/test/compile-fail/issue-31561.rs

diff --git a/src/librustc/middle/check_match.rs b/src/librustc/middle/check_match.rs
index 246a4e9f28f6b..7f0e9cae85e9b 100644
--- a/src/librustc/middle/check_match.rs
+++ b/src/librustc/middle/check_match.rs
@@ -1047,10 +1047,7 @@ fn is_refutable<A, F>(cx: &MatchCheckCtxt, pat: &Pat, refutable: F) -> Option<A>
 {
     let pats = Matrix(vec!(vec!(pat)));
     match is_useful(cx, &pats, &[DUMMY_WILD_PAT], ConstructWitness) {
-        UsefulWithWitness(pats) => {
-            assert_eq!(pats.len(), 1);
-            Some(refutable(&pats[0]))
-        },
+        UsefulWithWitness(pats) => Some(refutable(&pats[0])),
         NotUseful => None,
         Useful => unreachable!()
     }
diff --git a/src/test/compile-fail/issue-31561.rs b/src/test/compile-fail/issue-31561.rs
new file mode 100644
index 0000000000000..f8645c4d3a7e8
--- /dev/null
+++ b/src/test/compile-fail/issue-31561.rs
@@ -0,0 +1,20 @@
+// Copyright 2016 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.
+
+enum Thing {
+    Foo(u8),
+    Bar,
+    Baz
+}
+
+fn main() {
+    let Thing::Foo(y) = Thing::Foo(1);
+    //~^ ERROR refutable pattern in local binding: `Bar` not covered
+}

From a5487e86de205e0c0b84c68d402dd72c9d66da4f Mon Sep 17 00:00:00 2001
From: Niko Matsakis <niko@alum.mit.edu>
Date: Wed, 9 Mar 2016 16:53:19 -0500
Subject: [PATCH 5/7] Do not report errors from regionck if other errors were
 already reported during the lifetime of this inferencer. Fixes #30580.

---
 src/librustc/middle/infer/mod.rs     | 15 +++++++++++++--
 src/test/compile-fail/issue-30580.rs | 24 ++++++++++++++++++++++++
 2 files changed, 37 insertions(+), 2 deletions(-)
 create mode 100644 src/test/compile-fail/issue-30580.rs

diff --git a/src/librustc/middle/infer/mod.rs b/src/librustc/middle/infer/mod.rs
index 26ea5454693e1..250cb1062f753 100644
--- a/src/librustc/middle/infer/mod.rs
+++ b/src/librustc/middle/infer/mod.rs
@@ -1107,11 +1107,15 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
                                         .map(|method| resolve_ty(method.ty)))
     }
 
+    pub fn errors_since_creation(&self) -> bool {
+        self.tcx.sess.err_count() - self.err_count_on_creation != 0
+    }
+
     pub fn node_type(&self, id: ast::NodeId) -> Ty<'tcx> {
         match self.tables.borrow().node_types.get(&id) {
             Some(&t) => t,
             // FIXME
-            None if self.tcx.sess.err_count() - self.err_count_on_creation != 0 =>
+            None if self.errors_since_creation() =>
                 self.tcx.types.err,
             None => {
                 self.tcx.sess.bug(
@@ -1134,7 +1138,14 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
                                              free_regions: &FreeRegionMap,
                                              subject_node_id: ast::NodeId) {
         let errors = self.region_vars.resolve_regions(free_regions, subject_node_id);
-        self.report_region_errors(&errors); // see error_reporting.rs
+        if !self.errors_since_creation() {
+            // As a heuristic, just skip reporting region errors
+            // altogether if other errors have been reported while
+            // this infcx was in use.  This is totally hokey but
+            // otherwise we have a hard time separating legit region
+            // errors from silly ones.
+            self.report_region_errors(&errors); // see error_reporting.rs
+        }
     }
 
     pub fn ty_to_string(&self, t: Ty<'tcx>) -> String {
diff --git a/src/test/compile-fail/issue-30580.rs b/src/test/compile-fail/issue-30580.rs
new file mode 100644
index 0000000000000..908a2d47401e5
--- /dev/null
+++ b/src/test/compile-fail/issue-30580.rs
@@ -0,0 +1,24 @@
+// Copyright 2012 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.
+
+pub struct Foo { a: u32 }
+pub struct Pass<'a, 'tcx: 'a>(&'a mut &'a (), &'a &'tcx ());
+
+impl<'a, 'tcx> Pass<'a, 'tcx>
+{
+    pub fn tcx(&self) -> &'a &'tcx () { self.1 }
+    fn lol(&mut self, b: &Foo)
+    {
+        b.c; //~ ERROR no field with that name was found
+        self.tcx();
+    }
+}
+
+fn main() {}

From d2dac1b9389d7f3c3f3277012912a2aa1eb37387 Mon Sep 17 00:00:00 2001
From: Niko Matsakis <niko@alum.mit.edu>
Date: Thu, 10 Mar 2016 05:21:00 -0500
Subject: [PATCH 6/7] Add comment explaining purpose of test

---
 src/test/compile-fail/issue-30580.rs | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/src/test/compile-fail/issue-30580.rs b/src/test/compile-fail/issue-30580.rs
index 908a2d47401e5..88d4aef6d9ddc 100644
--- a/src/test/compile-fail/issue-30580.rs
+++ b/src/test/compile-fail/issue-30580.rs
@@ -8,6 +8,9 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+// Test that we do not see uninformative region-related errors
+// when we get some basic type-checking failure. See #30580.
+
 pub struct Foo { a: u32 }
 pub struct Pass<'a, 'tcx: 'a>(&'a mut &'a (), &'a &'tcx ());
 

From 48313e884a6ec9b73958a9956eebf0f616a54eb4 Mon Sep 17 00:00:00 2001
From: Alex Crichton <alex@alexcrichton.com>
Date: Wed, 16 Mar 2016 11:34:20 -0700
Subject: [PATCH 7/7] Bump beta to .2

---
 mk/main.mk | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mk/main.mk b/mk/main.mk
index 38842a3e8a7b9..a0a44cd345caf 100644
--- a/mk/main.mk
+++ b/mk/main.mk
@@ -18,7 +18,7 @@ CFG_RELEASE_NUM=1.8.0
 # An optional number to put after the label, e.g. '.2' -> '-beta.2'
 # NB Make sure it starts with a dot to conform to semver pre-release
 # versions (section 9)
-CFG_PRERELEASE_VERSION=.1
+CFG_PRERELEASE_VERSION=.2
 
 # Append a version-dependent hash to each library, so we can install different
 # versions in the same place