From 49d5f5a977a1c0e0dffc8e189cef1977476f5253 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Wed, 6 May 2020 07:43:18 +0200 Subject: [PATCH 01/10] Add `len` and `slice_from_raw_parts` to `NonNull<[T]>` This follows the precedent of the recently-added `<*const [T]>::len` (adding to its tracking issue https://github.com/rust-lang/rust/issues/71146) and `ptr::slice_from_raw_parts`. --- src/libcore/lib.rs | 2 ++ src/libcore/ptr/non_null.rs | 59 +++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/src/libcore/lib.rs b/src/libcore/lib.rs index 3b7929f00168a..ca13433caec8d 100644 --- a/src/libcore/lib.rs +++ b/src/libcore/lib.rs @@ -87,6 +87,8 @@ #![feature(const_generics)] #![feature(const_ptr_offset_from)] #![feature(const_result)] +#![feature(const_slice_from_raw_parts)] +#![feature(const_slice_ptr_len)] #![feature(const_type_name)] #![feature(custom_inner_attributes)] #![feature(decl_macro)] diff --git a/src/libcore/ptr/non_null.rs b/src/libcore/ptr/non_null.rs index 7d08503215ed0..ed99452397f9c 100644 --- a/src/libcore/ptr/non_null.rs +++ b/src/libcore/ptr/non_null.rs @@ -142,6 +142,65 @@ impl NonNull { } } +impl NonNull<[T]> { + /// Create a non-null raw slice from a thin pointer and a length. + /// + /// The `len` argument is the number of **elements**, not the number of bytes. + /// + /// This function is safe, but dereferencing the return value is unsafe. + /// See the documentation of [`slice::from_raw_parts`] for slice safety requirements. + /// + /// [`slice::from_raw_parts`]: ../../std/slice/fn.from_raw_parts.html + /// + /// # Examples + /// + /// ```rust + /// #![feature(nonnull_slice_from_raw_parts)] + /// + /// use std::ptr::NonNull; + /// + /// // create a slice pointer when starting out with a pointer to the first element + /// let mut x = [5, 6, 7]; + /// let nonnull_pointer = NonNull::new(x.as_mut_ptr()).unwrap(); + /// let slice = NonNull::slice_from_raw_parts(nonnull_pointer, 3); + /// assert_eq!(unsafe { slice.as_ref()[2] }, 7); + /// ``` + /// + /// (Note that this example artifically demonstrates a use of this method, + /// but `let slice = NonNull::from(&x[..]);` would be a better way to write code like this.) + #[unstable(feature = "nonnull_slice_from_raw_parts", issue = "71941")] + #[rustc_const_unstable(feature = "const_nonnull_slice_from_raw_parts", issue = "71941")] + #[inline] + pub const fn slice_from_raw_parts(data: NonNull, size: usize) -> Self { + // SAFETY: `data` is a `NonNull` pointer which is necessarily non-null + unsafe { Self::new_unchecked(super::slice_from_raw_parts_mut(data.as_ptr(), size)) } + } + + /// Return the length and a non-null raw slice. + /// + /// The returned value is the number of **elements**, not the number of bytes. + /// + /// This function is safe, even when the non-null raw slice cannot be dereferenced to a slice + /// because the pointer does not have a valid address. + /// + /// # Examples + /// + /// ```rust + /// #![feature(slice_ptr_len, nonnull_slice_from_raw_parts)] + /// + /// use std::ptr::NonNull; + /// + /// let slice: NonNull<[i8]> = NonNull::slice_from_raw_parts(NonNull::dangling(), 3); + /// assert_eq!(slice.len(), 3); + /// ``` + #[unstable(feature = "slice_ptr_len", issue = "71146")] + #[rustc_const_unstable(feature = "const_slice_ptr_len", issue = "71146")] + #[inline] + pub const fn len(self) -> usize { + self.as_ptr().len() + } +} + #[stable(feature = "nonnull", since = "1.25.0")] impl Clone for NonNull { #[inline] From 861dfaa855ba61bdc8eaccc0652e6fdb8eadf4c3 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Thu, 7 May 2020 07:47:24 +0200 Subject: [PATCH 02/10] Apply suggestions from code review Co-authored-by: kennytm --- src/libcore/ptr/non_null.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libcore/ptr/non_null.rs b/src/libcore/ptr/non_null.rs index ed99452397f9c..870364a61dd47 100644 --- a/src/libcore/ptr/non_null.rs +++ b/src/libcore/ptr/non_null.rs @@ -143,7 +143,7 @@ impl NonNull { } impl NonNull<[T]> { - /// Create a non-null raw slice from a thin pointer and a length. + /// Creates a non-null raw slice from a thin pointer and a length. /// /// The `len` argument is the number of **elements**, not the number of bytes. /// @@ -171,12 +171,12 @@ impl NonNull<[T]> { #[unstable(feature = "nonnull_slice_from_raw_parts", issue = "71941")] #[rustc_const_unstable(feature = "const_nonnull_slice_from_raw_parts", issue = "71941")] #[inline] - pub const fn slice_from_raw_parts(data: NonNull, size: usize) -> Self { + pub const fn slice_from_raw_parts(data: NonNull, len: usize) -> Self { // SAFETY: `data` is a `NonNull` pointer which is necessarily non-null - unsafe { Self::new_unchecked(super::slice_from_raw_parts_mut(data.as_ptr(), size)) } + unsafe { Self::new_unchecked(super::slice_from_raw_parts_mut(data.as_ptr(), len)) } } - /// Return the length and a non-null raw slice. + /// Returns the length of a non-null raw slice. /// /// The returned value is the number of **elements**, not the number of bytes. /// From fbc6f2c70cf3d3cc3722cbd194e600d560d40758 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Tue, 14 Apr 2020 10:30:33 +0200 Subject: [PATCH 03/10] make some cast helpers infallible --- src/librustc_mir/interpret/cast.rs | 46 +++++++++++------------------- 1 file changed, 17 insertions(+), 29 deletions(-) diff --git a/src/librustc_mir/interpret/cast.rs b/src/librustc_mir/interpret/cast.rs index 0e01652bc9002..c1ba9ae83dbba 100644 --- a/src/librustc_mir/interpret/cast.rs +++ b/src/librustc_mir/interpret/cast.rs @@ -106,14 +106,10 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { match src.layout.ty.kind { // Floating point Float(FloatTy::F32) => { - return Ok(self - .cast_from_float(src.to_scalar()?.to_f32()?, dest_layout.ty)? - .into()); + return Ok(self.cast_from_float(src.to_scalar()?.to_f32()?, dest_layout.ty).into()); } Float(FloatTy::F64) => { - return Ok(self - .cast_from_float(src.to_scalar()?.to_f64()?, dest_layout.ty)? - .into()); + return Ok(self.cast_from_float(src.to_scalar()?.to_f64()?, dest_layout.ty).into()); } // The rest is integer/pointer-"like", including fn ptr casts and casts from enums that // are represented as integers. @@ -135,7 +131,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { assert!(src.layout.is_zst()); let discr_layout = self.layout_of(discr.ty)?; return Ok(self - .cast_from_int_like(discr.val, discr_layout, dest_layout)? + .cast_from_int_like(discr.val, discr_layout, dest_layout) .into()); } } @@ -173,15 +169,15 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { // (b) cast from an integer-like (including bool, char, enums). // In both cases we want the bits. let bits = self.force_bits(src.to_scalar()?, src.layout.size)?; - Ok(self.cast_from_int_like(bits, src.layout, dest_layout)?.into()) + Ok(self.cast_from_int_like(bits, src.layout, dest_layout).into()) } - fn cast_from_int_like( + pub(super) fn cast_from_int_like( &self, v: u128, // raw bits src_layout: TyAndLayout<'tcx>, dest_layout: TyAndLayout<'tcx>, - ) -> InterpResult<'tcx, Scalar> { + ) -> Scalar { // Let's make sure v is sign-extended *if* it has a signed type. let signed = src_layout.abi.is_signed(); let v = if signed { self.sign_extend(v, src_layout) } else { v }; @@ -190,21 +186,17 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { match dest_layout.ty.kind { Int(_) | Uint(_) | RawPtr(_) => { let v = self.truncate(v, dest_layout); - Ok(Scalar::from_uint(v, dest_layout.size)) + Scalar::from_uint(v, dest_layout.size) } - Float(FloatTy::F32) if signed => { - Ok(Scalar::from_f32(Single::from_i128(v as i128).value)) - } - Float(FloatTy::F64) if signed => { - Ok(Scalar::from_f64(Double::from_i128(v as i128).value)) - } - Float(FloatTy::F32) => Ok(Scalar::from_f32(Single::from_u128(v).value)), - Float(FloatTy::F64) => Ok(Scalar::from_f64(Double::from_u128(v).value)), + Float(FloatTy::F32) if signed => Scalar::from_f32(Single::from_i128(v as i128).value), + Float(FloatTy::F64) if signed => Scalar::from_f64(Double::from_i128(v as i128).value), + Float(FloatTy::F32) => Scalar::from_f32(Single::from_u128(v).value), + Float(FloatTy::F64) => Scalar::from_f64(Double::from_u128(v).value), Char => { // `u8` to `char` cast - Ok(Scalar::from_u32(u8::try_from(v).unwrap().into())) + Scalar::from_u32(u8::try_from(v).unwrap().into()) } // Casts to bool are not permitted by rustc, no need to handle them here. @@ -212,11 +204,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { } } - fn cast_from_float( - &self, - f: F, - dest_ty: Ty<'tcx>, - ) -> InterpResult<'tcx, Scalar> + fn cast_from_float(&self, f: F, dest_ty: Ty<'tcx>) -> Scalar where F: Float + Into> + FloatConvert + FloatConvert, { @@ -229,7 +217,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { // (https://doc.rust-lang.org/nightly/nightly-rustc/rustc_apfloat/trait.Float.html#method.to_i128_r). let v = f.to_u128(usize::try_from(width).unwrap()).value; // This should already fit the bit width - Ok(Scalar::from_uint(v, Size::from_bits(width))) + Scalar::from_uint(v, Size::from_bits(width)) } // float -> int Int(t) => { @@ -237,12 +225,12 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { // `to_i128` is a saturating cast, which is what we need // (https://doc.rust-lang.org/nightly/nightly-rustc/rustc_apfloat/trait.Float.html#method.to_i128_r). let v = f.to_i128(usize::try_from(width).unwrap()).value; - Ok(Scalar::from_int(v, Size::from_bits(width))) + Scalar::from_int(v, Size::from_bits(width)) } // float -> f32 - Float(FloatTy::F32) => Ok(Scalar::from_f32(f.convert(&mut false).value)), + Float(FloatTy::F32) => Scalar::from_f32(f.convert(&mut false).value), // float -> f64 - Float(FloatTy::F64) => Ok(Scalar::from_f64(f.convert(&mut false).value)), + Float(FloatTy::F64) => Scalar::from_f64(f.convert(&mut false).value), // That's it. _ => bug!("invalid float to {:?} cast", dest_ty), } From b97ed1a46c8b7531435f8728ad66ca1d3fa4bef4 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sun, 24 May 2020 09:38:29 +0200 Subject: [PATCH 04/10] Miri casts: do not blindly rely on dest type --- src/librustc_mir/interpret/cast.rs | 122 +++++++++++++++++------------ src/librustc_mir/interpret/step.rs | 5 +- 2 files changed, 76 insertions(+), 51 deletions(-) diff --git a/src/librustc_mir/interpret/cast.rs b/src/librustc_mir/interpret/cast.rs index c1ba9ae83dbba..18483c70951b4 100644 --- a/src/librustc_mir/interpret/cast.rs +++ b/src/librustc_mir/interpret/cast.rs @@ -1,6 +1,5 @@ use std::convert::TryFrom; -use super::{FnVal, ImmTy, Immediate, InterpCx, Machine, OpTy, PlaceTy}; use rustc_apfloat::ieee::{Double, Single}; use rustc_apfloat::{Float, FloatConvert}; use rustc_ast::ast::FloatTy; @@ -12,25 +11,40 @@ use rustc_middle::ty::{self, Ty, TypeAndMut, TypeFoldable}; use rustc_span::symbol::sym; use rustc_target::abi::{LayoutOf, Size, Variants}; +use super::{truncate, FnVal, ImmTy, Immediate, InterpCx, Machine, OpTy, PlaceTy}; + impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { pub fn cast( &mut self, src: OpTy<'tcx, M::PointerTag>, - kind: CastKind, + cast_kind: CastKind, + cast_ty: Ty<'tcx>, dest: PlaceTy<'tcx, M::PointerTag>, ) -> InterpResult<'tcx> { use rustc_middle::mir::CastKind::*; - match kind { + // FIXME: In which cases should we trigger UB when the source is uninit? + match cast_kind { Pointer(PointerCast::Unsize) => { + assert_eq!( + cast_ty, dest.layout.ty, + "mismatch of cast type {} and place type {}", + cast_ty, dest.layout.ty + ); self.unsize_into(src, dest)?; } - Misc | Pointer(PointerCast::MutToConstPointer | PointerCast::ArrayToPointer) => { + Misc => { let src = self.read_immediate(src)?; - let res = self.cast_immediate(src, dest.layout)?; + let res = self.misc_cast(src, cast_ty)?; self.write_immediate(res, dest)?; } + Pointer(PointerCast::MutToConstPointer | PointerCast::ArrayToPointer) => { + // These are NOPs, but can be wide pointers. + let v = self.read_immediate(src)?; + self.write_immediate(*v, dest)?; + } + Pointer(PointerCast::ReifyFnPointer) => { // The src operand does not matter, just its type match src.layout.ty.kind { @@ -61,12 +75,12 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { Pointer(PointerCast::UnsafeFnPointer) => { let src = self.read_immediate(src)?; - match dest.layout.ty.kind { + match cast_ty.kind { ty::FnPtr(_) => { // No change to value self.write_immediate(*src, dest)?; } - _ => bug!("fn to unsafe fn cast on {:?}", dest.layout.ty), + _ => bug!("fn to unsafe fn cast on {:?}", cast_ty), } } @@ -95,21 +109,21 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { Ok(()) } - fn cast_immediate( + fn misc_cast( &self, src: ImmTy<'tcx, M::PointerTag>, - dest_layout: TyAndLayout<'tcx>, + cast_ty: Ty<'tcx>, ) -> InterpResult<'tcx, Immediate> { use rustc_middle::ty::TyKind::*; - trace!("Casting {:?}: {:?} to {:?}", *src, src.layout.ty, dest_layout.ty); + trace!("Casting {:?}: {:?} to {:?}", *src, src.layout.ty, cast_ty); match src.layout.ty.kind { // Floating point Float(FloatTy::F32) => { - return Ok(self.cast_from_float(src.to_scalar()?.to_f32()?, dest_layout.ty).into()); + return Ok(self.cast_from_float(src.to_scalar()?.to_f32()?, cast_ty).into()); } Float(FloatTy::F64) => { - return Ok(self.cast_from_float(src.to_scalar()?.to_f64()?, dest_layout.ty).into()); + return Ok(self.cast_from_float(src.to_scalar()?.to_f64()?, cast_ty).into()); } // The rest is integer/pointer-"like", including fn ptr casts and casts from enums that // are represented as integers. @@ -124,69 +138,79 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { ), } + // # First handle non-scalar source values. + // Handle cast from a univariant (ZST) enum. match src.layout.variants { Variants::Single { index } => { if let Some(discr) = src.layout.ty.discriminant_for_variant(*self.tcx, index) { assert!(src.layout.is_zst()); let discr_layout = self.layout_of(discr.ty)?; - return Ok(self - .cast_from_int_like(discr.val, discr_layout, dest_layout) - .into()); + return Ok(self.cast_from_scalar(discr.val, discr_layout, cast_ty).into()); } } Variants::Multiple { .. } => {} } - // Handle casting the metadata away from a fat pointer. - if src.layout.ty.is_unsafe_ptr() - && dest_layout.ty.is_unsafe_ptr() - && dest_layout.size != src.layout.size - { - assert_eq!(src.layout.size, 2 * self.memory.pointer_size()); - assert_eq!(dest_layout.size, self.memory.pointer_size()); - assert!(dest_layout.ty.is_unsafe_ptr()); - match *src { - Immediate::ScalarPair(data, _) => return Ok(data.into()), - Immediate::Scalar(..) => bug!( - "{:?} input to a fat-to-thin cast ({:?} -> {:?})", - *src, - src.layout.ty, - dest_layout.ty - ), - }; - } - // Handle casting any ptr to raw ptr (might be a fat ptr). - if src.layout.ty.is_any_ptr() && dest_layout.ty.is_unsafe_ptr() { - // The only possible size-unequal case was handled above. - assert_eq!(src.layout.size, dest_layout.size); - return Ok(*src); + if src.layout.ty.is_any_ptr() && cast_ty.is_unsafe_ptr() { + let dest_layout = self.layout_of(cast_ty)?; + if dest_layout.size == src.layout.size { + // Thin or fat pointer that just hast the ptr kind of target type changed. + return Ok(*src); + } else { + // Casting the metadata away from a fat ptr. + assert_eq!(src.layout.size, 2 * self.memory.pointer_size()); + assert_eq!(dest_layout.size, self.memory.pointer_size()); + assert!(src.layout.ty.is_unsafe_ptr()); + return match *src { + Immediate::ScalarPair(data, _) => Ok(data.into()), + Immediate::Scalar(..) => bug!( + "{:?} input to a fat-to-thin cast ({:?} -> {:?})", + *src, + src.layout.ty, + cast_ty + ), + }; + } } + // # The remaining source values are scalar. + // For all remaining casts, we either // (a) cast a raw ptr to usize, or // (b) cast from an integer-like (including bool, char, enums). // In both cases we want the bits. let bits = self.force_bits(src.to_scalar()?, src.layout.size)?; - Ok(self.cast_from_int_like(bits, src.layout, dest_layout).into()) + Ok(self.cast_from_scalar(bits, src.layout, cast_ty).into()) } - pub(super) fn cast_from_int_like( + pub(super) fn cast_from_scalar( &self, - v: u128, // raw bits + v: u128, // raw bits (there is no ScalarTy so we separate data+layout) src_layout: TyAndLayout<'tcx>, - dest_layout: TyAndLayout<'tcx>, + cast_ty: Ty<'tcx>, ) -> Scalar { // Let's make sure v is sign-extended *if* it has a signed type. - let signed = src_layout.abi.is_signed(); + let signed = src_layout.abi.is_signed(); // also checks that abi is `Scalar`. let v = if signed { self.sign_extend(v, src_layout) } else { v }; - trace!("cast_from_int: {}, {}, {}", v, src_layout.ty, dest_layout.ty); + trace!("cast_from_scalar: {}, {} -> {}", v, src_layout.ty, cast_ty); use rustc_middle::ty::TyKind::*; - match dest_layout.ty.kind { + match cast_ty.kind { Int(_) | Uint(_) | RawPtr(_) => { - let v = self.truncate(v, dest_layout); - Scalar::from_uint(v, dest_layout.size) + let size = match cast_ty.kind { + // FIXME: Isn't there a helper for this? The same pattern occurs below. + Int(t) => { + t.bit_width().map(Size::from_bits).unwrap_or_else(|| self.pointer_size()) + } + Uint(t) => { + t.bit_width().map(Size::from_bits).unwrap_or_else(|| self.pointer_size()) + } + RawPtr(_) => self.pointer_size(), + _ => bug!(), + }; + let v = truncate(v, size); + Scalar::from_uint(v, size) } Float(FloatTy::F32) if signed => Scalar::from_f32(Single::from_i128(v as i128).value), @@ -200,7 +224,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { } // Casts to bool are not permitted by rustc, no need to handle them here. - _ => bug!("invalid int to {:?} cast", dest_layout.ty), + _ => bug!("invalid int to {:?} cast", cast_ty), } } @@ -283,7 +307,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { src: OpTy<'tcx, M::PointerTag>, dest: PlaceTy<'tcx, M::PointerTag>, ) -> InterpResult<'tcx> { - trace!("Unsizing {:?} into {:?}", src, dest); + trace!("Unsizing {:?} of type {} into {:?}", *src, src.layout.ty, dest.layout.ty); match (&src.layout.ty.kind, &dest.layout.ty.kind) { (&ty::Ref(_, s, _), &ty::Ref(_, d, _) | &ty::RawPtr(TypeAndMut { ty: d, .. })) | (&ty::RawPtr(TypeAndMut { ty: s, .. }), &ty::RawPtr(TypeAndMut { ty: d, .. })) => { diff --git a/src/librustc_mir/interpret/step.rs b/src/librustc_mir/interpret/step.rs index bb4c0156c88cf..fd9815975c19f 100644 --- a/src/librustc_mir/interpret/step.rs +++ b/src/librustc_mir/interpret/step.rs @@ -253,9 +253,10 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { self.write_scalar(Scalar::from_machine_usize(layout.size.bytes(), self), dest)?; } - Cast(kind, ref operand, _) => { + Cast(cast_kind, ref operand, cast_ty) => { let src = self.eval_operand(operand, None)?; - self.cast(src, kind, dest)?; + let cast_ty = self.subst_from_current_frame_and_normalize_erasing_regions(cast_ty); + self.cast(src, cast_kind, cast_ty, dest)?; } Discriminant(place) => { From b9b1554dac889b7294cd9945691652c632e158bc Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sun, 24 May 2020 15:10:15 +0200 Subject: [PATCH 05/10] Fix unsizing casts --- src/librustc_mir/interpret/cast.rs | 38 ++++++++++++++---------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/src/librustc_mir/interpret/cast.rs b/src/librustc_mir/interpret/cast.rs index 18483c70951b4..96b2e92292489 100644 --- a/src/librustc_mir/interpret/cast.rs +++ b/src/librustc_mir/interpret/cast.rs @@ -25,12 +25,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { // FIXME: In which cases should we trigger UB when the source is uninit? match cast_kind { Pointer(PointerCast::Unsize) => { - assert_eq!( - cast_ty, dest.layout.ty, - "mismatch of cast type {} and place type {}", - cast_ty, dest.layout.ty - ); - self.unsize_into(src, dest)?; + let cast_ty = self.layout_of(cast_ty)?; + self.unsize_into(src, cast_ty, dest)?; } Misc => { @@ -266,11 +262,11 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { dest: PlaceTy<'tcx, M::PointerTag>, // The pointee types source_ty: Ty<'tcx>, - dest_ty: Ty<'tcx>, + cast_ty: Ty<'tcx>, ) -> InterpResult<'tcx> { // A -> A conversion let (src_pointee_ty, dest_pointee_ty) = - self.tcx.struct_lockstep_tails_erasing_lifetimes(source_ty, dest_ty, self.param_env); + self.tcx.struct_lockstep_tails_erasing_lifetimes(source_ty, cast_ty, self.param_env); match (&src_pointee_ty.kind, &dest_pointee_ty.kind) { (&ty::Array(_, length), &ty::Slice(_)) => { @@ -298,32 +294,33 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { self.write_immediate(val, dest) } - _ => bug!("invalid unsizing {:?} -> {:?}", src.layout.ty, dest.layout.ty), + _ => bug!("invalid unsizing {:?} -> {:?}", src.layout.ty, cast_ty), } } fn unsize_into( &mut self, src: OpTy<'tcx, M::PointerTag>, + cast_ty: TyAndLayout<'tcx>, dest: PlaceTy<'tcx, M::PointerTag>, ) -> InterpResult<'tcx> { - trace!("Unsizing {:?} of type {} into {:?}", *src, src.layout.ty, dest.layout.ty); - match (&src.layout.ty.kind, &dest.layout.ty.kind) { - (&ty::Ref(_, s, _), &ty::Ref(_, d, _) | &ty::RawPtr(TypeAndMut { ty: d, .. })) - | (&ty::RawPtr(TypeAndMut { ty: s, .. }), &ty::RawPtr(TypeAndMut { ty: d, .. })) => { - self.unsize_into_ptr(src, dest, s, d) + trace!("Unsizing {:?} of type {} into {:?}", *src, src.layout.ty, cast_ty.ty); + match (&src.layout.ty.kind, &cast_ty.ty.kind) { + (&ty::Ref(_, s, _), &ty::Ref(_, c, _) | &ty::RawPtr(TypeAndMut { ty: c, .. })) + | (&ty::RawPtr(TypeAndMut { ty: s, .. }), &ty::RawPtr(TypeAndMut { ty: c, .. })) => { + self.unsize_into_ptr(src, dest, s, c) } (&ty::Adt(def_a, _), &ty::Adt(def_b, _)) => { assert_eq!(def_a, def_b); if def_a.is_box() || def_b.is_box() { if !def_a.is_box() || !def_b.is_box() { - bug!("invalid unsizing between {:?} -> {:?}", src.layout, dest.layout); + bug!("invalid unsizing between {:?} -> {:?}", src.layout.ty, cast_ty.ty); } return self.unsize_into_ptr( src, dest, src.layout.ty.boxed_ty(), - dest.layout.ty.boxed_ty(), + cast_ty.ty.boxed_ty(), ); } @@ -331,15 +328,16 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { // Example: `Arc` -> `Arc` // here we need to increase the size of every &T thin ptr field to a fat ptr for i in 0..src.layout.fields.count() { - let dst_field = self.place_field(dest, i)?; - if dst_field.layout.is_zst() { + let cast_ty_field = cast_ty.field(self, i)?; + if cast_ty_field.is_zst() { continue; } let src_field = self.operand_field(src, i)?; - if src_field.layout.ty == dst_field.layout.ty { + let dst_field = self.place_field(dest, i)?; + if src_field.layout.ty == cast_ty_field.ty { self.copy_op(src_field, dst_field)?; } else { - self.unsize_into(src_field, dst_field)?; + self.unsize_into(src_field, cast_ty_field, dst_field)?; } } Ok(()) From be2fd61d78b815f2d1cb09b0df5f06d73a089ac8 Mon Sep 17 00:00:00 2001 From: Amanieu d'Antras Date: Sun, 24 May 2020 14:26:20 +0100 Subject: [PATCH 06/10] Fix InlineAsmOperand expresions being visited twice during liveness checking --- src/librustc_passes/liveness.rs | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/librustc_passes/liveness.rs b/src/librustc_passes/liveness.rs index 21512c566e1c5..2d7562ac1c34d 100644 --- a/src/librustc_passes/liveness.rs +++ b/src/librustc_passes/liveness.rs @@ -1460,26 +1460,20 @@ fn check_expr<'tcx>(this: &mut Liveness<'_, 'tcx>, expr: &'tcx Expr<'tcx>) { hir::ExprKind::InlineAsm(ref asm) => { for op in asm.operands { match op { - hir::InlineAsmOperand::In { expr, .. } - | hir::InlineAsmOperand::Const { expr, .. } - | hir::InlineAsmOperand::Sym { expr, .. } => this.visit_expr(expr), hir::InlineAsmOperand::Out { expr, .. } => { if let Some(expr) = expr { this.check_place(expr); - this.visit_expr(expr); } } hir::InlineAsmOperand::InOut { expr, .. } => { this.check_place(expr); - this.visit_expr(expr); } - hir::InlineAsmOperand::SplitInOut { in_expr, out_expr, .. } => { - this.visit_expr(in_expr); + hir::InlineAsmOperand::SplitInOut { out_expr, .. } => { if let Some(out_expr) = out_expr { this.check_place(out_expr); - this.visit_expr(out_expr); } } + _ => {} } } } From 7b98552cc0f37e886ac3d9b911e0cf1173313c53 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sun, 24 May 2020 19:17:30 +0200 Subject: [PATCH 07/10] use helper method for determining size of int type --- src/librustc_mir/interpret/cast.rs | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/src/librustc_mir/interpret/cast.rs b/src/librustc_mir/interpret/cast.rs index 96b2e92292489..63777b4abde3d 100644 --- a/src/librustc_mir/interpret/cast.rs +++ b/src/librustc_mir/interpret/cast.rs @@ -3,13 +3,14 @@ use std::convert::TryFrom; use rustc_apfloat::ieee::{Double, Single}; use rustc_apfloat::{Float, FloatConvert}; use rustc_ast::ast::FloatTy; +use rustc_attr as attr; use rustc_middle::mir::interpret::{InterpResult, PointerArithmetic, Scalar}; use rustc_middle::mir::CastKind; use rustc_middle::ty::adjustment::PointerCast; -use rustc_middle::ty::layout::TyAndLayout; +use rustc_middle::ty::layout::{IntegerExt, TyAndLayout}; use rustc_middle::ty::{self, Ty, TypeAndMut, TypeFoldable}; use rustc_span::symbol::sym; -use rustc_target::abi::{LayoutOf, Size, Variants}; +use rustc_target::abi::{Integer, LayoutOf, Variants}; use super::{truncate, FnVal, ImmTy, Immediate, InterpCx, Machine, OpTy, PlaceTy}; @@ -195,13 +196,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { match cast_ty.kind { Int(_) | Uint(_) | RawPtr(_) => { let size = match cast_ty.kind { - // FIXME: Isn't there a helper for this? The same pattern occurs below. - Int(t) => { - t.bit_width().map(Size::from_bits).unwrap_or_else(|| self.pointer_size()) - } - Uint(t) => { - t.bit_width().map(Size::from_bits).unwrap_or_else(|| self.pointer_size()) - } + Int(t) => Integer::from_attr(self, attr::IntType::SignedInt(t)).size(), + Uint(t) => Integer::from_attr(self, attr::IntType::UnsignedInt(t)).size(), RawPtr(_) => self.pointer_size(), _ => bug!(), }; @@ -232,20 +228,20 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { match dest_ty.kind { // float -> uint Uint(t) => { - let width = t.bit_width().unwrap_or_else(|| self.pointer_size().bits()); + let size = Integer::from_attr(self, attr::IntType::UnsignedInt(t)).size(); // `to_u128` is a saturating cast, which is what we need // (https://doc.rust-lang.org/nightly/nightly-rustc/rustc_apfloat/trait.Float.html#method.to_i128_r). - let v = f.to_u128(usize::try_from(width).unwrap()).value; + let v = f.to_u128(size.bits_usize()).value; // This should already fit the bit width - Scalar::from_uint(v, Size::from_bits(width)) + Scalar::from_uint(v, size) } // float -> int Int(t) => { - let width = t.bit_width().unwrap_or_else(|| self.pointer_size().bits()); + let size = Integer::from_attr(self, attr::IntType::SignedInt(t)).size(); // `to_i128` is a saturating cast, which is what we need // (https://doc.rust-lang.org/nightly/nightly-rustc/rustc_apfloat/trait.Float.html#method.to_i128_r). - let v = f.to_i128(usize::try_from(width).unwrap()).value; - Scalar::from_int(v, Size::from_bits(width)) + let v = f.to_i128(size.bits_usize()).value; + Scalar::from_int(v, size) } // float -> f32 Float(FloatTy::F32) => Scalar::from_f32(f.convert(&mut false).value), From 8b5ba4a3c62e9f663d5f8db1dc4fff245d291c4b Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sun, 24 May 2020 19:28:44 +0200 Subject: [PATCH 08/10] comment nit --- src/librustc_mir/interpret/cast.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustc_mir/interpret/cast.rs b/src/librustc_mir/interpret/cast.rs index 63777b4abde3d..0fd695586eb98 100644 --- a/src/librustc_mir/interpret/cast.rs +++ b/src/librustc_mir/interpret/cast.rs @@ -189,7 +189,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { cast_ty: Ty<'tcx>, ) -> Scalar { // Let's make sure v is sign-extended *if* it has a signed type. - let signed = src_layout.abi.is_signed(); // also checks that abi is `Scalar`. + let signed = src_layout.abi.is_signed(); // Also asserts that abi is `Scalar`. let v = if signed { self.sign_extend(v, src_layout) } else { v }; trace!("cast_from_scalar: {}, {} -> {}", v, src_layout.ty, cast_ty); use rustc_middle::ty::TyKind::*; From c3edb15f36653050070b0959682c2d02c2f49bce Mon Sep 17 00:00:00 2001 From: Aman Arora Date: Sat, 23 May 2020 19:29:49 -0400 Subject: [PATCH 09/10] librustc_middle: Rename upvars query to upvars_mentioned As part of supporting RFC 2229, we will be capturing all the Places that were mentioned in the closure. This commit modifies the name of the upvars query to upvars_mentioned. Co-authored-by: Aman Arora Co-authored-by: Chris Pardy --- src/librustc_middle/arena.rs | 2 +- src/librustc_middle/mir/mod.rs | 4 ++-- src/librustc_middle/query/mod.rs | 2 +- src/librustc_middle/ty/print/pretty.rs | 4 ++-- .../borrow_check/diagnostics/mod.rs | 13 ++++++---- src/librustc_mir_build/hair/cx/expr.rs | 2 +- src/librustc_passes/liveness.rs | 10 ++++---- src/librustc_passes/upvars.rs | 4 ++-- .../traits/error_reporting/suggestions.rs | 2 +- src/librustc_typeck/check/closure.rs | 24 ++++++++++--------- src/librustc_typeck/check/upvar.rs | 4 ++-- src/librustc_typeck/expr_use_visitor.rs | 2 +- src/librustc_typeck/mem_categorization.rs | 2 +- 13 files changed, 41 insertions(+), 34 deletions(-) diff --git a/src/librustc_middle/arena.rs b/src/librustc_middle/arena.rs index 2df878c3fb220..9b9207312e8dd 100644 --- a/src/librustc_middle/arena.rs +++ b/src/librustc_middle/arena.rs @@ -61,7 +61,7 @@ macro_rules! arena_types { [few] privacy_access_levels: rustc_middle::middle::privacy::AccessLevels, [few] foreign_module: rustc_middle::middle::cstore::ForeignModule, [few] foreign_modules: Vec, - [] upvars: rustc_data_structures::fx::FxIndexMap, + [] upvars_mentioned: rustc_data_structures::fx::FxIndexMap, [] object_safety_violations: rustc_middle::traits::ObjectSafetyViolation, [] codegen_unit: rustc_middle::mir::mono::CodegenUnit<$tcx>, [] attribute: rustc_ast::ast::Attribute, diff --git a/src/librustc_middle/mir/mod.rs b/src/librustc_middle/mir/mod.rs index 8247338ae0fad..c279213e5bd0e 100644 --- a/src/librustc_middle/mir/mod.rs +++ b/src/librustc_middle/mir/mod.rs @@ -2439,7 +2439,7 @@ impl<'tcx> Debug for Rvalue<'tcx> { }; let mut struct_fmt = fmt.debug_struct(&name); - if let Some(upvars) = tcx.upvars(def_id) { + if let Some(upvars) = tcx.upvars_mentioned(def_id) { for (&var_id, place) in upvars.keys().zip(places) { let var_name = tcx.hir().name(var_id); struct_fmt.field(&var_name.as_str(), place); @@ -2458,7 +2458,7 @@ impl<'tcx> Debug for Rvalue<'tcx> { let name = format!("[generator@{:?}]", tcx.hir().span(hir_id)); let mut struct_fmt = fmt.debug_struct(&name); - if let Some(upvars) = tcx.upvars(def_id) { + if let Some(upvars) = tcx.upvars_mentioned(def_id) { for (&var_id, place) in upvars.keys().zip(places) { let var_name = tcx.hir().name(var_id); struct_fmt.field(&var_name.as_str(), place); diff --git a/src/librustc_middle/query/mod.rs b/src/librustc_middle/query/mod.rs index f7f5c5df8d67b..2445d484754d0 100644 --- a/src/librustc_middle/query/mod.rs +++ b/src/librustc_middle/query/mod.rs @@ -1040,7 +1040,7 @@ rustc_queries! { desc { "generating a postorder list of CrateNums" } } - query upvars(_: DefId) -> Option<&'tcx FxIndexMap> { + query upvars_mentioned(_: DefId) -> Option<&'tcx FxIndexMap> { eval_always } query maybe_unused_trait_import(def_id: LocalDefId) -> bool { diff --git a/src/librustc_middle/ty/print/pretty.rs b/src/librustc_middle/ty/print/pretty.rs index f4b795e548867..7948fe12724e6 100644 --- a/src/librustc_middle/ty/print/pretty.rs +++ b/src/librustc_middle/ty/print/pretty.rs @@ -611,7 +611,7 @@ pub trait PrettyPrinter<'tcx>: let mut sep = " "; for (&var_id, upvar_ty) in self .tcx() - .upvars(did) + .upvars_mentioned(did) .as_ref() .iter() .flat_map(|v| v.keys()) @@ -660,7 +660,7 @@ pub trait PrettyPrinter<'tcx>: let mut sep = " "; for (&var_id, upvar_ty) in self .tcx() - .upvars(did) + .upvars_mentioned(did) .as_ref() .iter() .flat_map(|v| v.keys()) diff --git a/src/librustc_mir/borrow_check/diagnostics/mod.rs b/src/librustc_mir/borrow_check/diagnostics/mod.rs index c218e3906fff2..ca8e54ea28649 100644 --- a/src/librustc_mir/borrow_check/diagnostics/mod.rs +++ b/src/librustc_mir/borrow_check/diagnostics/mod.rs @@ -377,11 +377,16 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { self.describe_field_from_ty(&ty, field, variant_index) } ty::Closure(def_id, _) | ty::Generator(def_id, _, _) => { - // `tcx.upvars(def_id)` returns an `Option`, which is `None` in case + // `tcx.upvars_mentioned(def_id)` returns an `Option`, which is `None` in case // the closure comes from another crate. But in that case we wouldn't // be borrowck'ing it, so we can just unwrap: - let (&var_id, _) = - self.infcx.tcx.upvars(def_id).unwrap().get_index(field.index()).unwrap(); + let (&var_id, _) = self + .infcx + .tcx + .upvars_mentioned(def_id) + .unwrap() + .get_index(field.index()) + .unwrap(); self.infcx.tcx.hir().name(var_id).to_string() } @@ -809,7 +814,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { let expr = &self.infcx.tcx.hir().expect_expr(hir_id).kind; debug!("closure_span: hir_id={:?} expr={:?}", hir_id, expr); if let hir::ExprKind::Closure(.., body_id, args_span, _) = expr { - for (upvar, place) in self.infcx.tcx.upvars(def_id)?.values().zip(places) { + for (upvar, place) in self.infcx.tcx.upvars_mentioned(def_id)?.values().zip(places) { match place { Operand::Copy(place) | Operand::Move(place) if target_place == place.as_ref() => diff --git a/src/librustc_mir_build/hair/cx/expr.rs b/src/librustc_mir_build/hair/cx/expr.rs index 99b59d16029ff..114bf5710402f 100644 --- a/src/librustc_mir_build/hair/cx/expr.rs +++ b/src/librustc_mir_build/hair/cx/expr.rs @@ -386,7 +386,7 @@ fn make_mirror_unadjusted<'a, 'tcx>( }; let upvars = cx .tcx - .upvars(def_id) + .upvars_mentioned(def_id) .iter() .flat_map(|upvars| upvars.iter()) .zip(substs.upvar_tys()) diff --git a/src/librustc_passes/liveness.rs b/src/librustc_passes/liveness.rs index 21512c566e1c5..533d96d28c0b1 100644 --- a/src/librustc_passes/liveness.rs +++ b/src/librustc_passes/liveness.rs @@ -463,7 +463,7 @@ fn visit_expr<'tcx>(ir: &mut IrMaps<'tcx>, expr: &'tcx Expr<'tcx>) { hir::ExprKind::Path(hir::QPath::Resolved(_, ref path)) => { debug!("expr {}: path that leads to {:?}", expr.hir_id, path.res); if let Res::Local(var_hir_id) = path.res { - let upvars = ir.tcx.upvars(ir.body_owner); + let upvars = ir.tcx.upvars_mentioned(ir.body_owner); if !upvars.map_or(false, |upvars| upvars.contains_key(&var_hir_id)) { ir.add_live_node_for_node(expr.hir_id, ExprNode(expr.span)); } @@ -481,8 +481,8 @@ fn visit_expr<'tcx>(ir: &mut IrMaps<'tcx>, expr: &'tcx Expr<'tcx>) { // construction site. let mut call_caps = Vec::new(); let closure_def_id = ir.tcx.hir().local_def_id(expr.hir_id); - if let Some(upvars) = ir.tcx.upvars(closure_def_id) { - let parent_upvars = ir.tcx.upvars(ir.body_owner); + if let Some(upvars) = ir.tcx.upvars_mentioned(closure_def_id) { + let parent_upvars = ir.tcx.upvars_mentioned(ir.body_owner); call_caps.extend(upvars.iter().filter_map(|(&var_id, upvar)| { let has_parent = parent_upvars.map_or(false, |upvars| upvars.contains_key(&var_id)); @@ -1364,7 +1364,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> { ) -> LiveNode { match path.res { Res::Local(hid) => { - let upvars = self.ir.tcx.upvars(self.ir.body_owner); + let upvars = self.ir.tcx.upvars_mentioned(self.ir.body_owner); if !upvars.map_or(false, |upvars| upvars.contains_key(&hid)) { self.access_var(hir_id, hid, succ, acc, path.span) } else { @@ -1535,7 +1535,7 @@ impl<'tcx> Liveness<'_, 'tcx> { match expr.kind { hir::ExprKind::Path(hir::QPath::Resolved(_, ref path)) => { if let Res::Local(var_hid) = path.res { - let upvars = self.ir.tcx.upvars(self.ir.body_owner); + let upvars = self.ir.tcx.upvars_mentioned(self.ir.body_owner); if !upvars.map_or(false, |upvars| upvars.contains_key(&var_hid)) { // Assignment to an immutable variable or argument: only legal // if there is no later assignment. If this local is actually diff --git a/src/librustc_passes/upvars.rs b/src/librustc_passes/upvars.rs index fb986caa415c9..99b4ef9d12fcd 100644 --- a/src/librustc_passes/upvars.rs +++ b/src/librustc_passes/upvars.rs @@ -10,7 +10,7 @@ use rustc_middle::ty::TyCtxt; use rustc_span::Span; pub fn provide(providers: &mut Providers<'_>) { - providers.upvars = |tcx, def_id| { + providers.upvars_mentioned = |tcx, def_id| { if !tcx.is_closure(def_id) { return None; } @@ -89,7 +89,7 @@ impl Visitor<'tcx> for CaptureCollector<'a, 'tcx> { fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) { if let hir::ExprKind::Closure(..) = expr.kind { let closure_def_id = self.tcx.hir().local_def_id(expr.hir_id); - if let Some(upvars) = self.tcx.upvars(closure_def_id) { + if let Some(upvars) = self.tcx.upvars_mentioned(closure_def_id) { // Every capture of a closure expression is a local in scope, // that is moved/copied/borrowed into the closure value, and // for this analysis they are like any other access to a local. diff --git a/src/librustc_trait_selection/traits/error_reporting/suggestions.rs b/src/librustc_trait_selection/traits/error_reporting/suggestions.rs index 5c85855535e38..31992f298080e 100644 --- a/src/librustc_trait_selection/traits/error_reporting/suggestions.rs +++ b/src/librustc_trait_selection/traits/error_reporting/suggestions.rs @@ -1380,7 +1380,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { let mut interior_or_upvar_span = None; let mut interior_extra_info = None; - if let Some(upvars) = self.tcx.upvars(generator_did) { + if let Some(upvars) = self.tcx.upvars_mentioned(generator_did) { interior_or_upvar_span = upvars.iter().find_map(|(upvar_id, upvar)| { let upvar_ty = tables.node_type(*upvar_id); let upvar_ty = self.resolve_vars_if_possible(&upvar_ty); diff --git a/src/librustc_typeck/check/closure.rs b/src/librustc_typeck/check/closure.rs index f393121a0adb8..a196081ef7279 100644 --- a/src/librustc_typeck/check/closure.rs +++ b/src/librustc_typeck/check/closure.rs @@ -92,18 +92,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { base_substs.extend_to(self.tcx, expr_def_id.to_def_id(), |param, _| match param.kind { GenericParamDefKind::Lifetime => span_bug!(expr.span, "closure has lifetime param"), GenericParamDefKind::Type { .. } => if param.index as usize == tupled_upvars_idx { - self.tcx.mk_tup(self.tcx.upvars(expr_def_id).iter().flat_map(|upvars| { - upvars.iter().map(|(&var_hir_id, _)| { - // Create type variables (for now) to represent the transformed - // types of upvars. These will be unified during the upvar - // inference phase (`upvar.rs`). - self.infcx.next_ty_var(TypeVariableOrigin { - // FIXME(eddyb) distinguish upvar inference variables from the rest. - kind: TypeVariableOriginKind::ClosureSynthetic, - span: self.tcx.hir().span(var_hir_id), + self.tcx.mk_tup(self.tcx.upvars_mentioned(expr_def_id).iter().flat_map( + |upvars| { + upvars.iter().map(|(&var_hir_id, _)| { + // Create type variables (for now) to represent the transformed + // types of upvars. These will be unified during the upvar + // inference phase (`upvar.rs`). + self.infcx.next_ty_var(TypeVariableOrigin { + // FIXME(eddyb) distinguish upvar inference variables from the rest. + kind: TypeVariableOriginKind::ClosureSynthetic, + span: self.tcx.hir().span(var_hir_id), + }) }) - }) - })) + }, + )) } else { // Create type variables (for now) to represent the various // pieces of information kept in `{Closure,Generic}Substs`. diff --git a/src/librustc_typeck/check/upvar.rs b/src/librustc_typeck/check/upvar.rs index 6aa8242193d5f..8707e4fe84a22 100644 --- a/src/librustc_typeck/check/upvar.rs +++ b/src/librustc_typeck/check/upvar.rs @@ -111,7 +111,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { None }; - if let Some(upvars) = self.tcx.upvars(closure_def_id) { + if let Some(upvars) = self.tcx.upvars_mentioned(closure_def_id) { let mut upvar_list: FxIndexMap = FxIndexMap::with_capacity_and_hasher(upvars.len(), Default::default()); for (&var_hir_id, _) in upvars.iter() { @@ -218,7 +218,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let tcx = self.tcx; let closure_def_id = tcx.hir().local_def_id(closure_id); - tcx.upvars(closure_def_id) + tcx.upvars_mentioned(closure_def_id) .iter() .flat_map(|upvars| { upvars.iter().map(|(&var_hir_id, _)| { diff --git a/src/librustc_typeck/expr_use_visitor.rs b/src/librustc_typeck/expr_use_visitor.rs index 9ba00faec4978..53973eba22940 100644 --- a/src/librustc_typeck/expr_use_visitor.rs +++ b/src/librustc_typeck/expr_use_visitor.rs @@ -539,7 +539,7 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> { debug!("walk_captures({:?})", closure_expr); let closure_def_id = self.tcx().hir().local_def_id(closure_expr.hir_id); - if let Some(upvars) = self.tcx().upvars(closure_def_id) { + if let Some(upvars) = self.tcx().upvars_mentioned(closure_def_id) { for &var_id in upvars.keys() { let upvar_id = ty::UpvarId { var_path: ty::UpvarPath { hir_id: var_id }, diff --git a/src/librustc_typeck/mem_categorization.rs b/src/librustc_typeck/mem_categorization.rs index 71f3e2d03c9ff..93d01ccd66f1e 100644 --- a/src/librustc_typeck/mem_categorization.rs +++ b/src/librustc_typeck/mem_categorization.rs @@ -159,7 +159,7 @@ impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx> { infcx, param_env, body_owner, - upvars: infcx.tcx.upvars(body_owner), + upvars: infcx.tcx.upvars_mentioned(body_owner), } } From 698df11af56f1326bb6509e8c1329b463b0afc9c Mon Sep 17 00:00:00 2001 From: Andrew Lilley Brinker Date: Sun, 24 May 2020 15:30:48 -0700 Subject: [PATCH 10/10] First draft documenting Debug stability. Debug implementations of std types aren't stable, and neither are derived Debug implementations for any types, including user-defined types. This commit adds a section to the Debug documentatio noting this stability status. --- src/libcore/fmt/mod.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/libcore/fmt/mod.rs b/src/libcore/fmt/mod.rs index 95411b525d0db..3d90fe1fa2f21 100644 --- a/src/libcore/fmt/mod.rs +++ b/src/libcore/fmt/mod.rs @@ -441,6 +441,13 @@ impl Display for Arguments<'_> { /// `enum`s, it will use the name of the variant and, if applicable, `(`, then the /// `Debug` values of the fields, then `)`. /// +/// # Stability +/// +/// Derived `Debug` formats are not stable, and so may change with future Rust +/// versions. Additionally, `Debug` implementations of types provided by the +/// standard library (`libstd`, `libcore`, `liballoc`, etc.) are not stable, and +/// may also change with future Rust versions. +/// /// # Examples /// /// Deriving an implementation: