Skip to content

Commit 2a7fc22

Browse files
committed
introduce FnDef and AdtDef to UserTypeAnnotation
1 parent aed6e4a commit 2a7fc22

File tree

5 files changed

+32
-40
lines changed

5 files changed

+32
-40
lines changed

src/librustc/ich/impls_mir.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,14 @@ impl<'a, 'gcx> HashStable<StableHashingContext<'a>> for mir::UserTypeAnnotation<
597597
mir::UserTypeAnnotation::Ty(ref ty) => {
598598
ty.hash_stable(hcx, hasher);
599599
}
600+
mir::UserTypeAnnotation::FnDef(ref def_id, ref substs) => {
601+
def_id.hash_stable(hcx, hasher);
602+
substs.hash_stable(hcx, hasher);
603+
}
604+
mir::UserTypeAnnotation::AdtDef(ref def_id, ref substs) => {
605+
def_id.hash_stable(hcx, hasher);
606+
substs.hash_stable(hcx, hasher);
607+
}
600608
}
601609
}
602610
}

src/librustc/mir/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use syntax::ast::{self, Name};
3737
use syntax::symbol::InternedString;
3838
use syntax_pos::{Span, DUMMY_SP};
3939
use ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
40-
use ty::subst::{Subst, Substs};
40+
use ty::subst::{CanonicalSubsts, Subst, Substs};
4141
use ty::{self, AdtDef, CanonicalTy, ClosureSubsts, GeneratorSubsts, Region, Ty, TyCtxt};
4242
use util::ppaux;
4343

@@ -2413,11 +2413,15 @@ pub struct Constant<'tcx> {
24132413
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
24142414
pub enum UserTypeAnnotation<'tcx> {
24152415
Ty(CanonicalTy<'tcx>),
2416+
FnDef(DefId, CanonicalSubsts<'tcx>),
2417+
AdtDef(&'tcx AdtDef, CanonicalSubsts<'tcx>),
24162418
}
24172419

24182420
EnumTypeFoldableImpl! {
24192421
impl<'tcx> TypeFoldable<'tcx> for UserTypeAnnotation<'tcx> {
24202422
(UserTypeAnnotation::Ty)(ty),
2423+
(UserTypeAnnotation::FnDef)(def, substs),
2424+
(UserTypeAnnotation::AdtDef)(def, substs),
24212425
}
24222426
}
24232427

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,21 @@ pub(super) fn relate_type_and_user_type<'tcx>(
7171
a, v, user_ty, locations
7272
);
7373

74-
let (b, _values) = match user_ty {
74+
let b = match user_ty {
7575
UserTypeAnnotation::Ty(canonical_ty) => {
76-
infcx.instantiate_canonical_with_fresh_inference_vars(DUMMY_SP, &canonical_ty)
76+
let (ty, _) =
77+
infcx.instantiate_canonical_with_fresh_inference_vars(DUMMY_SP, &canonical_ty);
78+
ty
79+
}
80+
UserTypeAnnotation::FnDef(def_id, canonical_substs) => {
81+
let (substs, _) =
82+
infcx.instantiate_canonical_with_fresh_inference_vars(DUMMY_SP, &canonical_substs);
83+
infcx.tcx.mk_fn_def(def_id, substs)
84+
}
85+
UserTypeAnnotation::AdtDef(adt_def, canonical_substs) => {
86+
let (substs, _) =
87+
infcx.instantiate_canonical_with_fresh_inference_vars(DUMMY_SP, &canonical_substs);
88+
infcx.tcx.mk_adt(adt_def, substs)
7789
}
7890
};
7991

src/librustc_mir/hair/cx/expr.rs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -295,13 +295,7 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
295295
let substs = cx.tables().node_substs(fun.hir_id);
296296

297297
let user_ty = cx.tables().user_substs(fun.hir_id)
298-
.map(|user_substs| {
299-
UserTypeAnnotation::Ty(user_substs.unchecked_map(|user_substs| {
300-
// Here, we just pair an `AdtDef` with the
301-
// `user_substs`, so no new types etc are introduced.
302-
cx.tcx().mk_adt(adt_def, user_substs)
303-
}))
304-
});
298+
.map(|user_substs| UserTypeAnnotation::AdtDef(adt_def, user_substs));
305299

306300
let field_refs = args.iter()
307301
.enumerate()
@@ -774,14 +768,7 @@ fn user_substs_applied_to_def(
774768
Def::Method(_) |
775769
Def::StructCtor(_, CtorKind::Fn) |
776770
Def::VariantCtor(_, CtorKind::Fn) =>
777-
Some(
778-
UserTypeAnnotation::Ty(cx.tables().user_substs(hir_id)?.unchecked_map(|user_substs| {
779-
// Here, we just pair a `DefId` with the
780-
// `user_substs`, so no new types etc are introduced.
781-
cx.tcx().mk_fn_def(def.def_id(), user_substs)
782-
}),
783-
)
784-
),
771+
Some(UserTypeAnnotation::FnDef(def.def_id(), cx.tables().user_substs(hir_id)?)),
785772

786773
Def::Const(_def_id) |
787774
Def::AssociatedConst(_def_id) =>

src/librustc_mir/hair/util.rs

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,7 @@ crate trait UserAnnotatedTyHelpers<'gcx: 'tcx, 'tcx> {
2323
adt_def: &'tcx AdtDef,
2424
) -> Option<UserTypeAnnotation<'tcx>> {
2525
let user_substs = self.tables().user_substs(hir_id)?;
26-
Some(UserTypeAnnotation::Ty(user_substs.unchecked_map(
27-
|user_substs| {
28-
// Here, we just pair an `AdtDef` with the
29-
// `user_substs`, so no new types etc are introduced.
30-
self.tcx().mk_adt(adt_def, user_substs)
31-
},
32-
)))
26+
Some(UserTypeAnnotation::AdtDef(adt_def, user_substs))
3327
}
3428

3529
/// Looks up the type associated with this hir-id and applies the
@@ -41,21 +35,8 @@ crate trait UserAnnotatedTyHelpers<'gcx: 'tcx, 'tcx> {
4135
) -> Option<UserTypeAnnotation<'tcx>> {
4236
let user_substs = self.tables().user_substs(hir_id)?;
4337
match &self.tables().node_id_to_type(hir_id).sty {
44-
ty::Adt(adt_def, _) => Some(UserTypeAnnotation::Ty(user_substs.unchecked_map(
45-
|user_substs| {
46-
// Ok to call `unchecked_map` because we just pair an
47-
// `AdtDef` with the `user_substs`, so no new types
48-
// etc are introduced.
49-
self.tcx().mk_adt(adt_def, user_substs)
50-
},
51-
))),
52-
ty::FnDef(def_id, _) => Some(UserTypeAnnotation::Ty(user_substs.unchecked_map(
53-
|user_substs| {
54-
// Here, we just pair a `DefId` with the
55-
// `user_substs`, so no new types etc are introduced.
56-
self.tcx().mk_fn_def(*def_id, user_substs)
57-
},
58-
))),
38+
ty::Adt(adt_def, _) => Some(UserTypeAnnotation::AdtDef(adt_def, user_substs)),
39+
ty::FnDef(def_id, _) => Some(UserTypeAnnotation::FnDef(*def_id, user_substs)),
5940
sty => bug!(
6041
"sty: {:?} should not have user-substs {:?} recorded ",
6142
sty,

0 commit comments

Comments
 (0)