Skip to content

Commit

Permalink
refactor ExprKind to use new PointerCast enum
Browse files Browse the repository at this point in the history
  • Loading branch information
Saleem Jaffer committed Apr 16, 2019
1 parent 6321a32 commit 63b4764
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 99 deletions.
24 changes: 3 additions & 21 deletions src/librustc/ty/structural_impls.rs
Expand Up @@ -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};
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 1 addition & 5 deletions src/librustc_mir/build/expr/as_place.rs
Expand Up @@ -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 { .. }
Expand Down
31 changes: 2 additions & 29 deletions src/librustc_mir/build/expr/as_rvalue.rs
Expand Up @@ -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> {
Expand Down Expand Up @@ -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
Expand Down
6 changes: 1 addition & 5 deletions src/librustc_mir/build/expr/category.rs
Expand Up @@ -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 { .. }
Expand Down
6 changes: 1 addition & 5 deletions src/librustc_mir/build/expr/into.rs
Expand Up @@ -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 { .. }
Expand Down
30 changes: 10 additions & 20 deletions src/librustc_mir/hair/cx/expr.rs
Expand Up @@ -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::UnsafeFnPointer) => {
ExprKind::UnsafeFnPointer { 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::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
Expand Down Expand Up @@ -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 {
Expand Down
17 changes: 3 additions & 14 deletions src/librustc_mir/hair/mod.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 63b4764

Please sign in to comment.