From 0581605f11614becad3cd720ac39ae831a55ee45 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Fri, 3 Jan 2020 13:31:56 +0100 Subject: [PATCH 1/5] fix rustfmt fallout --- src/librustc/mir/interpret/error.rs | 5 +-- src/librustc/mir/interpret/mod.rs | 4 +-- src/librustc_mir/interpret/cast.rs | 20 +++++++----- src/librustc_mir/interpret/eval_context.rs | 12 +++++-- src/librustc_mir/interpret/memory.rs | 4 +-- src/librustc_mir/interpret/operand.rs | 38 +++++++++++++--------- src/librustc_mir/interpret/place.rs | 11 +++---- src/librustc_mir/interpret/validity.rs | 10 +++--- 8 files changed, 60 insertions(+), 44 deletions(-) diff --git a/src/librustc/mir/interpret/error.rs b/src/librustc/mir/interpret/error.rs index 2308da5d610e8..fee2f4913604f 100644 --- a/src/librustc/mir/interpret/error.rs +++ b/src/librustc/mir/interpret/error.rs @@ -33,7 +33,7 @@ impl ErrorHandled { ErrorHandled::Reported => {} ErrorHandled::TooGeneric => bug!( "MIR interpretation failed without reporting an error \ - even though it was fully monomorphized" + even though it was fully monomorphized" ), } } @@ -137,7 +137,8 @@ impl<'tcx> ConstEvalErr<'tcx> { ) -> Result, ErrorHandled> { let must_error = match self.error { InterpError::MachineStop(_) => bug!("CTFE does not stop"), - err_inval!(Layout(LayoutError::Unknown(_))) | err_inval!(TooGeneric) => { + err_inval!(Layout(LayoutError::Unknown(_))) + | err_inval!(TooGeneric) => { return Err(ErrorHandled::TooGeneric); } err_inval!(TypeckError) => return Err(ErrorHandled::Reported), diff --git a/src/librustc/mir/interpret/mod.rs b/src/librustc/mir/interpret/mod.rs index 185fa4cf9416f..2fd1d11bfad30 100644 --- a/src/librustc/mir/interpret/mod.rs +++ b/src/librustc/mir/interpret/mod.rs @@ -384,8 +384,8 @@ impl<'tcx> AllocMap<'tcx> { let next = self.next_id; self.next_id.0 = self.next_id.0.checked_add(1).expect( "You overflowed a u64 by incrementing by 1... \ - You've just earned yourself a free drink if we ever meet. \ - Seriously, how did you do that?!", + You've just earned yourself a free drink if we ever meet. \ + Seriously, how did you do that?!", ); next } diff --git a/src/librustc_mir/interpret/cast.rs b/src/librustc_mir/interpret/cast.rs index 852b7bb4bd654..c8081d739c7b8 100644 --- a/src/librustc_mir/interpret/cast.rs +++ b/src/librustc_mir/interpret/cast.rs @@ -118,15 +118,17 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { } // The rest is integer/pointer-"like", including fn ptr casts and casts from enums that // are represented as integers. - _ => assert!( - src.layout.ty.is_bool() - || src.layout.ty.is_char() - || src.layout.ty.is_enum() - || src.layout.ty.is_integral() - || src.layout.ty.is_any_ptr(), - "Unexpected cast from type {:?}", - src.layout.ty - ), + _ => { + assert!( + src.layout.ty.is_bool() + || src.layout.ty.is_char() + || src.layout.ty.is_enum() + || src.layout.ty.is_integral() + || src.layout.ty.is_any_ptr(), + "Unexpected cast from type {:?}", + src.layout.ty + ) + } } // Handle cast from a univariant (ZST) enum. diff --git a/src/librustc_mir/interpret/eval_context.rs b/src/librustc_mir/interpret/eval_context.rs index 766ef6ab6feac..d87d395e27544 100644 --- a/src/librustc_mir/interpret/eval_context.rs +++ b/src/librustc_mir/interpret/eval_context.rs @@ -152,10 +152,16 @@ impl<'tcx, Tag: Copy + 'static> LocalState<'tcx, Tag> { &mut self, ) -> InterpResult<'tcx, Result<&mut LocalValue, MemPlace>> { match self.value { - LocalValue::Dead => throw_unsup!(DeadLocal), - LocalValue::Live(Operand::Indirect(mplace)) => Ok(Err(mplace)), + LocalValue::Dead => { + throw_unsup!(DeadLocal) + } + LocalValue::Live(Operand::Indirect(mplace)) => { + Ok(Err(mplace)) + } ref mut local @ LocalValue::Live(Operand::Immediate(_)) - | ref mut local @ LocalValue::Uninitialized => Ok(Ok(local)), + | ref mut local @ LocalValue::Uninitialized => { + Ok(Ok(local)) + } } } } diff --git a/src/librustc_mir/interpret/memory.rs b/src/librustc_mir/interpret/memory.rs index cb676821fd438..3386254c93b75 100644 --- a/src/librustc_mir/interpret/memory.rs +++ b/src/librustc_mir/interpret/memory.rs @@ -581,9 +581,9 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> { Ok((layout.size, layout.align.abi)) } Some(GlobalAlloc::Memory(alloc)) => - // Need to duplicate the logic here, because the global allocations have - // different associated types than the interpreter-local ones. { + // Need to duplicate the logic here, because the global allocations have + // different associated types than the interpreter-local ones. Ok((alloc.size, alloc.align)) } Some(GlobalAlloc::Function(_)) => bug!("We already checked function pointers above"), diff --git a/src/librustc_mir/interpret/operand.rs b/src/librustc_mir/interpret/operand.rs index def979b63b52a..fe4e72e044480 100644 --- a/src/librustc_mir/interpret/operand.rs +++ b/src/librustc_mir/interpret/operand.rs @@ -436,9 +436,15 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { ) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> { use rustc::mir::ProjectionElem::*; Ok(match *proj_elem { - Field(field, _) => self.operand_field(base, field.index() as u64)?, - Downcast(_, variant) => self.operand_downcast(base, variant)?, - Deref => self.deref_operand(base)?.into(), + Field(field, _) => { + self.operand_field(base, field.index() as u64)? + } + Downcast(_, variant) => { + self.operand_downcast(base, variant)? + } + Deref => { + self.deref_operand(base)?.into() + } ConstantIndex { .. } | Index(_) if base.layout.is_zst() => { OpTy { op: Operand::Immediate(Scalar::zst().into()), @@ -575,7 +581,9 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { }; // Early-return cases. let val_val = match val.val { - ty::ConstKind::Param(_) => throw_inval!(TooGeneric), + ty::ConstKind::Param(_) => { + throw_inval!(TooGeneric) + } ty::ConstKind::Unevaluated(def_id, substs) => { let instance = self.resolve(def_id, substs)?; // We use `const_eval` here and `const_eval_raw` elsewhere in mir interpretation. @@ -593,7 +601,9 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { | ty::ConstKind::Placeholder(..) => { bug!("eval_const_to_op: Unexpected ConstKind {:?}", val) } - ty::ConstKind::Value(val_val) => val_val, + ty::ConstKind::Value(val_val) => { + val_val + } }; // Other cases need layout. let layout = from_known_layout(layout, || self.layout_of(val.ty))?; @@ -734,16 +744,14 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { let variant_index = variants_start .checked_add(variant_index_relative) .expect("oveflow computing absolute variant idx"); - assert!( - (variant_index as usize) - < rval - .layout - .ty - .ty_adt_def() - .expect("tagged layout for non adt") - .variants - .len() - ); + let variants_len = rval + .layout + .ty + .ty_adt_def() + .expect("tagged layout for non adt") + .variants + .len(); + assert!((variant_index as usize) < variants_len); (u128::from(variant_index), VariantIdx::from_u32(variant_index)) } else { (u128::from(dataful_variant.as_u32()), dataful_variant) diff --git a/src/librustc_mir/interpret/place.rs b/src/librustc_mir/interpret/place.rs index f4ac7de852af0..a3946b911ac5f 100644 --- a/src/librustc_mir/interpret/place.rs +++ b/src/librustc_mir/interpret/place.rs @@ -391,12 +391,11 @@ where // happens at run-time so that's okay. let align = match self.size_and_align_of(base.meta, field_layout)? { Some((_, align)) => align, - None if offset == Size::ZERO => - // An extern type at offset 0, we fall back to its static alignment. - // FIXME: Once we have made decisions for how to handle size and alignment - // of `extern type`, this should be adapted. It is just a temporary hack - // to get some code to work that probably ought to work. - { + None if offset == Size::ZERO => { + // An extern type at offset 0, we fall back to its static alignment. + // FIXME: Once we have made decisions for how to handle size and alignment + // of `extern type`, this should be adapted. It is just a temporary hack + // to get some code to work that probably ought to work. field_layout.align.abi } None => bug!("Cannot compute offset for extern type field at non-0 offset"), diff --git a/src/librustc_mir/interpret/validity.rs b/src/librustc_mir/interpret/validity.rs index b55f66dc75124..02952866a6a70 100644 --- a/src/librustc_mir/interpret/validity.rs +++ b/src/librustc_mir/interpret/validity.rs @@ -115,11 +115,11 @@ fn write_path(out: &mut String, path: &Vec) { TupleElem(idx) => write!(out, ".{}", idx), ArrayElem(idx) => write!(out, "[{}]", idx), Deref => - // This does not match Rust syntax, but it is more readable for long paths -- and - // some of the other items here also are not Rust syntax. Actually we can't - // even use the usual syntax because we are just showing the projections, - // not the root. { + // This does not match Rust syntax, but it is more readable for long paths -- and + // some of the other items here also are not Rust syntax. Actually we can't + // even use the usual syntax because we are just showing the projections, + // not the root. write!(out, ".") } Tag => write!(out, "."), @@ -207,8 +207,8 @@ impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, 'tcx, M // we might be projecting *to* a variant, or to a field *in*a variant. match layout.variants { layout::Variants::Single { index } => - // Inside a variant { + // Inside a variant PathElem::Field(def.variants[index].fields[field].ident.name) } _ => bug!(), From ff5b102e008dde17a8ec0ed0c364ac647d835a3e Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Fri, 3 Jan 2020 13:33:28 +0100 Subject: [PATCH 2/5] let rustfmt undo most of my edits :( --- src/librustc/mir/interpret/error.rs | 3 +-- src/librustc_mir/interpret/cast.rs | 20 +++++++++----------- src/librustc_mir/interpret/eval_context.rs | 12 +++--------- src/librustc_mir/interpret/memory.rs | 3 +-- src/librustc_mir/interpret/operand.rs | 20 +++++--------------- src/librustc_mir/interpret/validity.rs | 6 ++---- 6 files changed, 21 insertions(+), 43 deletions(-) diff --git a/src/librustc/mir/interpret/error.rs b/src/librustc/mir/interpret/error.rs index fee2f4913604f..0c3ba0df1468f 100644 --- a/src/librustc/mir/interpret/error.rs +++ b/src/librustc/mir/interpret/error.rs @@ -137,8 +137,7 @@ impl<'tcx> ConstEvalErr<'tcx> { ) -> Result, ErrorHandled> { let must_error = match self.error { InterpError::MachineStop(_) => bug!("CTFE does not stop"), - err_inval!(Layout(LayoutError::Unknown(_))) - | err_inval!(TooGeneric) => { + err_inval!(Layout(LayoutError::Unknown(_))) | err_inval!(TooGeneric) => { return Err(ErrorHandled::TooGeneric); } err_inval!(TypeckError) => return Err(ErrorHandled::Reported), diff --git a/src/librustc_mir/interpret/cast.rs b/src/librustc_mir/interpret/cast.rs index c8081d739c7b8..852b7bb4bd654 100644 --- a/src/librustc_mir/interpret/cast.rs +++ b/src/librustc_mir/interpret/cast.rs @@ -118,17 +118,15 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { } // The rest is integer/pointer-"like", including fn ptr casts and casts from enums that // are represented as integers. - _ => { - assert!( - src.layout.ty.is_bool() - || src.layout.ty.is_char() - || src.layout.ty.is_enum() - || src.layout.ty.is_integral() - || src.layout.ty.is_any_ptr(), - "Unexpected cast from type {:?}", - src.layout.ty - ) - } + _ => assert!( + src.layout.ty.is_bool() + || src.layout.ty.is_char() + || src.layout.ty.is_enum() + || src.layout.ty.is_integral() + || src.layout.ty.is_any_ptr(), + "Unexpected cast from type {:?}", + src.layout.ty + ), } // Handle cast from a univariant (ZST) enum. diff --git a/src/librustc_mir/interpret/eval_context.rs b/src/librustc_mir/interpret/eval_context.rs index d87d395e27544..766ef6ab6feac 100644 --- a/src/librustc_mir/interpret/eval_context.rs +++ b/src/librustc_mir/interpret/eval_context.rs @@ -152,16 +152,10 @@ impl<'tcx, Tag: Copy + 'static> LocalState<'tcx, Tag> { &mut self, ) -> InterpResult<'tcx, Result<&mut LocalValue, MemPlace>> { match self.value { - LocalValue::Dead => { - throw_unsup!(DeadLocal) - } - LocalValue::Live(Operand::Indirect(mplace)) => { - Ok(Err(mplace)) - } + LocalValue::Dead => throw_unsup!(DeadLocal), + LocalValue::Live(Operand::Indirect(mplace)) => Ok(Err(mplace)), ref mut local @ LocalValue::Live(Operand::Immediate(_)) - | ref mut local @ LocalValue::Uninitialized => { - Ok(Ok(local)) - } + | ref mut local @ LocalValue::Uninitialized => Ok(Ok(local)), } } } diff --git a/src/librustc_mir/interpret/memory.rs b/src/librustc_mir/interpret/memory.rs index 3386254c93b75..0bcdf9ae3c1f2 100644 --- a/src/librustc_mir/interpret/memory.rs +++ b/src/librustc_mir/interpret/memory.rs @@ -580,8 +580,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> { let layout = self.tcx.layout_of(ParamEnv::empty().and(ty)).unwrap(); Ok((layout.size, layout.align.abi)) } - Some(GlobalAlloc::Memory(alloc)) => - { + Some(GlobalAlloc::Memory(alloc)) => { // Need to duplicate the logic here, because the global allocations have // different associated types than the interpreter-local ones. Ok((alloc.size, alloc.align)) diff --git a/src/librustc_mir/interpret/operand.rs b/src/librustc_mir/interpret/operand.rs index fe4e72e044480..9e32f401062db 100644 --- a/src/librustc_mir/interpret/operand.rs +++ b/src/librustc_mir/interpret/operand.rs @@ -436,15 +436,9 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { ) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> { use rustc::mir::ProjectionElem::*; Ok(match *proj_elem { - Field(field, _) => { - self.operand_field(base, field.index() as u64)? - } - Downcast(_, variant) => { - self.operand_downcast(base, variant)? - } - Deref => { - self.deref_operand(base)?.into() - } + Field(field, _) => self.operand_field(base, field.index() as u64)?, + Downcast(_, variant) => self.operand_downcast(base, variant)?, + Deref => self.deref_operand(base)?.into(), ConstantIndex { .. } | Index(_) if base.layout.is_zst() => { OpTy { op: Operand::Immediate(Scalar::zst().into()), @@ -581,9 +575,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { }; // Early-return cases. let val_val = match val.val { - ty::ConstKind::Param(_) => { - throw_inval!(TooGeneric) - } + ty::ConstKind::Param(_) => throw_inval!(TooGeneric), ty::ConstKind::Unevaluated(def_id, substs) => { let instance = self.resolve(def_id, substs)?; // We use `const_eval` here and `const_eval_raw` elsewhere in mir interpretation. @@ -601,9 +593,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { | ty::ConstKind::Placeholder(..) => { bug!("eval_const_to_op: Unexpected ConstKind {:?}", val) } - ty::ConstKind::Value(val_val) => { - val_val - } + ty::ConstKind::Value(val_val) => val_val, }; // Other cases need layout. let layout = from_known_layout(layout, || self.layout_of(val.ty))?; diff --git a/src/librustc_mir/interpret/validity.rs b/src/librustc_mir/interpret/validity.rs index 02952866a6a70..cc1237f6539dc 100644 --- a/src/librustc_mir/interpret/validity.rs +++ b/src/librustc_mir/interpret/validity.rs @@ -114,8 +114,7 @@ fn write_path(out: &mut String, path: &Vec) { ClosureVar(name) => write!(out, ".", name), TupleElem(idx) => write!(out, ".{}", idx), ArrayElem(idx) => write!(out, "[{}]", idx), - Deref => - { + Deref => { // This does not match Rust syntax, but it is more readable for long paths -- and // some of the other items here also are not Rust syntax. Actually we can't // even use the usual syntax because we are just showing the projections, @@ -206,8 +205,7 @@ impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, 'tcx, M ty::Adt(def, ..) if def.is_enum() => { // we might be projecting *to* a variant, or to a field *in*a variant. match layout.variants { - layout::Variants::Single { index } => - { + layout::Variants::Single { index } => { // Inside a variant PathElem::Field(def.variants[index].fields[field].ident.name) } From e44f3e7cbe340b5e12704091d72dc7b1620a4e45 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Fri, 3 Jan 2020 13:37:34 +0100 Subject: [PATCH 3/5] manually format Miri error message matches --- src/librustc/mir/interpret/error.rs | 290 +++++++++++++++------------- 1 file changed, 158 insertions(+), 132 deletions(-) diff --git a/src/librustc/mir/interpret/error.rs b/src/librustc/mir/interpret/error.rs index 0c3ba0df1468f..f55a576dbb62d 100644 --- a/src/librustc/mir/interpret/error.rs +++ b/src/librustc/mir/interpret/error.rs @@ -438,139 +438,165 @@ pub enum UnsupportedOpInfo<'tcx> { impl fmt::Debug for UnsupportedOpInfo<'tcx> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { use UnsupportedOpInfo::*; + #[rustfmt::skip] // rustfmt and long matches do not go well together match self { - PointerOutOfBounds { ptr, msg, allocation_size } => write!( - f, - "{} failed: pointer must be in-bounds at offset {}, \ - but is outside bounds of allocation {} which has size {}", - msg, - ptr.offset.bytes(), - ptr.alloc_id, - allocation_size.bytes() - ), - ValidationFailure(ref err) => write!(f, "type validation failed: {}", err), - NoMirFor(ref func) => write!(f, "no MIR for `{}`", func), - FunctionAbiMismatch(caller_abi, callee_abi) => write!( - f, - "tried to call a function with ABI {:?} using caller ABI {:?}", - callee_abi, caller_abi - ), - FunctionArgMismatch(caller_ty, callee_ty) => write!( - f, - "tried to call a function with argument of type {:?} \ - passing data of type {:?}", - callee_ty, caller_ty - ), - TransmuteSizeDiff(from_ty, to_ty) => write!( - f, - "tried to transmute from {:?} to {:?}, but their sizes differed", - from_ty, to_ty - ), - FunctionRetMismatch(caller_ty, callee_ty) => write!( - f, - "tried to call a function with return type {:?} \ - passing return place of type {:?}", - callee_ty, caller_ty - ), - FunctionArgCountMismatch => { - write!(f, "tried to call a function with incorrect number of arguments") - } - ReallocatedWrongMemoryKind(ref old, ref new) => { - write!(f, "tried to reallocate memory from `{}` to `{}`", old, new) - } - DeallocatedWrongMemoryKind(ref old, ref new) => { - write!(f, "tried to deallocate `{}` memory but gave `{}` as the kind", old, new) - } - InvalidChar(c) => { - write!(f, "tried to interpret an invalid 32-bit value as a char: {}", c) - } - AlignmentCheckFailed { required, has } => write!( - f, - "tried to access memory with alignment {}, but alignment {} is required", - has.bytes(), - required.bytes() - ), - TypeNotPrimitive(ty) => write!(f, "expected primitive type, got {}", ty), - PathNotFound(ref path) => write!(f, "cannot find path {:?}", path), - IncorrectAllocationInformation(size, size2, align, align2) => write!( - f, - "incorrect alloc info: expected size {} and align {}, \ - got size {} and align {}", - size.bytes(), - align.bytes(), - size2.bytes(), - align2.bytes() - ), - InvalidMemoryAccess => write!(f, "tried to access memory through an invalid pointer"), - DanglingPointerDeref => write!(f, "dangling pointer was dereferenced"), - DoubleFree => write!(f, "tried to deallocate dangling pointer"), - InvalidFunctionPointer => { - write!(f, "tried to use a function pointer after offsetting it") - } - InvalidBool => write!(f, "invalid boolean value read"), - InvalidNullPointerUsage => write!(f, "invalid use of NULL pointer"), - ReadPointerAsBytes => write!( - f, - "a raw memory access tried to access part of a pointer value as raw \ - bytes" - ), - ReadBytesAsPointer => { - write!(f, "a memory access tried to interpret some bytes as a pointer") - } - ReadForeignStatic => write!(f, "tried to read from foreign (extern) static"), - InvalidPointerMath => write!( - f, - "attempted to do invalid arithmetic on pointers that would leak base \ - addresses, e.g., comparing pointers into different allocations" - ), - DeadLocal => write!(f, "tried to access a dead local variable"), - DerefFunctionPointer => write!(f, "tried to dereference a function pointer"), - ExecuteMemory => write!(f, "tried to treat a memory pointer as a function pointer"), - OutOfTls => write!(f, "reached the maximum number of representable TLS keys"), - TlsOutOfBounds => write!(f, "accessed an invalid (unallocated) TLS key"), - CalledClosureAsFunction => { - write!(f, "tried to call a closure through a function pointer") - } - VtableForArgumentlessMethod => { - write!(f, "tried to call a vtable function without arguments") - } - ModifiedConstantMemory => write!(f, "tried to modify constant memory"), - ModifiedStatic => write!( - f, - "tried to modify a static's initial value from another static's \ - initializer" - ), - ReallocateNonBasePtr => write!( - f, - "tried to reallocate with a pointer not to the beginning of an \ - existing object" - ), - DeallocateNonBasePtr => write!( - f, - "tried to deallocate with a pointer not to the beginning of an \ - existing object" - ), - HeapAllocZeroBytes => write!(f, "tried to re-, de- or allocate zero bytes on the heap"), - ReadFromReturnPointer => write!(f, "tried to read from the return pointer"), - UnimplementedTraitSelection => { - write!(f, "there were unresolved type arguments during trait selection") - } - InvalidBoolOp(_) => write!(f, "invalid boolean operation"), - UnterminatedCString(_) => write!( - f, - "attempted to get length of a null-terminated string, but no null \ - found before end of allocation" - ), - ReadUndefBytes(_) => write!(f, "attempted to read undefined bytes"), - HeapAllocNonPowerOfTwoAlignment(_) => write!( - f, - "tried to re-, de-, or allocate heap memory with alignment that is \ - not a power of two" - ), - Unsupported(ref msg) => write!(f, "{}", msg), - ConstPropUnsupported(ref msg) => { - write!(f, "Constant propagation encountered an unsupported situation: {}", msg) - } + PointerOutOfBounds { ptr, msg, allocation_size } => + write!( + f, + "{} failed: pointer must be in-bounds at offset {}, \ + but is outside bounds of allocation {} which has size {}", + msg, + ptr.offset.bytes(), + ptr.alloc_id, + allocation_size.bytes() + ), + ValidationFailure(ref err) => + write!(f, "type validation failed: {}", err), + NoMirFor(ref func) => + write!(f, "no MIR for `{}`", func), + FunctionAbiMismatch(caller_abi, callee_abi) => + write!( + f, + "tried to call a function with ABI {:?} using caller ABI {:?}", + callee_abi, caller_abi + ), + FunctionArgMismatch(caller_ty, callee_ty) => + write!( + f, + "tried to call a function with argument of type {:?} \ + passing data of type {:?}", + callee_ty, caller_ty + ), + TransmuteSizeDiff(from_ty, to_ty) => + write!( + f, + "tried to transmute from {:?} to {:?}, but their sizes differed", + from_ty, to_ty + ), + FunctionRetMismatch(caller_ty, callee_ty) => + write!( + f, + "tried to call a function with return type {:?} \ + passing return place of type {:?}", + callee_ty, caller_ty + ), + FunctionArgCountMismatch => + write!(f, "tried to call a function with incorrect number of arguments"), + ReallocatedWrongMemoryKind(ref old, ref new) => + write!(f, "tried to reallocate memory from `{}` to `{}`", old, new), + DeallocatedWrongMemoryKind(ref old, ref new) => + write!(f, "tried to deallocate `{}` memory but gave `{}` as the kind", old, new), + InvalidChar(c) => + write!(f, "tried to interpret an invalid 32-bit value as a char: {}", c), + AlignmentCheckFailed { required, has } => + write!( + f, + "tried to access memory with alignment {}, but alignment {} is required", + has.bytes(), + required.bytes() + ), + TypeNotPrimitive(ty) => + write!(f, "expected primitive type, got {}", ty), + PathNotFound(ref path) => + write!(f, "cannot find path {:?}", path), + IncorrectAllocationInformation(size, size2, align, align2) => + write!( + f, + "incorrect alloc info: expected size {} and align {}, \ + got size {} and align {}", + size.bytes(), + align.bytes(), + size2.bytes(), + align2.bytes() + ), + InvalidMemoryAccess => + write!(f, "tried to access memory through an invalid pointer"), + DanglingPointerDeref => + write!(f, "dangling pointer was dereferenced"), + DoubleFree => + write!(f, "tried to deallocate dangling pointer"), + InvalidFunctionPointer => + write!(f, "tried to use a function pointer after offsetting it"), + InvalidBool => + write!(f, "invalid boolean value read"), + InvalidNullPointerUsage => + write!(f, "invalid use of NULL pointer"), + ReadPointerAsBytes => + write!( + f, + "a raw memory access tried to access part of a pointer value as raw \ + bytes" + ), + ReadBytesAsPointer => + write!(f, "a memory access tried to interpret some bytes as a pointer"), + ReadForeignStatic => + write!(f, "tried to read from foreign (extern) static"), + InvalidPointerMath => + write!( + f, + "attempted to do invalid arithmetic on pointers that would leak base \ + addresses, e.g., comparing pointers into different allocations" + ), + DeadLocal => + write!(f, "tried to access a dead local variable"), + DerefFunctionPointer => + write!(f, "tried to dereference a function pointer"), + ExecuteMemory => + write!(f, "tried to treat a memory pointer as a function pointer"), + OutOfTls => + write!(f, "reached the maximum number of representable TLS keys"), + TlsOutOfBounds => + write!(f, "accessed an invalid (unallocated) TLS key"), + CalledClosureAsFunction => + write!(f, "tried to call a closure through a function pointer"), + VtableForArgumentlessMethod => + write!(f, "tried to call a vtable function without arguments"), + ModifiedConstantMemory => + write!(f, "tried to modify constant memory"), + ModifiedStatic => + write!( + f, + "tried to modify a static's initial value from another static's \ + initializer" + ), + ReallocateNonBasePtr => + write!( + f, + "tried to reallocate with a pointer not to the beginning of an \ + existing object" + ), + DeallocateNonBasePtr => + write!( + f, + "tried to deallocate with a pointer not to the beginning of an \ + existing object" + ), + HeapAllocZeroBytes => + write!(f, "tried to re-, de- or allocate zero bytes on the heap"), + ReadFromReturnPointer => + write!(f, "tried to read from the return pointer"), + UnimplementedTraitSelection => + write!(f, "there were unresolved type arguments during trait selection"), + InvalidBoolOp(_) => + write!(f, "invalid boolean operation"), + UnterminatedCString(_) => + write!( + f, + "attempted to get length of a null-terminated string, but no null \ + found before end of allocation" + ), + ReadUndefBytes(_) => + write!(f, "attempted to read undefined bytes"), + HeapAllocNonPowerOfTwoAlignment(_) => + write!( + f, + "tried to re-, de-, or allocate heap memory with alignment that is \ + not a power of two" + ), + Unsupported(ref msg) => + write!(f, "{}", msg), + ConstPropUnsupported(ref msg) => + write!(f, "Constant propagation encountered an unsupported situation: {}", msg), } } } From bc902c4d97d447a7b31399f42349996567c5eb7f Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Mon, 6 Jan 2020 11:35:55 +0100 Subject: [PATCH 4/5] adjust Deref comment --- src/librustc_mir/interpret/validity.rs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/librustc_mir/interpret/validity.rs b/src/librustc_mir/interpret/validity.rs index cc1237f6539dc..6f62d18d88511 100644 --- a/src/librustc_mir/interpret/validity.rs +++ b/src/librustc_mir/interpret/validity.rs @@ -114,13 +114,11 @@ fn write_path(out: &mut String, path: &Vec) { ClosureVar(name) => write!(out, ".", name), TupleElem(idx) => write!(out, ".{}", idx), ArrayElem(idx) => write!(out, "[{}]", idx), - Deref => { - // This does not match Rust syntax, but it is more readable for long paths -- and - // some of the other items here also are not Rust syntax. Actually we can't - // even use the usual syntax because we are just showing the projections, - // not the root. - write!(out, ".") - } + // `.` does not match Rust syntax, but it is more readable for long paths -- and + // some of the other items here also are not Rust syntax. Actually we can't + // even use the usual syntax because we are just showing the projections, + // not the root. + Deref => write!(out, "."), Tag => write!(out, "."), DynDowncast => write!(out, "."), } From 7a4a08f5030197a338a95133ebbd02653cef5e04 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Wed, 8 Jan 2020 11:35:02 +0100 Subject: [PATCH 5/5] add FIXME for rustfmt::skip --- src/librustc/mir/interpret/error.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustc/mir/interpret/error.rs b/src/librustc/mir/interpret/error.rs index f55a576dbb62d..f1af4481be786 100644 --- a/src/librustc/mir/interpret/error.rs +++ b/src/librustc/mir/interpret/error.rs @@ -438,7 +438,7 @@ pub enum UnsupportedOpInfo<'tcx> { impl fmt::Debug for UnsupportedOpInfo<'tcx> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { use UnsupportedOpInfo::*; - #[rustfmt::skip] // rustfmt and long matches do not go well together + #[rustfmt::skip] // FIXME: https://github.com/rust-lang/rustfmt/issues/3995 match self { PointerOutOfBounds { ptr, msg, allocation_size } => write!(