Skip to content

Refactor Adjust and CastKind #59987

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 20, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 3 additions & 21 deletions src/librustc/ty/structural_impls.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you lost the comment about looking at the comment on Adjust::Deref

Copy link
Contributor Author

@saleemjaffer saleemjaffer Apr 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// See the above comment for Adjust::Deref

I thought the comment was misplaced. Why do we mention about Adjust::Deref in Adjust::Unsize?

Also, it does not seem like the existing "above comment" spoke about Adjust::Deref.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's about the comment in line 94. This code exists solely to improve diagnostics. Maybe pull it out into a function instead of duplicating the logic?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a new function adjust_span for this.

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
Original file line number Diff line number Diff line change
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