1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
12 use rustc::ty::{self, AdtDef, CanonicalTy, TyCtxt};
14 crate trait UserAnnotatedTyHelpers<'gcx: 'tcx, 'tcx> {
15 fn tcx(&self) -> TyCtxt<'_, 'gcx, 'tcx>;
17 fn tables(&self) -> &ty::TypeckTables<'tcx>;
19 fn user_substs_applied_to_adt(
22 adt_def: &'tcx AdtDef,
23 ) -> Option<CanonicalTy<'tcx>> {
24 let user_substs = self.tables().user_substs(hir_id)?;
25 Some(user_substs.unchecked_map(|user_substs| {
26 // Here, we just pair an `AdtDef` with the
27 // `user_substs`, so no new types etc are introduced.
28 self.tcx().mk_adt(adt_def, user_substs)
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
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)
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)
50 "sty: {:?} should not have user-substs {:?} recorded ",