Skip to main content

rustc_mir_build/thir/
util.rs

1use rustc_data_structures::assert_matches;
2use rustc_hir as hir;
3use rustc_hir::def::DefKind;
4use rustc_middle::bug;
5use rustc_middle::ty::{self, CanonicalUserType, TyCtxt};
6use tracing::debug;
7
8/// Looks up the type associated with this hir-id and applies the
9/// user-given generic parameters; the hir-id must map to a suitable
10/// type.
11pub(crate) fn user_args_applied_to_ty_of_hir_id<'tcx>(
12    tcx: TyCtxt<'tcx>,
13    typeck_results: &ty::TypeckResults<'tcx>,
14    hir_id: hir::HirId,
15) -> Option<CanonicalUserType<'tcx>> {
16    let user_provided_types = typeck_results.user_provided_types();
17    let mut user_ty = *user_provided_types.get(hir_id)?;
18    {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_mir_build/src/thir/util.rs:18",
                        "rustc_mir_build::thir::util", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_build/src/thir/util.rs"),
                        ::tracing_core::__macro_support::Option::Some(18u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_mir_build::thir::util"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("user_subts_applied_to_ty_of_hir_id: user_ty={0:?}",
                                                    user_ty) as &dyn Value))])
            });
    } else { ; }
};debug!("user_subts_applied_to_ty_of_hir_id: user_ty={:?}", user_ty);
19    let ty = typeck_results.node_type(hir_id);
20    match ty.kind() {
21        ty::Adt(adt_def, ..) => {
22            // This "fixes" user type annotations for tupled ctor patterns for ADTs.
23            // That's because `type_of(ctor_did)` returns a FnDef, but we actually
24            // want to be annotating the type of the ADT itself. It's a bit goofy,
25            // but it's easier to adjust this here rather than in the path lowering
26            // code for patterns in HIR.
27            if let ty::UserTypeKind::TypeOf(did, _) = &mut user_ty.value.kind {
28                // This is either already set up correctly (struct, union, enum, or variant),
29                // or needs adjusting (ctor). Make sure we don't start adjusting other
30                // user annotations like consts or fn calls.
31                match tcx.def_kind(*did) {
    DefKind::Ctor(..) | DefKind::Struct | DefKind::Enum | DefKind::Union |
        DefKind::Variant => {}
    ref left_val => {
        ::core::panicking::assert_matches_failed(left_val,
            "DefKind::Ctor(..) | DefKind::Struct | DefKind::Enum | DefKind::Union |\nDefKind::Variant",
            ::core::option::Option::None);
    }
};assert_matches!(
32                    tcx.def_kind(*did),
33                    DefKind::Ctor(..)
34                        | DefKind::Struct
35                        | DefKind::Enum
36                        | DefKind::Union
37                        | DefKind::Variant
38                );
39                *did = adt_def.did();
40            }
41            Some(user_ty)
42        }
43        ty::FnDef(..) => Some(user_ty),
44        _ => ::rustc_middle::util::bug::bug_fmt(format_args!("ty: {0:?} should not have user provided type {1:?} recorded ",
        ty, user_ty))bug!("ty: {:?} should not have user provided type {:?} recorded ", ty, user_ty),
45    }
46}