From e47d6c7a6b643e836455d4ebf5e7fb1eaad5c870 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Thu, 5 May 2022 09:55:38 +0200 Subject: [PATCH] give it a scary name --- .../rustc_const_eval/src/interpret/operand.rs | 17 ++++++++++------- .../rustc_const_eval/src/interpret/place.rs | 4 ++-- .../rustc_const_eval/src/interpret/validity.rs | 2 +- compiler/rustc_mir_transform/src/const_prop.rs | 6 +++--- .../rustc_mir_transform/src/const_prop_lint.rs | 2 +- 5 files changed, 17 insertions(+), 14 deletions(-) diff --git a/compiler/rustc_const_eval/src/interpret/operand.rs b/compiler/rustc_const_eval/src/interpret/operand.rs index 494ce3787e3..a8a5ac2f9d9 100644 --- a/compiler/rustc_const_eval/src/interpret/operand.rs +++ b/compiler/rustc_const_eval/src/interpret/operand.rs @@ -252,7 +252,9 @@ pub fn to_const_int(self) -> ConstInt { impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { /// Try reading an immediate in memory; this is interesting particularly for `ScalarPair`. /// Returns `None` if the layout does not permit loading this as a value. - fn try_read_immediate_from_mplace( + /// + /// This is an internal function; call `read_immediate` instead. + fn read_immediate_from_mplace_raw( &self, mplace: &MPlaceTy<'tcx, M::PointerTag>, force: bool, @@ -312,9 +314,8 @@ fn try_read_immediate_from_mplace( return Ok(None); } - /// Try returning an immediate for the operand. - /// If the layout does not permit loading this as an immediate, return where in memory - /// we can find the data. + /// Try returning an immediate for the operand. If the layout does not permit loading this as an + /// immediate, return where in memory we can find the data. /// Note that for a given layout, this operation will either always fail or always /// succeed! Whether it succeeds depends on whether the layout can be represented /// in an `Immediate`, not on which data is stored there currently. @@ -322,14 +323,16 @@ fn try_read_immediate_from_mplace( /// If `force` is `true`, then even scalars with fields that can be ununit will be /// read. This means the load is lossy and should not be written back! /// This flag exists only for validity checking. - pub fn try_read_immediate( + /// + /// This is an internal function that should not usually be used; call `read_immediate` instead. + pub fn read_immediate_raw( &self, src: &OpTy<'tcx, M::PointerTag>, force: bool, ) -> InterpResult<'tcx, Result, MPlaceTy<'tcx, M::PointerTag>>> { Ok(match src.try_as_mplace() { Ok(ref mplace) => { - if let Some(val) = self.try_read_immediate_from_mplace(mplace, force)? { + if let Some(val) = self.read_immediate_from_mplace_raw(mplace, force)? { Ok(val) } else { Err(*mplace) @@ -345,7 +348,7 @@ pub fn read_immediate( &self, op: &OpTy<'tcx, M::PointerTag>, ) -> InterpResult<'tcx, ImmTy<'tcx, M::PointerTag>> { - if let Ok(imm) = self.try_read_immediate(op, /*force*/ false)? { + if let Ok(imm) = self.read_immediate_raw(op, /*force*/ false)? { Ok(imm) } else { span_bug!(self.cur_span(), "primitive read failed for type: {:?}", op.layout.ty); diff --git a/compiler/rustc_const_eval/src/interpret/place.rs b/compiler/rustc_const_eval/src/interpret/place.rs index 4cc4080c27a..df6e05bb13c 100644 --- a/compiler/rustc_const_eval/src/interpret/place.rs +++ b/compiler/rustc_const_eval/src/interpret/place.rs @@ -720,7 +720,7 @@ fn write_immediate_no_validate( } trace!("write_immediate: {:?} <- {:?}: {}", *dest, src, dest.layout.ty); - // See if we can avoid an allocation. This is the counterpart to `try_read_immediate`, + // See if we can avoid an allocation. This is the counterpart to `read_immediate_raw`, // but not factored as a separate function. let mplace = match dest.place { Place::Local { frame, local } => { @@ -879,7 +879,7 @@ fn copy_op_no_validate( } // Let us see if the layout is simple so we take a shortcut, avoid force_allocation. - let src = match self.try_read_immediate(src, /*force*/ false)? { + let src = match self.read_immediate_raw(src, /*force*/ false)? { Ok(src_val) => { assert!(!src.layout.is_unsized(), "cannot have unsized immediates"); // Yay, we got a value that we can write directly. diff --git a/compiler/rustc_const_eval/src/interpret/validity.rs b/compiler/rustc_const_eval/src/interpret/validity.rs index c2782db3221..92e3ac04dc4 100644 --- a/compiler/rustc_const_eval/src/interpret/validity.rs +++ b/compiler/rustc_const_eval/src/interpret/validity.rs @@ -492,7 +492,7 @@ fn read_immediate_forced( op: &OpTy<'tcx, M::PointerTag>, ) -> InterpResult<'tcx, Immediate> { Ok(*try_validation!( - self.ecx.try_read_immediate(op, /*force*/ true), + self.ecx.read_immediate_raw(op, /*force*/ true), self.path, err_unsup!(ReadPointerAsBytes) => { "(potentially part of) a pointer" } expected { "plain (non-pointer) bytes" }, ).unwrap()) diff --git a/compiler/rustc_mir_transform/src/const_prop.rs b/compiler/rustc_mir_transform/src/const_prop.rs index 3fdfa53289b..f7535d338da 100644 --- a/compiler/rustc_mir_transform/src/const_prop.rs +++ b/compiler/rustc_mir_transform/src/const_prop.rs @@ -415,7 +415,7 @@ fn get_const(&self, place: Place<'tcx>) -> Option> { // Try to read the local as an immediate so that if it is representable as a scalar, we can // handle it as such, but otherwise, just return the value as is. - Some(match self.ecx.try_read_immediate(&op, /*force*/ false) { + Some(match self.ecx.read_immediate_raw(&op, /*force*/ false) { Ok(Ok(imm)) => imm.into(), _ => op, }) @@ -709,8 +709,8 @@ fn replace_with_const( return; } - // FIXME> figure out what to do when try_read_immediate fails - let imm = self.use_ecx(|this| this.ecx.try_read_immediate(value, /*force*/ false)); + // FIXME> figure out what to do when read_immediate_raw fails + let imm = self.use_ecx(|this| this.ecx.read_immediate_raw(value, /*force*/ false)); if let Some(Ok(imm)) = imm { match *imm { diff --git a/compiler/rustc_mir_transform/src/const_prop_lint.rs b/compiler/rustc_mir_transform/src/const_prop_lint.rs index 2bc27873ac1..aa898cfd3ba 100644 --- a/compiler/rustc_mir_transform/src/const_prop_lint.rs +++ b/compiler/rustc_mir_transform/src/const_prop_lint.rs @@ -412,7 +412,7 @@ fn get_const(&self, place: Place<'tcx>) -> Option> { // Try to read the local as an immediate so that if it is representable as a scalar, we can // handle it as such, but otherwise, just return the value as is. - Some(match self.ecx.try_read_immediate(&op, /*force*/ false) { + Some(match self.ecx.read_immediate_raw(&op, /*force*/ false) { Ok(Ok(imm)) => imm.into(), _ => op, })