Skip to content

Commit 71fed06

Browse files
committed
extract more helper fns around user substs
1 parent f4b7115 commit 71fed06

File tree

2 files changed

+33
-26
lines changed

2 files changed

+33
-26
lines changed

src/librustc_mir/hair/cx/expr.rs

+7-25
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
476476
adt_def: adt,
477477
variant_index: 0,
478478
substs,
479-
user_ty: cx.user_annotated_ty_for_adt(expr.hir_id, adt),
479+
user_ty: cx.user_substs_applied_to_adt(expr.hir_id, adt),
480480
fields: field_refs(cx, fields),
481481
base: base.as_ref().map(|base| {
482482
FruInfo {
@@ -502,7 +502,7 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
502502
adt_def: adt,
503503
variant_index: index,
504504
substs,
505-
user_ty: cx.user_annotated_ty_for_adt(expr.hir_id, adt),
505+
user_ty: cx.user_substs_applied_to_adt(expr.hir_id, adt),
506506
fields: field_refs(cx, fields),
507507
base: None,
508508
}
@@ -788,30 +788,12 @@ fn user_annotated_ty_for_def(
788788
// user.
789789
Def::StructCtor(_def_id, CtorKind::Const) |
790790
Def::VariantCtor(_def_id, CtorKind::Const) =>
791-
match &cx.tables().node_id_to_type(hir_id).sty {
792-
ty::Adt(adt_def, _) => cx.user_annotated_ty_for_adt(hir_id, adt_def),
793-
sty => bug!("unexpected sty: {:?}", sty),
794-
},
791+
cx.user_substs_applied_to_ty_of_hir_id(hir_id),
795792

796793
// `Self` is used in expression as a tuple struct constructor or an unit struct constructor
797-
Def::SelfCtor(_) => {
798-
let sty = &cx.tables().node_id_to_type(hir_id).sty;
799-
match sty {
800-
ty::FnDef(ref def_id, _) => {
801-
Some(cx.tables().user_substs(hir_id)?.unchecked_map(|user_substs| {
802-
// Here, we just pair a `DefId` with the
803-
// `user_substs`, so no new types etc are introduced.
804-
cx.tcx().mk_fn_def(*def_id, user_substs)
805-
}))
806-
}
807-
ty::Adt(ref adt_def, _) => {
808-
cx.user_annotated_ty_for_adt(hir_id, adt_def)
809-
}
810-
_ => {
811-
bug!("unexpected sty: {:?}", sty)
812-
}
813-
}
814-
}
794+
Def::SelfCtor(_) =>
795+
cx.user_substs_applied_to_ty_of_hir_id(hir_id),
796+
815797
_ =>
816798
bug!("user_annotated_ty_for_def: unexpected def {:?} at {:?}", def, hir_id)
817799
}
@@ -931,7 +913,7 @@ fn convert_path_expr<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
931913
adt_def,
932914
variant_index: adt_def.variant_index_with_id(def_id),
933915
substs,
934-
user_ty: cx.user_annotated_ty_for_adt(expr.hir_id, adt_def),
916+
user_ty: cx.user_substs_applied_to_adt(expr.hir_id, adt_def),
935917
fields: vec![],
936918
base: None,
937919
}

src/librustc_mir/hair/util.rs

+26-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ crate trait UserAnnotatedTyHelpers<'gcx: 'tcx, 'tcx> {
1616

1717
fn tables(&self) -> &ty::TypeckTables<'tcx>;
1818

19-
fn user_annotated_ty_for_adt(
19+
fn user_substs_applied_to_adt(
2020
&self,
2121
hir_id: hir::HirId,
2222
adt_def: &'tcx AdtDef,
@@ -28,4 +28,29 @@ crate trait UserAnnotatedTyHelpers<'gcx: 'tcx, 'tcx> {
2828
self.tcx().mk_adt(adt_def, user_substs)
2929
}))
3030
}
31+
32+
/// Looks up the type associated with this hir-id and applies the
33+
/// user-given substitutions; the hir-id must map to a suitable
34+
/// type.
35+
fn user_substs_applied_to_ty_of_hir_id(&self, hir_id: hir::HirId) -> Option<CanonicalTy<'tcx>> {
36+
let user_substs = self.tables().user_substs(hir_id)?;
37+
match &self.tables().node_id_to_type(hir_id).sty {
38+
ty::Adt(adt_def, _) => Some(user_substs.unchecked_map(|user_substs| {
39+
// Ok to call `unchecked_map` because we just pair an
40+
// `AdtDef` with the `user_substs`, so no new types
41+
// etc are introduced.
42+
self.tcx().mk_adt(adt_def, user_substs)
43+
})),
44+
ty::FnDef(def_id, _) => Some(user_substs.unchecked_map(|user_substs| {
45+
// Here, we just pair a `DefId` with the
46+
// `user_substs`, so no new types etc are introduced.
47+
self.tcx().mk_fn_def(*def_id, user_substs)
48+
})),
49+
sty => bug!(
50+
"sty: {:?} should not have user-substs {:?} recorded ",
51+
sty,
52+
user_substs
53+
),
54+
}
55+
}
3156
}

0 commit comments

Comments
 (0)