Skip to content

Commit 28ce99d

Browse files
committed
Added mir::UserTypeProjection, a stub for a structure that projects *into* a given UserTypeAnnotation.
(That is, it will pull out some component type held or referenced by the type annotation.) Note: this still needs to actually do projection itself. That comes in a later commit
1 parent 36d8432 commit 28ce99d

File tree

10 files changed

+70
-32
lines changed

10 files changed

+70
-32
lines changed

src/librustc/ich/impls_mir.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,3 +606,5 @@ impl<'a, 'gcx> HashStable<StableHashingContext<'a>> for mir::UserTypeAnnotation<
606606
}
607607
}
608608
}
609+
610+
impl_stable_hash_for!(struct mir::UserTypeProjection<'tcx> { base });

src/librustc/mir/mod.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,7 @@ pub struct LocalDecl<'tcx> {
710710
/// e.g. via `let x: T`, then we carry that type here. The MIR
711711
/// borrow checker needs this information since it can affect
712712
/// region inference.
713-
pub user_ty: Option<(UserTypeAnnotation<'tcx>, Span)>,
713+
pub user_ty: Option<(UserTypeProjection<'tcx>, Span)>,
714714

715715
/// Name of the local, used in debuginfo and pretty-printing.
716716
///
@@ -1741,7 +1741,7 @@ pub enum StatementKind<'tcx> {
17411741
/// - `Contravariant` -- requires that `T_y :> T`
17421742
/// - `Invariant` -- requires that `T_y == T`
17431743
/// - `Bivariant` -- no effect
1744-
AscribeUserType(Place<'tcx>, ty::Variance, Box<UserTypeAnnotation<'tcx>>),
1744+
AscribeUserType(Place<'tcx>, ty::Variance, Box<UserTypeProjection<'tcx>>),
17451745

17461746
/// No-op. Useful for deleting instructions without affecting statement indices.
17471747
Nop,
@@ -2449,6 +2449,17 @@ EnumLiftImpl! {
24492449
}
24502450
}
24512451

2452+
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
2453+
pub struct UserTypeProjection<'tcx> {
2454+
pub base: UserTypeAnnotation<'tcx>,
2455+
}
2456+
2457+
BraceStructTypeFoldableImpl! {
2458+
impl<'tcx> TypeFoldable<'tcx> for UserTypeProjection<'tcx> {
2459+
base
2460+
}
2461+
}
2462+
24522463
newtype_index! {
24532464
pub struct Promoted {
24542465
DEBUG_FORMAT = "promoted[{}]"

src/librustc/mir/visit.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ macro_rules! make_mir_visitor {
147147
fn visit_ascribe_user_ty(&mut self,
148148
place: & $($mutability)* Place<'tcx>,
149149
variance: & $($mutability)* ty::Variance,
150-
user_ty: & $($mutability)* UserTypeAnnotation<'tcx>,
150+
user_ty: & $($mutability)* UserTypeProjection<'tcx>,
151151
location: Location) {
152152
self.super_ascribe_user_ty(place, variance, user_ty, location);
153153
}
@@ -213,6 +213,13 @@ macro_rules! make_mir_visitor {
213213
self.super_ty(ty);
214214
}
215215

216+
fn visit_user_type_projection(
217+
&mut self,
218+
ty: & $($mutability)* UserTypeProjection<'tcx>,
219+
) {
220+
self.super_user_type_projection(ty);
221+
}
222+
216223
fn visit_user_type_annotation(
217224
&mut self,
218225
ty: & $($mutability)* UserTypeAnnotation<'tcx>,
@@ -639,10 +646,10 @@ macro_rules! make_mir_visitor {
639646
fn super_ascribe_user_ty(&mut self,
640647
place: & $($mutability)* Place<'tcx>,
641648
_variance: & $($mutability)* ty::Variance,
642-
user_ty: & $($mutability)* UserTypeAnnotation<'tcx>,
649+
user_ty: & $($mutability)* UserTypeProjection<'tcx>,
643650
location: Location) {
644651
self.visit_place(place, PlaceContext::Validate, location);
645-
self.visit_user_type_annotation(user_ty);
652+
self.visit_user_type_projection(user_ty);
646653
}
647654

648655
fn super_place(&mut self,
@@ -737,7 +744,7 @@ macro_rules! make_mir_visitor {
737744
source_info: *source_info,
738745
});
739746
if let Some((user_ty, _)) = user_ty {
740-
self.visit_user_type_annotation(user_ty);
747+
self.visit_user_type_projection(user_ty);
741748
}
742749
self.visit_source_info(source_info);
743750
self.visit_source_scope(visibility_scope);
@@ -784,6 +791,16 @@ macro_rules! make_mir_visitor {
784791
self.visit_source_scope(scope);
785792
}
786793

794+
fn super_user_type_projection(
795+
&mut self,
796+
ty: & $($mutability)* UserTypeProjection<'tcx>,
797+
) {
798+
let UserTypeProjection {
799+
ref $($mutability)* base,
800+
} = *ty;
801+
self.visit_user_type_annotation(base)
802+
}
803+
787804
fn super_user_type_annotation(
788805
&mut self,
789806
_ty: & $($mutability)* UserTypeAnnotation<'tcx>,

src/librustc_mir/borrow_check/nll/constraint_generation.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc::mir::visit::TyContext;
1818
use rustc::mir::visit::Visitor;
1919
use rustc::mir::{BasicBlock, BasicBlockData, Location, Mir, Place, Rvalue};
2020
use rustc::mir::{Statement, Terminator};
21-
use rustc::mir::UserTypeAnnotation;
21+
use rustc::mir::UserTypeProjection;
2222
use rustc::ty::fold::TypeFoldable;
2323
use rustc::ty::subst::Substs;
2424
use rustc::ty::{self, ClosureSubsts, GeneratorSubsts, RegionVid};
@@ -183,7 +183,7 @@ impl<'cg, 'cx, 'gcx, 'tcx> Visitor<'tcx> for ConstraintGeneration<'cg, 'cx, 'gcx
183183
&mut self,
184184
_place: &Place<'tcx>,
185185
_variance: &ty::Variance,
186-
_user_ty: &UserTypeAnnotation<'tcx>,
186+
_user_ty: &UserTypeProjection<'tcx>,
187187
_location: Location,
188188
) {
189189
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ impl<'a, 'b, 'gcx, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'gcx, 'tcx> {
284284
if let Err(terr) = self.cx.relate_type_and_user_type(
285285
constant.ty,
286286
ty::Variance::Invariant,
287-
user_ty,
287+
UserTypeProjection { base: user_ty },
288288
location.to_locations(),
289289
ConstraintCategory::Boring,
290290
) {
@@ -971,7 +971,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
971971
&mut self,
972972
a: Ty<'tcx>,
973973
v: ty::Variance,
974-
user_ty: UserTypeAnnotation<'tcx>,
974+
user_ty: UserTypeProjection<'tcx>,
975975
locations: Locations,
976976
category: ConstraintCategory,
977977
) -> Fallible<()> {
@@ -980,7 +980,8 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
980980
a, v, user_ty, locations,
981981
);
982982

983-
match user_ty {
983+
// FIXME
984+
match user_ty.base {
984985
UserTypeAnnotation::Ty(canonical_ty) => {
985986
let (ty, _) = self.infcx
986987
.instantiate_canonical_with_fresh_inference_vars(DUMMY_SP, &canonical_ty);
@@ -1172,7 +1173,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
11721173
if let Err(terr) = self.relate_type_and_user_type(
11731174
rv_ty,
11741175
ty::Variance::Invariant,
1175-
user_ty,
1176+
UserTypeProjection { base: user_ty },
11761177
location.to_locations(),
11771178
ConstraintCategory::Boring,
11781179
) {

src/librustc_mir/build/expr/as_place.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
147147
kind: StatementKind::AscribeUserType(
148148
place.clone(),
149149
Variance::Invariant,
150-
box user_ty,
150+
box UserTypeProjection { base: user_ty },
151151
),
152152
},
153153
);
@@ -167,7 +167,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
167167
kind: StatementKind::AscribeUserType(
168168
Place::Local(temp.clone()),
169169
Variance::Invariant,
170-
box user_ty,
170+
box UserTypeProjection { base: user_ty },
171171
),
172172
},
173173
);

src/librustc_mir/build/matches/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
491491
pub(super) fn visit_bindings(
492492
&mut self,
493493
pattern: &Pattern<'tcx>,
494-
mut pattern_user_ty: Option<(PatternTypeAnnotation<'tcx>, Span)>,
494+
mut pattern_user_ty: Option<(PatternTypeProjection<'tcx>, Span)>,
495495
f: &mut impl FnMut(
496496
&mut Self,
497497
Mutability,
@@ -500,7 +500,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
500500
NodeId,
501501
Span,
502502
Ty<'tcx>,
503-
Option<(PatternTypeAnnotation<'tcx>, Span)>,
503+
Option<(PatternTypeProjection<'tcx>, Span)>,
504504
),
505505
) {
506506
match *pattern.kind {
@@ -626,7 +626,7 @@ struct Binding<'tcx> {
626626
struct Ascription<'tcx> {
627627
span: Span,
628628
source: Place<'tcx>,
629-
user_ty: PatternTypeAnnotation<'tcx>,
629+
user_ty: PatternTypeProjection<'tcx>,
630630
}
631631

632632
#[derive(Clone, Debug)]
@@ -1470,7 +1470,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
14701470
num_patterns: usize,
14711471
var_id: NodeId,
14721472
var_ty: Ty<'tcx>,
1473-
user_var_ty: Option<(PatternTypeAnnotation<'tcx>, Span)>,
1473+
user_var_ty: Option<(PatternTypeProjection<'tcx>, Span)>,
14741474
has_guard: ArmHasGuard,
14751475
opt_match_place: Option<(Option<Place<'tcx>>, Span)>,
14761476
pat_span: Span,
@@ -1489,7 +1489,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
14891489
let local = LocalDecl::<'tcx> {
14901490
mutability,
14911491
ty: var_ty,
1492-
user_ty: user_var_ty.map(|(pat_ty, span)|(pat_ty.user_ty(), span)),
1492+
user_ty: user_var_ty.map(|(ut, sp)| (ut.user_ty(), sp)),
14931493
name: Some(name),
14941494
source_info,
14951495
visibility_scope,

src/librustc_mir/hair/cx/block.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ fn mirror_stmts<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
9191
ty: pattern.ty,
9292
span: pattern.span,
9393
kind: Box::new(PatternKind::AscribeUserType {
94-
user_ty: PatternTypeAnnotation::from_c_ty(user_ty),
94+
user_ty: PatternTypeProjection::from_canonical_ty(user_ty),
9595
user_ty_span: ty.span,
9696
subpattern: pattern
9797
})

src/librustc_mir/hair/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use self::cx::Cx;
2727
pub mod cx;
2828

2929
pub mod pattern;
30-
pub use self::pattern::{BindingMode, Pattern, PatternKind, PatternTypeAnnotation, FieldPattern};
30+
pub use self::pattern::{BindingMode, Pattern, PatternKind, PatternTypeProjection, FieldPattern};
3131

3232
mod util;
3333

src/librustc_mir/hair/pattern/mod.rs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ use const_eval::{const_field, const_variant_index};
2020

2121
use hair::util::UserAnnotatedTyHelpers;
2222

23-
use rustc::mir::{fmt_const_val, Field, BorrowKind, Mutability, UserTypeAnnotation};
23+
use rustc::mir::{fmt_const_val, Field, BorrowKind, Mutability};
24+
use rustc::mir::{UserTypeAnnotation, UserTypeProjection};
2425
use rustc::mir::interpret::{Scalar, GlobalId, ConstValue, sign_extend};
2526
use rustc::ty::{self, Region, TyCtxt, AdtDef, Ty};
2627
use rustc::ty::subst::{Substs, Kind};
@@ -65,25 +66,30 @@ pub struct Pattern<'tcx> {
6566
}
6667

6768
#[derive(Copy, Clone, Debug)]
68-
pub struct PatternTypeAnnotation<'tcx>(UserTypeAnnotation<'tcx>);
69+
pub struct PatternTypeProjection<'tcx>(UserTypeProjection<'tcx>);
6970

70-
impl<'tcx> PatternTypeAnnotation<'tcx> {
71-
pub(crate) fn from_c_ty(c_ty: ty::CanonicalTy<'tcx>) -> Self {
72-
Self::from_u_ty(UserTypeAnnotation::Ty(c_ty))
71+
impl<'tcx> PatternTypeProjection<'tcx> {
72+
pub(crate) fn from_canonical_ty(c_ty: ty::CanonicalTy<'tcx>) -> Self {
73+
Self::from_user_type(UserTypeAnnotation::Ty(c_ty))
7374
}
74-
pub(crate) fn from_u_ty(u_ty: UserTypeAnnotation<'tcx>) -> Self {
75-
PatternTypeAnnotation(u_ty)
75+
76+
pub(crate) fn from_user_type(u_ty: UserTypeAnnotation<'tcx>) -> Self {
77+
Self::from_user_type_proj(UserTypeProjection { base: u_ty })
78+
}
79+
80+
pub(crate) fn from_user_type_proj(u_ty: UserTypeProjection<'tcx>) -> Self {
81+
PatternTypeProjection(u_ty)
7682
}
7783

78-
pub(crate) fn user_ty(self) -> UserTypeAnnotation<'tcx> { self.0 }
84+
pub(crate) fn user_ty(self) -> UserTypeProjection<'tcx> { self.0 }
7985
}
8086

8187
#[derive(Clone, Debug)]
8288
pub enum PatternKind<'tcx> {
8389
Wild,
8490

8591
AscribeUserType {
86-
user_ty: PatternTypeAnnotation<'tcx>,
92+
user_ty: PatternTypeProjection<'tcx>,
8793
subpattern: Pattern<'tcx>,
8894
user_ty_span: Span,
8995
},
@@ -704,7 +710,7 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
704710

705711
debug!("pattern user_ty = {:?} for pattern at {:?}", user_ty, span);
706712

707-
let pat_ty = PatternTypeAnnotation::from_u_ty(user_ty);
713+
let pat_ty = PatternTypeProjection::from_user_type(user_ty);
708714
kind = PatternKind::AscribeUserType {
709715
subpattern,
710716
user_ty: pat_ty,
@@ -995,7 +1001,8 @@ macro_rules! CloneImpls {
9951001
CloneImpls!{ <'tcx>
9961002
Span, Field, Mutability, ast::Name, ast::NodeId, usize, &'tcx ty::Const<'tcx>,
9971003
Region<'tcx>, Ty<'tcx>, BindingMode<'tcx>, &'tcx AdtDef,
998-
&'tcx Substs<'tcx>, &'tcx Kind<'tcx>, UserTypeAnnotation<'tcx>, PatternTypeAnnotation<'tcx>
1004+
&'tcx Substs<'tcx>, &'tcx Kind<'tcx>, UserTypeAnnotation<'tcx>,
1005+
UserTypeProjection<'tcx>, PatternTypeProjection<'tcx>
9991006
}
10001007

10011008
impl<'tcx> PatternFoldable<'tcx> for FieldPattern<'tcx> {

0 commit comments

Comments
 (0)