]> git.lizzy.rs Git - rust.git/commitdiff
code review fixes
authorSaleem Jaffer <ssaleem1992@gmail.com>
Mon, 29 Jul 2019 14:47:52 +0000 (20:17 +0530)
committerSaleem Jaffer <ssaleem1992@gmail.com>
Mon, 29 Jul 2019 14:47:52 +0000 (20:17 +0530)
15 files changed:
src/librustc/mir/interpret/error.rs
src/librustc/mir/interpret/mod.rs
src/librustc/mir/interpret/pointer.rs
src/librustc_mir/const_eval.rs
src/librustc_mir/interpret/cast.rs
src/librustc_mir/interpret/eval_context.rs
src/librustc_mir/interpret/intern.rs
src/librustc_mir/interpret/intrinsics.rs
src/librustc_mir/interpret/machine.rs
src/librustc_mir/interpret/memory.rs
src/librustc_mir/interpret/operand.rs
src/librustc_mir/interpret/snapshot.rs
src/librustc_mir/interpret/terminator.rs
src/librustc_mir/interpret/validity.rs
src/librustc_mir/transform/const_prop.rs

index b347000ae046e6f16b1de72d43fc9ee7cb21275c..36635e62c15e1b93090d13807e0df9c636a78e59 100644 (file)
@@ -314,12 +314,12 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 
 #[derive(Clone, RustcEncodable, RustcDecodable, HashStable)]
 pub enum InvalidProgramInfo<'tcx> {
-    /// Resolution can fail if we are in a too generic context
+    /// Resolution can fail if we are in a too generic context.
     TooGeneric,
     /// Cannot compute this constant because it depends on another one
-    /// which already produced an error
+    /// which already produced an error.
     ReferencedConstant,
-    /// Abort in case type errors are reached
+    /// Abort in case type errors are reached.
     TypeckError,
     /// An error occurred during layout computation.
     Layout(layout::LayoutError<'tcx>),
@@ -362,7 +362,7 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 }
 
 #[derive(Clone, RustcEncodable, RustcDecodable, HashStable)]
-pub enum UnsupportedInfo<'tcx> {
+pub enum UnsupportedOpInfo<'tcx> {
     /// Handle cases which for which we do not have a fixed variant.
     Unimplemented(String),
 
@@ -426,9 +426,9 @@ pub enum UnsupportedInfo<'tcx> {
     PathNotFound(Vec<String>),
 }
 
