]> git.lizzy.rs Git - rust.git/commitdiff
basic refactor. Adding PointerCast enum
authorSaleem Jaffer <saleem@acko.com>
Mon, 15 Apr 2019 13:50:44 +0000 (19:20 +0530)
committerSaleem Jaffer <saleem@acko.com>
Mon, 15 Apr 2019 14:59:13 +0000 (20:29 +0530)
src/librustc/mir/mod.rs
src/librustc/ty/adjustment.rs
src/librustc_codegen_ssa/mir/rvalue.rs
src/librustc_mir/borrow_check/nll/explain_borrow/mod.rs
src/librustc_mir/borrow_check/nll/type_check/mod.rs
src/librustc_mir/build/expr/as_rvalue.rs
src/librustc_mir/build/matches/test.rs
src/librustc_mir/interpret/cast.rs
src/librustc_mir/monomorphize/collector.rs
src/librustc_mir/transform/qualify_consts.rs
src/librustc_mir/transform/qualify_min_const_fn.rs

index 8424c096e88c019d2907d05436dcfd12c07bdc7f..22c0f0e5c3945869918bce9e092ebaf089ad762a 100644 (file)
@@ -35,6 +35,7 @@
     UserTypeAnnotationIndex,
 };
 use crate::ty::print::{FmtPrinter, Printer};
+use crate::ty::adjustment::{PointerCast};
 
 pub use crate::mir::interpret::AssertMessage;
 
@@ -2248,29 +2249,11 @@ pub enum Rvalue<'tcx> {
     Aggregate(Box<AggregateKind<'tcx>>, Vec<Operand<'tcx>>),
 }
 
+
 #[derive(Clone, Copy, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable, HashStable)]
 pub enum CastKind {
     Misc,
-
-    /// Converts unique, zero-sized type for a fn to fn()
-    ReifyFnPointer,
-
-    /// Converts non capturing closure to fn() or unsafe fn().
-    /// It cannot convert a closure that requires unsafe.
-    ClosureFnPointer(hir::Unsafety),
-
-    /// Converts safe fn() to unsafe fn()
-    UnsafeFnPointer,
-
-    /// Coerces *mut T to *const T, preserving T.
-    MutToConstPointer,
-
-    /// "Unsize" -- convert a thin-or-fat pointer to a fat pointer.
-    /// codegen must figure out the details once full monomorphization
-    /// is known. For example, this could be used to cast from a
-    /// `&[i32;N]` to a `&[i32]`, or a `Box<T>` to a `Box<dyn Trait>`
-    /// (presuming `T: Trait`).
-    Unsize,
+    Pointer(PointerCast),
 }
 
 #[derive(Clone, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable, HashStable)]
index c2ef08c4c40fe1ddda50ef6d4f3ec4400c588b1a..7b81f7c6979098d94a2d26348c0a46dbc7891706 100644 (file)
@@ -5,6 +5,15 @@
 use rustc_macros::HashStable;
 
 
+#[derive(Clone, Copy, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable, HashStable)]
+pub enum PointerCast {
+    ReifyFnPointer,
+    UnsafeFnPointer,
+    ClosureFnPointer(hir::Unsafety),
+    MutToConstPointer,
+    Unsize,
+}
+
 /// Represents coercing a value to a different type of value.
 ///
 /// We transform values by following a number of `Adjust` steps in order.
index 53640284a2ca94b1f84dce4b81ecfd2e30bb792b..35e9d918aa6f975cbc0df7d4464c92bab7680e24 100644 (file)
@@ -1,4 +1,4 @@
-use rustc::ty::{self, Ty};
+use rustc::ty::{self, Ty, adjustment::{PointerCast}};
 use rustc::ty::cast::{CastTy, IntTy};
 use rustc::ty::layout::{self, LayoutOf, HasTyCtxt};
 use rustc::mir;
@@ -37,7 +37,7 @@ pub fn codegen_rvalue(
                bx
            }
 
