]> git.lizzy.rs Git - rust.git/commitdiff
turn casts-to-raw into a proper raw-reborrow; that is just cleaner
authorRalf Jung <post@ralfj.de>
Thu, 18 Oct 2018 09:59:56 +0000 (11:59 +0200)
committerRalf Jung <post@ralfj.de>
Thu, 18 Oct 2018 11:11:09 +0000 (13:11 +0200)
src/librustc_mir/interpret/cast.rs

index 62c42c4a49380e208e4fccc4aa28dd10457dfd46..ff07f44ac5fd386f21f79b0e7028f8060cdc6dc8 100644 (file)
@@ -37,7 +37,6 @@ pub fn cast(
         kind: CastKind,
         dest: PlaceTy<'tcx, M::PointerTag>,
     ) -> EvalResult<'tcx> {
-        let src_layout = src.layout;
         use rustc::mir::CastKind::*;
         match kind {
             Unsize => {
@@ -45,29 +44,28 @@ pub fn cast(
             }
 
             Misc => {
+                let src_layout = src.layout;
                 let src = self.read_value(src)?;
 
-                if M::ENABLE_PTR_TRACKING_HOOKS &&
-                    src.layout.ty.is_region_ptr() && dest.layout.ty.is_unsafe_ptr()
-                {
+                let src = if M::ENABLE_PTR_TRACKING_HOOKS && src_layout.ty.is_region_ptr() {
+                    // The only `Misc` casts on references are those creating raw pointers.
+                    assert!(dest.layout.ty.is_unsafe_ptr());
                     // For the purpose of the "ptr tag hooks", treat this as creating
                     // a new, raw reference.
                     let place = self.ref_to_mplace(src)?;
-                    let _val = self.create_ref(place, None)?;
-                    // FIXME: The blog post said we should now also erase the tag.
-                    // That would amount to using `_val` instead of `src` from here on.
-                    // However, do we really want to do that?  `transmute` doesn't
-                    // do it either and we have to support that, somehow.
-                }
+                    self.create_ref(place, None)?
+                } else {
+                    *src
+                };
 
-                if self.type_is_fat_ptr(src.layout.ty) {
-                    match (*src, self.type_is_fat_ptr(dest.layout.ty)) {
+                if self.type_is_fat_ptr(src_layout.ty) {
+                    match (src, self.type_is_fat_ptr(dest.layout.ty)) {
                         // pointers to extern types
                         (Value::Scalar(_),_) |
                         // slices and trait objects to other slices/trait objects
                         (Value::ScalarPair(..), true) => {
                             // No change to value
-                            self.write_value(*src, dest)?;
+                            self.write_value(src, dest)?;
                         }
                         // slices and trait objects to thin pointers (dropping the metadata)
                         (Value::ScalarPair(data, _), false) => {
@@ -79,7 +77,7 @@ pub fn cast(
                         layout::Variants::Single { index } => {
                             if let Some(def) = src_layout.ty.ty_adt_def() {
                                 // Cast from a univariant enum
-                                assert!(src.layout.is_zst());
+                                assert!(src_layout.is_zst());
                                 let discr_val = def
                                     .discriminant_for_variant(*self.tcx, index)
                                     .val;