From 5cc292eb1dcb22bd6a46478165b5820f8177c87f Mon Sep 17 00:00:00 2001 From: est31 Date: Sat, 19 Feb 2022 00:47:43 +0100 Subject: rustc_const_eval: adopt let else in more places --- .../rustc_const_eval/src/interpret/eval_context.rs | 14 ++--- compiler/rustc_const_eval/src/interpret/intern.rs | 27 +++++----- compiler/rustc_const_eval/src/interpret/memory.rs | 60 ++++++++++------------ compiler/rustc_const_eval/src/interpret/operand.rs | 15 +++--- compiler/rustc_const_eval/src/interpret/place.rs | 11 ++-- compiler/rustc_const_eval/src/interpret/step.rs | 15 +++--- .../rustc_const_eval/src/interpret/terminator.rs | 7 ++- .../rustc_const_eval/src/interpret/validity.rs | 9 ++-- 8 files changed, 67 insertions(+), 91 deletions(-) (limited to 'compiler/rustc_const_eval/src/interpret') diff --git a/compiler/rustc_const_eval/src/interpret/eval_context.rs b/compiler/rustc_const_eval/src/interpret/eval_context.rs index 1b86bcfa8c9..ab50c709143 100644 --- a/compiler/rustc_const_eval/src/interpret/eval_context.rs +++ b/compiler/rustc_const_eval/src/interpret/eval_context.rs @@ -631,15 +631,11 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { // the last field). Can't have foreign types here, how would we // adjust alignment and size for them? let field = layout.field(self, layout.fields.count() - 1); - let (unsized_size, unsized_align) = - match self.size_and_align_of(metadata, &field)? { - Some(size_and_align) => size_and_align, - None => { - // A field with an extern type. We don't know the actual dynamic size - // or the alignment. - return Ok(None); - } - }; + let Some((unsized_size, unsized_align)) = self.size_and_align_of(metadata, &field)? else { + // A field with an extern type. We don't know the actual dynamic size + // or the alignment. + return Ok(None); + }; // FIXME (#26403, #27023): We should be adding padding // to `sized_size` (to accommodate the `unsized_align` diff --git a/compiler/rustc_const_eval/src/interpret/intern.rs b/compiler/rustc_const_eval/src/interpret/intern.rs index a1dd587c17a..b1f50bc56c9 100644 --- a/compiler/rustc_const_eval/src/interpret/intern.rs +++ b/compiler/rustc_const_eval/src/interpret/intern.rs @@ -84,22 +84,19 @@ fn intern_shallow<'rt, 'mir, 'tcx, M: CompileTimeMachine<'mir, 'tcx, const_eval: trace!("intern_shallow {:?} with {:?}", alloc_id, mode); // remove allocation let tcx = ecx.tcx; - let (kind, mut alloc) = match ecx.memory.alloc_map.remove(&alloc_id) { - Some(entry) => entry, - None => { - // Pointer not found in local memory map. It is either a pointer to the global - // map, or dangling. - // If the pointer is dangling (neither in local nor global memory), we leave it - // to validation to error -- it has the much better error messages, pointing out where - // in the value the dangling reference lies. - // The `delay_span_bug` ensures that we don't forget such a check in validation. - if tcx.get_global_alloc(alloc_id).is_none() { - tcx.sess.delay_span_bug(ecx.tcx.span, "tried to intern dangling pointer"); - } - // treat dangling pointers like other statics - // just to stop trying to recurse into them - return Some(IsStaticOrFn); + let Some((kind, mut alloc)) = ecx.memory.alloc_map.remove(&alloc_id) else { + // Pointer not found in local memory map. It is either a pointer to the global + // map, or dangling. + // If the pointer is dangling (neither in local nor global memory), we leave it + // to validation to error -- it has the much better error messages, pointing out where + // in the value the dangling reference lies. + // The `delay_span_bug` ensures that we don't forget such a check in validation. + if tcx.get_global_alloc(alloc_id).is_none() { + tcx.sess.delay_span_bug(ecx.tcx.span, "tried to intern dangling pointer"); } + // treat dangling pointers like other statics + // just to stop trying to recurse into them + return Some(IsStaticOrFn); }; // This match is just a canary for future changes to `MemoryKind`, which most likely need // changes in this function. diff --git a/compiler/rustc_const_eval/src/interpret/memory.rs b/compiler/rustc_const_eval/src/interpret/memory.rs index 4aa3c83cc02..a1f94b095cf 100644 --- a/compiler/rustc_const_eval/src/interpret/memory.rs +++ b/compiler/rustc_const_eval/src/interpret/memory.rs @@ -291,21 +291,18 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> { ); } - let (alloc_kind, mut alloc) = match self.alloc_map.remove(&alloc_id) { - Some(alloc) => alloc, - None => { - // Deallocating global memory -- always an error - return Err(match self.tcx.get_global_alloc(alloc_id) { - Some(GlobalAlloc::Function(..)) => { - err_ub_format!("deallocating {}, which is a function", alloc_id) - } - Some(GlobalAlloc::Static(..) | GlobalAlloc::Memory(..)) => { - err_ub_format!("deallocating {}, which is static memory", alloc_id) - } - None => err_ub!(PointerUseAfterFree(alloc_id)), + let Some((alloc_kind, mut alloc)) = self.alloc_map.remove(&alloc_id) else { + // Deallocating global memory -- always an error + return Err(match self.tcx.get_global_alloc(alloc_id) { + Some(GlobalAlloc::Function(..)) => { + err_ub_format!("deallocating {}, which is a function", alloc_id) } - .into()); + Some(GlobalAlloc::Static(..) | GlobalAlloc::Memory(..)) => { + err_ub_format!("deallocating {}, which is static memory", alloc_id) + } + None => err_ub!(PointerUseAfterFree(alloc_id)), } + .into()); }; if alloc.mutability == Mutability::Not { @@ -957,9 +954,9 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> { ptr: Pointer>, size: Size, ) -> InterpResult<'tcx, &[u8]> { - let alloc_ref = match self.get(ptr, size, Align::ONE)? { - Some(a) => a, - None => return Ok(&[]), // zero-sized access + let Some(alloc_ref) = self.get(ptr, size, Align::ONE)? else { + // zero-sized access + return Ok(&[]); }; // Side-step AllocRef and directly access the underlying bytes more efficiently. // (We are staying inside the bounds here so all is good.) @@ -983,17 +980,14 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> { assert_eq!(lower, len, "can only write iterators with a precise length"); let size = Size::from_bytes(len); - let alloc_ref = match self.get_mut(ptr, size, Align::ONE)? { - Some(alloc_ref) => alloc_ref, - None => { - // zero-sized access - assert_matches!( - src.next(), - None, - "iterator said it was empty but returned an element" - ); - return Ok(()); - } + let Some(alloc_ref) = self.get_mut(ptr, size, Align::ONE)? else { + // zero-sized access + assert_matches!( + src.next(), + None, + "iterator said it was empty but returned an element" + ); + return Ok(()); }; // Side-step AllocRef and directly access the underlying bytes more efficiently. @@ -1043,18 +1037,18 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> { // and once below to get the underlying `&[mut] Allocation`. // Source alloc preparations and access hooks. - let (src_alloc_id, src_offset, src) = match src_parts { - None => return Ok(()), // Zero-sized *source*, that means dst is also zero-sized and we have nothing to do. - Some(src_ptr) => src_ptr, + let Some((src_alloc_id, src_offset, src)) = src_parts else { + // Zero-sized *source*, that means dst is also zero-sized and we have nothing to do. + return Ok(()); }; let src_alloc = self.get_raw(src_alloc_id)?; let src_range = alloc_range(src_offset, size); M::memory_read(&self.extra, &src_alloc.extra, src.provenance, src_range)?; // We need the `dest` ptr for the next operation, so we get it now. // We already did the source checks and called the hooks so we are good to return early. - let (dest_alloc_id, dest_offset, dest) = match dest_parts { - None => return Ok(()), // Zero-sized *destiantion*. - Some(dest_ptr) => dest_ptr, + let Some((dest_alloc_id, dest_offset, dest)) = dest_parts else { + // Zero-sized *destiantion*. + return Ok(()); }; // This checks relocation edges on the src, which needs to happen before diff --git a/compiler/rustc_const_eval/src/interpret/operand.rs b/compiler/rustc_const_eval/src/interpret/operand.rs index ec5eafcd633..60e915a7eee 100644 --- a/compiler/rustc_const_eval/src/interpret/operand.rs +++ b/compiler/rustc_const_eval/src/interpret/operand.rs @@ -258,15 +258,12 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { return Ok(None); } - let alloc = match self.get_alloc(mplace)? { - Some(ptr) => ptr, - None => { - return Ok(Some(ImmTy { - // zero-sized type - imm: Scalar::ZST.into(), - layout: mplace.layout, - })); - } + let Some(alloc) = self.get_alloc(mplace)? else { + return Ok(Some(ImmTy { + // zero-sized type + imm: Scalar::ZST.into(), + layout: mplace.layout, + })); }; match mplace.layout.abi { diff --git a/compiler/rustc_const_eval/src/interpret/place.rs b/compiler/rustc_const_eval/src/interpret/place.rs index 7b06ffaf15d..e9b2df53a33 100644 --- a/compiler/rustc_const_eval/src/interpret/place.rs +++ b/compiler/rustc_const_eval/src/interpret/place.rs @@ -420,9 +420,8 @@ where ) -> InterpResult<'tcx, impl Iterator>> + 'a> { let len = base.len(self)?; // also asserts that we have a type where this makes sense - let stride = match base.layout.fields { - FieldsShape::Array { stride, .. } => stride, - _ => span_bug!(self.cur_span(), "mplace_array_fields: expected an array layout"), + let FieldsShape::Array { stride, .. } = base.layout.fields else { + span_bug!(self.cur_span(), "mplace_array_fields: expected an array layout"); }; let layout = base.layout.field(self, 0); let dl = &self.tcx.data_layout; @@ -747,9 +746,9 @@ where // Invalid places are a thing: the return place of a diverging function let tcx = *self.tcx; - let mut alloc = match self.get_alloc_mut(dest)? { - Some(a) => a, - None => return Ok(()), // zero-sized access + let Some(mut alloc) = self.get_alloc_mut(dest)? else { + // zero-sized access + return Ok(()); }; // FIXME: We should check that there are dest.layout.size many bytes available in diff --git a/compiler/rustc_const_eval/src/interpret/step.rs b/compiler/rustc_const_eval/src/interpret/step.rs index 57ba9b40992..0701e0ded97 100644 --- a/compiler/rustc_const_eval/src/interpret/step.rs +++ b/compiler/rustc_const_eval/src/interpret/step.rs @@ -46,15 +46,12 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { return Ok(false); } - let loc = match self.frame().loc { - Ok(loc) => loc, - Err(_) => { - // We are unwinding and this fn has no cleanup code. - // Just go on unwinding. - trace!("unwinding: skipping frame"); - self.pop_stack_frame(/* unwinding */ true)?; - return Ok(true); - } + let Ok(loc) = self.frame().loc else { + // We are unwinding and this fn has no cleanup code. + // Just go on unwinding. + trace!("unwinding: skipping frame"); + self.pop_stack_frame(/* unwinding */ true)?; + return Ok(true); }; let basic_block = &self.body().basic_blocks()[loc.block]; diff --git a/compiler/rustc_const_eval/src/interpret/terminator.rs b/compiler/rustc_const_eval/src/interpret/terminator.rs index f3910c9765d..8094bf0cf2e 100644 --- a/compiler/rustc_const_eval/src/interpret/terminator.rs +++ b/compiler/rustc_const_eval/src/interpret/terminator.rs @@ -321,10 +321,9 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { | ty::InstanceDef::CloneShim(..) | ty::InstanceDef::Item(_) => { // We need MIR for this fn - let (body, instance) = - match M::find_mir_or_eval_fn(self, instance, caller_abi, args, ret, unwind)? { - Some(body) => body, - None => return Ok(()), + let Some((body, instance)) = + M::find_mir_or_eval_fn(self, instance, caller_abi, args, ret, unwind)? else { + return Ok(()); }; // Compute callee information using the `instance` returned by diff --git a/compiler/rustc_const_eval/src/interpret/validity.rs b/compiler/rustc_const_eval/src/interpret/validity.rs index 4060bee7e05..c82594cf4a9 100644 --- a/compiler/rustc_const_eval/src/interpret/validity.rs +++ b/compiler/rustc_const_eval/src/interpret/validity.rs @@ -851,12 +851,9 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M> // to reject those pointers, we just do not have the machinery to // talk about parts of a pointer. // We also accept uninit, for consistency with the slow path. - let alloc = match self.ecx.memory.get(mplace.ptr, size, mplace.align)? { - Some(a) => a, - None => { - // Size 0, nothing more to check. - return Ok(()); - } + let Some(alloc) = self.ecx.memory.get(mplace.ptr, size, mplace.align)? else { + // Size 0, nothing more to check. + return Ok(()); }; let allow_uninit_and_ptr = !M::enforce_number_validity(self.ecx); -- cgit 1.4.1-3-g733a5 From c358ffe7b378923830937c2317efd5e0763b96e3 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sun, 20 Feb 2022 16:43:21 +0100 Subject: Implement LowerHex on Scalar to clean up their display in rustdoc --- compiler/rustc_const_eval/src/interpret/validity.rs | 2 +- compiler/rustc_middle/src/mir/interpret/error.rs | 2 +- compiler/rustc_middle/src/mir/interpret/value.rs | 18 +++++++++++------- src/librustdoc/clean/utils.rs | 6 +----- 4 files changed, 14 insertions(+), 14 deletions(-) (limited to 'compiler/rustc_const_eval/src/interpret') diff --git a/compiler/rustc_const_eval/src/interpret/validity.rs b/compiler/rustc_const_eval/src/interpret/validity.rs index 4060bee7e05..a41ed0b469b 100644 --- a/compiler/rustc_const_eval/src/interpret/validity.rs +++ b/compiler/rustc_const_eval/src/interpret/validity.rs @@ -697,7 +697,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M> this.ecx.read_discriminant(op), this.path, err_ub!(InvalidTag(val)) => - { "{}", val } expected { "a valid enum tag" }, + { "{:x}", val } expected { "a valid enum tag" }, err_ub!(InvalidUninitBytes(None)) => { "uninitialized bytes" } expected { "a valid enum tag" }, err_unsup!(ReadPointerAsBytes) => diff --git a/compiler/rustc_middle/src/mir/interpret/error.rs b/compiler/rustc_middle/src/mir/interpret/error.rs index e9a857d0912..c5866924eda 100644 --- a/compiler/rustc_middle/src/mir/interpret/error.rs +++ b/compiler/rustc_middle/src/mir/interpret/error.rs @@ -370,7 +370,7 @@ impl fmt::Display for UndefinedBehaviorInfo<'_> { InvalidChar(c) => { write!(f, "interpreting an invalid 32-bit value as a char: 0x{:08x}", c) } - InvalidTag(val) => write!(f, "enum value has invalid tag: {}", val), + InvalidTag(val) => write!(f, "enum value has invalid tag: {:x}", val), InvalidFunctionPointer(p) => { write!(f, "using {:?} as function pointer but it does not point to a function", p) } diff --git a/compiler/rustc_middle/src/mir/interpret/value.rs b/compiler/rustc_middle/src/mir/interpret/value.rs index aa8730bf9cd..abcf416109b 100644 --- a/compiler/rustc_middle/src/mir/interpret/value.rs +++ b/compiler/rustc_middle/src/mir/interpret/value.rs @@ -153,7 +153,16 @@ impl fmt::Display for Scalar { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Scalar::Ptr(ptr, _size) => write!(f, "pointer to {:?}", ptr), - Scalar::Int(int) => write!(f, "{:?}", int), + Scalar::Int(int) => write!(f, "{}", int), + } + } +} + +impl fmt::LowerHex for Scalar { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Scalar::Ptr(ptr, _size) => write!(f, "pointer to {:?}", ptr), + Scalar::Int(int) => write!(f, "0x{:x}", int), } } } @@ -456,11 +465,6 @@ impl<'tcx, Tag: Provenance> Scalar { // Going through `u64` to check size and truncation. Ok(Double::from_bits(self.to_u64()?.into())) } - - // FIXME: Replace current `impl Display for Scalar` with `impl LowerHex`. - pub fn rustdoc_display(&self) -> String { - if let Scalar::Int(int) = self { int.to_string() } else { self.to_string() } - } } #[derive(Clone, Copy, Eq, PartialEq, TyEncodable, TyDecodable, HashStable, Hash)] @@ -494,7 +498,7 @@ impl fmt::Display for ScalarMaybeUninit { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { ScalarMaybeUninit::Uninit => write!(f, "uninitialized bytes"), - ScalarMaybeUninit::Scalar(s) => write!(f, "{}", s), + ScalarMaybeUninit::Scalar(s) => write!(f, "{:x}", s), } } } diff --git a/src/librustdoc/clean/utils.rs b/src/librustdoc/clean/utils.rs index fe1992a5d7e..1d312df1f78 100644 --- a/src/librustdoc/clean/utils.rs +++ b/src/librustdoc/clean/utils.rs @@ -302,11 +302,7 @@ fn print_const_with_custom_print_scalar(tcx: TyCtxt<'_>, ct: ty::Const<'_>) -> S // For all other types, fallback to the original `pretty_print_const`. match (ct.val(), ct.ty().kind()) { (ty::ConstKind::Value(ConstValue::Scalar(int)), ty::Uint(ui)) => { - format!( - "{}{}", - format_integer_with_underscore_sep(&int.rustdoc_display()), - ui.name_str() - ) + format!("{}{}", format_integer_with_underscore_sep(&int.to_string()), ui.name_str()) } (ty::ConstKind::Value(ConstValue::Scalar(int)), ty::Int(i)) => { let ty = tcx.lift(ct.ty()).unwrap(); -- cgit 1.4.1-3-g733a5 From 413f3f787c9e8e4c2cb8abd73cdc16cb8d175590 Mon Sep 17 00:00:00 2001 From: est31 Date: Mon, 21 Feb 2022 08:28:20 +0100 Subject: Fix typo Co-authored-by: lcnr --- compiler/rustc_const_eval/src/interpret/memory.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'compiler/rustc_const_eval/src/interpret') diff --git a/compiler/rustc_const_eval/src/interpret/memory.rs b/compiler/rustc_const_eval/src/interpret/memory.rs index a1f94b095cf..73e7d862ad6 100644 --- a/compiler/rustc_const_eval/src/interpret/memory.rs +++ b/compiler/rustc_const_eval/src/interpret/memory.rs @@ -1047,7 +1047,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> { // We need the `dest` ptr for the next operation, so we get it now. // We already did the source checks and called the hooks so we are good to return early. let Some((dest_alloc_id, dest_offset, dest)) = dest_parts else { - // Zero-sized *destiantion*. + // Zero-sized *destination*. return Ok(()); }; -- cgit 1.4.1-3-g733a5