rustc_mir_build/thir/
util.rs

1use rustc_hir as hir;
2use rustc_middle::bug;
3use rustc_middle::ty::{self, CanonicalUserType};
4use tracing::debug;
5
6/// Looks up the type associated with this hir-id and applies the
7/// user-given generic parameters; the hir-id must map to a suitable
8/// type.
9pub(crate) fn user_args_applied_to_ty_of_hir_id<'tcx>(
10    typeck_results: &ty::TypeckResults<'tcx>,
11    hir_id: hir::HirId,
12) -> Option<CanonicalUserType<'tcx>> {
13    let user_provided_types = typeck_results.user_provided_types();
14    let mut user_ty = *user_provided_types.get(hir_id)?;
15    debug!("user_subts_applied_to_ty_of_hir_id: user_ty={:?}", user_ty);
16    let ty = typeck_results.node_type(hir_id);
17    match ty.kind() {
18        ty::Adt(adt_def, ..) => {
19            if let ty::UserTypeKind::TypeOf(ref mut did, _) = &mut user_ty.value.kind {
20                *did = adt_def.did();
21            }
22            Some(user_ty)
23        }
24        ty::FnDef(..) => Some(user_ty),
25        _ => bug!("ty: {:?} should not have user provided type {:?} recorded ", ty, user_ty),
26    }
27}