Skip to content

Commit 6321a32

Browse files
author
Saleem Jaffer
committed
refactor Adjustment to use new PointerCast enum
1 parent 5be6b0b commit 6321a32

File tree

11 files changed

+85
-84
lines changed

11 files changed

+85
-84
lines changed

src/librustc/middle/expr_use_visitor.rs

Lines changed: 1 addition & 5 deletions
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

Lines changed: 2 additions & 6 deletions
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/ty/adjustment.rs

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,29 @@ use rustc_macros::HashStable;
77

88
#[derive(Clone, Copy, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable, HashStable)]
99
pub enum PointerCast {
10+
/// Go from a fn-item type to a fn-pointer type.
1011
ReifyFnPointer,
12+
13+
/// Go from a safe fn pointer to an unsafe fn pointer.
1114
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.
1218
ClosureFnPointer(hir::Unsafety),
19+
20+
/// Go from a mut raw pointer to a const raw pointer.
1321
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.
1433
Unsize,
1534
}
1635

@@ -65,36 +84,13 @@ pub enum Adjust<'tcx> {
6584
/// Go from ! to any type.
6685
NeverToAny,
6786

68-
/// Go from a fn-item type to a fn-pointer type.
69-
ReifyFnPointer,
70-
71-
/// Go from a safe fn pointer to an unsafe fn pointer.
72-
UnsafeFnPointer,
73-
74-
/// Go from a non-capturing closure to an fn pointer or an unsafe fn pointer.
75-
/// It cannot convert a closure that requires unsafe.
76-
ClosureFnPointer(hir::Unsafety),
77-
78-
/// Go from a mut raw pointer to a const raw pointer.
79-
MutToConstPointer,
80-
8187
/// Dereference once, producing a place.
8288
Deref(Option<OverloadedDeref<'tcx>>),
8389

8490
/// Take the address and produce either a `&` or `*` pointer.
8591
Borrow(AutoBorrow<'tcx>),
8692

87-
/// Unsize a pointer/reference value, e.g., `&[T; n]` to
88-
/// `&[T]`. Note that the source could be a thin or fat pointer.
89-
/// This will do things like convert thin pointers to fat
90-
/// pointers, or convert structs containing thin pointers to
91-
/// structs containing fat pointers, or convert between fat
92-
/// pointers. We don't store the details of how the transform is
93-
/// done (in fact, we don't know that, because it might depend on
94-
/// the precise type parameters). We just store the target
95-
/// type. Codegen backends and miri figure out what has to be done
96-
/// based on the precise source/target type at hand.
97-
Unsize,
93+
Pointer(PointerCast),
9894
}
9995

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

src/librustc/ty/structural_impls.rs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::hir::def::Namespace;
77
use crate::mir::ProjectionKind;
88
use crate::mir::interpret::ConstValue;
99
use crate::ty::{self, Lift, Ty, TyCtxt, ConstVid};
10+
use crate::ty::adjustment::{PointerCast};
1011
use crate::ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
1112
use crate::ty::print::{FmtPrinter, Printer};
1213
use rustc_data_structures::indexed_vec::{IndexVec, Idx};
@@ -626,16 +627,16 @@ 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(PointerCast::ReifyFnPointer) =>
631+
Some(ty::adjustment::Adjust::Pointer(PointerCast::ReifyFnPointer)),
632+
ty::adjustment::Adjust::Pointer(PointerCast::UnsafeFnPointer) =>
633+
Some(ty::adjustment::Adjust::Pointer(PointerCast::UnsafeFnPointer)),
634+
ty::adjustment::Adjust::Pointer(PointerCast::ClosureFnPointer(unsafety)) =>
635+
Some(ty::adjustment::Adjust::Pointer(PointerCast::ClosureFnPointer(unsafety))),
636+
ty::adjustment::Adjust::Pointer(PointerCast::MutToConstPointer) =>
637+
Some(ty::adjustment::Adjust::Pointer(PointerCast::MutToConstPointer)),
638+
ty::adjustment::Adjust::Pointer(PointerCast::Unsize) =>
639+
Some(ty::adjustment::Adjust::Pointer(PointerCast::Unsize)),
639640
ty::adjustment::Adjust::Deref(ref overloaded) => {
640641
tcx.lift(overloaded).map(ty::adjustment::Adjust::Deref)
641642
}
@@ -1185,16 +1186,22 @@ BraceStructTypeFoldableImpl! {
11851186
EnumTypeFoldableImpl! {
11861187
impl<'tcx> TypeFoldable<'tcx> for ty::adjustment::Adjust<'tcx> {
11871188
(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),
1189+
(ty::adjustment::Adjust::Pointer)(a),
11931190
(ty::adjustment::Adjust::Deref)(a),
11941191
(ty::adjustment::Adjust::Borrow)(a),
11951192
}
11961193
}
11971194

1195+
EnumTypeFoldableImpl! {
1196+
impl<'tcx> TypeFoldable<'tcx> for ty::adjustment::PointerCast {
1197+
(ty::adjustment::PointerCast::ReifyFnPointer),
1198+
(ty::adjustment::PointerCast::UnsafeFnPointer),
1199+
(ty::adjustment::PointerCast::ClosureFnPointer)(a),
1200+
(ty::adjustment::PointerCast::MutToConstPointer),
1201+
(ty::adjustment::PointerCast::Unsize),
1202+
}
1203+
}
1204+
11981205
BraceStructTypeFoldableImpl! {
11991206
impl<'tcx> TypeFoldable<'tcx> for ty::adjustment::OverloadedDeref<'tcx> {
12001207
region, mutbl,

src/librustc_mir/hair/cx/expr.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_data_structures::indexed_vec::Idx;
77
use rustc::hir::def::{CtorOf, Def, CtorKind};
88
use rustc::mir::interpret::{GlobalId, ErrorHandled, ConstValue};
99
use rustc::ty::{self, AdtKind, Ty};
10-
use rustc::ty::adjustment::{Adjustment, Adjust, AutoBorrow, AutoBorrowMutability};
10+
use rustc::ty::adjustment::{Adjustment, Adjust, AutoBorrow, AutoBorrowMutability, PointerCast};
1111
use rustc::ty::subst::{InternalSubsts, SubstsRef};
1212
use rustc::hir;
1313
use rustc::hir::def_id::LocalDefId;
@@ -75,19 +75,19 @@ fn apply_adjustment<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
7575
-> Expr<'tcx> {
7676
let Expr { temp_lifetime, mut span, .. } = expr;
7777
let kind = match adjustment.kind {
78-
Adjust::ReifyFnPointer => {
78+
Adjust::Pointer(PointerCast::ReifyFnPointer) => {
7979
ExprKind::ReifyFnPointer { source: expr.to_ref() }
8080
}
81-
Adjust::UnsafeFnPointer => {
81+
Adjust::Pointer(PointerCast::UnsafeFnPointer) => {
8282
ExprKind::UnsafeFnPointer { source: expr.to_ref() }
8383
}
84-
Adjust::ClosureFnPointer(unsafety) => {
84+
Adjust::Pointer(PointerCast::ClosureFnPointer(unsafety)) => {
8585
ExprKind::ClosureFnPointer { source: expr.to_ref(), unsafety }
8686
}
8787
Adjust::NeverToAny => {
8888
ExprKind::NeverToAny { source: expr.to_ref() }
8989
}
90-
Adjust::MutToConstPointer => {
90+
Adjust::Pointer(PointerCast::MutToConstPointer) => {
9191
ExprKind::MutToConstPointer { source: expr.to_ref() }
9292
}
9393
Adjust::Deref(None) => {
@@ -187,7 +187,7 @@ fn apply_adjustment<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
187187
// since they get rid of a borrow implicitly.
188188
ExprKind::Use { source: cast_expr.to_ref() }
189189
}
190-
Adjust::Unsize => {
190+
Adjust::Pointer(PointerCast::Unsize) => {
191191
// See the above comment for Adjust::Deref
192192
if let ExprKind::Block { body } = expr.kind {
193193
if let Some(ref last_expr) = body.expr {

src/librustc_mir/transform/qualify_consts.rs

Lines changed: 2 additions & 6 deletions
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, adjustment::{PointerCast}};
15+
use rustc::ty::{self, TyCtxt, Ty, TypeFoldable};
1616
use rustc::ty::cast::CastTy;
1717
use rustc::ty::query::Providers;
1818
use rustc::mir::*;
@@ -1106,11 +1106,7 @@ 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::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), ..) |
1109+
Rvalue::Cast(CastKind::Pointer(_), ..) |
11141110
Rvalue::Discriminant(..) |
11151111
Rvalue::Len(_) |
11161112
Rvalue::Ref(..) |

src/librustc_passes/rvalue_promotion.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -584,12 +584,8 @@ fn check_adjustments<'a, 'tcx>(
584584
while let Some(adjustment) = adjustments.next() {
585585
match adjustment.kind {
586586
Adjust::NeverToAny |
587-
Adjust::ReifyFnPointer |
588-
Adjust::UnsafeFnPointer |
589-
Adjust::ClosureFnPointer(_) |
590-
Adjust::MutToConstPointer |
591-
Adjust::Borrow(_) |
592-
Adjust::Unsize => {}
587+
Adjust::Pointer(_) |
588+
Adjust::Borrow(_) => {}
593589

594590
Adjust::Deref(_) => {
595591
if let Some(next_adjustment) = adjustments.peek() {

src/librustc_typeck/check/coercion.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ use rustc::hir::def_id::DefId;
5757
use rustc::infer::{Coercion, InferResult, InferOk};
5858
use rustc::infer::type_variable::TypeVariableOrigin;
5959
use rustc::traits::{self, ObligationCause, ObligationCauseCode};
60-
use rustc::ty::adjustment::{Adjustment, Adjust, AllowTwoPhase, AutoBorrow, AutoBorrowMutability};
60+
use rustc::ty::adjustment::{
61+
Adjustment, Adjust, AllowTwoPhase, AutoBorrow, AutoBorrowMutability, PointerCast
62+
};
6163
use rustc::ty::{self, TypeAndMut, Ty, ClosureSubsts};
6264
use rustc::ty::fold::TypeFoldable;
6365
use rustc::ty::error::TypeError;
@@ -512,7 +514,7 @@ impl<'f, 'gcx, 'tcx> Coerce<'f, 'gcx, 'tcx> {
512514
let coerce_target = self.next_ty_var(origin);
513515
let mut coercion = self.unify_and(coerce_target, target, |target| {
514516
let unsize = Adjustment {
515-
kind: Adjust::Unsize,
517+
kind: Adjust::Pointer(PointerCast::Unsize),
516518
target
517519
};
518520
match reborrow {
@@ -661,7 +663,7 @@ impl<'f, 'gcx, 'tcx> Coerce<'f, 'gcx, 'tcx> {
661663
debug!("coerce_from_fn_pointer(a={:?}, b={:?})", a, b);
662664

663665
self.coerce_from_safe_fn(a, fn_ty_a, b,
664-
simple(Adjust::UnsafeFnPointer), identity)
666+
simple(Adjust::Pointer(PointerCast::UnsafeFnPointer)), identity)
665667
}
666668

667669
fn coerce_from_fn_item(&self,
@@ -687,11 +689,17 @@ impl<'f, 'gcx, 'tcx> Coerce<'f, 'gcx, 'tcx> {
687689
b,
688690
|unsafe_ty| {
689691
vec![
690-
Adjustment { kind: Adjust::ReifyFnPointer, target: a_fn_pointer },
691-
Adjustment { kind: Adjust::UnsafeFnPointer, target: unsafe_ty },
692+
Adjustment {
693+
kind: Adjust::Pointer(PointerCast::ReifyFnPointer),
694+
target: a_fn_pointer
695+
},
696+
Adjustment {
697+
kind: Adjust::Pointer(PointerCast::UnsafeFnPointer),
698+
target: unsafe_ty
699+
},
692700
]
693701
},
694-
simple(Adjust::ReifyFnPointer)
702+
simple(Adjust::Pointer(PointerCast::ReifyFnPointer))
695703
)?;
696704

697705
obligations.extend(o2);
@@ -727,7 +735,9 @@ impl<'f, 'gcx, 'tcx> Coerce<'f, 'gcx, 'tcx> {
727735
let pointer_ty = self.tcx.coerce_closure_fn_ty(sig, unsafety);
728736
debug!("coerce_closure_to_fn(a={:?}, b={:?}, pty={:?})",
729737
a, b, pointer_ty);
730-
self.unify_and(pointer_ty, b, simple(Adjust::ClosureFnPointer(unsafety)))
738+
self.unify_and(pointer_ty, b, simple(
739+
Adjust::Pointer(PointerCast::ClosureFnPointer(unsafety))
740+
))
731741
}
732742
_ => self.unify_and(a, b, identity),
733743
}
@@ -766,7 +776,9 @@ impl<'f, 'gcx, 'tcx> Coerce<'f, 'gcx, 'tcx> {
766776
}]
767777
})
768778
} else if mt_a.mutbl != mutbl_b {
769-
self.unify_and(a_unsafe, b, simple(Adjust::MutToConstPointer))
779+
self.unify_and(
780+
a_unsafe, b, simple(Adjust::Pointer(PointerCast::MutToConstPointer))
781+
)
770782
} else {
771783
self.unify_and(a_unsafe, b, identity)
772784
}
@@ -857,7 +869,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
857869
// The only adjustment that can produce an fn item is
858870
// `NeverToAny`, so this should always be valid.
859871
self.apply_adjustments(expr, vec![Adjustment {
860-
kind: Adjust::ReifyFnPointer,
872+
kind: Adjust::Pointer(PointerCast::ReifyFnPointer),
861873
target: fn_ptr
862874
}]);
863875
}

src/librustc_typeck/check/method/confirm.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::hir::def_id::DefId;
77
use rustc::ty::subst::{Subst, SubstsRef};
88
use rustc::traits;
99
use rustc::ty::{self, Ty, GenericParamDefKind};
10-
use rustc::ty::adjustment::{Adjustment, Adjust, OverloadedDeref};
10+
use rustc::ty::adjustment::{Adjustment, Adjust, OverloadedDeref, PointerCast};
1111
use rustc::ty::adjustment::{AllowTwoPhase, AutoBorrow, AutoBorrowMutability};
1212
use rustc::ty::fold::TypeFoldable;
1313
use rustc::infer::{self, InferOk};
@@ -179,7 +179,7 @@ impl<'a, 'gcx, 'tcx> ConfirmContext<'a, 'gcx, 'tcx> {
179179
ty: unsize_target
180180
});
181181
adjustments.push(Adjustment {
182-
kind: Adjust::Unsize,
182+
kind: Adjust::Pointer(PointerCast::Unsize),
183183
target
184184
});
185185
}
@@ -565,7 +565,7 @@ impl<'a, 'gcx, 'tcx> ConfirmContext<'a, 'gcx, 'tcx> {
565565
// If we have an autoref followed by unsizing at the end, fix the unsize target.
566566
match adjustments[..] {
567567
[.., Adjustment { kind: Adjust::Borrow(AutoBorrow::Ref(..)), .. },
568-
Adjustment { kind: Adjust::Unsize, ref mut target }] => {
568+
Adjustment { kind: Adjust::Pointer(PointerCast::Unsize), ref mut target }] => {
569569
*target = method.sig.inputs()[0];
570570
}
571571
_ => {}

src/librustc_typeck/check/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ use rustc::ty::{
106106
self, AdtKind, CanonicalUserType, Ty, TyCtxt, GenericParamDefKind, Visibility,
107107
ToPolyTraitRef, ToPredicate, RegionKind, UserType
108108
};
109-
use rustc::ty::adjustment::{Adjust, Adjustment, AllowTwoPhase, AutoBorrow, AutoBorrowMutability};
109+
use rustc::ty::adjustment::{
110+
Adjust, Adjustment, AllowTwoPhase, AutoBorrow, AutoBorrowMutability, PointerCast
111+
};
110112
use rustc::ty::fold::TypeFoldable;
111113
use rustc::ty::query::Providers;
112114
use rustc::ty::subst::{UnpackedKind, Subst, InternalSubsts, SubstsRef, UserSelfTy, UserSubsts};
@@ -2664,7 +2666,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
26642666
}
26652667
if unsize {
26662668
adjustments.push(Adjustment {
2667-
kind: Adjust::Unsize,
2669+
kind: Adjust::Pointer(PointerCast::Unsize),
26682670
target: method.sig.inputs()[0]
26692671
});
26702672
}

src/librustc_typeck/check/writeback.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc::hir;
88
use rustc::hir::def_id::{DefId, DefIndex};
99
use rustc::hir::intravisit::{self, NestedVisitorMap, Visitor};
1010
use rustc::infer::InferCtxt;
11-
use rustc::ty::adjustment::{Adjust, Adjustment};
11+
use rustc::ty::adjustment::{Adjust, Adjustment, PointerCast};
1212
use rustc::ty::fold::{BottomUpFolder, TypeFoldable, TypeFolder};
1313
use rustc::ty::subst::UnpackedKind;
1414
use rustc::ty::{self, Ty, TyCtxt};
@@ -197,7 +197,7 @@ impl<'cx, 'gcx, 'tcx> WritebackCx<'cx, 'gcx, 'tcx> {
197197
// Since this is "after" the other adjustment to be
198198
// discarded, we do an extra `pop()`
199199
Some(Adjustment {
200-
kind: Adjust::Unsize,
200+
kind: Adjust::Pointer(PointerCast::Unsize),
201201
..
202202
}) => {
203203
// So the borrow discard actually happens here

0 commit comments

Comments
 (0)