Skip to content

Commit 72bc620

Browse files
committed
Auto merge of #59987 - saleemjaffer:refactor_adjust_castkinds, r=oli-obk
Refactor Adjust and CastKind fixes #59588
2 parents 0d17322 + a2f8269 commit 72bc620

File tree

25 files changed

+161
-219
lines changed

25 files changed

+161
-219
lines changed

src/librustc/middle/expr_use_visitor.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -705,11 +705,7 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
705705
debug!("walk_adjustment expr={:?} adj={:?}", expr, adjustment);
706706
match adjustment.kind {
707707
adjustment::Adjust::NeverToAny |
708-
adjustment::Adjust::ReifyFnPointer |
709-
adjustment::Adjust::UnsafeFnPointer |
710-
adjustment::Adjust::ClosureFnPointer(_) |
711-
adjustment::Adjust::MutToConstPointer |
712-
adjustment::Adjust::Unsize => {
708+
adjustment::Adjust::Pointer(_) => {
713709
// Creating a closure/fn-pointer or unsizing consumes
714710
// the input and stores it into the resulting rvalue.
715711
self.delegate_consume(expr.hir_id, expr.span, &cmt);

src/librustc/middle/mem_categorization.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -619,12 +619,8 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
619619
}
620620

621621
adjustment::Adjust::NeverToAny |
622-
adjustment::Adjust::ReifyFnPointer |
623-
adjustment::Adjust::UnsafeFnPointer |
624-
adjustment::Adjust::ClosureFnPointer(_) |
625-
adjustment::Adjust::MutToConstPointer |
626-
adjustment::Adjust::Borrow(_) |
627-
adjustment::Adjust::Unsize => {
622+
adjustment::Adjust::Pointer(_) |
623+
adjustment::Adjust::Borrow(_) => {
628624
// Result is an rvalue.
629625
Ok(self.cat_rvalue_node(expr.hir_id, expr.span, target))
630626
}

src/librustc/mir/mod.rs

+3-20
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ use crate::ty::{
3636
UserTypeAnnotationIndex,
3737
};
3838
use crate::ty::print::{FmtPrinter, Printer};
39+
use crate::ty::adjustment::{PointerCast};
3940

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

@@ -2342,29 +2343,11 @@ pub enum Rvalue<'tcx> {
23422343
Aggregate(Box<AggregateKind<'tcx>>, Vec<Operand<'tcx>>),
23432344
}
23442345

2346+
23452347
#[derive(Clone, Copy, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable, HashStable)]
23462348
pub enum CastKind {
23472349
Misc,
2348-
2349-
/// Converts unique, zero-sized type for a fn to fn()
2350-
ReifyFnPointer,
2351-
2352-
/// Converts non capturing closure to fn() or unsafe fn().
2353-
/// It cannot convert a closure that requires unsafe.
2354-
ClosureFnPointer(hir::Unsafety),
2355-
2356-
/// Converts safe fn() to unsafe fn()
2357-
UnsafeFnPointer,
2358-
2359-
/// Coerces *mut T to *const T, preserving T.
2360-
MutToConstPointer,
2361-
2362-
/// "Unsize" -- convert a thin-or-fat pointer to a fat pointer.
2363-
/// codegen must figure out the details once full monomorphization
2364-
/// is known. For example, this could be used to cast from a
2365-
/// `&[i32;N]` to a `&[i32]`, or a `Box<T>` to a `Box<dyn Trait>`
2366-
/// (presuming `T: Trait`).
2367-
Unsize,
2350+
Pointer(PointerCast),
23682351
}
23692352

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

src/librustc/ty/adjustment.rs

+29-24
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,34 @@ 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+
/// Go from a fn-item type to a fn-pointer type.
11+
ReifyFnPointer,
12+
13+
/// Go from a safe fn pointer to an unsafe fn pointer.
14+
UnsafeFnPointer,
15+
16+
/// Go from a non-capturing closure to an fn pointer or an unsafe fn pointer.
17+
/// It cannot convert a closure that requires unsafe.
18+
ClosureFnPointer(hir::Unsafety),
19+
20+
/// Go from a mut raw pointer to a const raw pointer.
21+
MutToConstPointer,
22+
23+
/// Unsize a pointer/reference value, e.g., `&[T; n]` to
24+
/// `&[T]`. Note that the source could be a thin or fat pointer.
25+
/// This will do things like convert thin pointers to fat
26+
/// pointers, or convert structs containing thin pointers to
27+
/// structs containing fat pointers, or convert between fat
28+
/// pointers. We don't store the details of how the transform is
29+
/// done (in fact, we don't know that, because it might depend on
30+
/// the precise type parameters). We just store the target
31+
/// type. Codegen backends and miri figure out what has to be done
32+
/// based on the precise source/target type at hand.
33+
Unsize,
34+
}
35+
836
/// Represents coercing a value to a different type of value.
937
///
1038
/// We transform values by following a number of `Adjust` steps in order.
@@ -56,36 +84,13 @@ pub enum Adjust<'tcx> {
5684
/// Go from ! to any type.
5785
NeverToAny,
5886

59-
/// Go from a fn-item type to a fn-pointer type.
60-
ReifyFnPointer,
61-
62-
/// Go from a safe fn pointer to an unsafe fn pointer.
63-
UnsafeFnPointer,
64-
65-
/// Go from a non-capturing closure to an fn pointer or an unsafe fn pointer.
66-
/// It cannot convert a closure that requires unsafe.
67-
ClosureFnPointer(hir::Unsafety),
68-
69-
/// Go from a mut raw pointer to a const raw pointer.
70-
MutToConstPointer,
71-
7287
/// Dereference once, producing a place.
7388
Deref(Option<OverloadedDeref<'tcx>>),
7489

7590
/// Take the address and produce either a `&` or `*` pointer.
7691
Borrow(AutoBorrow<'tcx>),
7792

78-
/// Unsize a pointer/reference value, e.g., `&[T; n]` to
79-
/// `&[T]`. Note that the source could be a thin or fat pointer.
80-
/// This will do things like convert thin pointers to fat
81-
/// pointers, or convert structs containing thin pointers to
82-
/// structs containing fat pointers, or convert between fat
83-
/// pointers. We don't store the details of how the transform is
84-
/// done (in fact, we don't know that, because it might depend on
85-
/// the precise type parameters). We just store the target
86-
/// type. Codegen backends and miri figure out what has to be done
87-
/// based on the precise source/target type at hand.
88-
Unsize,
93+
Pointer(PointerCast),
8994
}
9095

9196
/// An overloaded autoderef step, representing a `Deref(Mut)::deref(_mut)`

src/librustc/ty/structural_impls.rs

+4-15
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ CloneTypeFoldableAndLiftImpls! {
326326
crate::ty::IntVarValue,
327327
crate::ty::ParamConst,
328328
crate::ty::ParamTy,
329+
crate::ty::adjustment::PointerCast,
329330
crate::ty::RegionVid,
330331
crate::ty::UniverseIndex,
331332
crate::ty::Variance,
@@ -626,16 +627,8 @@ impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::Adjust<'a> {
626627
match *self {
627628
ty::adjustment::Adjust::NeverToAny =>
628629
Some(ty::adjustment::Adjust::NeverToAny),
629-
ty::adjustment::Adjust::ReifyFnPointer =>
630-
Some(ty::adjustment::Adjust::ReifyFnPointer),
631-
ty::adjustment::Adjust::UnsafeFnPointer =>
632-
Some(ty::adjustment::Adjust::UnsafeFnPointer),
633-
ty::adjustment::Adjust::ClosureFnPointer(unsafety) =>
634-
Some(ty::adjustment::Adjust::ClosureFnPointer(unsafety)),
635-
ty::adjustment::Adjust::MutToConstPointer =>
636-
Some(ty::adjustment::Adjust::MutToConstPointer),
637-
ty::adjustment::Adjust::Unsize =>
638-
Some(ty::adjustment::Adjust::Unsize),
630+
ty::adjustment::Adjust::Pointer(ptr) =>
631+
Some(ty::adjustment::Adjust::Pointer(ptr)),
639632
ty::adjustment::Adjust::Deref(ref overloaded) => {
640633
tcx.lift(overloaded).map(ty::adjustment::Adjust::Deref)
641634
}
@@ -1185,11 +1178,7 @@ BraceStructTypeFoldableImpl! {
11851178
EnumTypeFoldableImpl! {
11861179
impl<'tcx> TypeFoldable<'tcx> for ty::adjustment::Adjust<'tcx> {
11871180
(ty::adjustment::Adjust::NeverToAny),
1188-
(ty::adjustment::Adjust::ReifyFnPointer),
1189-
(ty::adjustment::Adjust::UnsafeFnPointer),
1190-
(ty::adjustment::Adjust::ClosureFnPointer)(a),
1191-
(ty::adjustment::Adjust::MutToConstPointer),
1192-
(ty::adjustment::Adjust::Unsize),
1181+
(ty::adjustment::Adjust::Pointer)(a),
11931182
(ty::adjustment::Adjust::Deref)(a),
11941183
(ty::adjustment::Adjust::Borrow)(a),
11951184
}

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_place.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
193193
| ExprKind::Cast { .. }
194194
| ExprKind::Use { .. }
195195
| ExprKind::NeverToAny { .. }
196-
| ExprKind::ReifyFnPointer { .. }
197-
| ExprKind::ClosureFnPointer { .. }
198-
| ExprKind::UnsafeFnPointer { .. }
199-
| ExprKind::MutToConstPointer { .. }
200-
| ExprKind::Unsize { .. }
196+
| ExprKind::Pointer { .. }
201197
| ExprKind::Repeat { .. }
202198
| ExprKind::Borrow { .. }
203199
| ExprKind::If { .. }

src/librustc_mir/build/expr/as_rvalue.rs

+2-18
Original file line numberDiff line numberDiff line change
@@ -154,25 +154,9 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
154154
let source = unpack!(block = this.as_operand(block, scope, source));
155155
block.and(Rvalue::Use(source))
156156
}
157-
ExprKind::ReifyFnPointer { source } => {
157+
ExprKind::Pointer { cast, source } => {
158158
let source = unpack!(block = this.as_operand(block, scope, source));
159-
block.and(Rvalue::Cast(CastKind::ReifyFnPointer, source, expr.ty))
160-
}
161-
ExprKind::UnsafeFnPointer { source } => {
162-
let source = unpack!(block = this.as_operand(block, scope, source));
163-
block.and(Rvalue::Cast(CastKind::UnsafeFnPointer, source, expr.ty))
164-
}
165-
ExprKind::ClosureFnPointer { source, unsafety } => {
166-
let source = unpack!(block = this.as_operand(block, scope, source));
167-
block.and(Rvalue::Cast(CastKind::ClosureFnPointer(unsafety), source, expr.ty))
168-
}
169-
ExprKind::MutToConstPointer { source } => {
170-
let source = unpack!(block = this.as_operand(block, scope, source));
171-
block.and(Rvalue::Cast(CastKind::MutToConstPointer, source, expr.ty))
172-
}
173-
ExprKind::Unsize { source } => {
174-
let source = unpack!(block = this.as_operand(block, scope, source));
175-
block.and(Rvalue::Cast(CastKind::Unsize, source, expr.ty))
159+
block.and(Rvalue::Cast(CastKind::Pointer(cast), source, expr.ty))
176160
}
177161
ExprKind::Array { fields } => {
178162
// (*) We would (maybe) be closer to codegen if we

src/librustc_mir/build/expr/category.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,7 @@ impl Category {
5959
| ExprKind::Box { .. }
6060
| ExprKind::Cast { .. }
6161
| ExprKind::Use { .. }
62-
| ExprKind::ReifyFnPointer { .. }
63-
| ExprKind::ClosureFnPointer { .. }
64-
| ExprKind::UnsafeFnPointer { .. }
65-
| ExprKind::MutToConstPointer { .. }
66-
| ExprKind::Unsize { .. }
62+
| ExprKind::Pointer { .. }
6763
| ExprKind::Repeat { .. }
6864
| ExprKind::Borrow { .. }
6965
| ExprKind::Assign { .. }

src/librustc_mir/build/expr/into.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -380,11 +380,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
380380
| ExprKind::Box { .. }
381381
| ExprKind::Cast { .. }
382382
| ExprKind::Use { .. }
383-
| ExprKind::ReifyFnPointer { .. }
384-
| ExprKind::ClosureFnPointer { .. }
385-
| ExprKind::UnsafeFnPointer { .. }
386-
| ExprKind::MutToConstPointer { .. }
387-
| ExprKind::Unsize { .. }
383+
| ExprKind::Pointer { .. }
388384
| ExprKind::Repeat { .. }
389385
| ExprKind::Borrow { .. }
390386
| ExprKind::Array { .. }

0 commit comments

Comments
 (0)