From 63b476455674571402596edc13bff887732b51a5 Mon Sep 17 00:00:00 2001 From: Saleem Jaffer Date: Tue, 16 Apr 2019 17:36:41 +0530 Subject: [PATCH] refactor ExprKind to use new PointerCast enum --- src/librustc/ty/structural_impls.rs | 24 +++--------------- src/librustc_mir/build/expr/as_place.rs | 6 +---- src/librustc_mir/build/expr/as_rvalue.rs | 31 ++---------------------- src/librustc_mir/build/expr/category.rs | 6 +---- src/librustc_mir/build/expr/into.rs | 6 +---- src/librustc_mir/hair/cx/expr.rs | 30 ++++++++--------------- src/librustc_mir/hair/mod.rs | 17 +++---------- 7 files changed, 21 insertions(+), 99 deletions(-) diff --git a/src/librustc/ty/structural_impls.rs b/src/librustc/ty/structural_impls.rs index fe7ea775fdf..9aaff33e933 100644 --- a/src/librustc/ty/structural_impls.rs +++ b/src/librustc/ty/structural_impls.rs @@ -7,7 +7,6 @@ use crate::hir::def::Namespace; use crate::mir::ProjectionKind; use crate::mir::interpret::ConstValue; use crate::ty::{self, Lift, Ty, TyCtxt, ConstVid}; -use crate::ty::adjustment::{PointerCast}; use crate::ty::fold::{TypeFoldable, TypeFolder, TypeVisitor}; use crate::ty::print::{FmtPrinter, Printer}; use rustc_data_structures::indexed_vec::{IndexVec, Idx}; @@ -327,6 +326,7 @@ CloneTypeFoldableAndLiftImpls! { crate::ty::IntVarValue, crate::ty::ParamConst, crate::ty::ParamTy, + crate::ty::adjustment::PointerCast, crate::ty::RegionVid, crate::ty::UniverseIndex, crate::ty::Variance, @@ -627,16 +627,8 @@ impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::Adjust<'a> { match *self { ty::adjustment::Adjust::NeverToAny => Some(ty::adjustment::Adjust::NeverToAny), - ty::adjustment::Adjust::Pointer(PointerCast::ReifyFnPointer) => - Some(ty::adjustment::Adjust::Pointer(PointerCast::ReifyFnPointer)), - ty::adjustment::Adjust::Pointer(PointerCast::UnsafeFnPointer) => - Some(ty::adjustment::Adjust::Pointer(PointerCast::UnsafeFnPointer)), - ty::adjustment::Adjust::Pointer(PointerCast::ClosureFnPointer(unsafety)) => - Some(ty::adjustment::Adjust::Pointer(PointerCast::ClosureFnPointer(unsafety))), - ty::adjustment::Adjust::Pointer(PointerCast::MutToConstPointer) => - Some(ty::adjustment::Adjust::Pointer(PointerCast::MutToConstPointer)), - ty::adjustment::Adjust::Pointer(PointerCast::Unsize) => - Some(ty::adjustment::Adjust::Pointer(PointerCast::Unsize)), + ty::adjustment::Adjust::Pointer(ptr) => + Some(ty::adjustment::Adjust::Pointer(ptr)), ty::adjustment::Adjust::Deref(ref overloaded) => { tcx.lift(overloaded).map(ty::adjustment::Adjust::Deref) } @@ -1192,16 +1184,6 @@ EnumTypeFoldableImpl! { } } -EnumTypeFoldableImpl! { - impl<'tcx> TypeFoldable<'tcx> for ty::adjustment::PointerCast { - (ty::adjustment::PointerCast::ReifyFnPointer), - (ty::adjustment::PointerCast::UnsafeFnPointer), - (ty::adjustment::PointerCast::ClosureFnPointer)(a), - (ty::adjustment::PointerCast::MutToConstPointer), - (ty::adjustment::PointerCast::Unsize), - } -} - BraceStructTypeFoldableImpl! { impl<'tcx> TypeFoldable<'tcx> for ty::adjustment::OverloadedDeref<'tcx> { region, mutbl, diff --git a/src/librustc_mir/build/expr/as_place.rs b/src/librustc_mir/build/expr/as_place.rs index f7cf0902013..b500060684f 100644 --- a/src/librustc_mir/build/expr/as_place.rs +++ b/src/librustc_mir/build/expr/as_place.rs @@ -193,11 +193,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> { | ExprKind::Cast { .. } | ExprKind::Use { .. } | ExprKind::NeverToAny { .. } - | ExprKind::ReifyFnPointer { .. } - | ExprKind::ClosureFnPointer { .. } - | ExprKind::UnsafeFnPointer { .. } - | ExprKind::MutToConstPointer { .. } - | ExprKind::Unsize { .. } + | ExprKind::Pointer { .. } | ExprKind::Repeat { .. } | ExprKind::Borrow { .. } | ExprKind::If { .. } diff --git a/src/librustc_mir/build/expr/as_rvalue.rs b/src/librustc_mir/build/expr/as_rvalue.rs index 56552424717..b9fec22da63 100644 --- a/src/librustc_mir/build/expr/as_rvalue.rs +++ b/src/librustc_mir/build/expr/as_rvalue.rs @@ -10,7 +10,6 @@ use rustc::middle::region; 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> { @@ -155,35 +154,9 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> { let source = unpack!(block = this.as_operand(block, scope, source)); block.and(Rvalue::Use(source)) } - ExprKind::ReifyFnPointer { source } => { + ExprKind::Pointer { cast, source } => { let source = unpack!(block = this.as_operand(block, scope, source)); - 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::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::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::Pointer(PointerCast::MutToConstPointer), source, expr.ty) - ) - } - ExprKind::Unsize { source } => { - let source = unpack!(block = this.as_operand(block, scope, source)); - block.and(Rvalue::Cast( - CastKind::Pointer(PointerCast::Unsize), source, expr.ty) - ) + block.and(Rvalue::Cast(CastKind::Pointer(cast), source, expr.ty)) } ExprKind::Array { fields } => { // (*) We would (maybe) be closer to codegen if we diff --git a/src/librustc_mir/build/expr/category.rs b/src/librustc_mir/build/expr/category.rs index c8c30ac3ce4..52f1d0eca5e 100644 --- a/src/librustc_mir/build/expr/category.rs +++ b/src/librustc_mir/build/expr/category.rs @@ -59,11 +59,7 @@ impl Category { | ExprKind::Box { .. } | ExprKind::Cast { .. } | ExprKind::Use { .. } - | ExprKind::ReifyFnPointer { .. } - | ExprKind::ClosureFnPointer { .. } - | ExprKind::UnsafeFnPointer { .. } - | ExprKind::MutToConstPointer { .. } - | ExprKind::Unsize { .. } + | ExprKind::Pointer { .. } | ExprKind::Repeat { .. } | ExprKind::Borrow { .. } | ExprKind::Assign { .. } diff --git a/src/librustc_mir/build/expr/into.rs b/src/librustc_mir/build/expr/into.rs index 4d0418beea4..8967572ea8f 100644 --- a/src/librustc_mir/build/expr/into.rs +++ b/src/librustc_mir/build/expr/into.rs @@ -380,11 +380,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> { | ExprKind::Box { .. } | ExprKind::Cast { .. } | ExprKind::Use { .. } - | ExprKind::ReifyFnPointer { .. } - | ExprKind::ClosureFnPointer { .. } - | ExprKind::UnsafeFnPointer { .. } - | ExprKind::MutToConstPointer { .. } - | ExprKind::Unsize { .. } + | ExprKind::Pointer { .. } | ExprKind::Repeat { .. } | ExprKind::Borrow { .. } | ExprKind::Array { .. } diff --git a/src/librustc_mir/hair/cx/expr.rs b/src/librustc_mir/hair/cx/expr.rs index 2133cd26e13..28fedd9d256 100644 --- a/src/librustc_mir/hair/cx/expr.rs +++ b/src/librustc_mir/hair/cx/expr.rs @@ -75,21 +75,21 @@ fn apply_adjustment<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>, -> Expr<'tcx> { let Expr { temp_lifetime, mut span, .. } = expr; let kind = match adjustment.kind { - Adjust::Pointer(PointerCast::ReifyFnPointer) => { - ExprKind::ReifyFnPointer { source: expr.to_ref() } + Adjust::Pointer(PointerCast::Unsize) => { + if let ExprKind::Block { body } = expr.kind { + if let Some(ref last_expr) = body.expr { + span = last_expr.span; + expr.span = span; + } + } + ExprKind::Pointer { cast: PointerCast::Unsize, source: expr.to_ref() } } - Adjust::Pointer(PointerCast::UnsafeFnPointer) => { - ExprKind::UnsafeFnPointer { source: expr.to_ref() } - } - Adjust::Pointer(PointerCast::ClosureFnPointer(unsafety)) => { - ExprKind::ClosureFnPointer { source: expr.to_ref(), unsafety } + Adjust::Pointer(cast) => { + ExprKind::Pointer { cast, source: expr.to_ref() } } Adjust::NeverToAny => { ExprKind::NeverToAny { source: expr.to_ref() } } - Adjust::Pointer(PointerCast::MutToConstPointer) => { - ExprKind::MutToConstPointer { source: expr.to_ref() } - } Adjust::Deref(None) => { // Adjust the span from the block, to the last expression of the // block. This is a better span when returning a mutable reference @@ -187,16 +187,6 @@ fn apply_adjustment<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>, // since they get rid of a borrow implicitly. ExprKind::Use { source: cast_expr.to_ref() } } - Adjust::Pointer(PointerCast::Unsize) => { - // See the above comment for Adjust::Deref - if let ExprKind::Block { body } = expr.kind { - if let Some(ref last_expr) = body.expr { - span = last_expr.span; - expr.span = span; - } - } - ExprKind::Unsize { source: expr.to_ref() } - } }; Expr { diff --git a/src/librustc_mir/hair/mod.rs b/src/librustc_mir/hair/mod.rs index a661649db0f..2e53bee3f3d 100644 --- a/src/librustc_mir/hair/mod.rs +++ b/src/librustc_mir/hair/mod.rs @@ -10,6 +10,7 @@ use rustc::infer::canonical::Canonical; use rustc::middle::region; use rustc::ty::subst::SubstsRef; use rustc::ty::{AdtDef, UpvarSubsts, Ty, Const, UserType}; +use rustc::ty::adjustment::{PointerCast}; use rustc::ty::layout::VariantIdx; use rustc::hir; use syntax_pos::Span; @@ -180,20 +181,8 @@ pub enum ExprKind<'tcx> { NeverToAny { source: ExprRef<'tcx>, }, - ReifyFnPointer { - source: ExprRef<'tcx>, - }, - ClosureFnPointer { - source: ExprRef<'tcx>, - unsafety: hir::Unsafety, - }, - UnsafeFnPointer { - source: ExprRef<'tcx>, - }, - MutToConstPointer { - source: ExprRef<'tcx>, - }, - Unsize { + Pointer { + cast: PointerCast, source: ExprRef<'tcx>, }, If {