Skip to main content

rustc_hir_analysis/
hir_wf_check.rs

1use rustc_hir::def::DefKind;
2use rustc_hir::intravisit::{self, Visitor, VisitorExt};
3use rustc_hir::{self as hir, AmbigArg, ForeignItem, ForeignItemKind};
4use rustc_infer::infer::TyCtxtInferExt;
5use rustc_infer::traits::{ObligationCause, ObligationCauseCode, WellFormedLoc};
6use rustc_middle::bug;
7use rustc_middle::ty::{self, TyCtxt, TypeVisitableExt, TypingMode, fold_regions};
8use rustc_span::def_id::LocalDefId;
9use rustc_trait_selection::traits::{self, ObligationCtxt};
10use tracing::debug;
11
12use crate::collect::ItemCtxt;
13
14// Ideally, this would be in `rustc_trait_selection`, but we
15// need access to `ItemCtxt`
16pub(super) fn diagnostic_hir_wf_check<'tcx>(
17    tcx: TyCtxt<'tcx>,
18    (predicate, loc): (ty::Predicate<'tcx>, WellFormedLoc),
19) -> Option<ObligationCause<'tcx>> {
20    let def_id = match loc {
21        WellFormedLoc::Ty(def_id) => def_id,
22        WellFormedLoc::Param { function, param_idx: _ } => function,
23    };
24    let hir_id = tcx.local_def_id_to_hir_id(def_id);
25
26    // HIR wfcheck should only ever happen as part of improving an existing error
27    tcx.dcx()
28        .span_delayed_bug(tcx.def_span(def_id), "Performed HIR wfcheck without an existing error!");
29
30    let icx = ItemCtxt::new(tcx, def_id);
31
32    // To perform HIR-based WF checking, we iterate over all HIR types
33    // that occur 'inside' the item we're checking. For example,
34    // given the type `Option<MyStruct<u8>>`, we will check
35    // `Option<MyStruct<u8>>`, `MyStruct<u8>`, and `u8`.
36    // For each type, we perform a well-formed check, and see if we get
37    // an error that matches our expected predicate. We save
38    // the `ObligationCause` corresponding to the *innermost* type,
39    // which is the most specific type that we can point to.
40    // In general, the different components of an `hir::Ty` may have
41    // completely different spans due to macro invocations. Pointing
42    // to the most accurate part of the type can be the difference
43    // between a useless span (e.g. the macro invocation site)
44    // and a useful span (e.g. a user-provided type passed into the macro).
45    //
46    // This approach is quite inefficient - we redo a lot of work done
47    // by the normal WF checker. However, this code is run at most once
48    // per reported error - it will have no impact when compilation succeeds,
49    // and should only have an impact if a very large number of errors is
50    // displayed to the user.
51    struct HirWfCheck<'tcx> {
52        tcx: TyCtxt<'tcx>,
53        predicate: ty::Predicate<'tcx>,
54        cause: Option<ObligationCause<'tcx>> = None,
55        cause_depth: usize = 0,
56        icx: ItemCtxt<'tcx>,
57        def_id: LocalDefId,
58        param_env: ty::ParamEnv<'tcx>,
59        depth: usize = 0,
60    }
61
62    impl<'tcx> Visitor<'tcx> for HirWfCheck<'tcx> {
63        fn visit_ty(&mut self, ty: &'tcx hir::Ty<'tcx, AmbigArg>) {
64            let infcx = self.tcx.infer_ctxt().build(TypingMode::non_body_analysis());
65            let ocx = ObligationCtxt::new_with_diagnostics(&infcx);
66
67            // We don't handle infer vars but we wouldn't handle them anyway as we're creating a
68            // fresh `InferCtxt` in this function.
69            let tcx_ty = self.icx.lower_ty(ty.as_unambig_ty());
70            // This visitor can walk into binders, resulting in the `tcx_ty` to
71            // potentially reference escaping bound variables. We simply erase
72            // those here.
73            let tcx_ty = fold_regions(self.tcx, tcx_ty, |r, _| {
74                if r.is_bound() { self.tcx.lifetimes.re_erased } else { r }
75            });
76
77            // We may be checking the WFness of a type in an opaque with a non-lifetime bound.
78            // Perhaps we could rebind all the escaping bound vars, but they're coming from
79            // arbitrary debruijn indices and aren't particularly important anyways, since they
80            // are only coming from `feature(non_lifetime_binders)` anyways.
81            if tcx_ty.has_escaping_bound_vars() {
82                return;
83            }
84
85            let cause = traits::ObligationCause::new(
86                ty.span,
87                self.def_id,
88                traits::ObligationCauseCode::WellFormed(None),
89            );
90
91            ocx.register_obligation(traits::Obligation::new(
92                self.tcx,
93                cause,
94                self.param_env,
95                ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(tcx_ty.into())),
96            ));
97
98            for error in ocx.evaluate_obligations_error_on_ambiguity() {
99                {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_analysis/src/hir_wf_check.rs:99",
                        "rustc_hir_analysis::hir_wf_check", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/hir_wf_check.rs"),
                        ::tracing_core::__macro_support::Option::Some(99u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::hir_wf_check"),
                        ::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!("Wf-check got error for {0:?}: {1:?}",
                                                    ty, error) as &dyn Value))])
            });
    } else { ; }
};debug!("Wf-check got error for {:?}: {:?}", ty, error);
100                if error.obligation.predicate == self.predicate {
101                    // Save the cause from the greatest depth - this corresponds
102                    // to picking more-specific types (e.g. `MyStruct<u8>`)
103                    // over less-specific types (e.g. `Option<MyStruct<u8>>`)
104                    if self.depth >= self.cause_depth {
105                        self.cause = Some(error.obligation.cause);
106                        if let hir::TyKind::TraitObject(..) = ty.kind
107                            && let DefKind::AssocTy | DefKind::AssocConst | DefKind::AssocFn =
108                                self.tcx.def_kind(self.def_id)
109                        {
110                            self.cause = Some(ObligationCause::new(
111                                ty.span,
112                                self.def_id,
113                                ObligationCauseCode::DynCompatible(ty.span),
114                            ));
115                        }
116                        self.cause_depth = self.depth
117                    }
118                }
119            }
120
121            self.depth += 1;
122            intravisit::walk_ty(self, ty);
123            self.depth -= 1;
124        }
125    }
126
127    let param_env = tcx.param_env(def_id.to_def_id());
128    let mut visitor = HirWfCheck { tcx, predicate, icx, def_id, param_env, .. };
129
130    // Get the starting `hir::Ty` using our `WellFormedLoc`.
131    // We will walk 'into' this type to try to find
132    // a more precise span for our predicate.
133    let tys = match loc {
134        WellFormedLoc::Ty(_) => match tcx.hir_node(hir_id) {
135            hir::Node::ImplItem(item) => match item.kind {
136                hir::ImplItemKind::Type(ty) => ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [ty]))vec![ty],
137                hir::ImplItemKind::Const(ty, _) => ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [ty]))vec![ty],
138                ref item => ::rustc_middle::util::bug::bug_fmt(format_args!("Unexpected ImplItem {0:?}",
        item))bug!("Unexpected ImplItem {:?}", item),
