]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/hair/util.rs
4618cd42686fa2e4ffb251e6185051d5a412fc84
[rust.git] / src / librustc_mir / hair / util.rs
1 use rustc::hir;
2 use rustc::ty::{self, CanonicalUserType, TyCtxt, UserType};
3
4 crate trait UserAnnotatedTyHelpers<'gcx: 'tcx, 'tcx> {
5     fn tcx(&self) -> TyCtxt<'_, 'gcx, 'tcx>;
6
7     fn tables(&self) -> &ty::TypeckTables<'tcx>;
8
9     /// Looks up the type associated with this hir-id and applies the
10     /// user-given substitutions; the hir-id must map to a suitable
11     /// type.
12     fn user_substs_applied_to_ty_of_hir_id(
13         &self,
14         hir_id: hir::HirId,
15     ) -> Option<CanonicalUserType<'tcx>> {
16         let user_provided_types = self.tables().user_provided_types();
17         let mut user_ty = *user_provided_types.get(hir_id)?;
18         debug!("user_subts_applied_to_ty_of_hir_id: user_ty={:?}", user_ty);
19         match &self.tables().node_type(hir_id).sty {
20             ty::Adt(adt_def, ..) => {
21                 if let UserType::TypeOf(ref mut did, _) = &mut user_ty.value {
22                     *did = adt_def.did;
23                 }
24                 Some(user_ty)
25             }
26             ty::FnDef(..) => Some(user_ty),
27             sty =>
28                 bug!("sty: {:?} should not have user provided type {:?} recorded ", sty, user_ty),
29         }
30     }
31 }