-impl fmt::Debug for UnsupportedInfo<'tcx> {
+impl fmt::Debug for UnsupportedOpInfo<'tcx> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        use UnsupportedInfo::*;
+        use UnsupportedOpInfo::*;
         match self {
             PointerOutOfBounds { ptr, msg, allocation_size } => {
                 write!(f, "{} failed: pointer must be in-bounds at offset {}, \
@@ -574,7 +574,7 @@ pub enum InterpError<'tcx> {
     UndefinedBehaviour(UndefinedBehaviourInfo),
     /// The program did something the interpreter does not support (some of these *might* be UB
     /// but the interpreter is not sure).
-    Unsupported(UnsupportedInfo<'tcx>),
+    Unsupported(UnsupportedOpInfo<'tcx>),
     /// The program was invalid (ill-typed, not sufficiently monomorphized, ...).
     InvalidProgram(InvalidProgramInfo<'tcx>),
     /// The program exhausted the interpreter's resources (stack/heap too big,
index 4b09da87d314bfc68f8e3f40ccf1d15dfe6a8f9e..e4bf8efad72eb9479bb27800bee486e929f69c6c 100644 (file)
@@ -4,7 +4,7 @@
 macro_rules! err {
     ($($tt:tt)*) => {
         Err($crate::mir::interpret::InterpError::Unsupported(
-            $crate::mir::interpret::UnsupportedInfo::$($tt)*
+            $crate::mir::interpret::UnsupportedOpInfo::$($tt)*
         ).into())
     };
 }
@@ -52,7 +52,7 @@ macro_rules! err_exhaust {
 
 pub use self::error::{
     InterpErrorInfo, InterpResult, InterpError, AssertMessage, ConstEvalErr, struct_error,
-    FrameInfo, ConstEvalRawResult, ConstEvalResult, ErrorHandled, PanicInfo, UnsupportedInfo,
+    FrameInfo, ConstEvalRawResult, ConstEvalResult, ErrorHandled, PanicInfo, UnsupportedOpInfo,
     InvalidProgramInfo, ResourceExhaustionInfo, UndefinedBehaviourInfo,
 };
 
index faca04412402e36da7ae9dbaadcf472a1b298bd0..fbc726deb3dc355d8c1fb23eef3f5aa5afe75099 100644 (file)
@@ -196,7 +196,7 @@ pub fn check_in_alloc(
         msg: CheckInAllocMsg,
     ) -> InterpResult<'tcx, ()> {
         if self.offset > allocation_size {
-            err!(PointerOutOfBounds { ptr: self.erase_tag(),msg,allocation_size })
+            err!(PointerOutOfBounds { ptr: self.erase_tag(), msg, allocation_size })
         } else {
             Ok(())
         }
index bbee3b392345e37384f448a735194657bff94035..e059d766a6f5be7d86f7b34e72e5452a1c5a8721 100644 (file)
@@ -24,8 +24,7 @@
     RawConst, ConstValue,
     InterpResult, InterpErrorInfo, InterpError, GlobalId, InterpCx, StackPopCleanup,
     Allocation, AllocId, MemoryKind,
-    snapshot, RefTracking, intern_const_alloc_recursive, UnsupportedInfo::*,
-    InvalidProgramInfo::*,
+    snapshot, RefTracking, intern_const_alloc_recursive, UnsupportedOpInfo,
 };
 
 /// Number of steps until the detector even starts doing anything.
@@ -184,7 +183,7 @@ fn eval_body_using_ecx<'mir, 'tcx>(
 
 impl<'tcx> Into<InterpErrorInfo<'tcx>> for ConstEvalError {
     fn into(self) -> InterpErrorInfo<'tcx> {
-        InterpError::Unsupported(MachineError(self.to_string())).into()
+        InterpError::Unsupported(UnsupportedOpInfo::MachineError(self.to_string())).into()
     }
 }
 
@@ -361,7 +360,7 @@ fn find_fn(
         Ok(Some(match ecx.load_mir(instance.def) {
             Ok(body) => body,
             Err(err) => {
-                if let InterpError::Unsupported(NoMirFor(ref path)) = err.kind {
+                if let InterpError::Unsupported(UnsupportedOpInfo::NoMirFor(ref path)) = err.kind {
                     return Err(
                         ConstEvalError::NeedsRfc(format!("calling extern function `{}`", path))
                             .into(),
@@ -698,6 +697,7 @@ pub fn const_eval_raw_provider<'tcx>(
                 // promoting runtime code is only allowed to error if it references broken constants
                 // any other kind of error will be reported to the user as a deny-by-default lint
                 _ => if let Some(p) = cid.promoted {
+                    use crate::interpret::InvalidProgramInfo::*;
                     let span = tcx.promoted_mir(def_id)[p].span;
                     if let InterpError::InvalidProgram(ReferencedConstant) = err.error {
                         err.report_as_error(
index 7157e714f05221d35e9b9f98179060496dbab3f4..c7a56c29b4b54159264998b2b0786252b529192c 100644 (file)
@@ -9,8 +9,7 @@
 use rustc::mir::interpret::{
     Scalar, InterpResult, Pointer, PointerArithmetic, InterpError,
 };
-use rustc::mir::{CastKind, interpret::{InvalidProgramInfo::*}};
-
+use rustc::mir::CastKind;
 
 use super::{InterpCx, Machine, PlaceTy, OpTy, Immediate, FnVal};
 
@@ -75,6 +74,7 @@ pub fn cast(
             }
 
             Pointer(PointerCast::ReifyFnPointer) => {
+                use rustc::mir::interpret::InvalidProgramInfo::TooGeneric;
                 // The src operand does not matter, just its type
                 match src.layout.ty.sty {
                     ty::FnDef(def_id, substs) => {
index 1ee6871f5efc71b4b3260477282f29fd6dc6a7b0..76de3b32fc7feb2de11fee0360da4c4771e579d9 100644 (file)
@@ -17,7 +17,7 @@
     ErrorHandled,
     GlobalId, Scalar, Pointer, FrameInfo, AllocId,
     InterpResult, InterpError,
-    truncate, sign_extend, InvalidProgramInfo::*,
+    truncate, sign_extend, InvalidProgramInfo,
 };
 use rustc_data_structures::fx::FxHashMap;
 
@@ -190,8 +190,11 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> LayoutOf for InterpCx<'mir, 'tcx, M> {
 
     #[inline]
     fn layout_of(&self, ty: Ty<'tcx>) -> Self::TyLayout {
-        self.tcx.layout_of(self.param_env.and(ty))
-            .map_err(|layout| InterpError::InvalidProgram(Layout(layout)).into())
+        self.tcx
+            .layout_of(self.param_env.and(ty))
+            .map_err(|layout| {
+                InterpError::InvalidProgram(InvalidProgramInfo::Layout(layout)).into()
+            })
     }
 }
 
@@ -302,7 +305,7 @@ pub(super) fn subst_and_normalize_erasing_regions<T: TypeFoldable<'tcx>>(
                 &substs,
             )),
             None => if substs.needs_subst() {
-                err_inval!(TooGeneric).into()
+                err_inval!(TooGeneric)
             } else {
                 Ok(substs)
             },
@@ -323,7 +326,7 @@ pub(super) fn resolve(
             self.param_env,
             def_id,
             substs,
-        ).ok_or_else(|| InterpError::InvalidProgram(TooGeneric).into())
+        ).ok_or_else(|| InterpError::InvalidProgram(InvalidProgramInfo::TooGeneric).into())
     }
 
     pub fn load_mir(
@@ -694,8 +697,10 @@ pub fn const_eval_raw(
         // `Memory::get_static_alloc` which has to use `const_eval_raw` to avoid cycles.
         let val = self.tcx.const_eval_raw(param_env.and(gid)).map_err(|err| {
             match err {
-                ErrorHandled::Reported => InterpError::InvalidProgram(ReferencedConstant),
-                ErrorHandled::TooGeneric => InterpError::InvalidProgram(TooGeneric),
+                ErrorHandled::Reported =>
+                    InterpError::InvalidProgram(InvalidProgramInfo::ReferencedConstant),
+                ErrorHandled::TooGeneric =>
+                    InterpError::InvalidProgram(InvalidProgramInfo::TooGeneric),
             }
         })?;
         self.raw_const_to_mplace(val)
index 261cfbce171448d6dcd76e184c59db8c4c3d64be..af7da6fa532291d3cacee28d4ef746ad15f4da5a 100644 (file)
@@ -5,7 +5,7 @@
 
 use rustc::ty::{Ty, TyCtxt, ParamEnv, self};
 use rustc::mir::interpret::{
-    InterpResult, ErrorHandled, UnsupportedInfo::*,
+    InterpResult, ErrorHandled, UnsupportedOpInfo,
 };
 use rustc::hir;
 use rustc::hir::def_id::DefId;
@@ -293,7 +293,7 @@ pub fn intern_const_alloc_recursive(
         if let Err(error) = interned {
             // This can happen when e.g. the tag of an enum is not a valid discriminant. We do have
             // to read enum discriminants in order to find references in enum variant fields.
-            if let InterpError::Unsupported(ValidationFailure(_)) = error.kind {
+            if let InterpError::Unsupported(UnsupportedOpInfo::ValidationFailure(_)) = error.kind {
                 let err = crate::const_eval::error_to_const_error(&ecx, error);
                 match err.struct_error(ecx.tcx, "it is undefined behavior to use this value") {
                     Ok(mut diag) => {
index 9605395b84b177f5c2e3ced1200e8beefaf89f06..0b4df6cc7bc5964a74300b5398c7fd72b4b01da6 100644 (file)
@@ -7,7 +7,7 @@
 use rustc::ty::layout::{LayoutOf, Primitive, Size};
 use rustc::mir::BinOp;
 use rustc::mir::interpret::{
-    InterpResult, InterpError, Scalar, PanicInfo, UnsupportedInfo::*,
+    InterpResult, InterpError, Scalar, PanicInfo, UnsupportedOpInfo,
 };
 
 use super::{
@@ -100,7 +100,7 @@ pub fn emulate_intrinsic(
                 let bits = self.read_scalar(args[0])?.to_bits(layout_of.size)?;
                 let kind = match layout_of.abi {
                     ty::layout::Abi::Scalar(ref scalar) => scalar.value,
-                    _ => Err(InterpError::Unsupported(TypeNotPrimitive(ty)))?,
+                    _ => Err(InterpError::Unsupported(UnsupportedOpInfo::TypeNotPrimitive(ty)))?,
                 };
                 let out_val = if intrinsic_name.ends_with("_nonzero") {
                     if bits == 0 {
index 4f04803addd8b673c8b800d454cf0265f39ab73e..064ca0f4253bb729c9cc16e05b78dddf72eefcd8 100644 (file)
@@ -11,7 +11,7 @@
 
 use super::{
     Allocation, AllocId, InterpResult, InterpError, Scalar, AllocationExtra,
-    InterpCx, PlaceTy, OpTy, ImmTy, MemoryKind, Pointer, Memory, UnsupportedInfo::*
+    InterpCx, PlaceTy, OpTy, ImmTy, MemoryKind, Pointer, Memory, UnsupportedOpInfo,
 };
 
 /// Whether this kind of memory is allowed to leak
@@ -240,9 +240,9 @@ fn int_to_ptr(
         int: u64,
     ) -> InterpResult<'tcx, Pointer<Self::PointerTag>> {
         Err((if int == 0 {
-            InterpError::Unsupported(InvalidNullPointerUsage)
+            InterpError::Unsupported(UnsupportedOpInfo::InvalidNullPointerUsage)
         } else {
-            InterpError::Unsupported(ReadBytesAsPointer)
+            InterpError::Unsupported(UnsupportedOpInfo::ReadBytesAsPointer)
         }).into())
     }
 
index 72feb73f751a477689777a774e320af35842b989..edaf7855e872ff39260c5bc4699fe13163a78a7a 100644 (file)
@@ -19,8 +19,7 @@
 use super::{
     Pointer, AllocId, Allocation, GlobalId, AllocationExtra,
     InterpResult, Scalar, InterpError, GlobalAlloc, PointerArithmetic,
-    Machine, AllocMap, MayLeak, ErrorHandled, CheckInAllocMsg, UnsupportedInfo::*,
-    InvalidProgramInfo::*
+    Machine, AllocMap, MayLeak, ErrorHandled, CheckInAllocMsg, InvalidProgramInfo,
 };
 
 #[derive(Debug, PartialEq, Eq, Copy, Clone, Hash)]
@@ -438,9 +437,11 @@ fn get_static_alloc(
                             assert!(tcx.is_static(def_id));
                             match err {
                                 ErrorHandled::Reported =>
-                                    InterpError::InvalidProgram(ReferencedConstant),
+                                    InterpError::InvalidProgram(
+                                        InvalidProgramInfo::ReferencedConstant
+                                    ),
                                 ErrorHandled::TooGeneric =>
-                                    InterpError::InvalidProgram(TooGeneric),
+                                    InterpError::InvalidProgram(InvalidProgramInfo::TooGeneric),
                             }
                         })?;
                     // Make sure we use the ID of the resolved memory, not the lazy one!
@@ -590,7 +591,7 @@ fn get_fn_alloc(&self, id: AllocId) -> InterpResult<'tcx, FnVal<'tcx, M::ExtraFn
         } else {
             match self.tcx.alloc_map.lock().get(id) {
                 Some(GlobalAlloc::Function(instance)) => Ok(FnVal::Instance(instance)),
-                _ => Err(InterpError::Unsupported(ExecuteMemory).into()),
+                _ => err!(ExecuteMemory),
             }
         }
     }
index 1896c3cda13d6b981c8b8f0e0211b97d267e3e77..4dd308f5bf4da52c77295b27502418843a367bfb 100644 (file)
@@ -12,7 +12,7 @@
     GlobalId, AllocId,
     ConstValue, Pointer, Scalar,
     InterpResult, InterpError,
-    sign_extend, truncate, UnsupportedInfo::*,
+    sign_extend, truncate, UnsupportedOpInfo,
 };
 use super::{
     InterpCx, Machine,
@@ -331,8 +331,9 @@ pub fn read_str(
     ) -> InterpResult<'tcx, &str> {
         let len = mplace.len(self)?;
         let bytes = self.memory.read_bytes(mplace.ptr, Size::from_bytes(len as u64))?;
-        let str = ::std::str::from_utf8(bytes)
-            .map_err(|err| InterpError::Unsupported(ValidationFailure(err.to_string())))?;
+        let str = ::std::str::from_utf8(bytes).map_err(|err| {
+            InterpError::Unsupported(UnsupportedOpInfo::ValidationFailure(err.to_string()))
+        })?;
         Ok(str)
     }
 
@@ -603,6 +604,7 @@ pub fn read_discriminant(
         let raw_discr = discr_val.to_scalar_or_undef();
         trace!("discr value: {:?}", raw_discr);
         // post-process
+        use rustc::mir::interpret::UnsupportedOpInfo::InvalidDiscriminant;
         Ok(match *discr_kind {
             layout::DiscriminantKind::Tag => {
                 let bits_discr = match raw_discr.to_bits(discr_val.layout.size) {
index 112e592329f5f8965f62b612b065b0df7d391e87..ebf7dd3b6628cdeb9d4bf3dc5f97f6c03554f4cc 100644 (file)
@@ -12,7 +12,7 @@
 use rustc::mir::interpret::{
     AllocId, Pointer, Scalar,
     Relocations, Allocation, UndefMask,
-    InterpResult, InterpError, ResourceExhaustionInfo::*,
+    InterpResult, InterpError, ResourceExhaustionInfo,
 };
 
 use rustc::ty::{self, TyCtxt};
@@ -77,7 +77,7 @@ pub fn observe_and_analyze(
         }
 
         // Second cycle
-        Err(InterpError::ResourceExhaustion(InfiniteLoop).into())
+        Err(InterpError::ResourceExhaustion(ResourceExhaustionInfo::InfiniteLoop).into())
     }
 }
 
index 14b92f2b3d49d350420f21d615cedadef3fe3de9..b1706e72dd47551716f0621eed8ac98625201b79 100644 (file)
@@ -9,7 +9,7 @@
 use super::{
     InterpResult, PointerArithmetic, InterpError, Scalar,
     InterpCx, Machine, Immediate, OpTy, ImmTy, PlaceTy, MPlaceTy, StackPopCleanup, FnVal,
-    UnsupportedInfo::*,
+    UnsupportedOpInfo,
 };
 
 impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
@@ -221,7 +221,7 @@ fn pass_argument(
             return Ok(());
         }
         let caller_arg = caller_arg.next()
-            .ok_or_else(|| InterpError::Unsupported(FunctionArgCountMismatch))?;
+            .ok_or_else(|| InterpError::Unsupported(UnsupportedOpInfo::FunctionArgCountMismatch))?;
         if rust_abi {
             debug_assert!(!caller_arg.layout.is_zst(), "ZSTs must have been already filtered out");
         }
index ea8b88e51f0a0c00b4e99cd0c9a958579c1b60cd..9404711bd9708aa2d0f9fc7a68ac543b87ea5c2a 100644 (file)
@@ -11,7 +11,7 @@
 
 use super::{
     GlobalAlloc, InterpResult, InterpError,
-    OpTy, Machine, InterpCx, ValueVisitor, MPlaceTy, UnsupportedInfo::*,
+    OpTy, Machine, InterpCx, ValueVisitor, MPlaceTy, UnsupportedOpInfo,
 };
 
 macro_rules! validation_failure {
@@ -297,11 +297,11 @@ fn visit_value(&mut self, op: OpTy<'tcx, M::PointerTag>) -> InterpResult<'tcx>
         match self.walk_value(op) {
             Ok(()) => Ok(()),
             Err(err) => match err.kind {
-                InterpError::Unsupported(InvalidDiscriminant(val)) =>
+                InterpError::Unsupported(UnsupportedOpInfo::InvalidDiscriminant(val)) =>
                     validation_failure!(
                         val, self.path, "a valid enum discriminant"
                     ),
-                InterpError::Unsupported(ReadPointerAsBytes) =>
+                InterpError::Unsupported(UnsupportedOpInfo::ReadPointerAsBytes) =>
                     validation_failure!(
                         "a pointer", self.path, "plain (non-pointer) bytes"
                     ),
@@ -405,6 +405,7 @@ fn visit_primitive(&mut self, value: OpTy<'tcx, M::PointerTag>) -> InterpResult<
                             "{:?} did not pass access check for size {:?}, align {:?}",
                             ptr, size, align
                         );
+                        use super::UnsupportedOpInfo::*;
                         match err.kind {
                             InterpError::Unsupported(InvalidNullPointerUsage) =>
                                 return validation_failure!("NULL reference", self.path),
@@ -608,7 +609,7 @@ fn visit_aggregate(
                     Err(err) => {
                         // For some errors we might be able to provide extra information
                         match err.kind {
-                            InterpError::Unsupported(ReadUndefBytes(offset)) => {
+                            InterpError::Unsupported(UnsupportedOpInfo::ReadUndefBytes(offset)) => {
                                 // Some byte was undefined, determine which
                                 // element that byte belongs to so we can
                                 // provide an index.
index ec8a33f7a43fa0b96302f738da9952b619c657bc..191c6f2dc8aebaaa935b741d4f881855a0b6272e 100644 (file)
@@ -257,9 +257,9 @@ fn use_ecx<F, T>(
             Err(error) => {
                 let diagnostic = error_to_const_error(&self.ecx, error);
                 use rustc::mir::interpret::InterpError::*;
-                use rustc::mir::interpret::UnsupportedInfo::*;
+                use rustc::mir::interpret::UnsupportedOpInfo::*;
                 match diagnostic.error {
-                    Exit(_) => {},
+                    Exit(_) => bug!("the CTFE program cannot exit"),
 
                     | Unsupported(OutOfTls)
                     | Unsupported(TlsOutOfBounds)