Skip to content

Commit ed871cb

Browse files
committed
Use a struct for user type annotations
1 parent 5ca6bd5 commit ed871cb

File tree

13 files changed

+70
-31
lines changed

13 files changed

+70
-31
lines changed

src/librustc/ich/impls_ty.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,6 +1240,12 @@ impl_stable_hash_for!(
12401240
}
12411241
);
12421242

1243+
impl_stable_hash_for!(
1244+
struct ty::CanonicalUserTypeAnnotation<'tcx> {
1245+
user_ty, span
1246+
}
1247+
);
1248+
12431249
impl<'a, 'gcx> HashStable<StableHashingContext<'a>> for ty::UserType<'gcx> {
12441250
fn hash_stable<W: StableHasherResult>(&self,
12451251
hcx: &mut StableHashingContext<'a>,

src/librustc/mir/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use ty::subst::{Subst, Substs};
3131
use ty::layout::VariantIdx;
3232
use ty::{
3333
self, AdtDef, CanonicalUserTypeAnnotations, ClosureSubsts, GeneratorSubsts, Region, Ty, TyCtxt,
34-
UserTypeAnnotationIndex, UserType,
34+
UserTypeAnnotationIndex,
3535
};
3636
use util::ppaux;
3737

src/librustc/mir/visit.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use hir::def_id::DefId;
2-
use infer::canonical::Canonical;
32
use ty::subst::Substs;
4-
use ty::{ClosureSubsts, GeneratorSubsts, Region, Ty};
3+
use ty::{CanonicalUserTypeAnnotation, ClosureSubsts, GeneratorSubsts, Region, Ty};
54
use mir::*;
65
use syntax_pos::Span;
76

@@ -221,7 +220,7 @@ macro_rules! make_mir_visitor {
221220
fn visit_user_type_annotation(
222221
&mut self,
223222
index: UserTypeAnnotationIndex,
224-
ty: & $($mutability)* Canonical<'tcx, UserType<'tcx>>,
223+
ty: & $($mutability)* CanonicalUserTypeAnnotation<'tcx>,
225224
) {
226225
self.super_user_type_annotation(index, ty);
227226
}
@@ -309,12 +308,15 @@ macro_rules! make_mir_visitor {
309308
self.visit_local_decl(local, & $($mutability)* mir.local_decls[local]);
310309
}
311310

312-
for index in mir.user_type_annotations.indices() {
313-
let (span, annotation) = & $($mutability)* mir.user_type_annotations[index];
311+
macro_rules! type_annotations {
312+
(mut) => (mir.user_type_annotations.iter_enumerated_mut());
313+
() => (mir.user_type_annotations.iter_enumerated());
314+
};
315+
316+
for (index, annotation) in type_annotations!($($mutability)*) {
314317
self.visit_user_type_annotation(
315318
index, annotation
316319
);
317-
self.visit_span(span);
318320
}
319321

320322
self.visit_span(&$($mutability)* mir.span);
@@ -882,8 +884,9 @@ macro_rules! make_mir_visitor {
882884
fn super_user_type_annotation(
883885
&mut self,
884886
_index: UserTypeAnnotationIndex,
885-
_ty: & $($mutability)* Canonical<'tcx, UserType<'tcx>>,
887+
ty: & $($mutability)* CanonicalUserTypeAnnotation<'tcx>,
886888
) {
889+
self.visit_span(& $($mutability)* ty.span);
887890
}
888891

889892
fn super_ty(&mut self, _ty: & $($mutability)* Ty<'tcx>) {

src/librustc/ty/context.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,27 @@ newtype_index! {
807807

808808
/// Mapping of type annotation indices to canonical user type annotations.
809809
pub type CanonicalUserTypeAnnotations<'tcx> =
810-
IndexVec<UserTypeAnnotationIndex, (Span, CanonicalUserType<'tcx>)>;
810+
IndexVec<UserTypeAnnotationIndex, CanonicalUserTypeAnnotation<'tcx>>;
811+
812+
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
813+
pub struct CanonicalUserTypeAnnotation<'tcx> {
814+
pub user_ty: CanonicalUserType<'tcx>,
815+
pub span: Span,
816+
}
817+
818+
BraceStructTypeFoldableImpl! {
819+
impl<'tcx> TypeFoldable<'tcx> for CanonicalUserTypeAnnotation<'tcx> {
820+
user_ty, span
821+
}
822+
}
823+
824+
BraceStructLiftImpl! {
825+
impl<'a, 'tcx> Lift<'tcx> for CanonicalUserTypeAnnotation<'a> {
826+
type Lifted = CanonicalUserTypeAnnotation<'tcx>;
827+
user_ty, span
828+
}
829+
}
830+
811831

812832
/// Canonicalized user type annotation.
813833
pub type CanonicalUserType<'gcx> = Canonical<'gcx, UserType<'gcx>>;

src/librustc/ty/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub use self::context::{TyCtxt, FreeRegionInfo, GlobalArenas, AllArenas, tls, ke
7474
pub use self::context::{Lift, TypeckTables, CtxtInterners};
7575
pub use self::context::{
7676
UserTypeAnnotationIndex, UserType, CanonicalUserType,
77-
CanonicalUserTypeAnnotations,
77+
CanonicalUserTypeAnnotation, CanonicalUserTypeAnnotations,
7878
};
7979

8080
pub use self::instance::{Instance, InstanceDef};

src/librustc_mir/borrow_check/nll/renumber.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
use rustc::infer::canonical::Canonical;
21
use rustc::ty::subst::Substs;
32
use rustc::ty::{
4-
self, ClosureSubsts, GeneratorSubsts, Ty, TypeFoldable, UserType,
5-
UserTypeAnnotationIndex,
3+
self, ClosureSubsts, GeneratorSubsts, Ty, TypeFoldable,
4+
UserTypeAnnotationIndex, CanonicalUserTypeAnnotation
65
};
76
use rustc::mir::{Location, Mir};
87
use rustc::mir::visit::{MutVisitor, TyContext};
@@ -62,7 +61,7 @@ impl<'a, 'gcx, 'tcx> MutVisitor<'tcx> for NLLVisitor<'a, 'gcx, 'tcx> {
6261
fn visit_user_type_annotation(
6362
&mut self,
6463
_index: UserTypeAnnotationIndex,
65-
_ty: &mut Canonical<'tcx, UserType<'tcx>>,
64+
_ty: &mut CanonicalUserTypeAnnotation,
6665
) {
6766
// User type annotations represent the types that the user
6867
// wrote in the progarm. We don't want to erase the regions

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use rustc::ty::fold::TypeFoldable;
3838
use rustc::ty::subst::{Subst, Substs, UnpackedKind};
3939
use rustc::ty::{
4040
self, RegionVid, ToPolyTraitRef, Ty, TyCtxt, TyKind, UserType,
41-
UserTypeAnnotationIndex,
41+
CanonicalUserTypeAnnotation, UserTypeAnnotationIndex,
4242
};
4343
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
4444
use rustc_data_structures::indexed_vec::{IndexVec, Idx};
@@ -920,13 +920,14 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
920920
self.mir.user_type_annotations
921921
);
922922
for annotation_index in self.mir.user_type_annotations.indices() {
923-
let (span, canonical_annotation) = &self.mir.user_type_annotations[annotation_index];
923+
let CanonicalUserTypeAnnotation { span, ref user_ty } =
924+
self.mir.user_type_annotations[annotation_index];
924925
let (mut annotation, _) = self.infcx.instantiate_canonical_with_fresh_inference_vars(
925-
*span, &canonical_annotation
926+
span, user_ty
926927
);
927928
match annotation {
928929
UserType::Ty(ref mut ty) =>
929-
*ty = self.normalize(ty, Locations::All(*span)),
930+
*ty = self.normalize(ty, Locations::All(span)),
930931
_ => {},
931932
}
932933
self.instantiated_type_annotations.insert(annotation_index, annotation);

src/librustc_mir/build/expr/as_constant.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use build::Builder;
44
use hair::*;
55
use rustc::mir::*;
6+
use rustc::ty::CanonicalUserTypeAnnotation;
67

78
impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
89
/// Compile `expr`, yielding a compile-time constant. Assumes that
@@ -31,7 +32,10 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
3132
} => this.as_constant(value),
3233
ExprKind::Literal { literal, user_ty } => {
3334
let user_ty = user_ty.map(|ty| {
34-
this.canonical_user_type_annotations.push((span, ty))
35+
this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
36+
span,
37+
user_ty: ty,
38+
})
3539
});
3640
Constant {
3741
span,

src/librustc_mir/build/expr/as_place.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use build::{BlockAnd, BlockAndExtension, Builder};
66
use hair::*;
77
use rustc::mir::interpret::EvalErrorKind::BoundsCheck;
88
use rustc::mir::*;
9-
use rustc::ty::Variance;
9+
use rustc::ty::{CanonicalUserTypeAnnotation, Variance};
1010

1111
use rustc_data_structures::indexed_vec::Idx;
1212

@@ -134,7 +134,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
134134
let place = unpack!(block = this.as_place(block, source));
135135
if let Some(user_ty) = user_ty {
136136
let annotation_index = this.canonical_user_type_annotations.push(
137-
(source_info.span, user_ty)
137+
CanonicalUserTypeAnnotation { span: source_info.span, user_ty }
138138
);
139139
this.cfg.push(
140140
block,
@@ -157,7 +157,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
157157
);
158158
if let Some(user_ty) = user_ty {
159159
let annotation_index = this.canonical_user_type_annotations.push(
160-
(source_info.span, user_ty)
160+
CanonicalUserTypeAnnotation { span: source_info.span, user_ty }
161161
);
162162
this.cfg.push(
163163
block,

src/librustc_mir/build/expr/as_rvalue.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use hair::*;
99
use rustc::middle::region;
1010
use rustc::mir::interpret::EvalErrorKind;
1111
use rustc::mir::*;
12-
use rustc::ty::{self, Ty, UpvarSubsts};
12+
use rustc::ty::{self, CanonicalUserTypeAnnotation, Ty, UpvarSubsts};
1313
use syntax_pos::Span;
1414

1515
impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
@@ -332,7 +332,10 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
332332
};
333333

334334
let user_ty = user_ty.map(|ty| {
335-
this.canonical_user_type_annotations.push((expr_span, ty))
335+
this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
336+
span: source_info.span,
337+
user_ty: ty,
338+
})
336339
});
337340
let adt = box AggregateKind::Adt(
338341
adt_def,

src/librustc_mir/build/matches/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use build::{BlockAnd, BlockAndExtension, Builder};
99
use build::{GuardFrame, GuardFrameLocal, LocalsForNode};
1010
use hair::*;
1111
use rustc::mir::*;
12-
use rustc::ty::{self, Ty};
12+
use rustc::ty::{self, CanonicalUserTypeAnnotation, Ty};
1313
use rustc::ty::layout::VariantIdx;
1414
use rustc_data_structures::bit_set::BitSet;
1515
use rustc_data_structures::fx::FxHashMap;
@@ -570,7 +570,10 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
570570
//
571571
// Note that the variance doesn't apply here, as we are tracking the effect
572572
// of `user_ty` on any bindings contained with subpattern.
573-
let annotation = (user_ty_span, user_ty.base);
573+
let annotation = CanonicalUserTypeAnnotation {
574+
span: user_ty_span,
575+
user_ty: user_ty.base,
576+
};
574577
let projection = UserTypeProjection {
575578
base: self.canonical_user_type_annotations.push(annotation),
576579
projs: user_ty.projs.clone(),

src/librustc_mir/hair/pattern/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use hair::constant::*;
1414
use rustc::mir::{fmt_const_val, Field, BorrowKind, Mutability};
1515
use rustc::mir::{ProjectionElem, UserTypeProjection};
1616
use rustc::mir::interpret::{Scalar, GlobalId, ConstValue, sign_extend};
17-
use rustc::ty::{self, Region, TyCtxt, AdtDef, Ty, Lift};
18-
use rustc::ty::{CanonicalUserType, CanonicalUserTypeAnnotations, UserType};
17+
use rustc::ty::{self, Region, TyCtxt, AdtDef, Ty, Lift, UserType};
18+
use rustc::ty::{CanonicalUserType, CanonicalUserTypeAnnotation, CanonicalUserTypeAnnotations};
1919
use rustc::ty::subst::{Substs, Kind};
2020
use rustc::ty::layout::VariantIdx;
2121
use rustc::hir::{self, PatKind, RangeEnd};
@@ -78,7 +78,7 @@ impl<'tcx> PatternTypeProjection<'tcx> {
7878
span: Span,
7979
) -> UserTypeProjection<'tcx> {
8080
UserTypeProjection {
81-
base: annotations.push((span, self.base)),
81+
base: annotations.push(CanonicalUserTypeAnnotation{ span, user_ty: self.base }),
8282
projs: self.projs
8383
}
8484
}

src/librustc_mir/util/pretty.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -632,8 +632,8 @@ fn write_user_type_annotations(mir: &Mir, w: &mut dyn Write) -> io::Result<()> {
632632
if !mir.user_type_annotations.is_empty() {
633633
writeln!(w, "| User Type Annotations")?;
634634
}
635-
for (index, (span, annotation)) in mir.user_type_annotations.iter_enumerated() {
636-
writeln!(w, "| {:?}: {:?} at {:?}", index.index(), annotation, span)?;
635+
for (index, annotation) in mir.user_type_annotations.iter_enumerated() {
636+
writeln!(w, "| {:?}: {:?} at {:?}", index.index(), annotation.user_ty, annotation.span)?;
637637
}
638638
if !mir.user_type_annotations.is_empty() {
639639
writeln!(w, "|")?;

0 commit comments

Comments
 (0)