refactor ExprKind to use new PointerCast enum

This commit is contained in:
Saleem Jaffer 2019-04-16 17:36:41 +05:30
parent 6321a323cc
commit 63b4764556
7 changed files with 21 additions and 99 deletions

View File

@ -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,

View File

@ -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 { .. }

View File

@ -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

View File

@ -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 { .. }

View File

@ -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 { .. }

View File

@ -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;
}
Adjust::Pointer(PointerCast::UnsafeFnPointer) => {
ExprKind::UnsafeFnPointer { source: expr.to_ref() }
}
Adjust::Pointer(PointerCast::ClosureFnPointer(unsafety)) => {
ExprKind::ClosureFnPointer { source: expr.to_ref(), unsafety }
ExprKind::Pointer { cast: PointerCast::Unsize, source: expr.to_ref() }
}
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 {

View File

@ -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 {