Skip to content

Commit 5be6b0b

Browse files
author
Saleem Jaffer
committed
basic refactor. Adding PointerCast enum
1 parent 3750348 commit 5be6b0b

File tree

11 files changed

+85
-65
lines changed

11 files changed

+85
-65
lines changed

src/librustc/mir/mod.rs

+3-20
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ use crate::ty::{
3535
UserTypeAnnotationIndex,
3636
};
3737
use crate::ty::print::{FmtPrinter, Printer};
38+
use crate::ty::adjustment::{PointerCast};
3839

3940
pub use crate::mir::interpret::AssertMessage;
4041

@@ -2248,29 +2249,11 @@ pub enum Rvalue<'tcx> {
22482249
Aggregate(Box<AggregateKind<'tcx>>, Vec<Operand<'tcx>>),
22492250
}
22502251

2252+
22512253
#[derive(Clone, Copy, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable, HashStable)]
22522254
pub enum CastKind {
22532255
Misc,
2254-
2255-
/// Converts unique, zero-sized type for a fn to fn()
2256-
ReifyFnPointer,
2257-
2258-
/// Converts non capturing closure to fn() or unsafe fn().
2259-
/// It cannot convert a closure that requires unsafe.
2260-
ClosureFnPointer(hir::Unsafety),
2261-
2262-
/// Converts safe fn() to unsafe fn()
2263-
UnsafeFnPointer,
2264-
2265-
/// Coerces *mut T to *const T, preserving T.
2266-
MutToConstPointer,
2267-
2268-
/// "Unsize" -- convert a thin-or-fat pointer to a fat pointer.
2269-
/// codegen must figure out the details once full monomorphization
2270-
/// is known. For example, this could be used to cast from a
2271-
/// `&[i32;N]` to a `&[i32]`, or a `Box<T>` to a `Box<dyn Trait>`
2272-
/// (presuming `T: Trait`).
2273-
Unsize,
2256+
Pointer(PointerCast),
22742257
}
22752258

22762259
#[derive(Clone, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable, HashStable)]

src/librustc/ty/adjustment.rs

+9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ use crate::ty::subst::SubstsRef;
55
use rustc_macros::HashStable;
66

77

8+
#[derive(Clone, Copy, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable, HashStable)]
9+
pub enum PointerCast {
10+
ReifyFnPointer,
11+
UnsafeFnPointer,
12+
ClosureFnPointer(hir::Unsafety),
13+
MutToConstPointer,
14+
Unsize,
15+
}
16+
817
/// Represents coercing a value to a different type of value.
918
///
1019
/// We transform values by following a number of `Adjust` steps in order.

src/librustc_codegen_ssa/mir/rvalue.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc::ty::{self, Ty};
1+
use rustc::ty::{self, Ty, adjustment::{PointerCast}};
22
use rustc::ty::cast::{CastTy, IntTy};
33
use rustc::ty::layout::{self, LayoutOf, HasTyCtxt};
44
use rustc::mir;
@@ -37,7 +37,7 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
3737
bx
3838
}
3939

