]> git.lizzy.rs Git - rust.git/blobdiff - src/validation.rs
Fix some clippy lints
[rust.git] / src / validation.rs
index 8f444f19bd867246a83faabd73ba64e27aa04acd..758fd5d274701c409415b8e42f1504a0978eaf8d 100644 (file)
@@ -8,11 +8,11 @@
 use rustc::traits::{self, TraitEngine};
 use rustc::infer::InferCtxt;
 use rustc::middle::region;
-use rustc::middle::const_val::ConstVal;
+use rustc::mir::interpret::{ConstValue};
 use rustc_data_structures::indexed_vec::Idx;
-use rustc_mir::interpret::{HasMemory, eval_body};
+use rustc_mir::interpret::HasMemory;
 
-use super::{EvalContext, Place, PlaceExtra, ValTy};
+use super::{EvalContext, Place, PlaceExtra, ValTy, ScalarExt};
 use rustc::mir::interpret::{DynamicLifetime, AccessKind, EvalErrorKind, Value, EvalError, EvalResult};
 use locks::MemoryExt;
 
@@ -119,7 +119,7 @@ fn abstract_place_projection(&self, proj: &mir::PlaceProjection<'tcx>) -> EvalRe
             Index(v) => {
                 let value = self.frame().get_local(v)?;
                 let ty = self.tcx.tcx.types.usize;
-                let n = self.value_to_primval(ValTy { value, ty })?.to_u64()?;
+                let n = self.value_to_scalar(ValTy { value, ty })?.to_usize(self)?;
                 Index(n)
             },
             ConstantIndex { offset, min_length, from_end } =>
@@ -135,10 +135,10 @@ fn abstract_place_projection(&self, proj: &mir::PlaceProjection<'tcx>) -> EvalRe
     }
 
     fn abstract_place(&self, place: &mir::Place<'tcx>) -> EvalResult<'tcx, AbsPlace<'tcx>> {
-        Ok(match place {
-            &mir::Place::Local(l) => AbsPlace::Local(l),
-            &mir::Place::Static(ref s) => AbsPlace::Static(s.def_id),
-            &mir::Place::Projection(ref p) =>
+        Ok(match *place {
+            mir::Place::Local(l) => AbsPlace::Local(l),
+            mir::Place::Static(ref s) => AbsPlace::Static(s.def_id),
+            mir::Place::Projection(ref p) =>
                 AbsPlace::Projection(Box::new(self.abstract_place_projection(&*p)?)),
         })
     }
@@ -378,11 +378,8 @@ fn field_with_lifetimes(
         mut layout: ty::layout::TyLayout<'tcx>,
         i: usize,
     ) -> EvalResult<'tcx, Ty<'tcx>> {
-        match base {
-            Place::Ptr { extra: PlaceExtra::DowncastVariant(variant_index), .. } => {
-                layout = layout.for_variant(&self, variant_index);
-            }
-            _ => {}
+        if let Place::Ptr { extra: PlaceExtra::DowncastVariant(variant_index), .. } = base {
+            layout = layout.for_variant(&self, variant_index);
         }
         let tcx = self.tcx.tcx;
         Ok(match layout.ty.sty {
@@ -401,7 +398,7 @@ fn field_with_lifetimes(
             }
 
             // Potentially-fat pointers.
-            ty::TyRef(_, ty::TypeAndMut { ty: pointee, .. }) |
+            ty::TyRef(_, pointee, _) |
             ty::TyRawPtr(ty::TypeAndMut { ty: pointee, .. }) => {
                 assert!(i < 2);
 
@@ -652,17 +649,13 @@ fn validate(
                 TyBool | TyFloat(_) | TyChar => {
                     if mode.acquiring() {
                         let val = self.read_place(query.place.1)?;
-                        let val = self.value_to_primval(ValTy { value: val, ty: query.ty })?;
+                        let val = self.value_to_scalar(ValTy { value: val, ty: query.ty })?;
                         val.to_bytes()?;
                         // TODO: Check if these are valid bool/float/codepoint/UTF-8
                     }
                 }
                 TyNever => return err!(ValidationFailure(format!("The empty type is never valid."))),
-                TyRef(region,
-                    ty::TypeAndMut {
-                        ty: pointee_ty,
-                        mutbl,
-                    }) => {
+                TyRef(region, pointee_ty, mutbl) => {
                     let val = self.read_place(query.place.1)?;
                     // Sharing restricts our context
                     if mutbl == MutImmutable {
@@ -671,12 +664,11 @@ fn validate(
                     // Inner lifetimes *outlive* outer ones, so only if we have no lifetime restriction yet,
                     // we record the region of this borrow to the context.
                     if query.re == None {
-                        match *region {
-                            ReScope(scope) => query.re = Some(scope),
-                            // It is possible for us to encounter erased lifetimes here because the lifetimes in
-                            // this functions' Subst will be erased.
-                            _ => {}
+                        if let ReScope(scope) = *region {
+                            query.re = Some(scope);
                         }
+                        // It is possible for us to encounter erased lifetimes here because the lifetimes in
+                        // this functions' Subst will be erased.
                     }
                     self.validate_ptr(val, query.place.0, pointee_ty, query.re, query.mutbl, mode)?;
                 }
@@ -722,18 +714,17 @@ fn validate(
                     }
                 }
                 TyArray(elem_ty, len) => {
-                    let len_val = match len.val {
-                        ConstVal::Unevaluated(def_id, substs) => {
-                            eval_body(self.tcx.tcx, GlobalId {
+                    let len = match len.val {
+                        ConstValue::Unevaluated(def_id, substs) => {
+                            self.tcx.const_eval(self.tcx.param_env(def_id).and(GlobalId {
                                 instance: Instance::new(def_id, substs),
                                 promoted: None,
-                            }, ty::ParamEnv::reveal_all())
-                                .ok_or_else(||EvalErrorKind::MachineError("<already reported>".to_string()))?
-                                .0
+                            }))
+                                .map_err(|_err|EvalErrorKind::MachineError("<already reported>".to_string()))?
                         }
-                        ConstVal::Value(val) => val,
+                        _ => len,
                     };
-                    let len = ConstVal::Value(len_val).unwrap_u64();
+                    let len = len.unwrap_usize(self.tcx.tcx);
                     for i in 0..len {
                         let inner_place = self.place_index(query.place.1, query.ty, i as u64)?;
                         self.validate(
@@ -777,7 +768,7 @@ fn validate(
                             let variant_idx = self.read_discriminant_as_variant_index(query.place.1, query.ty)?;
                             let variant = &adt.variants[variant_idx];
 
-                            if variant.fields.len() > 0 {
+                            if !variant.fields.is_empty() {
                                 // Downcast to this variant, if needed
                                 let place = if adt.is_enum() {
                                     (