139            },
140            hir::Node::TraitItem(item) => match item.kind {
141                hir::TraitItemKind::Type(_, ty) => ty.into_iter().collect(),
142                hir::TraitItemKind::Const(ty, _, _) => ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [ty]))vec![ty],
143                ref item => ::rustc_middle::util::bug::bug_fmt(format_args!("Unexpected TraitItem {0:?}",
        item))bug!("Unexpected TraitItem {:?}", item),
144            },
145            hir::Node::Item(item) => match item.kind {
146                hir::ItemKind::TyAlias(_, _, ty)
147                | hir::ItemKind::Static(_, _, ty, _)
148                | hir::ItemKind::Const(_, _, ty, _) => ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [ty]))vec![ty],
149                hir::ItemKind::Impl(impl_) => match impl_.of_trait {
150                    Some(of_trait) => of_trait
151                        .trait_ref
152                        .path
153                        .segments
154                        .last()
155                        .iter()
156                        .flat_map(|seg| seg.args().args)
157                        .filter_map(|arg| {
158                            if let hir::GenericArg::Type(ty) = arg {
159                                Some(ty.as_unambig_ty())
160                            } else {
161                                None
162                            }
163                        })
164                        .chain([impl_.self_ty])
165                        .collect(),
166                    None => {
167                        ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [impl_.self_ty]))vec![impl_.self_ty]
168                    }
169                },
170                ref item => ::rustc_middle::util::bug::bug_fmt(format_args!("Unexpected item {0:?}",
        item))bug!("Unexpected item {:?}", item),
171            },
172            hir::Node::Field(field) => ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [field.ty]))vec![field.ty],
173            hir::Node::ForeignItem(ForeignItem {
174                kind: ForeignItemKind::Static(ty, _, _), ..
175            }) => ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [*ty]))vec![*ty],
176            hir::Node::GenericParam(hir::GenericParam {
177                kind: hir::GenericParamKind::Type { default: Some(ty), .. },
178                ..
179            }) => ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [*ty]))vec![*ty],
180            hir::Node::AnonConst(_) => {
181                if let Some(const_param_id) = tcx.hir_opt_const_param_default_param_def_id(hir_id)
182                    && let hir::Node::GenericParam(hir::GenericParam {
183                        kind: hir::GenericParamKind::Const { ty, .. },
184                        ..
185                    }) = tcx.hir_node_by_def_id(const_param_id)
186                {
187                    ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [*ty]))vec![*ty]
188                } else {
189                    ::alloc::vec::Vec::new()vec![]
190                }
191            }
192            ref node => ::rustc_middle::util::bug::bug_fmt(format_args!("Unexpected node {0:?}",
        node))bug!("Unexpected node {:?}", node),
193        },
194        WellFormedLoc::Param { function: _, param_idx } => {
195            let fn_decl = tcx.hir_fn_decl_by_hir_id(hir_id).unwrap();
196            // Get return type
197            if param_idx as usize == fn_decl.inputs.len() {
198                match fn_decl.output {
199                    hir::FnRetTy::Return(ty) => ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [ty]))vec![ty],
200                    // The unit type `()` is always well-formed
201                    hir::FnRetTy::DefaultReturn(_span) => ::alloc::vec::Vec::new()vec![],
202                }
203            } else {
204                ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [&fn_decl.inputs[param_idx as usize]]))vec![&fn_decl.inputs[param_idx as usize]]
205            }
206        }
207    };
208    for ty in tys {
209        visitor.visit_ty_unambig(ty);
210    }
211    visitor.cause
212}