40-
mir::Rvalue::Cast(mir::CastKind::Unsize, ref source, _) => {
40+
mir::Rvalue::Cast(mir::CastKind::Pointer(PointerCast::Unsize), ref source, _) => {
4141
// The destination necessarily contains a fat pointer, so if
4242
// it's a scalar pair, it's a fat pointer or newtype thereof.
4343
if bx.cx().is_backend_scalar_pair(dest.layout) {
@@ -178,7 +178,7 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
178178
let cast = bx.cx().layout_of(self.monomorphize(&mir_cast_ty));
179179

180180
let val = match *kind {
181-
mir::CastKind::ReifyFnPointer => {
181+
mir::CastKind::Pointer(PointerCast::ReifyFnPointer) => {
182182
match operand.layout.ty.sty {
183183
ty::FnDef(def_id, substs) => {
184184
if bx.cx().tcx().has_attr(def_id, "rustc_args_required_const") {
@@ -193,7 +193,7 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
193193
}
194194
}
195195
}
196-
mir::CastKind::ClosureFnPointer(_) => {
196+
mir::CastKind::Pointer(PointerCast::ClosureFnPointer(_)) => {
197197
match operand.layout.ty.sty {
198198
ty::Closure(def_id, substs) => {
199199
let instance = monomorphize::resolve_closure(
@@ -205,11 +205,11 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
205205
}
206206
}
207207
}
208-
mir::CastKind::UnsafeFnPointer => {
208+
mir::CastKind::Pointer(PointerCast::UnsafeFnPointer) => {
209209
// this is a no-op at the LLVM level
210210
operand.val
211211
}
212-
mir::CastKind::Unsize => {
212+
mir::CastKind::Pointer(PointerCast::Unsize) => {
213213
assert!(bx.cx().is_backend_scalar_pair(cast));
214214
match operand.val {
215215
OperandValue::Pair(lldata, llextra) => {
@@ -236,7 +236,7 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
236236
}
237237
}
238238
}
239-
mir::CastKind::MutToConstPointer
239+
mir::CastKind::Pointer(PointerCast::MutToConstPointer)
240240
| mir::CastKind::Misc if bx.cx().is_backend_scalar_pair(operand.layout) => {
241241
if let OperandValue::Pair(data_ptr, meta) = operand.val {
242242
if bx.cx().is_backend_scalar_pair(cast) {
@@ -254,7 +254,7 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
254254
bug!("Unexpected non-Pair operand")
255255
}
256256
}
257-
mir::CastKind::MutToConstPointer
257+
mir::CastKind::Pointer(PointerCast::MutToConstPointer)
258258
| mir::CastKind::Misc => {
259259
assert!(bx.cx().is_backend_immediate(cast));
260260
let ll_t_out = bx.cx().immediate_backend_type(cast);

src/librustc_mir/borrow_check/nll/explain_borrow/mod.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use rustc::mir::{
1010
Projection, ProjectionElem, Rvalue, Statement, StatementKind, TerminatorKind,
1111
};
1212
use rustc::ty::{self, TyCtxt};
13+
use rustc::ty::adjustment::{PointerCast};
1314
use rustc_data_structures::fx::FxHashSet;
1415
use rustc_errors::DiagnosticBuilder;
1516
use syntax_pos::Span;
@@ -580,7 +581,9 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
580581
},
581582
// If we see a unsized cast, then if it is our data we should check
582583
// whether it is being cast to a trait object.
583-
Rvalue::Cast(CastKind::Unsize, operand, ty) => match operand {
584+
Rvalue::Cast(
585+
CastKind::Pointer(PointerCast::Unsize), operand, ty
586+
) => match operand {
584587
Operand::Copy(Place::Base(PlaceBase::Local(from)))
585588
| Operand::Move(Place::Base(PlaceBase::Local(from)))
586589
if *from == target =>

src/librustc_mir/borrow_check/nll/type_check/mod.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ use rustc::traits::query::type_op;
3636
use rustc::traits::query::type_op::custom::CustomTypeOp;
3737
use rustc::traits::query::{Fallible, NoSolution};
3838
use rustc::traits::{ObligationCause, PredicateObligations};
39+
use rustc::ty::adjustment::{PointerCast};
3940
use rustc::ty::fold::TypeFoldable;
4041
use rustc::ty::subst::{Subst, SubstsRef, UnpackedKind, UserSubsts};
4142
use rustc::ty::{
@@ -1972,7 +1973,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
19721973

19731974
Rvalue::Cast(cast_kind, op, ty) => {
19741975
match cast_kind {
1975-
CastKind::ReifyFnPointer => {
1976+
CastKind::Pointer(PointerCast::ReifyFnPointer) => {
19761977
let fn_sig = op.ty(mir, tcx).fn_sig(tcx);
19771978

19781979
// The type that we see in the fcx is like
@@ -2001,7 +2002,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
20012002
}
20022003
}
20032004

2004-
CastKind::ClosureFnPointer(unsafety) => {
2005+
CastKind::Pointer(PointerCast::ClosureFnPointer(unsafety)) => {
20052006
let sig = match op.ty(mir, tcx).sty {
20062007
ty::Closure(def_id, substs) => {
20072008
substs.closure_sig_ty(def_id, tcx).fn_sig(tcx)
@@ -2027,7 +2028,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
20272028
}
20282029
}
20292030

2030-
CastKind::UnsafeFnPointer => {
2031+
CastKind::Pointer(PointerCast::UnsafeFnPointer) => {
20312032
let fn_sig = op.ty(mir, tcx).fn_sig(tcx);
20322033

20332034
// The type that we see in the fcx is like
@@ -2056,7 +2057,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
20562057
}
20572058
}
20582059

2059-
CastKind::Unsize => {
2060+
CastKind::Pointer(PointerCast::Unsize) => {
20602061
let &ty = ty;
20612062
let trait_ref = ty::TraitRef {
20622063
def_id: tcx.lang_items().coerce_unsized_trait().unwrap(),
@@ -2070,7 +2071,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
20702071
);
20712072
}
20722073

2073-
CastKind::MutToConstPointer => {
2074+
CastKind::Pointer(PointerCast::MutToConstPointer) => {
20742075
let ty_from = match op.ty(mir, tcx).sty {
20752076
ty::RawPtr(ty::TypeAndMut {
20762077
ty: ty_from,

src/librustc_mir/build/expr/as_rvalue.rs

+16-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use rustc::middle::region;
1010
use rustc::mir::interpret::InterpError;
1111
use rustc::mir::*;
1212
use rustc::ty::{self, CanonicalUserTypeAnnotation, Ty, UpvarSubsts};
13+
use rustc::ty::adjustment::{PointerCast};
1314
use syntax_pos::Span;
1415

1516
impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
@@ -156,23 +157,33 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
156157
}
157158
ExprKind::ReifyFnPointer { source } => {
158159
let source = unpack!(block = this.as_operand(block, scope, source));
159-
block.and(Rvalue::Cast(CastKind::ReifyFnPointer, source, expr.ty))
160+
block.and(Rvalue::Cast(
161+
CastKind::Pointer(PointerCast::ReifyFnPointer), source, expr.ty)
162+
)
160163
}
161164
ExprKind::UnsafeFnPointer { source } => {
162165
let source = unpack!(block = this.as_operand(block, scope, source));
163-
block.and(Rvalue::Cast(CastKind::UnsafeFnPointer, source, expr.ty))
166+
block.and(Rvalue::Cast(
167+
CastKind::Pointer(PointerCast::UnsafeFnPointer), source, expr.ty)
168+
)
164169
}
165170
ExprKind::ClosureFnPointer { source, unsafety } => {
166171
let source = unpack!(block = this.as_operand(block, scope, source));
167-
block.and(Rvalue::Cast(CastKind::ClosureFnPointer(unsafety), source, expr.ty))
172+
block.and(Rvalue::Cast(
173+
CastKind::Pointer(PointerCast::ClosureFnPointer(unsafety)), source, expr.ty)
174+
)
168175
}
169176
ExprKind::MutToConstPointer { source } => {
170177
let source = unpack!(block = this.as_operand(block, scope, source));
171-
block.and(Rvalue::Cast(CastKind::MutToConstPointer, source, expr.ty))
178+
block.and(Rvalue::Cast(
179+
CastKind::Pointer(PointerCast::MutToConstPointer), source, expr.ty)
180+
)
172181
}
173182
ExprKind::Unsize { source } => {
174183
let source = unpack!(block = this.as_operand(block, scope, source));
175-
block.and(Rvalue::Cast(CastKind::Unsize, source, expr.ty))
184+
block.and(Rvalue::Cast(
185+
CastKind::Pointer(PointerCast::Unsize), source, expr.ty)
186+
)
176187
}
177188
ExprKind::Array { fields } => {
178189
// (*) We would (maybe) be closer to codegen if we

src/librustc_mir/build/matches/test.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::hair::*;
1111
use crate::hair::pattern::compare_const_vals;
1212
use rustc_data_structures::bit_set::BitSet;
1313
use rustc_data_structures::fx::FxHashMap;
14-
use rustc::ty::{self, Ty};
14+
use rustc::ty::{self, Ty, adjustment::{PointerCast}};
1515
use rustc::ty::util::IntTypeExt;
1616
use rustc::ty::layout::VariantIdx;
1717
use rustc::mir::*;
@@ -280,8 +280,11 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
280280
ty = tcx.mk_imm_ref(region, tcx.mk_slice(elem_ty));
281281
if opt_ref_ty.is_some() {
282282
place = self.temp(ty, test.span);
283-
self.cfg.push_assign(block, source_info, &place,
284-
Rvalue::Cast(CastKind::Unsize, val, ty));
283+
self.cfg.push_assign(
284+
block, source_info, &place, Rvalue::Cast(
285+
CastKind::Pointer(PointerCast::Unsize), val, ty
286+
)
287+
);
285288
}
286289
if opt_ref_test_ty.is_some() {
287290
let array = self.literal_operand(
@@ -291,8 +294,11 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
291294
);
292295

293296
let slice = self.temp(ty, test.span);
294-
self.cfg.push_assign(block, source_info, &slice,
295-
Rvalue::Cast(CastKind::Unsize, array, ty));
297+
self.cfg.push_assign(
298+
block, source_info, &slice, Rvalue::Cast(
299+
CastKind::Pointer(PointerCast::Unsize), array, ty
300+
)
301+
);
296302
expect = Operand::Move(slice);
297303
}
298304
},

src/librustc_mir/interpret/cast.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use rustc::ty::{self, Ty, TypeAndMut};
22
use rustc::ty::layout::{self, TyLayout, Size};
3+
use rustc::ty::adjustment::{PointerCast};
34
use syntax::ast::{FloatTy, IntTy, UintTy};
45

56
use rustc_apfloat::ieee::{Single, Double};
@@ -29,11 +30,11 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> InterpretCx<'a, 'mir, 'tcx, M>
2930
) -> EvalResult<'tcx> {
3031
use rustc::mir::CastKind::*;
3132
match kind {
32-
Unsize => {
33+
Pointer(PointerCast::Unsize) => {
3334
self.unsize_into(src, dest)?;
3435
}
3536

36-
Misc | MutToConstPointer => {
37+
Misc | Pointer(PointerCast::MutToConstPointer) => {
3738
let src = self.read_immediate(src)?;
3839

3940
if self.type_is_fat_ptr(src.layout.ty) {
@@ -72,7 +73,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> InterpretCx<'a, 'mir, 'tcx, M>
7273
}
7374
}
7475

75-
ReifyFnPointer => {
76+
Pointer(PointerCast::ReifyFnPointer) => {
7677
// The src operand does not matter, just its type
7778
match src.layout.ty.sty {
7879
ty::FnDef(def_id, substs) => {
@@ -93,7 +94,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> InterpretCx<'a, 'mir, 'tcx, M>
9394
}
9495
}
9596

96-
UnsafeFnPointer => {
97+
Pointer(PointerCast::UnsafeFnPointer) => {
9798
let src = self.read_immediate(src)?;
9899
match dest.layout.ty.sty {
99100
ty::FnPtr(_) => {
@@ -104,7 +105,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> InterpretCx<'a, 'mir, 'tcx, M>
104105
}
105106
}
106107

107-
ClosureFnPointer(_) => {
108+
Pointer(PointerCast::ClosureFnPointer(_)) => {
108109
// The src operand does not matter, just its type
109110
match src.layout.ty.sty {
110111
ty::Closure(def_id, substs) => {

src/librustc_mir/monomorphize/collector.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ use rustc::mir::interpret::{AllocId, ConstValue};
182182
use rustc::middle::lang_items::{ExchangeMallocFnLangItem, StartFnLangItem};
183183
use rustc::ty::subst::{InternalSubsts, SubstsRef};
184184
use rustc::ty::{self, TypeFoldable, Ty, TyCtxt, GenericParamDefKind};
185-
use rustc::ty::adjustment::CustomCoerceUnsized;
185+
use rustc::ty::adjustment::{CustomCoerceUnsized, PointerCast};
186186
use rustc::session::config::EntryFnType;
187187
use rustc::mir::{self, Location, Place, PlaceBase, Promoted, Static, StaticKind};
188188
use rustc::mir::visit::Visitor as MirVisitor;
@@ -529,7 +529,9 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirNeighborCollector<'a, 'tcx> {
529529
// When doing an cast from a regular pointer to a fat pointer, we
530530
// have to instantiate all methods of the trait being cast to, so we
531531
// can build the appropriate vtable.
532-
mir::Rvalue::Cast(mir::CastKind::Unsize, ref operand, target_ty) => {
532+
mir::Rvalue::Cast(
533+
mir::CastKind::Pointer(PointerCast::Unsize), ref operand, target_ty
534+
) => {
533535
let target_ty = self.tcx.subst_and_normalize_erasing_regions(
534536
self.param_substs,
535537
ty::ParamEnv::reveal_all(),
@@ -554,7 +556,9 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirNeighborCollector<'a, 'tcx> {
554556
self.output);
555557
}
556558
}
557-
mir::Rvalue::Cast(mir::CastKind::ReifyFnPointer, ref operand, _) => {
559+
mir::Rvalue::Cast(
560+
mir::CastKind::Pointer(PointerCast::ReifyFnPointer), ref operand, _
561+
) => {
558562
let fn_ty = operand.ty(self.mir, self.tcx);
559563
let fn_ty = self.tcx.subst_and_normalize_erasing_regions(
560564
self.param_substs,
@@ -563,7 +567,9 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirNeighborCollector<'a, 'tcx> {
563567
);
564568
visit_fn_use(self.tcx, fn_ty, false, &mut self.output);
565569
}
566-
mir::Rvalue::Cast(mir::CastKind::ClosureFnPointer(_), ref operand, _) => {
570+
mir::Rvalue::Cast(
571+
mir::CastKind::Pointer(PointerCast::ClosureFnPointer(_)), ref operand, _
572+
) => {
567573
let source_ty = operand.ty(self.mir, self.tcx);
568574
let source_ty = self.tcx.subst_and_normalize_erasing_regions(
569575
self.param_substs,

src/librustc_mir/transform/qualify_consts.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_target::spec::abi::Abi;
1212
use rustc::hir;
1313
use rustc::hir::def_id::DefId;
1414
use rustc::traits::{self, TraitEngine};
15-
use rustc::ty::{self, TyCtxt, Ty, TypeFoldable};
15+
use rustc::ty::{self, TyCtxt, Ty, TypeFoldable, adjustment::{PointerCast}};
1616
use rustc::ty::cast::CastTy;
1717
use rustc::ty::query::Providers;
1818
use rustc::mir::*;
@@ -1106,11 +1106,11 @@ impl<'a, 'tcx> Visitor<'tcx> for Checker<'a, 'tcx> {
11061106
Rvalue::UnaryOp(UnOp::Not, _) |
11071107
Rvalue::NullaryOp(NullOp::SizeOf, _) |
11081108
Rvalue::CheckedBinaryOp(..) |
1109-
Rvalue::Cast(CastKind::ReifyFnPointer, ..) |
1110-
Rvalue::Cast(CastKind::UnsafeFnPointer, ..) |
1111-
Rvalue::Cast(CastKind::ClosureFnPointer(_), ..) |
1112-
Rvalue::Cast(CastKind::Unsize, ..) |
1113-
Rvalue::Cast(CastKind::MutToConstPointer, ..) |
1109+
Rvalue::Cast(CastKind::Pointer(PointerCast::ReifyFnPointer), ..) |
1110+
Rvalue::Cast(CastKind::Pointer(PointerCast::UnsafeFnPointer), ..) |
1111+
Rvalue::Cast(CastKind::Pointer(PointerCast::ClosureFnPointer(_)), ..) |
1112+
Rvalue::Cast(CastKind::Pointer(PointerCast::Unsize), ..) |
1113+
Rvalue::Cast(CastKind::Pointer(PointerCast::MutToConstPointer), ..) |
11141114
Rvalue::Discriminant(..) |
11151115
Rvalue::Len(_) |
11161116
Rvalue::Ref(..) |

0 commit comments

Comments
 (0)