-            mir::Rvalue::Cast(mir::CastKind::Unsize, ref source, _) => {
+            mir::Rvalue::Cast(mir::CastKind::Pointer(PointerCast::Unsize), ref source, _) => {
                 // The destination necessarily contains a fat pointer, so if
                 // it's a scalar pair, it's a fat pointer or newtype thereof.
                 if bx.cx().is_backend_scalar_pair(dest.layout) {
@@ -178,7 +178,7 @@ pub fn codegen_rvalue_operand(
                 let cast = bx.cx().layout_of(self.monomorphize(&mir_cast_ty));
 
                 let val = match *kind {
-                    mir::CastKind::ReifyFnPointer => {
+                    mir::CastKind::Pointer(PointerCast::ReifyFnPointer) => {
                         match operand.layout.ty.sty {
                             ty::FnDef(def_id, substs) => {
                                 if bx.cx().tcx().has_attr(def_id, "rustc_args_required_const") {
@@ -193,7 +193,7 @@ pub fn codegen_rvalue_operand(
                             }
                         }
                     }
-                    mir::CastKind::ClosureFnPointer(_) => {
+                    mir::CastKind::Pointer(PointerCast::ClosureFnPointer(_)) => {
                         match operand.layout.ty.sty {
                             ty::Closure(def_id, substs) => {
                                 let instance = monomorphize::resolve_closure(
@@ -205,11 +205,11 @@ pub fn codegen_rvalue_operand(
                             }
                         }
                     }
-                    mir::CastKind::UnsafeFnPointer => {
+                    mir::CastKind::Pointer(PointerCast::UnsafeFnPointer) => {
                         // this is a no-op at the LLVM level
                         operand.val
                     }
-                    mir::CastKind::Unsize => {
+                    mir::CastKind::Pointer(PointerCast::Unsize) => {
                         assert!(bx.cx().is_backend_scalar_pair(cast));
                         match operand.val {
                             OperandValue::Pair(lldata, llextra) => {
@@ -236,7 +236,7 @@ pub fn codegen_rvalue_operand(
                             }
                         }
                     }
-                    mir::CastKind::MutToConstPointer
+                    mir::CastKind::Pointer(PointerCast::MutToConstPointer)
                     | mir::CastKind::Misc if bx.cx().is_backend_scalar_pair(operand.layout) => {
                         if let OperandValue::Pair(data_ptr, meta) = operand.val {
                             if bx.cx().is_backend_scalar_pair(cast) {
@@ -254,7 +254,7 @@ pub fn codegen_rvalue_operand(
                             bug!("Unexpected non-Pair operand")
                         }
                     }
-                    mir::CastKind::MutToConstPointer
+                    mir::CastKind::Pointer(PointerCast::MutToConstPointer)
                     | mir::CastKind::Misc => {
                         assert!(bx.cx().is_backend_immediate(cast));
                         let ll_t_out = bx.cx().immediate_backend_type(cast);
index e30938bc32659397f136593ebfd597cc78523894..5ad54080c5a6122ecb4eb67de048c4eea9422c35 100644 (file)
@@ -10,6 +10,7 @@
     Projection, ProjectionElem, Rvalue, Statement, StatementKind, TerminatorKind,
 };
 use rustc::ty::{self, TyCtxt};
+use rustc::ty::adjustment::{PointerCast};
 use rustc_data_structures::fx::FxHashSet;
 use rustc_errors::DiagnosticBuilder;
 use syntax_pos::Span;
@@ -580,7 +581,9 @@ fn was_captured_by_trait_object(&self, borrow: &BorrowData<'tcx>) -> bool {
                         },
                         // If we see a unsized cast, then if it is our data we should check
                         // whether it is being cast to a trait object.
-                        Rvalue::Cast(CastKind::Unsize, operand, ty) => match operand {
+                        Rvalue::Cast(
+                            CastKind::Pointer(PointerCast::Unsize), operand, ty
+                        ) => match operand {
                             Operand::Copy(Place::Base(PlaceBase::Local(from)))
                             | Operand::Move(Place::Base(PlaceBase::Local(from)))
                                 if *from == target =>
index ec5637d17072d4840148ee91990b5809ec71b015..d123f3e405936542aac55a38364c6e7b84f81301 100644 (file)
@@ -36,6 +36,7 @@
 use rustc::traits::query::type_op::custom::CustomTypeOp;
 use rustc::traits::query::{Fallible, NoSolution};
 use rustc::traits::{ObligationCause, PredicateObligations};
+use rustc::ty::adjustment::{PointerCast};
 use rustc::ty::fold::TypeFoldable;
 use rustc::ty::subst::{Subst, SubstsRef, UnpackedKind, UserSubsts};
 use rustc::ty::{
@@ -1972,7 +1973,7 @@ fn check_rvalue(&mut self, mir: &Mir<'tcx>, rvalue: &Rvalue<'tcx>, location: Loc
 
             Rvalue::Cast(cast_kind, op, ty) => {
                 match cast_kind {
-                    CastKind::ReifyFnPointer => {
+                    CastKind::Pointer(PointerCast::ReifyFnPointer) => {
                         let fn_sig = op.ty(mir, tcx).fn_sig(tcx);
 
                         // The type that we see in the fcx is like
@@ -2001,7 +2002,7 @@ fn check_rvalue(&mut self, mir: &Mir<'tcx>, rvalue: &Rvalue<'tcx>, location: Loc
                         }
                     }
 
-                    CastKind::ClosureFnPointer(unsafety) => {
+                    CastKind::Pointer(PointerCast::ClosureFnPointer(unsafety)) => {
                         let sig = match op.ty(mir, tcx).sty {
                             ty::Closure(def_id, substs) => {
                                 substs.closure_sig_ty(def_id, tcx).fn_sig(tcx)
@@ -2027,7 +2028,7 @@ fn check_rvalue(&mut self, mir: &Mir<'tcx>, rvalue: &Rvalue<'tcx>, location: Loc
                         }
                     }
 
-                    CastKind::UnsafeFnPointer => {
+                    CastKind::Pointer(PointerCast::UnsafeFnPointer) => {
                         let fn_sig = op.ty(mir, tcx).fn_sig(tcx);
 
                         // The type that we see in the fcx is like
@@ -2056,7 +2057,7 @@ fn check_rvalue(&mut self, mir: &Mir<'tcx>, rvalue: &Rvalue<'tcx>, location: Loc
                         }
                     }
 
-                    CastKind::Unsize => {
+                    CastKind::Pointer(PointerCast::Unsize) => {
                         let &ty = ty;
                         let trait_ref = ty::TraitRef {
                             def_id: tcx.lang_items().coerce_unsized_trait().unwrap(),
@@ -2070,7 +2071,7 @@ fn check_rvalue(&mut self, mir: &Mir<'tcx>, rvalue: &Rvalue<'tcx>, location: Loc
                         );
                     }
 
-                    CastKind::MutToConstPointer => {
+                    CastKind::Pointer(PointerCast::MutToConstPointer) => {
                         let ty_from = match op.ty(mir, tcx).sty {
                             ty::RawPtr(ty::TypeAndMut {
                                 ty: ty_from,
index 7289dd96edb8dcf4a733f8221984c345b7658e2a..565524247174fe0533435ba0363f5287e04a6fa8 100644 (file)
@@ -10,6 +10,7 @@
 use rustc::mir::interpret::InterpError;
 use rustc::mir::*;
 use rustc::ty::{self, CanonicalUserTypeAnnotation, Ty, UpvarSubsts};
+use rustc::ty::adjustment::{PointerCast};
 use syntax_pos::Span;
 
 impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
@@ -156,23 +157,33 @@ fn expr_as_rvalue(
             }
             ExprKind::ReifyFnPointer { source } => {
                 let source = unpack!(block = this.as_operand(block, scope, source));
-                block.and(Rvalue::Cast(CastKind::ReifyFnPointer, source, expr.ty))
+                block.and(Rvalue::Cast(
+                    CastKind::Pointer(PointerCast::ReifyFnPointer), source, expr.ty)
+                )
             }
             ExprKind::UnsafeFnPointer { source } => {
                 let source = unpack!(block = this.as_operand(block, scope, source));
-                block.and(Rvalue::Cast(CastKind::UnsafeFnPointer, source, expr.ty))
+                block.and(Rvalue::Cast(
+                    CastKind::Pointer(PointerCast::UnsafeFnPointer), source, expr.ty)
+                )
             }
             ExprKind::ClosureFnPointer { source, unsafety } => {
                 let source = unpack!(block = this.as_operand(block, scope, source));
-                block.and(Rvalue::Cast(CastKind::ClosureFnPointer(unsafety), source, expr.ty))
+                block.and(Rvalue::Cast(
+                    CastKind::Pointer(PointerCast::ClosureFnPointer(unsafety)), source, expr.ty)
+                )
             }
             ExprKind::MutToConstPointer { source } => {
                 let source = unpack!(block = this.as_operand(block, scope, source));
-                block.and(Rvalue::Cast(CastKind::MutToConstPointer, source, expr.ty))
+                block.and(Rvalue::Cast(
+                    CastKind::Pointer(PointerCast::MutToConstPointer), source, expr.ty)
+                )
             }
             ExprKind::Unsize { source } => {
                 let source = unpack!(block = this.as_operand(block, scope, source));
-                block.and(Rvalue::Cast(CastKind::Unsize, source, expr.ty))
+                block.and(Rvalue::Cast(
+                    CastKind::Pointer(PointerCast::Unsize), source, expr.ty)
+                )
             }
             ExprKind::Array { fields } => {
                 // (*) We would (maybe) be closer to codegen if we
index b06022196106a43d035bbfa2865042997f9d7219..1eaf3d7ba99822ad7e49b61d000b5b04ed7a25c4 100644 (file)
@@ -11,7 +11,7 @@
 use crate::hair::pattern::compare_const_vals;
 use rustc_data_structures::bit_set::BitSet;
 use rustc_data_structures::fx::FxHashMap;
-use rustc::ty::{self, Ty};
+use rustc::ty::{self, Ty, adjustment::{PointerCast}};
 use rustc::ty::util::IntTypeExt;
 use rustc::ty::layout::VariantIdx;
 use rustc::mir::*;
@@ -280,8 +280,11 @@ pub fn perform_test(&mut self,
                             ty = tcx.mk_imm_ref(region, tcx.mk_slice(elem_ty));
                             if opt_ref_ty.is_some() {
                                 place = self.temp(ty, test.span);
-                                self.cfg.push_assign(block, source_info, &place,
-                                                    Rvalue::Cast(CastKind::Unsize, val, ty));
+                                self.cfg.push_assign(
+                                    block, source_info, &place, Rvalue::Cast(
+                                        CastKind::Pointer(PointerCast::Unsize), val, ty
+                                    )
+                                );
                             }
                             if opt_ref_test_ty.is_some() {
                                 let array = self.literal_operand(
@@ -291,8 +294,11 @@ pub fn perform_test(&mut self,
                                 );
 
                                 let slice = self.temp(ty, test.span);
-                                self.cfg.push_assign(block, source_info, &slice,
-                                                    Rvalue::Cast(CastKind::Unsize, array, ty));
+                                self.cfg.push_assign(
+                                    block, source_info, &slice, Rvalue::Cast(
+                                        CastKind::Pointer(PointerCast::Unsize), array, ty
+                                    )
+                                );
                                 expect = Operand::Move(slice);
                             }
                         },
index 5056d79bec4b1c0dc92e41799bc747faf09c6a2d..32f218d49cea2bc7859f5eb96568a7865da27648 100644 (file)
@@ -1,5 +1,6 @@
 use rustc::ty::{self, Ty, TypeAndMut};
 use rustc::ty::layout::{self, TyLayout, Size};
+use rustc::ty::adjustment::{PointerCast};
 use syntax::ast::{FloatTy, IntTy, UintTy};
 
 use rustc_apfloat::ieee::{Single, Double};
@@ -29,11 +30,11 @@ pub fn cast(
     ) -> EvalResult<'tcx> {
         use rustc::mir::CastKind::*;
         match kind {
-            Unsize => {
+            Pointer(PointerCast::Unsize) => {
                 self.unsize_into(src, dest)?;
             }
 
-            Misc | MutToConstPointer => {
+            Misc | Pointer(PointerCast::MutToConstPointer) => {
                 let src = self.read_immediate(src)?;
 
                 if self.type_is_fat_ptr(src.layout.ty) {
@@ -72,7 +73,7 @@ pub fn cast(
                 }
             }
 
-            ReifyFnPointer => {
+            Pointer(PointerCast::ReifyFnPointer) => {
                 // The src operand does not matter, just its type
                 match src.layout.ty.sty {
                     ty::FnDef(def_id, substs) => {
@@ -93,7 +94,7 @@ pub fn cast(
                 }
             }
 
-            UnsafeFnPointer => {
+            Pointer(PointerCast::UnsafeFnPointer) => {
                 let src = self.read_immediate(src)?;
                 match dest.layout.ty.sty {
                     ty::FnPtr(_) => {
@@ -104,7 +105,7 @@ pub fn cast(
                 }
             }
 
-            ClosureFnPointer(_) => {
+            Pointer(PointerCast::ClosureFnPointer(_)) => {
                 // The src operand does not matter, just its type
                 match src.layout.ty.sty {
                     ty::Closure(def_id, substs) => {
index af875c2f9e8a183ea13bbb3a93797849421b3005..2f4b3fa6ca8f7ca3e3e1c0723b52a6d79e45c2b4 100644 (file)
 use rustc::middle::lang_items::{ExchangeMallocFnLangItem, StartFnLangItem};
 use rustc::ty::subst::{InternalSubsts, SubstsRef};
 use rustc::ty::{self, TypeFoldable, Ty, TyCtxt, GenericParamDefKind};
-use rustc::ty::adjustment::CustomCoerceUnsized;
+use rustc::ty::adjustment::{CustomCoerceUnsized, PointerCast};
 use rustc::session::config::EntryFnType;
 use rustc::mir::{self, Location, Place, PlaceBase, Promoted, Static, StaticKind};
 use rustc::mir::visit::Visitor as MirVisitor;
@@ -529,7 +529,9 @@ fn visit_rvalue(&mut self, rvalue: &mir::Rvalue<'tcx>, location: Location) {
             // When doing an cast from a regular pointer to a fat pointer, we
             // have to instantiate all methods of the trait being cast to, so we
             // can build the appropriate vtable.
-            mir::Rvalue::Cast(mir::CastKind::Unsize, ref operand, target_ty) => {
+            mir::Rvalue::Cast(
+                mir::CastKind::Pointer(PointerCast::Unsize), ref operand, target_ty
+            ) => {
                 let target_ty = self.tcx.subst_and_normalize_erasing_regions(
                     self.param_substs,
                     ty::ParamEnv::reveal_all(),
@@ -554,7 +556,9 @@ fn visit_rvalue(&mut self, rvalue: &mir::Rvalue<'tcx>, location: Location) {
                                                          self.output);
                 }
             }
-            mir::Rvalue::Cast(mir::CastKind::ReifyFnPointer, ref operand, _) => {
+            mir::Rvalue::Cast(
+                mir::CastKind::Pointer(PointerCast::ReifyFnPointer), ref operand, _
+            ) => {
                 let fn_ty = operand.ty(self.mir, self.tcx);
                 let fn_ty = self.tcx.subst_and_normalize_erasing_regions(
                     self.param_substs,
@@ -563,7 +567,9 @@ fn visit_rvalue(&mut self, rvalue: &mir::Rvalue<'tcx>, location: Location) {
                 );
                 visit_fn_use(self.tcx, fn_ty, false, &mut self.output);
             }
-            mir::Rvalue::Cast(mir::CastKind::ClosureFnPointer(_), ref operand, _) => {
+            mir::Rvalue::Cast(
+                mir::CastKind::Pointer(PointerCast::ClosureFnPointer(_)), ref operand, _
+            ) => {
                 let source_ty = operand.ty(self.mir, self.tcx);
                 let source_ty = self.tcx.subst_and_normalize_erasing_regions(
                     self.param_substs,
index 1faa9ad0d0da4e19c5585e0e889db982422a2455..111f54439962097bc1f9deb52e659fcbe8f5efb1 100644 (file)
@@ -12,7 +12,7 @@
 use rustc::hir;
 use rustc::hir::def_id::DefId;
 use rustc::traits::{self, TraitEngine};
-use rustc::ty::{self, TyCtxt, Ty, TypeFoldable};
+use rustc::ty::{self, TyCtxt, Ty, TypeFoldable, adjustment::{PointerCast}};
 use rustc::ty::cast::CastTy;
 use rustc::ty::query::Providers;
 use rustc::mir::*;
@@ -1106,11 +1106,11 @@ fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, location: Location) {
             Rvalue::UnaryOp(UnOp::Not, _) |
             Rvalue::NullaryOp(NullOp::SizeOf, _) |
             Rvalue::CheckedBinaryOp(..) |
-            Rvalue::Cast(CastKind::ReifyFnPointer, ..) |
-            Rvalue::Cast(CastKind::UnsafeFnPointer, ..) |
-            Rvalue::Cast(CastKind::ClosureFnPointer(_), ..) |
-            Rvalue::Cast(CastKind::Unsize, ..) |
-            Rvalue::Cast(CastKind::MutToConstPointer, ..) |
+            Rvalue::Cast(CastKind::Pointer(PointerCast::ReifyFnPointer), ..) |
+            Rvalue::Cast(CastKind::Pointer(PointerCast::UnsafeFnPointer), ..) |
+            Rvalue::Cast(CastKind::Pointer(PointerCast::ClosureFnPointer(_)), ..) |
+            Rvalue::Cast(CastKind::Pointer(PointerCast::Unsize), ..) |
+            Rvalue::Cast(CastKind::Pointer(PointerCast::MutToConstPointer), ..) |
             Rvalue::Discriminant(..) |
             Rvalue::Len(_) |
             Rvalue::Ref(..) |
index 87459571b529cba42d32499fa8dcf31decd35759..201dac1524e4da127929e07e8089733943da90c9 100644 (file)
@@ -1,7 +1,7 @@
 use rustc::hir::def_id::DefId;
 use rustc::hir;
 use rustc::mir::*;
-use rustc::ty::{self, Predicate, TyCtxt};
+use rustc::ty::{self, Predicate, TyCtxt, adjustment::{PointerCast}};
 use rustc_target::spec::abi;
 use std::borrow::Cow;
 use syntax_pos::Span;
@@ -152,16 +152,16 @@ fn check_rvalue(
                 _ => check_operand(tcx, mir, operand, span),
             }
         }
-        Rvalue::Cast(CastKind::MutToConstPointer, operand, _) => {
+        Rvalue::Cast(CastKind::Pointer(PointerCast::MutToConstPointer), operand, _) => {
             check_operand(tcx, mir, operand, span)
         }
-        Rvalue::Cast(CastKind::UnsafeFnPointer, _, _) |
-        Rvalue::Cast(CastKind::ClosureFnPointer(_), _, _) |
-        Rvalue::Cast(CastKind::ReifyFnPointer, _, _) => Err((
+        Rvalue::Cast(CastKind::Pointer(PointerCast::UnsafeFnPointer), _, _) |
+        Rvalue::Cast(CastKind::Pointer(PointerCast::ClosureFnPointer(_)), _, _) |
+        Rvalue::Cast(CastKind::Pointer(PointerCast::ReifyFnPointer), _, _) => Err((
             span,
             "function pointer casts are not allowed in const fn".into(),
         )),
-        Rvalue::Cast(CastKind::Unsize, _, _) => Err((
+        Rvalue::Cast(CastKind::Pointer(PointerCast::Unsize), _, _) => Err((
             span,
             "unsizing casts are not allowed in const fn".into(),
         )),