]> git.lizzy.rs Git - rust.git/commitdiff
extract more helper fns around user substs
authorNiko Matsakis <niko@alum.mit.edu>
Fri, 5 Oct 2018 14:40:35 +0000 (10:40 -0400)
committerNiko Matsakis <niko@alum.mit.edu>
Mon, 8 Oct 2018 23:20:37 +0000 (19:20 -0400)
src/librustc_mir/hair/cx/expr.rs
src/librustc_mir/hair/util.rs

index ef7be184ab051083b9362a289197841d195ad50e..c969a3ef3485ff1fabe2c221add74a8473b596ca 100644 (file)
@@ -476,7 +476,7 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
                                 adt_def: adt,
                                 variant_index: 0,
                                 substs,
-                                user_ty: cx.user_annotated_ty_for_adt(expr.hir_id, adt),
+                                user_ty: cx.user_substs_applied_to_adt(expr.hir_id, adt),
                                 fields: field_refs(cx, fields),
                                 base: base.as_ref().map(|base| {
                                     FruInfo {
@@ -502,7 +502,7 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
                                         adt_def: adt,
                                         variant_index: index,
                                         substs,
-                                        user_ty: cx.user_annotated_ty_for_adt(expr.hir_id, adt),
+                                        user_ty: cx.user_substs_applied_to_adt(expr.hir_id, adt),
                                         fields: field_refs(cx, fields),
                                         base: None,
                                     }
@@ -788,30 +788,12 @@ fn user_annotated_ty_for_def(
         // user.
         Def::StructCtor(_def_id, CtorKind::Const) |
         Def::VariantCtor(_def_id, CtorKind::Const) =>
-            match &cx.tables().node_id_to_type(hir_id).sty {
-                ty::Adt(adt_def, _) => cx.user_annotated_ty_for_adt(hir_id, adt_def),
-                sty => bug!("unexpected sty: {:?}", sty),
-            },
+            cx.user_substs_applied_to_ty_of_hir_id(hir_id),
 
         // `Self` is used in expression as a tuple struct constructor or an unit struct constructor
-        Def::SelfCtor(_) => {
-            let sty = &cx.tables().node_id_to_type(hir_id).sty;
-            match sty {
-                ty::FnDef(ref def_id, _) => {
-                    Some(cx.tables().user_substs(hir_id)?.unchecked_map(|user_substs| {
-                        // Here, we just pair a `DefId` with the
-                        // `user_substs`, so no new types etc are introduced.
-                        cx.tcx().mk_fn_def(*def_id, user_substs)
-                    }))
-                }
-                ty::Adt(ref adt_def, _) => {
-                    cx.user_annotated_ty_for_adt(hir_id, adt_def)
-                }
-                _ => {
-                    bug!("unexpected sty: {:?}", sty)
-                }
-            }
-        }
+        Def::SelfCtor(_) =>
+            cx.user_substs_applied_to_ty_of_hir_id(hir_id),
+
         _ =>
             bug!("user_annotated_ty_for_def: unexpected def {:?} at {:?}", def, hir_id)
     }
@@ -931,7 +913,7 @@ fn convert_path_expr<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
                         adt_def,
                         variant_index: adt_def.variant_index_with_id(def_id),
                         substs,
-                        user_ty: cx.user_annotated_ty_for_adt(expr.hir_id, adt_def),
+                        user_ty: cx.user_substs_applied_to_adt(expr.hir_id, adt_def),
                         fields: vec![],
                         base: None,
                     }
index 803ce615747edffcc0608de28f37f53dd4f2d068..48a2e67a3dcc3a5dbd2d5c4d718731fcf9e7c37d 100644 (file)
@@ -16,7 +16,7 @@
 
     fn tables(&self) -> &ty::TypeckTables<'tcx>;
 
-    fn user_annotated_ty_for_adt(
+    fn user_substs_applied_to_adt(
         &self,
         hir_id: hir::HirId,
         adt_def: &'tcx AdtDef,
@@ -28,4 +28,29 @@ fn user_annotated_ty_for_adt(
             self.tcx().mk_adt(adt_def, user_substs)
         }))
     }
+
+    /// Looks up the type associated with this hir-id and applies the
+    /// user-given substitutions; the hir-id must map to a suitable
+    /// type.
+    fn user_substs_applied_to_ty_of_hir_id(&self, hir_id: hir::HirId) -> Option<CanonicalTy<'tcx>> {
+        let user_substs = self.tables().user_substs(hir_id)?;
+        match &self.tables().node_id_to_type(hir_id).sty {
+            ty::Adt(adt_def, _) => Some(user_substs.unchecked_map(|user_substs| {
+                // Ok to call `unchecked_map` because we just pair an
+                // `AdtDef` with the `user_substs`, so no new types
+                // etc are introduced.
+                self.tcx().mk_adt(adt_def, user_substs)
+            })),
+            ty::FnDef(def_id, _) => Some(user_substs.unchecked_map(|user_substs| {
+                // Here, we just pair a `DefId` with the
+                // `user_substs`, so no new types etc are introduced.
+                self.tcx().mk_fn_def(*def_id, user_substs)
+            })),
+            sty => bug!(
+                "sty: {:?} should not have user-substs {:?} recorded ",
+                sty,
+                user_substs
+            ),
+        }
+    }
 }