about summary refs log tree commit diff
diff options
context:
space:
mode:
authorOliver Scherer <github35764891676564198441@oli-obk.de>2019-03-22 17:30:15 +0100
committerOliver Scherer <github35764891676564198441@oli-obk.de>2019-08-05 17:48:04 +0200
commit437f017e2ed2e8c60b6f4af9dc71559ca87fd645 (patch)
tree7354fb41aee7af9e7d754c5220918f2a0d035d9d
parent4be067558962c004b638e4c6f162d50f7c0c98b6 (diff)
downloadrust-437f017e2ed2e8c60b6f4af9dc71559ca87fd645.tar.gz
rust-437f017e2ed2e8c60b6f4af9dc71559ca87fd645.zip
Clean up the `ty::Const::assert*` methods
-rw-r--r--src/librustc/ty/mod.rs2
-rw-r--r--src/librustc/ty/sty.rs34
-rw-r--r--src/librustc_mir/build/matches/test.rs11
-rw-r--r--src/librustc_mir/hair/pattern/_match.rs16
-rw-r--r--src/librustc_mir/hair/pattern/mod.rs14
-rw-r--r--src/librustc_mir/transform/simplify_branches.rs3
6 files changed, 30 insertions, 50 deletions
diff --git a/src/librustc/ty/mod.rs b/src/librustc/ty/mod.rs
index 8e170578227..e71f8d94e7c 100644
--- a/src/librustc/ty/mod.rs
+++ b/src/librustc/ty/mod.rs
@@ -2368,7 +2368,7 @@ impl<'tcx> AdtDef {
         match tcx.const_eval(param_env.and(cid)) {
             Ok(val) => {
                 // FIXME: Find the right type and use it instead of `val.ty` here
-                if let Some(b) = val.assert_bits(tcx.global_tcx(), param_env.and(val.ty)) {
+                if let Some(b) = val.assert_bits(tcx.global_tcx(), val.ty) {
                     trace!("discriminants: {} ({:?})", b, repr_type);
                     Some(Discr {
                         val: b,
diff --git a/src/librustc/ty/sty.rs b/src/librustc/ty/sty.rs
index 2b14558de69..7849ae3d516 100644
--- a/src/librustc/ty/sty.rs
+++ b/src/librustc/ty/sty.rs
@@ -15,7 +15,7 @@ use crate::ty::{self, AdtDef, Discr, DefIdTree, TypeFlags, Ty, TyCtxt, TypeFolda
 use crate::ty::{List, TyS, ParamEnvAnd, ParamEnv};
 use crate::ty::layout::VariantIdx;
 use crate::util::captures::Captures;
-use crate::mir::interpret::{Scalar, Pointer};
+use crate::mir::interpret::Scalar;
 
 use smallvec::SmallVec;
 use std::borrow::Cow;
@@ -2291,29 +2291,16 @@ impl<'tcx> Const<'tcx> {
     }
 
     #[inline]
-    pub fn to_bits(&self, tcx: TyCtxt<'tcx>, ty: ParamEnvAnd<'tcx, Ty<'tcx>>) -> Option<u128> {
-        if self.ty != ty.value {
-            return None;
-        }
-        let size = tcx.layout_of(ty).ok()?.size;
-        self.val.try_to_bits(size)
-    }
-
-    #[inline]
-    pub fn to_ptr(&self) -> Option<Pointer> {
-        self.val.try_to_ptr()
-    }
-
-    #[inline]
-    pub fn assert_bits(&self, tcx: TyCtxt<'tcx>, ty: ParamEnvAnd<'tcx, Ty<'tcx>>) -> Option<u128> {
-        assert_eq!(self.ty, ty.value);
-        let size = tcx.layout_of(ty).ok()?.size;
+    pub fn assert_bits(&self, tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Option<u128> {
+        assert_eq!(self.ty, ty);
+        let ty = tcx.lift_to_global(&ty).unwrap();
+        let size = tcx.layout_of(ParamEnv::empty().and(ty)).ok()?.size;
         self.val.try_to_bits(size)
     }
 
     #[inline]
     pub fn assert_bool(&self, tcx: TyCtxt<'tcx>) -> Option<bool> {
-        self.assert_bits(tcx, ParamEnv::empty().and(tcx.types.bool)).and_then(|v| match v {
+        self.assert_bits(tcx, tcx.types.bool).and_then(|v| match v {
             0 => Some(false),
             1 => Some(true),
             _ => None,
@@ -2322,19 +2309,18 @@ impl<'tcx> Const<'tcx> {
 
     #[inline]
     pub fn assert_usize(&self, tcx: TyCtxt<'tcx>) -> Option<u64> {
-        self.assert_bits(tcx, ParamEnv::empty().and(tcx.types.usize)).map(|v| v as u64)
+        self.assert_bits(tcx, tcx.types.usize).map(|v| v as u64)
     }
 
     #[inline]
-    pub fn unwrap_bits(&self, tcx: TyCtxt<'tcx>, ty: ParamEnvAnd<'tcx, Ty<'tcx>>) -> u128 {
+    pub fn unwrap_bits(&self, tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> u128 {
         self.assert_bits(tcx, ty).unwrap_or_else(||
-            bug!("expected bits of {}, got {:#?}", ty.value, self))
+            bug!("expected bits of {}, got {:#?}", ty, self))
     }
 
     #[inline]
     pub fn unwrap_usize(&self, tcx: TyCtxt<'tcx>) -> u64 {
-        self.assert_usize(tcx).unwrap_or_else(||
-            bug!("expected constant usize, got {:#?}", self))
+        self.unwrap_bits(tcx, tcx.types.usize) as u64
     }
 }
 
diff --git a/src/librustc_mir/build/matches/test.rs b/src/librustc_mir/build/matches/test.rs
index 528dfbef694..45efbfe9b1a 100644
--- a/src/librustc_mir/build/matches/test.rs
+++ b/src/librustc_mir/build/matches/test.rs
@@ -109,7 +109,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
 
         match *match_pair.pattern.kind {
             PatternKind::Constant { value } => {
-                let switch_ty = ty::ParamEnv::empty().and(switch_ty);
                 indices.entry(value)
                        .or_insert_with(|| {
                            options.push(value.unwrap_bits(self.hir.tcx(), switch_ty));
@@ -653,11 +652,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
                     use std::cmp::Ordering::*;
                     use rustc::hir::RangeEnd::*;
 
-                    let param_env = ty::ParamEnv::empty().and(test.ty);
                     let tcx = self.hir.tcx();
 
-                    let lo = compare_const_vals(tcx, test.lo, pat.hi, param_env)?;
-                    let hi = compare_const_vals(tcx, test.hi, pat.lo, param_env)?;
+                    let lo = compare_const_vals(tcx, test.lo, pat.hi, test.ty)?;
+                    let hi = compare_const_vals(tcx, test.hi, pat.lo, test.ty)?;
 
                     match (test.end, pat.end, lo, hi) {
                         // pat < test
@@ -772,11 +770,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
     ) -> Option<bool> {
         use std::cmp::Ordering::*;
 
-        let param_env = ty::ParamEnv::empty().and(range.ty);
         let tcx = self.hir.tcx();
 
-        let a = compare_const_vals(tcx, range.lo, value, param_env)?;
-        let b = compare_const_vals(tcx, value, range.hi, param_env)?;
+        let a = compare_const_vals(tcx, range.lo, value, range.ty)?;
+        let b = compare_const_vals(tcx, value, range.hi, range.ty)?;
 
         match (b, range.end) {
             (Less, _) |
diff --git a/src/librustc_mir/hair/pattern/_match.rs b/src/librustc_mir/hair/pattern/_match.rs
index b1a317ee65f..8baefc3162b 100644
--- a/src/librustc_mir/hair/pattern/_match.rs
+++ b/src/librustc_mir/hair/pattern/_match.rs
@@ -856,7 +856,7 @@ impl<'tcx> IntRange<'tcx> {
             }
             ConstantValue(val) if is_integral(val.ty) => {
                 let ty = val.ty;
-                if let Some(val) = val.assert_bits(tcx, ty::ParamEnv::empty().and(ty)) {
+                if let Some(val) = val.assert_bits(tcx, ty) {
                     let bias = IntRange::signed_bias(tcx, ty);
                     let val = val ^ bias;
                     Some(IntRange { range: val..=val, ty })
@@ -873,8 +873,8 @@ impl<'tcx> IntRange<'tcx> {
             match pat.kind {
                 box PatternKind::Constant { value } => break ConstantValue(value),
                 box PatternKind::Range(PatternRange { lo, hi, ty, end }) => break ConstantRange(
-                    lo.to_bits(tcx, ty::ParamEnv::empty().and(ty)).unwrap(),
-                    hi.to_bits(tcx, ty::ParamEnv::empty().and(ty)).unwrap(),
+                    lo.unwrap_bits(tcx, ty),
+                    hi.unwrap_bits(tcx, ty),
                     ty,
                     end,
                 ),
@@ -1327,8 +1327,8 @@ fn pat_constructors<'tcx>(cx: &mut MatchCheckCtxt<'_, 'tcx>,
         PatternKind::Constant { value } => Some(vec![ConstantValue(value)]),
         PatternKind::Range(PatternRange { lo, hi, ty, end }) =>
             Some(vec![ConstantRange(
-                lo.to_bits(cx.tcx, ty::ParamEnv::empty().and(ty)).unwrap(),
-                hi.to_bits(cx.tcx, ty::ParamEnv::empty().and(ty)).unwrap(),
+                lo.unwrap_bits(cx.tcx, ty),
+                hi.unwrap_bits(cx.tcx, ty),
                 ty,
                 end,
             )]),
@@ -1464,7 +1464,7 @@ fn slice_pat_covered_by_const<'tcx>(
     {
         match pat.kind {
             box PatternKind::Constant { value } => {
-                let b = value.unwrap_bits(tcx, ty::ParamEnv::empty().and(pat.ty));
+                let b = value.unwrap_bits(tcx, pat.ty);
                 assert_eq!(b as u8 as u128, b);
                 if b as u8 != *ch {
                     return Ok(false);
@@ -1641,9 +1641,9 @@ fn constructor_covered_by_range<'tcx>(
         _ => bug!("`constructor_covered_by_range` called with {:?}", pat),
     };
     trace!("constructor_covered_by_range {:#?}, {:#?}, {:#?}, {}", ctor, from, to, ty);
-    let cmp_from = |c_from| compare_const_vals(tcx, c_from, from, ty::ParamEnv::empty().and(ty))
+    let cmp_from = |c_from| compare_const_vals(tcx, c_from, from, ty)
         .map(|res| res != Ordering::Less);
-    let cmp_to = |c_to| compare_const_vals(tcx, c_to, to, ty::ParamEnv::empty().and(ty));
+    let cmp_to = |c_to| compare_const_vals(tcx, c_to, to, ty);
     macro_rules! some_or_ok {
         ($e:expr) => {
             match $e {
diff --git a/src/librustc_mir/hair/pattern/mod.rs b/src/librustc_mir/hair/pattern/mod.rs
index f759ec7f219..745266a6485 100644
--- a/src/librustc_mir/hair/pattern/mod.rs
+++ b/src/librustc_mir/hair/pattern/mod.rs
@@ -446,7 +446,7 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
                             self.tcx,
                             lo,
                             hi,
-                            self.param_env.and(ty),
+                            ty,
                         );
                         match (end, cmp) {
                             (RangeEnd::Excluded, Some(Ordering::Less)) =>
@@ -1451,7 +1451,7 @@ pub fn compare_const_vals<'tcx>(
     tcx: TyCtxt<'tcx>,
     a: &'tcx ty::Const<'tcx>,
     b: &'tcx ty::Const<'tcx>,
-    ty: ty::ParamEnvAnd<'tcx, Ty<'tcx>>,
+    ty: Ty<'tcx>,
 ) -> Option<Ordering> {
     trace!("compare_const_vals: {:?}, {:?}", a, b);
 
@@ -1466,15 +1466,13 @@ pub fn compare_const_vals<'tcx>(
     let fallback = || from_bool(a == b);
 
     // Use the fallback if any type differs
-    if a.ty != b.ty || a.ty != ty.value {
+    if a.ty != b.ty || a.ty != ty {
         return fallback();
     }
 
-    // FIXME: This should use assert_bits(ty) instead of use_bits
-    // but triggers possibly bugs due to mismatching of arrays and slices
-    if let (Some(a), Some(b)) = (a.to_bits(tcx, ty), b.to_bits(tcx, ty)) {
+    if let (Some(a), Some(b)) = (a.assert_bits(tcx, ty), b.assert_bits(tcx, ty)) {
         use ::rustc_apfloat::Float;
-        return match ty.value.sty {
+        return match ty.sty {
             ty::Float(ast::FloatTy::F32) => {
                 let l = ::rustc_apfloat::ieee::Single::from_bits(a);
                 let r = ::rustc_apfloat::ieee::Single::from_bits(b);
@@ -1497,7 +1495,7 @@ pub fn compare_const_vals<'tcx>(
         }
     }
 
-    if let ty::Str = ty.value.sty {
+    if let ty::Str = ty.sty {
         match (a.val, b.val) {
             (
                 ConstValue::Slice { data: alloc_a, start: offset_a, end: end_a },
diff --git a/src/librustc_mir/transform/simplify_branches.rs b/src/librustc_mir/transform/simplify_branches.rs
index f39c71ef42d..dd45595a4fe 100644
--- a/src/librustc_mir/transform/simplify_branches.rs
+++ b/src/librustc_mir/transform/simplify_branches.rs
@@ -1,6 +1,6 @@
 //! A pass that simplifies branches when their condition is known.
 
-use rustc::ty::{TyCtxt, ParamEnv};
+use rustc::ty::TyCtxt;
 use rustc::mir::*;
 use crate::transform::{MirPass, MirSource};
 
@@ -26,7 +26,6 @@ impl MirPass for SimplifyBranches {
                 TerminatorKind::SwitchInt {
                     discr: Operand::Constant(ref c), switch_ty, ref values, ref targets, ..
                 } => {
-                    let switch_ty = ParamEnv::empty().and(switch_ty);
                     let constant = c.literal.assert_bits(tcx, switch_ty);
                     if let Some(constant) = constant {
                         let (otherwise, targets) = targets.split_last().unwrap();