Skip to main content

rustc_hir_analysis/check/
check.rs

1use std::cell::LazyCell;
2use std::ops::ControlFlow;
3
4use rustc_abi::{ExternAbi, FieldIdx, ScalableElt};
5use rustc_data_structures::unord::{UnordMap, UnordSet};
6use rustc_errors::codes::*;
7use rustc_errors::{Diag, DiagCtxtHandle, Diagnostic, EmissionGuarantee, Level, MultiSpan};
8use rustc_hir as hir;
9use rustc_hir::attrs::ReprAttr::ReprPacked;
10use rustc_hir::def::{CtorKind, DefKind};
11use rustc_hir::{LangItem, Node, find_attr, intravisit};
12use rustc_infer::infer::{RegionVariableOrigin, TyCtxtInferExt};
13use rustc_infer::traits::{Obligation, ObligationCauseCode, WellFormedLoc};
14use rustc_lint_defs::builtin::{REPR_TRANSPARENT_NON_ZST_FIELDS, UNSUPPORTED_CALLING_CONVENTIONS};
15use rustc_macros::Diagnostic;
16use rustc_middle::hir::nested_filter;
17use rustc_middle::middle::resolve_bound_vars::ResolvedArg;
18use rustc_middle::middle::stability::EvalResult;
19use rustc_middle::ty::error::TypeErrorToStringExt;
20use rustc_middle::ty::layout::{LayoutError, MAX_SIMD_LANES};
21use rustc_middle::ty::util::Discr;
22use rustc_middle::ty::{
23    AdtDef, BottomUpFolder, FnSig, GenericArgKind, RegionKind, TypeFoldable, TypeSuperVisitable,
24    TypeVisitable, TypeVisitableExt, fold_regions,
25};
26use rustc_session::lint::builtin::UNINHABITED_STATIC;
27use rustc_span::source_map::Spanned;
28use rustc_target::spec::{AbiMap, AbiMapping};
29use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
30use rustc_trait_selection::traits;
31use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt;
32use tracing::{debug, instrument};
33use ty::TypingMode;
34
35use super::compare_impl_item::check_type_bounds;
36use super::*;
37use crate::check::wfcheck::{
38    check_associated_item, check_trait_item, check_variances_for_type_defn, check_where_clauses,
39    enter_wf_checking_ctxt,
40};
41
42fn add_abi_diag_help<T: EmissionGuarantee>(abi: ExternAbi, diag: &mut Diag<'_, T>) {
43    if let ExternAbi::Cdecl { unwind } = abi {
44        let c_abi = ExternAbi::C { unwind };
45        diag.help(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("use `extern {0}` instead", c_abi))
    })format!("use `extern {c_abi}` instead",));
46    } else if let ExternAbi::Stdcall { unwind } = abi {
47        let c_abi = ExternAbi::C { unwind };
48        let system_abi = ExternAbi::System { unwind };
49        diag.help(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("if you need `extern {0}` on win32 and `extern {1}` everywhere else, use `extern {2}`",
                abi, c_abi, system_abi))
    })format!(
50            "if you need `extern {abi}` on win32 and `extern {c_abi}` everywhere else, \
51                use `extern {system_abi}`"
52        ));
53    }
54}
55
56pub fn check_abi(tcx: TyCtxt<'_>, hir_id: hir::HirId, span: Span, abi: ExternAbi) {
57    struct UnsupportedCallingConventions {
58        abi: ExternAbi,
59    }
60
61    impl<'a> Diagnostic<'a, ()> for UnsupportedCallingConventions {
62        fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, ()> {
63            let Self { abi } = self;
64            let mut lint = Diag::new(
65                dcx,
66                level,
67                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0} is not a supported ABI for the current target",
                abi))
    })format!("{abi} is not a supported ABI for the current target"),
68            );
69            add_abi_diag_help(abi, &mut lint);
70            lint
71        }
72    }
73    // FIXME: This should be checked earlier, e.g. in `rustc_ast_lowering`, as this
74    // currently only guards function imports, function definitions, and function pointer types.
75    // Functions in trait declarations can still use "deprecated" ABIs without any warning.
76
77    match AbiMap::from_target(&tcx.sess.target).canonize_abi(abi, false) {
78        AbiMapping::Direct(..) => (),
79        // already erred in rustc_ast_lowering
80        AbiMapping::Invalid => {
81            tcx.dcx().span_delayed_bug(span, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0} should be rejected in ast_lowering",
                abi))
    })format!("{abi} should be rejected in ast_lowering"));
82        }
83        AbiMapping::Deprecated(..) => {
84            tcx.emit_node_span_lint(
85                UNSUPPORTED_CALLING_CONVENTIONS,
86                hir_id,
87                span,
88                UnsupportedCallingConventions { abi },
89            );
90        }
91    }
92}
93
94pub fn check_custom_abi(tcx: TyCtxt<'_>, def_id: LocalDefId, fn_sig: FnSig<'_>, fn_sig_span: Span) {
95    if fn_sig.abi == ExternAbi::Custom {
96        // Function definitions that use `extern "custom"` must be naked functions.
97        if !{

        #[allow(deprecated)]
        {
            {
                'done:
                    {
                    for i in tcx.get_all_attrs(def_id) {
                        #[allow(unused_imports)]
                        use rustc_hir::attrs::AttributeKind::*;
                        let i: &rustc_hir::Attribute = i;
                        match i {
                            rustc_hir::Attribute::Parsed(Naked(_)) => {
                                break 'done Some(());
                            }
                            rustc_hir::Attribute::Unparsed(..) =>
                                {}
                                #[deny(unreachable_patterns)]
                                _ => {}
                        }
                    }
                    None
                }
            }
        }
    }.is_some()find_attr!(tcx, def_id, Naked(_)) {
98            tcx.dcx().emit_err(crate::errors::AbiCustomClothedFunction {
99                span: fn_sig_span,
100                naked_span: tcx.def_span(def_id).shrink_to_lo(),
101            });
102        }
103    }
104}
105
106fn check_struct(tcx: TyCtxt<'_>, def_id: LocalDefId) {
107    let def = tcx.adt_def(def_id);
108    let span = tcx.def_span(def_id);
109    def.destructor(tcx); // force the destructor to be evaluated
110
111    if let Some(scalable) = def.repr().scalable {
112        check_scalable_vector(tcx, span, def_id, scalable);
113    } else if def.repr().simd() {
114        check_simd(tcx, span, def_id);
115    }
116
117    check_transparent(tcx, def);
118    check_packed(tcx, span, def);
119}
120
121fn check_union(tcx: TyCtxt<'_>, def_id: LocalDefId) {
122    let def = tcx.adt_def(def_id);
123    let span = tcx.def_span(def_id);
124    def.destructor(tcx); // force the destructor to be evaluated
125    check_transparent(tcx, def);
126    check_union_fields(tcx, span, def_id);
127    check_packed(tcx, span, def);
128}
129
130fn allowed_union_or_unsafe_field<'tcx>(
131    tcx: TyCtxt<'tcx>,
132    ty: Ty<'tcx>,
133    typing_env: ty::TypingEnv<'tcx>,
134    span: Span,
135) -> bool {
136    // HACK (not that bad of a hack don't worry): Some codegen tests don't even define proper
137    // impls for `Copy`. Let's short-circuit here for this validity check, since a lot of them
138    // use unions. We should eventually fix all the tests to define that lang item or use
139    // minicore stubs.
140    if ty.is_trivially_pure_clone_copy() {
141        return true;
142    }
143    // If `BikeshedGuaranteedNoDrop` is not defined in a `#[no_core]` test, fall back to `Copy`.
144    // This is an underapproximation of `BikeshedGuaranteedNoDrop`,
145    let def_id = tcx
146        .lang_items()
147        .get(LangItem::BikeshedGuaranteedNoDrop)
148        .unwrap_or_else(|| tcx.require_lang_item(LangItem::Copy, span));
149    let Ok(ty) = tcx.try_normalize_erasing_regions(typing_env, ty) else {
150        tcx.dcx().span_delayed_bug(span, "could not normalize field type");
151        return true;
152    };
153    let (infcx, param_env) = tcx.infer_ctxt().build_with_typing_env(typing_env);
154    infcx.predicate_must_hold_modulo_regions(&Obligation::new(
155        tcx,
156        ObligationCause::dummy_with_span(span),
157        param_env,
158        ty::TraitRef::new(tcx, def_id, [ty]),
159    ))
160}
161
162/// Check that the fields of the `union` do not need dropping.
163fn check_union_fields(tcx: TyCtxt<'_>, span: Span, item_def_id: LocalDefId) -> bool {
164    let def = tcx.adt_def(item_def_id);
165    if !def.is_union() {
    ::core::panicking::panic("assertion failed: def.is_union()")
};assert!(def.is_union());
166
167    let typing_env = ty::TypingEnv::non_body_analysis(tcx, item_def_id);
168    let args = ty::GenericArgs::identity_for_item(tcx, item_def_id);
169
170    for field in &def.non_enum_variant().fields {
171        if !allowed_union_or_unsafe_field(tcx, field.ty(tcx, args), typing_env, span) {
172            let (field_span, ty_span) = match tcx.hir_get_if_local(field.did) {
173                // We are currently checking the type this field came from, so it must be local.
174                Some(Node::Field(field)) => (field.span, field.ty.span),
175                _ => {
    ::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
            format_args!("mir field has to correspond to hir field")));
}unreachable!("mir field has to correspond to hir field"),
176            };
177            tcx.dcx().emit_err(errors::InvalidUnionField {
178                field_span,
179                sugg: errors::InvalidUnionFieldSuggestion {
180                    lo: ty_span.shrink_to_lo(),
181                    hi: ty_span.shrink_to_hi(),
182                },
183                note: (),
184            });
185            return false;
186        }
187    }
188
189    true
190}
191
192/// Check that a `static` is inhabited.
193fn check_static_inhabited(tcx: TyCtxt<'_>, def_id: LocalDefId) {
194    #[derive(const _: () =
    {
        impl<'_sess, G> rustc_errors::Diagnostic<'_sess, G> for
            StaticOfUninhabitedType where G: rustc_errors::EmissionGuarantee {
            #[track_caller]
            fn into_diag(self, dcx: rustc_errors::DiagCtxtHandle<'_sess>,
                level: rustc_errors::Level) -> rustc_errors::Diag<'_sess, G> {
                match self {
                    StaticOfUninhabitedType => {
                        let mut diag =
                            rustc_errors::Diag::new(dcx, level,
                                rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("static of uninhabited type")));
                        diag.note(rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("uninhabited statics cannot be initialized, and any access would be an immediate error")));
                        ;
                        diag
                    }
                }
            }
        }
    };Diagnostic)]
195    #[diag("static of uninhabited type")]
196    #[note("uninhabited statics cannot be initialized, and any access would be an immediate error")]
197    struct StaticOfUninhabitedType;
198
199    // Make sure statics are inhabited.
200    // Other parts of the compiler assume that there are no uninhabited places. In principle it
201    // would be enough to check this for `extern` statics, as statics with an initializer will
202    // have UB during initialization if they are uninhabited, but there also seems to be no good
203    // reason to allow any statics to be uninhabited.
204    let ty = tcx.type_of(def_id).instantiate_identity();
205    let span = tcx.def_span(def_id);
206    let layout = match tcx.layout_of(ty::TypingEnv::fully_monomorphized().as_query_input(ty)) {
207        Ok(l) => l,
208        // Foreign statics that overflow their allowed size should emit an error
209        Err(LayoutError::SizeOverflow(_))
210            if #[allow(non_exhaustive_omitted_patterns)] match tcx.def_kind(def_id) {
    DefKind::Static { .. } if
        tcx.def_kind(tcx.local_parent(def_id)) == DefKind::ForeignMod => true,
    _ => false,
}matches!(tcx.def_kind(def_id), DefKind::Static{ .. }
211                if tcx.def_kind(tcx.local_parent(def_id)) == DefKind::ForeignMod) =>
212        {
213            tcx.dcx().emit_err(errors::TooLargeStatic { span });
214            return;
215        }
216        // SIMD types with invalid layout (e.g., zero-length) should emit an error
217        Err(e @ LayoutError::InvalidSimd { .. }) => {
218            let ty_span = tcx.ty_span(def_id);
219            tcx.dcx().emit_err(Spanned { span: ty_span, node: e.into_diagnostic() });
220            return;
221        }
222        // Generic statics are rejected, but we still reach this case.
223        Err(e) => {
224            tcx.dcx().span_delayed_bug(span, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0:?}", e))
    })format!("{e:?}"));
225            return;
226        }
227    };
228    if layout.is_uninhabited() {
229        tcx.emit_node_span_lint(
230            UNINHABITED_STATIC,
231            tcx.local_def_id_to_hir_id(def_id),
232            span,
233            StaticOfUninhabitedType,
234        );
235    }
236}
237
238/// Checks that an opaque type does not contain cycles and does not use `Self` or `T::Foo`
239/// projections that would result in "inheriting lifetimes".
240fn check_opaque(tcx: TyCtxt<'_>, def_id: LocalDefId) {
241    let hir::OpaqueTy { origin, .. } = *tcx.hir_expect_opaque_ty(def_id);
242
243    // HACK(jynelson): trying to infer the type of `impl trait` breaks documenting
244    // `async-std` (and `pub async fn` in general).
245    // Since rustdoc doesn't care about the hidden type behind `impl Trait`, just don't look at it!
246    // See https://github.com/rust-lang/rust/issues/75100
247    if tcx.sess.opts.actually_rustdoc {
248        return;
249    }
250
251    if tcx.type_of(def_id).instantiate_identity().references_error() {
252        return;
253    }
254    if check_opaque_for_cycles(tcx, def_id).is_err() {
255        return;
256    }
257
258    let _ = check_opaque_meets_bounds(tcx, def_id, origin);
259}
260
261/// Checks that an opaque type does not contain cycles.
262pub(super) fn check_opaque_for_cycles<'tcx>(
263    tcx: TyCtxt<'tcx>,
264    def_id: LocalDefId,
265) -> Result<(), ErrorGuaranteed> {
266    let args = GenericArgs::identity_for_item(tcx, def_id);
267
268    // First, try to look at any opaque expansion cycles, considering coroutine fields
269    // (even though these aren't necessarily true errors).
270    if tcx.try_expand_impl_trait_type(def_id.to_def_id(), args).is_err() {
271        let reported = opaque_type_cycle_error(tcx, def_id);
272        return Err(reported);
273    }
274
275    Ok(())
276}
277
278/// Check that the hidden type behind `impl Trait` actually implements `Trait`.
279///
280/// This is mostly checked at the places that specify the opaque type, but we
281/// check those cases in the `param_env` of that function, which may have
282/// bounds not on this opaque type:
283///
284/// ```ignore (illustrative)
285/// type X<T> = impl Clone;
286/// fn f<T: Clone>(t: T) -> X<T> {
287///     t
288/// }
289/// ```
290///
291/// Without this check the above code is incorrectly accepted: we would ICE if
292/// some tried, for example, to clone an `Option<X<&mut ()>>`.
293#[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("check_opaque_meets_bounds",
                                    "rustc_hir_analysis::check::check", ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/check/check.rs"),
                                    ::tracing_core::__macro_support::Option::Some(293u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::check::check"),
                                    ::tracing_core::field::FieldSet::new(&["def_id", "origin"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = meta.fields().iter();
                                meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&def_id)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&origin)
                                                            as &dyn Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return: Result<(), ErrorGuaranteed> =
                loop {};
            return __tracing_attr_fake_return;
        }
        {
            let (span, definition_def_id) =
                if let Some((span, def_id)) =
                        best_definition_site_of_opaque(tcx, def_id, origin) {
                    (span, Some(def_id))
                } else { (tcx.def_span(def_id), None) };
            let defining_use_anchor =
                match origin {
                    hir::OpaqueTyOrigin::FnReturn { parent, .. } |
                        hir::OpaqueTyOrigin::AsyncFn { parent, .. } |
                        hir::OpaqueTyOrigin::TyAlias { parent, .. } => parent,
                };
            let param_env = tcx.param_env(defining_use_anchor);
            let infcx =
                tcx.infer_ctxt().build(if tcx.next_trait_solver_globally() {
                        TypingMode::post_borrowck_analysis(tcx, defining_use_anchor)
                    } else {
                        TypingMode::analysis_in_body(tcx, defining_use_anchor)
                    });
            let ocx = ObligationCtxt::new_with_diagnostics(&infcx);
            let args =
                match origin {
                    hir::OpaqueTyOrigin::FnReturn { parent, .. } |
                        hir::OpaqueTyOrigin::AsyncFn { parent, .. } |
                        hir::OpaqueTyOrigin::TyAlias { parent, .. } =>
                        GenericArgs::identity_for_item(tcx,
                                parent).extend_to(tcx, def_id.to_def_id(),
                            |param, _|
                                {
                                    tcx.map_opaque_lifetime_to_parent_lifetime(param.def_id.expect_local()).into()
                                }),
                };
            let opaque_ty = Ty::new_opaque(tcx, def_id.to_def_id(), args);
            let hidden_ty =
                tcx.type_of(def_id.to_def_id()).instantiate(tcx, args);
            let hidden_ty =
                fold_regions(tcx, hidden_ty,
                    |re, _dbi|
                        match re.kind() {
                            ty::ReErased =>
                                infcx.next_region_var(RegionVariableOrigin::Misc(span)),
                            _ => re,
                        });
            for (predicate, pred_span) in
                tcx.explicit_item_bounds(def_id).iter_instantiated_copied(tcx,
                    args) {
                let predicate =
                    predicate.fold_with(&mut BottomUpFolder {
                                tcx,
                                ty_op: |ty| if ty == opaque_ty { hidden_ty } else { ty },
                                lt_op: |lt| lt,
                                ct_op: |ct| ct,
                            });
                ocx.register_obligation(Obligation::new(tcx,
                        ObligationCause::new(span, def_id,
                            ObligationCauseCode::OpaqueTypeBound(pred_span,
                                definition_def_id)), param_env, predicate));
            }
            let misc_cause = ObligationCause::misc(span, def_id);
            match ocx.eq(&misc_cause, param_env, opaque_ty, hidden_ty) {
                Ok(()) => {}
                Err(ty_err) => {
                    let ty_err = ty_err.to_string(tcx);
                    let guar =
                        tcx.dcx().span_delayed_bug(span,
                            ::alloc::__export::must_use({
                                    ::alloc::fmt::format(format_args!("could not unify `{0}` with revealed type:\n{1}",
                                            hidden_ty, ty_err))
                                }));
                    return Err(guar);
                }
            }
            let predicate =
                ty::Binder::dummy(ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(hidden_ty.into())));
            ocx.register_obligation(Obligation::new(tcx, misc_cause.clone(),
                    param_env, predicate));
            let errors = ocx.evaluate_obligations_error_on_ambiguity();
            if !errors.is_empty() {
                let guar = infcx.err_ctxt().report_fulfillment_errors(errors);
                return Err(guar);
            }
            let wf_tys =
                ocx.assumed_wf_types_and_report_errors(param_env,
                        defining_use_anchor)?;
            ocx.resolve_regions_and_report_errors(defining_use_anchor,
                    param_env, wf_tys)?;
            if infcx.next_trait_solver() {
                Ok(())
            } else if let hir::OpaqueTyOrigin::FnReturn { .. } |
                    hir::OpaqueTyOrigin::AsyncFn { .. } = origin {
                let _ = infcx.take_opaque_types();
                Ok(())
            } else {
                for (mut key, mut ty) in infcx.take_opaque_types() {
                    ty.ty = infcx.resolve_vars_if_possible(ty.ty);
                    key = infcx.resolve_vars_if_possible(key);
                    sanity_check_found_hidden_type(tcx, key, ty)?;
                }
                Ok(())
            }
        }
    }
}#[instrument(level = "debug", skip(tcx))]
294fn check_opaque_meets_bounds<'tcx>(
295    tcx: TyCtxt<'tcx>,
296    def_id: LocalDefId,
297    origin: hir::OpaqueTyOrigin<LocalDefId>,
298) -> Result<(), ErrorGuaranteed> {
299    let (span, definition_def_id) =
300        if let Some((span, def_id)) = best_definition_site_of_opaque(tcx, def_id, origin) {
301            (span, Some(def_id))
302        } else {
303            (tcx.def_span(def_id), None)
304        };
305
306    let defining_use_anchor = match origin {
307        hir::OpaqueTyOrigin::FnReturn { parent, .. }
308        | hir::OpaqueTyOrigin::AsyncFn { parent, .. }
309        | hir::OpaqueTyOrigin::TyAlias { parent, .. } => parent,
310    };
311    let param_env = tcx.param_env(defining_use_anchor);
312
313    // FIXME(#132279): Once `PostBorrowckAnalysis` is supported in the old solver, this branch should be removed.
314    let infcx = tcx.infer_ctxt().build(if tcx.next_trait_solver_globally() {
315        TypingMode::post_borrowck_analysis(tcx, defining_use_anchor)
316    } else {
317        TypingMode::analysis_in_body(tcx, defining_use_anchor)
318    });
319    let ocx = ObligationCtxt::new_with_diagnostics(&infcx);
320
321    let args = match origin {
322        hir::OpaqueTyOrigin::FnReturn { parent, .. }
323        | hir::OpaqueTyOrigin::AsyncFn { parent, .. }
324        | hir::OpaqueTyOrigin::TyAlias { parent, .. } => GenericArgs::identity_for_item(
325            tcx, parent,
326        )
327        .extend_to(tcx, def_id.to_def_id(), |param, _| {
328            tcx.map_opaque_lifetime_to_parent_lifetime(param.def_id.expect_local()).into()
329        }),
330    };
331
332    let opaque_ty = Ty::new_opaque(tcx, def_id.to_def_id(), args);
333
334    // `ReErased` regions appear in the "parent_args" of closures/coroutines.
335    // We're ignoring them here and replacing them with fresh region variables.
336    // See tests in ui/type-alias-impl-trait/closure_{parent_args,wf_outlives}.rs.
337    //
338    // FIXME: Consider wrapping the hidden type in an existential `Binder` and instantiating it
339    // here rather than using ReErased.
340    let hidden_ty = tcx.type_of(def_id.to_def_id()).instantiate(tcx, args);
341    let hidden_ty = fold_regions(tcx, hidden_ty, |re, _dbi| match re.kind() {
342        ty::ReErased => infcx.next_region_var(RegionVariableOrigin::Misc(span)),
343        _ => re,
344    });
345
346    // HACK: We eagerly instantiate some bounds to report better errors for them...
347    // This isn't necessary for correctness, since we register these bounds when
348    // equating the opaque below, but we should clean this up in the new solver.
349    for (predicate, pred_span) in
350        tcx.explicit_item_bounds(def_id).iter_instantiated_copied(tcx, args)
351    {
352        let predicate = predicate.fold_with(&mut BottomUpFolder {
353            tcx,
354            ty_op: |ty| if ty == opaque_ty { hidden_ty } else { ty },
355            lt_op: |lt| lt,
356            ct_op: |ct| ct,
357        });
358
359        ocx.register_obligation(Obligation::new(
360            tcx,
361            ObligationCause::new(
362                span,
363                def_id,
364                ObligationCauseCode::OpaqueTypeBound(pred_span, definition_def_id),
365            ),
366            param_env,
367            predicate,
368        ));
369    }
370
371    let misc_cause = ObligationCause::misc(span, def_id);
372    // FIXME: We should just register the item bounds here, rather than equating.
373    // FIXME(const_trait_impl): When we do that, please make sure to also register
374    // the `[const]` bounds.
375    match ocx.eq(&misc_cause, param_env, opaque_ty, hidden_ty) {
376        Ok(()) => {}
377        Err(ty_err) => {
378            // Some types may be left "stranded" if they can't be reached
379            // from a lowered rustc_middle bound but they're mentioned in the HIR.
380            // This will happen, e.g., when a nested opaque is inside of a non-
381            // existent associated type, like `impl Trait<Missing = impl Trait>`.
382            // See <tests/ui/impl-trait/stranded-opaque.rs>.
383            let ty_err = ty_err.to_string(tcx);
384            let guar = tcx.dcx().span_delayed_bug(
385                span,
386                format!("could not unify `{hidden_ty}` with revealed type:\n{ty_err}"),
387            );
388            return Err(guar);
389        }
390    }
391
392    // Additionally require the hidden type to be well-formed with only the generics of the opaque type.
393    // Defining use functions may have more bounds than the opaque type, which is ok, as long as the
394    // hidden type is well formed even without those bounds.
395    let predicate =
396        ty::Binder::dummy(ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(hidden_ty.into())));
397    ocx.register_obligation(Obligation::new(tcx, misc_cause.clone(), param_env, predicate));
398
399    // Check that all obligations are satisfied by the implementation's
400    // version.
401    let errors = ocx.evaluate_obligations_error_on_ambiguity();
402    if !errors.is_empty() {
403        let guar = infcx.err_ctxt().report_fulfillment_errors(errors);
404        return Err(guar);
405    }
406
407    let wf_tys = ocx.assumed_wf_types_and_report_errors(param_env, defining_use_anchor)?;
408    ocx.resolve_regions_and_report_errors(defining_use_anchor, param_env, wf_tys)?;
409
410    if infcx.next_trait_solver() {
411        Ok(())
412    } else if let hir::OpaqueTyOrigin::FnReturn { .. } | hir::OpaqueTyOrigin::AsyncFn { .. } =
413        origin
414    {
415        // HACK: this should also fall through to the hidden type check below, but the original
416        // implementation had a bug where equivalent lifetimes are not identical. This caused us
417        // to reject existing stable code that is otherwise completely fine. The real fix is to
418        // compare the hidden types via our type equivalence/relation infra instead of doing an
419        // identity check.
420        let _ = infcx.take_opaque_types();
421        Ok(())
422    } else {
423        // Check that any hidden types found during wf checking match the hidden types that `type_of` sees.
424        for (mut key, mut ty) in infcx.take_opaque_types() {
425            ty.ty = infcx.resolve_vars_if_possible(ty.ty);
426            key = infcx.resolve_vars_if_possible(key);
427            sanity_check_found_hidden_type(tcx, key, ty)?;
428        }
429        Ok(())
430    }
431}
432
433fn best_definition_site_of_opaque<'tcx>(
434    tcx: TyCtxt<'tcx>,
435    opaque_def_id: LocalDefId,
436    origin: hir::OpaqueTyOrigin<LocalDefId>,
437) -> Option<(Span, LocalDefId)> {
438    struct TaitConstraintLocator<'tcx> {
439        opaque_def_id: LocalDefId,
440        tcx: TyCtxt<'tcx>,
441    }
442    impl<'tcx> TaitConstraintLocator<'tcx> {
443        fn check(&self, item_def_id: LocalDefId) -> ControlFlow<(Span, LocalDefId)> {
444            if !self.tcx.has_typeck_results(item_def_id) {
445                return ControlFlow::Continue(());
446            }
447
448            let opaque_types_defined_by = self.tcx.opaque_types_defined_by(item_def_id);
449            // Don't try to check items that cannot possibly constrain the type.
450            if !opaque_types_defined_by.contains(&self.opaque_def_id) {
451                return ControlFlow::Continue(());
452            }
453
454            if let Some(hidden_ty) = self
455                .tcx
456                .mir_borrowck(item_def_id)
457                .ok()
458                .and_then(|opaque_types| opaque_types.get(&self.opaque_def_id))
459            {
460                ControlFlow::Break((hidden_ty.span, item_def_id))
461            } else {
462                ControlFlow::Continue(())
463            }
464        }
465    }
466    impl<'tcx> intravisit::Visitor<'tcx> for TaitConstraintLocator<'tcx> {
467        type NestedFilter = nested_filter::All;
468        type Result = ControlFlow<(Span, LocalDefId)>;
469        fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
470            self.tcx
471        }
472        fn visit_expr(&mut self, ex: &'tcx hir::Expr<'tcx>) -> Self::Result {
473            intravisit::walk_expr(self, ex)
474        }
475        fn visit_item(&mut self, it: &'tcx hir::Item<'tcx>) -> Self::Result {
476            self.check(it.owner_id.def_id)?;
477            intravisit::walk_item(self, it)
478        }
479        fn visit_impl_item(&mut self, it: &'tcx hir::ImplItem<'tcx>) -> Self::Result {
480            self.check(it.owner_id.def_id)?;
481            intravisit::walk_impl_item(self, it)
482        }
483        fn visit_trait_item(&mut self, it: &'tcx hir::TraitItem<'tcx>) -> Self::Result {
484            self.check(it.owner_id.def_id)?;
485            intravisit::walk_trait_item(self, it)
486        }
487        fn visit_foreign_item(&mut self, it: &'tcx hir::ForeignItem<'tcx>) -> Self::Result {
488            intravisit::walk_foreign_item(self, it)
489        }
490    }
491
492    let mut locator = TaitConstraintLocator { tcx, opaque_def_id };
493    match origin {
494        hir::OpaqueTyOrigin::FnReturn { parent, .. }
495        | hir::OpaqueTyOrigin::AsyncFn { parent, .. } => locator.check(parent).break_value(),
496        hir::OpaqueTyOrigin::TyAlias { parent, in_assoc_ty: true } => {
497            let impl_def_id = tcx.local_parent(parent);
498            for assoc in tcx.associated_items(impl_def_id).in_definition_order() {
499                match assoc.kind {
500                    ty::AssocKind::Const { .. } | ty::AssocKind::Fn { .. } => {
501                        if let ControlFlow::Break(span) = locator.check(assoc.def_id.expect_local())
502                        {
503                            return Some(span);
504                        }
505                    }
506                    ty::AssocKind::Type { .. } => {}
507                }
508            }
509
510            None
511        }
512        hir::OpaqueTyOrigin::TyAlias { in_assoc_ty: false, .. } => {
513            tcx.hir_walk_toplevel_module(&mut locator).break_value()
514        }
515    }
516}
517
518fn sanity_check_found_hidden_type<'tcx>(
519    tcx: TyCtxt<'tcx>,
520    key: ty::OpaqueTypeKey<'tcx>,
521    mut ty: ty::ProvisionalHiddenType<'tcx>,
522) -> Result<(), ErrorGuaranteed> {
523    if ty.ty.is_ty_var() {
524        // Nothing was actually constrained.
525        return Ok(());
526    }
527    if let ty::Alias(ty::Opaque, alias) = ty.ty.kind() {
528        if alias.def_id == key.def_id.to_def_id() && alias.args == key.args {
529            // Nothing was actually constrained, this is an opaque usage that was
530            // only discovered to be opaque after inference vars resolved.
531            return Ok(());
532        }
533    }
534    let erase_re_vars = |ty: Ty<'tcx>| {
535        fold_regions(tcx, ty, |r, _| match r.kind() {
536            RegionKind::ReVar(_) => tcx.lifetimes.re_erased,
537            _ => r,
538        })
539    };
540    // Closures frequently end up containing erased lifetimes in their final representation.
541    // These correspond to lifetime variables that never got resolved, so we patch this up here.
542    ty.ty = erase_re_vars(ty.ty);
543    // Get the hidden type.
544    let hidden_ty = tcx.type_of(key.def_id).instantiate(tcx, key.args);
545    let hidden_ty = erase_re_vars(hidden_ty);
546
547    // If the hidden types differ, emit a type mismatch diagnostic.
548    if hidden_ty == ty.ty {
549        Ok(())
550    } else {
551        let span = tcx.def_span(key.def_id);
552        let other = ty::ProvisionalHiddenType { ty: hidden_ty, span };
553        Err(ty.build_mismatch_error(&other, tcx)?.emit())
554    }
555}
556
557/// Check that the opaque's precise captures list is valid (if present).
558/// We check this for regular `impl Trait`s and also RPITITs, even though the latter
559/// are technically GATs.
560///
561/// This function is responsible for:
562/// 1. Checking that all type/const params are mention in the captures list.
563/// 2. Checking that all lifetimes that are implicitly captured are mentioned.
564/// 3. Asserting that all parameters mentioned in the captures list are invariant.
565fn check_opaque_precise_captures<'tcx>(tcx: TyCtxt<'tcx>, opaque_def_id: LocalDefId) {
566    let hir::OpaqueTy { bounds, .. } = *tcx.hir_node_by_def_id(opaque_def_id).expect_opaque_ty();
567    let Some(precise_capturing_args) = bounds.iter().find_map(|bound| match *bound {
568        hir::GenericBound::Use(bounds, ..) => Some(bounds),
569        _ => None,
570    }) else {
571        // No precise capturing args; nothing to validate
572        return;
573    };
574
575    let mut expected_captures = UnordSet::default();
576    let mut shadowed_captures = UnordSet::default();
577    let mut seen_params = UnordMap::default();
578    let mut prev_non_lifetime_param = None;
579    for arg in precise_capturing_args {
580        let (hir_id, ident) = match *arg {
581            hir::PreciseCapturingArg::Param(hir::PreciseCapturingNonLifetimeArg {
582                hir_id,
583                ident,
584                ..
585            }) => {
586                if prev_non_lifetime_param.is_none() {
587                    prev_non_lifetime_param = Some(ident);
588                }
589                (hir_id, ident)
590            }
591            hir::PreciseCapturingArg::Lifetime(&hir::Lifetime { hir_id, ident, .. }) => {
592                if let Some(prev_non_lifetime_param) = prev_non_lifetime_param {
593                    tcx.dcx().emit_err(errors::LifetimesMustBeFirst {
594                        lifetime_span: ident.span,
595                        name: ident.name,
596                        other_span: prev_non_lifetime_param.span,
597                    });
598                }
599                (hir_id, ident)
600            }
601        };
602
603        let ident = ident.normalize_to_macros_2_0();
604        if let Some(span) = seen_params.insert(ident, ident.span) {
605            tcx.dcx().emit_err(errors::DuplicatePreciseCapture {
606                name: ident.name,
607                first_span: span,
608                second_span: ident.span,
609            });
610        }
611
612        match tcx.named_bound_var(hir_id) {
613            Some(ResolvedArg::EarlyBound(def_id)) => {
614                expected_captures.insert(def_id.to_def_id());
615
616                // Make sure we allow capturing these lifetimes through `Self` and
617                // `T::Assoc` projection syntax, too. These will occur when we only
618                // see lifetimes are captured after hir-lowering -- this aligns with
619                // the cases that were stabilized with the `impl_trait_projection`
620                // feature -- see <https://github.com/rust-lang/rust/pull/115659>.
621                if let DefKind::LifetimeParam = tcx.def_kind(def_id)
622                    && let Some(def_id) = tcx
623                        .map_opaque_lifetime_to_parent_lifetime(def_id)
624                        .opt_param_def_id(tcx, tcx.parent(opaque_def_id.to_def_id()))
625                {
626                    shadowed_captures.insert(def_id);
627                }
628            }
629            _ => {
630                tcx.dcx()
631                    .span_delayed_bug(tcx.hir_span(hir_id), "parameter should have been resolved");
632            }
633        }
634    }
635
636    let variances = tcx.variances_of(opaque_def_id);
637    let mut def_id = Some(opaque_def_id.to_def_id());
638    while let Some(generics) = def_id {
639        let generics = tcx.generics_of(generics);
640        def_id = generics.parent;
641
642        for param in &generics.own_params {
643            if expected_captures.contains(&param.def_id) {
644                match (&variances[param.index as usize], &ty::Invariant) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::Some(format_args!("precise captured param should be invariant")));
        }
    }
};assert_eq!(
645                    variances[param.index as usize],
646                    ty::Invariant,
647                    "precise captured param should be invariant"
648                );
649                continue;
650            }
651            // If a param is shadowed by a early-bound (duplicated) lifetime, then
652            // it may or may not be captured as invariant, depending on if it shows
653            // up through `Self` or `T::Assoc` syntax.
654            if shadowed_captures.contains(&param.def_id) {
655                continue;
656            }
657
658            match param.kind {
659                ty::GenericParamDefKind::Lifetime => {
660                    let use_span = tcx.def_span(param.def_id);
661                    let opaque_span = tcx.def_span(opaque_def_id);
662                    // Check if the lifetime param was captured but isn't named in the precise captures list.
663                    if variances[param.index as usize] == ty::Invariant {
664                        if let DefKind::OpaqueTy = tcx.def_kind(tcx.parent(param.def_id))
665                            && let Some(def_id) = tcx
666                                .map_opaque_lifetime_to_parent_lifetime(param.def_id.expect_local())
667                                .opt_param_def_id(tcx, tcx.parent(opaque_def_id.to_def_id()))
668                        {
669                            tcx.dcx().emit_err(errors::LifetimeNotCaptured {
670                                opaque_span,
671                                use_span,
672                                param_span: tcx.def_span(def_id),
673                            });
674                        } else {
675                            if tcx.def_kind(tcx.parent(param.def_id)) == DefKind::Trait {
676                                tcx.dcx().emit_err(errors::LifetimeImplicitlyCaptured {
677                                    opaque_span,
678                                    param_span: tcx.def_span(param.def_id),
679                                });
680                            } else {
681                                // If the `use_span` is actually just the param itself, then we must
682                                // have not duplicated the lifetime but captured the original.
683                                // The "effective" `use_span` will be the span of the opaque itself,
684                                // and the param span will be the def span of the param.
685                                tcx.dcx().emit_err(errors::LifetimeNotCaptured {
686                                    opaque_span,
687                                    use_span: opaque_span,
688                                    param_span: use_span,
689                                });
690                            }
691                        }
692                        continue;
693                    }
694                }
695                ty::GenericParamDefKind::Type { .. } => {
696                    if #[allow(non_exhaustive_omitted_patterns)] match tcx.def_kind(param.def_id) {
    DefKind::Trait | DefKind::TraitAlias => true,
    _ => false,
}matches!(tcx.def_kind(param.def_id), DefKind::Trait | DefKind::TraitAlias) {
697                        // FIXME(precise_capturing): Structured suggestion for this would be useful
698                        tcx.dcx().emit_err(errors::SelfTyNotCaptured {
699                            trait_span: tcx.def_span(param.def_id),
700                            opaque_span: tcx.def_span(opaque_def_id),
701                        });
702                    } else {
703                        // FIXME(precise_capturing): Structured suggestion for this would be useful
704                        tcx.dcx().emit_err(errors::ParamNotCaptured {
705                            param_span: tcx.def_span(param.def_id),
706                            opaque_span: tcx.def_span(opaque_def_id),
707                            kind: "type",
708                        });
709                    }
710                }
711                ty::GenericParamDefKind::Const { .. } => {
712                    // FIXME(precise_capturing): Structured suggestion for this would be useful
713                    tcx.dcx().emit_err(errors::ParamNotCaptured {
714                        param_span: tcx.def_span(param.def_id),
715                        opaque_span: tcx.def_span(opaque_def_id),
716                        kind: "const",
717                    });
718                }
719            }
720        }
721    }
722}
723
724fn is_enum_of_nonnullable_ptr<'tcx>(
725    tcx: TyCtxt<'tcx>,
726    adt_def: AdtDef<'tcx>,
727    args: GenericArgsRef<'tcx>,
728) -> bool {
729    if adt_def.repr().inhibit_enum_layout_opt() {
730        return false;
731    }
732
733    let [var_one, var_two] = &adt_def.variants().raw[..] else {
734        return false;
735    };
736    let (([], [field]) | ([field], [])) = (&var_one.fields.raw[..], &var_two.fields.raw[..]) else {
737        return false;
738    };
739    #[allow(non_exhaustive_omitted_patterns)] match field.ty(tcx, args).kind() {
    ty::FnPtr(..) | ty::Ref(..) => true,
    _ => false,
}matches!(field.ty(tcx, args).kind(), ty::FnPtr(..) | ty::Ref(..))
740}
741
742fn check_static_linkage(tcx: TyCtxt<'_>, def_id: LocalDefId) {
743    if tcx.codegen_fn_attrs(def_id).import_linkage.is_some() {
744        if match tcx.type_of(def_id).instantiate_identity().kind() {
745            ty::RawPtr(_, _) => false,
746            ty::Adt(adt_def, args) => !is_enum_of_nonnullable_ptr(tcx, *adt_def, *args),
747            _ => true,
748        } {
749            tcx.dcx().emit_err(errors::LinkageType { span: tcx.def_span(def_id) });
750        }
751    }
752}
753
754pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(), ErrorGuaranteed> {
755    let mut res = Ok(());
756    let generics = tcx.generics_of(def_id);
757
758    for param in &generics.own_params {
759        match param.kind {
760            ty::GenericParamDefKind::Lifetime { .. } => {}
761            ty::GenericParamDefKind::Type { has_default, .. } => {
762                if has_default {
763                    tcx.ensure_ok().type_of(param.def_id);
764                }
765            }
766            ty::GenericParamDefKind::Const { has_default, .. } => {
767                tcx.ensure_ok().type_of(param.def_id);
768                if has_default {
769                    // need to store default and type of default
770                    let ct = tcx.const_param_default(param.def_id).skip_binder();
771                    if let ty::ConstKind::Unevaluated(uv) = ct.kind() {
772                        tcx.ensure_ok().type_of(uv.def);
773                    }
774                }
775            }
776        }
777    }
778
779    match tcx.def_kind(def_id) {
780        DefKind::Static { .. } => {
781            tcx.ensure_ok().generics_of(def_id);
782            tcx.ensure_ok().type_of(def_id);
783            tcx.ensure_ok().predicates_of(def_id);
784
785            check_static_inhabited(tcx, def_id);
786            check_static_linkage(tcx, def_id);
787            let ty = tcx.type_of(def_id).instantiate_identity();
788            res = res.and(wfcheck::check_static_item(
789                tcx, def_id, ty, /* should_check_for_sync */ true,
790            ));
791
792            // Only `Node::Item` and `Node::ForeignItem` still have HIR based
793            // checks. Returning early here does not miss any checks and
794            // avoids this query from having a direct dependency edge on the HIR
795            return res;
796        }
797        DefKind::Enum => {
798            tcx.ensure_ok().generics_of(def_id);
799            tcx.ensure_ok().type_of(def_id);
800            tcx.ensure_ok().predicates_of(def_id);
801            crate::collect::lower_enum_variant_types(tcx, def_id);
802            check_enum(tcx, def_id);
803            check_variances_for_type_defn(tcx, def_id);
804        }
805        DefKind::Fn => {
806            tcx.ensure_ok().generics_of(def_id);
807            tcx.ensure_ok().type_of(def_id);
808            tcx.ensure_ok().predicates_of(def_id);
809            tcx.ensure_ok().fn_sig(def_id);
810            tcx.ensure_ok().codegen_fn_attrs(def_id);
811            if let Some(i) = tcx.intrinsic(def_id) {
812                intrinsic::check_intrinsic_type(
813                    tcx,
814                    def_id,
815                    tcx.def_ident_span(def_id).unwrap(),
816                    i.name,
817                )
818            }
819        }
820        DefKind::Impl { of_trait } => {
821            tcx.ensure_ok().generics_of(def_id);
822            tcx.ensure_ok().type_of(def_id);
823            tcx.ensure_ok().predicates_of(def_id);
824            tcx.ensure_ok().associated_items(def_id);
825            if of_trait {
826                let impl_trait_header = tcx.impl_trait_header(def_id);
827                res = res.and(
828                    tcx.ensure_result()
829                        .coherent_trait(impl_trait_header.trait_ref.instantiate_identity().def_id),
830                );
831
832                if res.is_ok() {
833                    // Checking this only makes sense if the all trait impls satisfy basic
834                    // requirements (see `coherent_trait` query), otherwise
835                    // we run into infinite recursions a lot.
836                    check_impl_items_against_trait(tcx, def_id, impl_trait_header);
837                }
838            }
839        }
840        DefKind::Trait => {
841            tcx.ensure_ok().generics_of(def_id);
842            tcx.ensure_ok().trait_def(def_id);
843            tcx.ensure_ok().explicit_super_predicates_of(def_id);
844            tcx.ensure_ok().predicates_of(def_id);
845            tcx.ensure_ok().associated_items(def_id);
846            let assoc_items = tcx.associated_items(def_id);
847
848            for &assoc_item in assoc_items.in_definition_order() {
849                match assoc_item.kind {
850                    ty::AssocKind::Type { .. } if assoc_item.defaultness(tcx).has_value() => {
851                        let trait_args = GenericArgs::identity_for_item(tcx, def_id);
852                        let _: Result<_, rustc_errors::ErrorGuaranteed> = check_type_bounds(
853                            tcx,
854                            assoc_item,
855                            assoc_item,
856                            ty::TraitRef::new_from_args(tcx, def_id.to_def_id(), trait_args),
857                        );
858                    }
859                    _ => {}
860                }
861            }
862        }
863        DefKind::TraitAlias => {
864            tcx.ensure_ok().generics_of(def_id);
865            tcx.ensure_ok().explicit_implied_predicates_of(def_id);
866            tcx.ensure_ok().explicit_super_predicates_of(def_id);
867            tcx.ensure_ok().predicates_of(def_id);
868        }
869        def_kind @ (DefKind::Struct | DefKind::Union) => {
870            tcx.ensure_ok().generics_of(def_id);
871            tcx.ensure_ok().type_of(def_id);
872            tcx.ensure_ok().predicates_of(def_id);
873
874            let adt = tcx.adt_def(def_id).non_enum_variant();
875            for f in adt.fields.iter() {
876                tcx.ensure_ok().generics_of(f.did);
877                tcx.ensure_ok().type_of(f.did);
878                tcx.ensure_ok().predicates_of(f.did);
879            }
880
881            if let Some((_, ctor_def_id)) = adt.ctor {
882                crate::collect::lower_variant_ctor(tcx, ctor_def_id.expect_local());
883            }
884            match def_kind {
885                DefKind::Struct => check_struct(tcx, def_id),
886                DefKind::Union => check_union(tcx, def_id),
887                _ => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
888            }
889            check_variances_for_type_defn(tcx, def_id);
890        }
891        DefKind::OpaqueTy => {
892            check_opaque_precise_captures(tcx, def_id);
893
894            let origin = tcx.local_opaque_ty_origin(def_id);
895            if let hir::OpaqueTyOrigin::FnReturn { parent: fn_def_id, .. }
896            | hir::OpaqueTyOrigin::AsyncFn { parent: fn_def_id, .. } = origin
897                && let hir::Node::TraitItem(trait_item) = tcx.hir_node_by_def_id(fn_def_id)
898                && let (_, hir::TraitFn::Required(..)) = trait_item.expect_fn()
899            {
900                // Skip opaques from RPIT in traits with no default body.
901            } else {
902                check_opaque(tcx, def_id);
903            }
904
905            tcx.ensure_ok().predicates_of(def_id);
906            tcx.ensure_ok().explicit_item_bounds(def_id);
907            tcx.ensure_ok().explicit_item_self_bounds(def_id);
908            if tcx.is_conditionally_const(def_id) {
909                tcx.ensure_ok().explicit_implied_const_bounds(def_id);
910                tcx.ensure_ok().const_conditions(def_id);
911            }
912
913            // Only `Node::Item` and `Node::ForeignItem` still have HIR based
914            // checks. Returning early here does not miss any checks and
915            // avoids this query from having a direct dependency edge on the HIR
916            return res;
917        }
918        DefKind::Const { .. } => {
919            tcx.ensure_ok().generics_of(def_id);
920            tcx.ensure_ok().type_of(def_id);
921            tcx.ensure_ok().predicates_of(def_id);
922
923            res = res.and(enter_wf_checking_ctxt(tcx, def_id, |wfcx| {
924                let ty = tcx.type_of(def_id).instantiate_identity();
925                let ty_span = tcx.ty_span(def_id);
926                let ty = wfcx.deeply_normalize(ty_span, Some(WellFormedLoc::Ty(def_id)), ty);
927                wfcx.register_wf_obligation(ty_span, Some(WellFormedLoc::Ty(def_id)), ty.into());
928                wfcx.register_bound(
929                    traits::ObligationCause::new(
930                        ty_span,
931                        def_id,
932                        ObligationCauseCode::SizedConstOrStatic,
933                    ),
934                    tcx.param_env(def_id),
935                    ty,
936                    tcx.require_lang_item(LangItem::Sized, ty_span),
937                );
938                check_where_clauses(wfcx, def_id);
939
940                if tcx.is_type_const(def_id) {
941                    wfcheck::check_type_const(wfcx, def_id, ty, true)?;
942                }
943                Ok(())
944            }));
945
946            // Only `Node::Item` and `Node::ForeignItem` still have HIR based
947            // checks. Returning early here does not miss any checks and
948            // avoids this query from having a direct dependency edge on the HIR
949            return res;
950        }
951        DefKind::TyAlias => {
952            tcx.ensure_ok().generics_of(def_id);
953            tcx.ensure_ok().type_of(def_id);
954            tcx.ensure_ok().predicates_of(def_id);
955            check_type_alias_type_params_are_used(tcx, def_id);
956            if tcx.type_alias_is_lazy(def_id) {
957                res = res.and(enter_wf_checking_ctxt(tcx, def_id, |wfcx| {
958                    let ty = tcx.type_of(def_id).instantiate_identity();
959                    let span = tcx.def_span(def_id);
960                    let item_ty = wfcx.deeply_normalize(span, Some(WellFormedLoc::Ty(def_id)), ty);
961                    wfcx.register_wf_obligation(
962                        span,
963                        Some(WellFormedLoc::Ty(def_id)),
964                        item_ty.into(),
965                    );
966                    check_where_clauses(wfcx, def_id);
967                    Ok(())
968                }));
969                check_variances_for_type_defn(tcx, def_id);
970            }
971
972            // Only `Node::Item` and `Node::ForeignItem` still have HIR based
973            // checks. Returning early here does not miss any checks and
974            // avoids this query from having a direct dependency edge on the HIR
975            return res;
976        }
977        DefKind::ForeignMod => {
978            let it = tcx.hir_expect_item(def_id);
979            let hir::ItemKind::ForeignMod { abi, items } = it.kind else {
980                return Ok(());
981            };
982
983            check_abi(tcx, it.hir_id(), it.span, abi);
984
985            for &item in items {
986                let def_id = item.owner_id.def_id;
987
988                let generics = tcx.generics_of(def_id);
989                let own_counts = generics.own_counts();
990                if generics.own_params.len() - own_counts.lifetimes != 0 {
991                    let (kinds, kinds_pl, egs) = match (own_counts.types, own_counts.consts) {
992                        (_, 0) => ("type", "types", Some("u32")),
993                        // We don't specify an example value, because we can't generate
994                        // a valid value for any type.
995                        (0, _) => ("const", "consts", None),
996                        _ => ("type or const", "types or consts", None),
997                    };
998                    let name = if {

        #[allow(deprecated)]
        {
            {
                'done:
                    {
                    for i in tcx.get_all_attrs(def_id) {
                        #[allow(unused_imports)]
                        use rustc_hir::attrs::AttributeKind::*;
                        let i: &rustc_hir::Attribute = i;
                        match i {
                            rustc_hir::Attribute::Parsed(RustcEiiForeignItem) => {
                                break 'done Some(());
                            }
                            rustc_hir::Attribute::Unparsed(..) =>
                                {}
                                #[deny(unreachable_patterns)]
                                _ => {}
                        }
                    }
                    None
                }
            }
        }
    }.is_some()find_attr!(tcx, def_id, RustcEiiForeignItem) {
999                        "externally implementable items"
1000                    } else {
1001                        "foreign items"
1002                    };
1003
1004                    let span = tcx.def_span(def_id);
1005                    {
    tcx.dcx().struct_span_err(span,
            ::alloc::__export::must_use({
                    ::alloc::fmt::format(format_args!("{0} may not have {1} parameters",
                            name, kinds))
                })).with_code(E0044)
}struct_span_code_err!(
1006                        tcx.dcx(),
1007                        span,
1008                        E0044,
1009                        "{name} may not have {kinds} parameters",
1010                    )
1011                    .with_span_label(span, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("can\'t have {0} parameters",
                kinds))
    })format!("can't have {kinds} parameters"))
1012                    .with_help(
1013                        // FIXME: once we start storing spans for type arguments, turn this
1014                        // into a suggestion.
1015                        ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("replace the {0} parameters with concrete {1}{2}",
                kinds, kinds_pl,
                egs.map(|egs|
                            ::alloc::__export::must_use({
                                    ::alloc::fmt::format(format_args!(" like `{0}`", egs))
                                })).unwrap_or_default()))
    })format!(
1016                            "replace the {} parameters with concrete {}{}",
1017                            kinds,
1018                            kinds_pl,
1019                            egs.map(|egs| format!(" like `{egs}`")).unwrap_or_default(),
1020                        ),
1021                    )
1022                    .emit();
1023                }
1024
1025                tcx.ensure_ok().generics_of(def_id);
1026                tcx.ensure_ok().type_of(def_id);
1027                tcx.ensure_ok().predicates_of(def_id);
1028                if tcx.is_conditionally_const(def_id) {
1029                    tcx.ensure_ok().explicit_implied_const_bounds(def_id);
1030                    tcx.ensure_ok().const_conditions(def_id);
1031                }
1032                match tcx.def_kind(def_id) {
1033                    DefKind::Fn => {
1034                        tcx.ensure_ok().codegen_fn_attrs(def_id);
1035                        tcx.ensure_ok().fn_sig(def_id);
1036                        let item = tcx.hir_foreign_item(item);
1037                        let hir::ForeignItemKind::Fn(sig, ..) = item.kind else { ::rustc_middle::util::bug::bug_fmt(format_args!("impossible case reached"))bug!() };
1038                        check_c_variadic_abi(tcx, sig.decl, abi, item.span);
1039                    }
1040                    DefKind::Static { .. } => {
1041                        tcx.ensure_ok().codegen_fn_attrs(def_id);
1042                    }
1043                    _ => (),
1044                }
1045            }
1046        }
1047        DefKind::Closure => {
1048            // This is guaranteed to be called by metadata encoding,
1049            // we still call it in wfcheck eagerly to ensure errors in codegen
1050            // attrs prevent lints from spamming the output.
1051            tcx.ensure_ok().codegen_fn_attrs(def_id);
1052            // We do not call `type_of` for closures here as that
1053            // depends on typecheck and would therefore hide
1054            // any further errors in case one typeck fails.
1055
1056            // Only `Node::Item` and `Node::ForeignItem` still have HIR based
1057            // checks. Returning early here does not miss any checks and
1058            // avoids this query from having a direct dependency edge on the HIR
1059            return res;
1060        }
1061        DefKind::AssocFn => {
1062            tcx.ensure_ok().codegen_fn_attrs(def_id);
1063            tcx.ensure_ok().type_of(def_id);
1064            tcx.ensure_ok().fn_sig(def_id);
1065            tcx.ensure_ok().predicates_of(def_id);
1066            res = res.and(check_associated_item(tcx, def_id));
1067            let assoc_item = tcx.associated_item(def_id);
1068            match assoc_item.container {
1069                ty::AssocContainer::InherentImpl | ty::AssocContainer::TraitImpl(_) => {}
1070                ty::AssocContainer::Trait => {
1071                    res = res.and(check_trait_item(tcx, def_id));
1072                }
1073            }
1074
1075            // Only `Node::Item` and `Node::ForeignItem` still have HIR based
1076            // checks. Returning early here does not miss any checks and
1077            // avoids this query from having a direct dependency edge on the HIR
1078            return res;
1079        }
1080        DefKind::AssocConst { .. } => {
1081            tcx.ensure_ok().type_of(def_id);
1082            tcx.ensure_ok().predicates_of(def_id);
1083            res = res.and(check_associated_item(tcx, def_id));
1084            let assoc_item = tcx.associated_item(def_id);
1085            match assoc_item.container {
1086                ty::AssocContainer::InherentImpl | ty::AssocContainer::TraitImpl(_) => {}
1087                ty::AssocContainer::Trait => {
1088                    res = res.and(check_trait_item(tcx, def_id));
1089                }
1090            }
1091
1092            // Only `Node::Item` and `Node::ForeignItem` still have HIR based
1093            // checks. Returning early here does not miss any checks and
1094            // avoids this query from having a direct dependency edge on the HIR
1095            return res;
1096        }
1097        DefKind::AssocTy => {
1098            tcx.ensure_ok().predicates_of(def_id);
1099            res = res.and(check_associated_item(tcx, def_id));
1100
1101            let assoc_item = tcx.associated_item(def_id);
1102            let has_type = match assoc_item.container {
1103                ty::AssocContainer::InherentImpl | ty::AssocContainer::TraitImpl(_) => true,
1104                ty::AssocContainer::Trait => {
1105                    tcx.ensure_ok().explicit_item_bounds(def_id);
1106                    tcx.ensure_ok().explicit_item_self_bounds(def_id);
1107                    if tcx.is_conditionally_const(def_id) {
1108                        tcx.ensure_ok().explicit_implied_const_bounds(def_id);
1109                        tcx.ensure_ok().const_conditions(def_id);
1110                    }
1111                    res = res.and(check_trait_item(tcx, def_id));
1112                    assoc_item.defaultness(tcx).has_value()
1113                }
1114            };
1115            if has_type {
1116                tcx.ensure_ok().type_of(def_id);
1117            }
1118
1119            // Only `Node::Item` and `Node::ForeignItem` still have HIR based
1120            // checks. Returning early here does not miss any checks and
1121            // avoids this query from having a direct dependency edge on the HIR
1122            return res;
1123        }
1124
1125        // Only `Node::Item` and `Node::ForeignItem` still have HIR based
1126        // checks. Returning early here does not miss any checks and
1127        // avoids this query from having a direct dependency edge on the HIR
1128        DefKind::AnonConst | DefKind::InlineConst => return res,
1129        _ => {}
1130    }
1131    let node = tcx.hir_node_by_def_id(def_id);
1132    res.and(match node {
1133        hir::Node::Crate(_) => ::rustc_middle::util::bug::bug_fmt(format_args!("check_well_formed cannot be applied to the crate root"))bug!("check_well_formed cannot be applied to the crate root"),
1134        hir::Node::Item(item) => wfcheck::check_item(tcx, item),
1135        hir::Node::ForeignItem(item) => wfcheck::check_foreign_item(tcx, item),
1136        _ => {
    ::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
            format_args!("{0:?}", node)));
}unreachable!("{node:?}"),
1137    })
1138}
1139
1140pub(super) fn check_specialization_validity<'tcx>(
1141    tcx: TyCtxt<'tcx>,
1142    trait_def: &ty::TraitDef,
1143    trait_item: ty::AssocItem,
1144    impl_id: DefId,
1145    impl_item: DefId,
1146) {
1147    let Ok(ancestors) = trait_def.ancestors(tcx, impl_id) else { return };
1148    let mut ancestor_impls = ancestors.skip(1).filter_map(|parent| {
1149        if parent.is_from_trait() {
1150            None
1151        } else {
1152            Some((parent, parent.item(tcx, trait_item.def_id)))
1153        }
1154    });
1155
1156    let opt_result = ancestor_impls.find_map(|(parent_impl, parent_item)| {
1157        match parent_item {
1158            // Parent impl exists, and contains the parent item we're trying to specialize, but
1159            // doesn't mark it `default`.
1160            Some(parent_item) if traits::impl_item_is_final(tcx, &parent_item) => {
1161                Some(Err(parent_impl.def_id()))
1162            }
1163
1164            // Parent impl contains item and makes it specializable.
1165            Some(_) => Some(Ok(())),
1166
1167            // Parent impl doesn't mention the item. This means it's inherited from the
1168            // grandparent. In that case, if parent is a `default impl`, inherited items use the
1169            // "defaultness" from the grandparent, else they are final.
1170            None => {
1171                if tcx.defaultness(parent_impl.def_id()).is_default() {
1172                    None
1173                } else {
1174                    Some(Err(parent_impl.def_id()))
1175                }
1176            }
1177        }
1178    });
1179
1180    // If `opt_result` is `None`, we have only encountered `default impl`s that don't contain the
1181    // item. This is allowed, the item isn't actually getting specialized here.
1182    let result = opt_result.unwrap_or(Ok(()));
1183
1184    if let Err(parent_impl) = result {
1185        if !tcx.is_impl_trait_in_trait(impl_item) {
1186            let span = tcx.def_span(impl_item);
1187            let ident = tcx.item_ident(impl_item);
1188
1189            let err = match tcx.span_of_impl(parent_impl) {
1190                Ok(sp) => errors::ImplNotMarkedDefault::Ok { span, ident, ok_label: sp },
1191                Err(cname) => errors::ImplNotMarkedDefault::Err { span, ident, cname },
1192            };
1193
1194            tcx.dcx().emit_err(err);
1195        } else {
1196            tcx.dcx().delayed_bug(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("parent item: {0:?} not marked as default",
                parent_impl))
    })format!("parent item: {parent_impl:?} not marked as default"));
1197        }
1198    }
1199}
1200
1201fn check_overriding_final_trait_item<'tcx>(
1202    tcx: TyCtxt<'tcx>,
1203    trait_item: ty::AssocItem,
1204    impl_item: ty::AssocItem,
1205) {
1206    if trait_item.defaultness(tcx).is_final() {
1207        tcx.dcx().emit_err(errors::OverridingFinalTraitFunction {
1208            impl_span: tcx.def_span(impl_item.def_id),
1209            trait_span: tcx.def_span(trait_item.def_id),
1210            ident: tcx.item_ident(impl_item.def_id),
1211        });
1212    }
1213}
1214
1215fn check_impl_items_against_trait<'tcx>(
1216    tcx: TyCtxt<'tcx>,
1217    impl_id: LocalDefId,
1218    impl_trait_header: ty::ImplTraitHeader<'tcx>,
1219) {
1220    let trait_ref = impl_trait_header.trait_ref.instantiate_identity();
1221    // If the trait reference itself is erroneous (so the compilation is going
1222    // to fail), skip checking the items here -- the `impl_item` table in `tcx`
1223    // isn't populated for such impls.
1224    if trait_ref.references_error() {
1225        return;
1226    }
1227
1228    let impl_item_refs = tcx.associated_item_def_ids(impl_id);
1229
1230    // Negative impls are not expected to have any items
1231    match impl_trait_header.polarity {
1232        ty::ImplPolarity::Reservation | ty::ImplPolarity::Positive => {}
1233        ty::ImplPolarity::Negative => {
1234            if let [first_item_ref, ..] = *impl_item_refs {
1235                let first_item_span = tcx.def_span(first_item_ref);
1236                {
    tcx.dcx().struct_span_err(first_item_span,
            ::alloc::__export::must_use({
                    ::alloc::fmt::format(format_args!("negative impls cannot have any items"))
                })).with_code(E0749)
}struct_span_code_err!(
1237                    tcx.dcx(),
1238                    first_item_span,
1239                    E0749,
1240                    "negative impls cannot have any items"
1241                )
1242                .emit();
1243            }
1244            return;
1245        }
1246    }
1247
1248    let trait_def = tcx.trait_def(trait_ref.def_id);
1249
1250    let self_is_guaranteed_unsize_self = tcx.impl_self_is_guaranteed_unsized(impl_id);
1251
1252    for &impl_item in impl_item_refs {
1253        let ty_impl_item = tcx.associated_item(impl_item);
1254        let ty_trait_item = match ty_impl_item.expect_trait_impl() {
1255            Ok(trait_item_id) => tcx.associated_item(trait_item_id),
1256            Err(ErrorGuaranteed { .. }) => continue,
1257        };
1258
1259        let res = tcx.ensure_result().compare_impl_item(impl_item.expect_local());
1260        if res.is_ok() {
1261            match ty_impl_item.kind {
1262                ty::AssocKind::Fn { .. } => {
1263                    compare_impl_item::refine::check_refining_return_position_impl_trait_in_trait(
1264                        tcx,
1265                        ty_impl_item,
1266                        ty_trait_item,
1267                        tcx.impl_trait_ref(ty_impl_item.container_id(tcx)).instantiate_identity(),
1268                    );
1269                }
1270                ty::AssocKind::Const { .. } => {}
1271                ty::AssocKind::Type { .. } => {}
1272            }
1273        }
1274
1275        if self_is_guaranteed_unsize_self && tcx.generics_require_sized_self(ty_trait_item.def_id) {
1276            tcx.emit_node_span_lint(
1277                rustc_lint_defs::builtin::DEAD_CODE,
1278                tcx.local_def_id_to_hir_id(ty_impl_item.def_id.expect_local()),
1279                tcx.def_span(ty_impl_item.def_id),
1280                errors::UselessImplItem,
1281            )
1282        }
1283
1284        check_specialization_validity(
1285            tcx,
1286            trait_def,
1287            ty_trait_item,
1288            impl_id.to_def_id(),
1289            impl_item,
1290        );
1291
1292        check_overriding_final_trait_item(tcx, ty_trait_item, ty_impl_item);
1293    }
1294
1295    if let Ok(ancestors) = trait_def.ancestors(tcx, impl_id.to_def_id()) {
1296        // Check for missing items from trait
1297        let mut missing_items = Vec::new();
1298
1299        let mut must_implement_one_of: Option<&[Ident]> =
1300            trait_def.must_implement_one_of.as_deref();
1301
1302        for &trait_item_id in tcx.associated_item_def_ids(trait_ref.def_id) {
1303            let leaf_def = ancestors.leaf_def(tcx, trait_item_id);
1304
1305            let is_implemented = leaf_def
1306                .as_ref()
1307                .is_some_and(|node_item| node_item.item.defaultness(tcx).has_value());
1308
1309            if !is_implemented
1310                && tcx.defaultness(impl_id).is_final()
1311                // unsized types don't need to implement methods that have `Self: Sized` bounds.
1312                && !(self_is_guaranteed_unsize_self && tcx.generics_require_sized_self(trait_item_id))
1313            {
1314                missing_items.push(tcx.associated_item(trait_item_id));
1315            }
1316
1317            // true if this item is specifically implemented in this impl
1318            let is_implemented_here =
1319                leaf_def.as_ref().is_some_and(|node_item| !node_item.defining_node.is_from_trait());
1320
1321            if !is_implemented_here {
1322                let full_impl_span = tcx.hir_span_with_body(tcx.local_def_id_to_hir_id(impl_id));
1323                match tcx.eval_default_body_stability(trait_item_id, full_impl_span) {
1324                    EvalResult::Deny { feature, reason, issue, .. } => default_body_is_unstable(
1325                        tcx,
1326                        full_impl_span,
1327                        trait_item_id,
1328                        feature,
1329                        reason,
1330                        issue,
1331                    ),
1332
1333                    // Unmarked default bodies are considered stable (at least for now).
1334                    EvalResult::Allow | EvalResult::Unmarked => {}
1335                }
1336            }
1337
1338            if let Some(required_items) = &must_implement_one_of {
1339                if is_implemented_here {
1340                    let trait_item = tcx.associated_item(trait_item_id);
1341                    if required_items.contains(&trait_item.ident(tcx)) {
1342                        must_implement_one_of = None;
1343                    }
1344                }
1345            }
1346
1347            if let Some(leaf_def) = &leaf_def
1348                && !leaf_def.is_final()
1349                && let def_id = leaf_def.item.def_id
1350                && tcx.impl_method_has_trait_impl_trait_tys(def_id)
1351            {
1352                let def_kind = tcx.def_kind(def_id);
1353                let descr = tcx.def_kind_descr(def_kind, def_id);
1354                let (msg, feature) = if tcx.asyncness(def_id).is_async() {
1355                    (
1356                        ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("async {0} in trait cannot be specialized",
                descr))
    })format!("async {descr} in trait cannot be specialized"),
1357                        "async functions in traits",
1358                    )
1359                } else {
1360                    (
1361                        ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0} with return-position `impl Trait` in trait cannot be specialized",
                descr))
    })format!(
1362                            "{descr} with return-position `impl Trait` in trait cannot be specialized"
1363                        ),
1364                        "return position `impl Trait` in traits",
1365                    )
1366                };
1367                tcx.dcx()
1368                    .struct_span_err(tcx.def_span(def_id), msg)
1369                    .with_note(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("specialization behaves in inconsistent and surprising ways with {0}, and for now is disallowed",
                feature))
    })format!(
1370                        "specialization behaves in inconsistent and surprising ways with \
1371                        {feature}, and for now is disallowed"
1372                    ))
1373                    .emit();
1374            }
1375        }
1376
1377        if !missing_items.is_empty() {
1378            let full_impl_span = tcx.hir_span_with_body(tcx.local_def_id_to_hir_id(impl_id));
1379            missing_items_err(tcx, impl_id, &missing_items, full_impl_span);
1380        }
1381
1382        if let Some(missing_items) = must_implement_one_of {
1383            let attr_span = {

    #[allow(deprecated)]
    {
        {
            'done:
                {
                for i in tcx.get_all_attrs(trait_ref.def_id) {
                    #[allow(unused_imports)]
                    use rustc_hir::attrs::AttributeKind::*;
                    let i: &rustc_hir::Attribute = i;
                    match i {
                        rustc_hir::Attribute::Parsed(RustcMustImplementOneOf {
                            attr_span, .. }) => {
                            break 'done Some(*attr_span);
                        }
                        rustc_hir::Attribute::Unparsed(..) =>
                            {}
                            #[deny(unreachable_patterns)]
                            _ => {}
                    }
                }
                None
            }
        }
    }
}find_attr!(tcx, trait_ref.def_id, RustcMustImplementOneOf {attr_span, ..} => *attr_span);
1384
1385            missing_items_must_implement_one_of_err(
1386                tcx,
1387                tcx.def_span(impl_id),
1388                missing_items,
1389                attr_span,
1390            );
1391        }
1392    }
1393}
1394
1395fn check_simd(tcx: TyCtxt<'_>, sp: Span, def_id: LocalDefId) {
1396    let t = tcx.type_of(def_id).instantiate_identity();
1397    if let ty::Adt(def, args) = t.kind()
1398        && def.is_struct()
1399    {
1400        let fields = &def.non_enum_variant().fields;
1401        if fields.is_empty() {
1402            {
    tcx.dcx().struct_span_err(sp,
            ::alloc::__export::must_use({
                    ::alloc::fmt::format(format_args!("SIMD vector cannot be empty"))
                })).with_code(E0075)
}struct_span_code_err!(tcx.dcx(), sp, E0075, "SIMD vector cannot be empty").emit();
1403            return;
1404        }
1405
1406        let array_field = &fields[FieldIdx::ZERO];
1407        let array_ty = array_field.ty(tcx, args);
1408        let ty::Array(element_ty, len_const) = array_ty.kind() else {
1409            {
    tcx.dcx().struct_span_err(sp,
            ::alloc::__export::must_use({
                    ::alloc::fmt::format(format_args!("SIMD vector\'s only field must be an array"))
                })).with_code(E0076)
}struct_span_code_err!(
1410                tcx.dcx(),
1411                sp,
1412                E0076,
1413                "SIMD vector's only field must be an array"
1414            )
1415            .with_span_label(tcx.def_span(array_field.did), "not an array")
1416            .emit();
1417            return;
1418        };
1419
1420        if let Some(second_field) = fields.get(FieldIdx::ONE) {
1421            {
    tcx.dcx().struct_span_err(sp,
            ::alloc::__export::must_use({
                    ::alloc::fmt::format(format_args!("SIMD vector cannot have multiple fields"))
                })).with_code(E0075)
}struct_span_code_err!(tcx.dcx(), sp, E0075, "SIMD vector cannot have multiple fields")
1422                .with_span_label(tcx.def_span(second_field.did), "excess field")
1423                .emit();
1424            return;
1425        }
1426
1427        // FIXME(repr_simd): This check is nice, but perhaps unnecessary due to the fact
1428        // we do not expect users to implement their own `repr(simd)` types. If they could,
1429        // this check is easily side-steppable by hiding the const behind normalization.
1430        // The consequence is that the error is, in general, only observable post-mono.
1431        if let Some(len) = len_const.try_to_target_usize(tcx) {
1432            if len == 0 {
1433                {
    tcx.dcx().struct_span_err(sp,
            ::alloc::__export::must_use({
                    ::alloc::fmt::format(format_args!("SIMD vector cannot be empty"))
                })).with_code(E0075)
}struct_span_code_err!(tcx.dcx(), sp, E0075, "SIMD vector cannot be empty").emit();
1434                return;
1435            } else if len > MAX_SIMD_LANES {
1436                {
    tcx.dcx().struct_span_err(sp,
            ::alloc::__export::must_use({
                    ::alloc::fmt::format(format_args!("SIMD vector cannot have more than {0} elements",
                            MAX_SIMD_LANES))
                })).with_code(E0075)
}struct_span_code_err!(
1437                    tcx.dcx(),
1438                    sp,
1439                    E0075,
1440                    "SIMD vector cannot have more than {MAX_SIMD_LANES} elements",
1441                )
1442                .emit();
1443                return;
1444            }
1445        }
1446
1447        // Check that we use types valid for use in the lanes of a SIMD "vector register"
1448        // These are scalar types which directly match a "machine" type
1449        // Yes: Integers, floats, "thin" pointers
1450        // No: char, "wide" pointers, compound types
1451        match element_ty.kind() {
1452            ty::Param(_) => (), // pass struct<T>([T; 4]) through, let monomorphization catch errors
1453            ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::RawPtr(_, _) => (), // struct([u8; 4]) is ok
1454            _ => {
1455                {
    tcx.dcx().struct_span_err(sp,
            ::alloc::__export::must_use({
                    ::alloc::fmt::format(format_args!("SIMD vector element type should be a primitive scalar (integer/float/pointer) type"))
                })).with_code(E0077)
}struct_span_code_err!(
1456                    tcx.dcx(),
1457                    sp,
1458                    E0077,
1459                    "SIMD vector element type should be a \
1460                        primitive scalar (integer/float/pointer) type"
1461                )
1462                .emit();
1463                return;
1464            }
1465        }
1466    }
1467}
1468
1469#[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("check_scalable_vector",
                                    "rustc_hir_analysis::check::check", ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/check/check.rs"),
                                    ::tracing_core::__macro_support::Option::Some(1469u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::check::check"),
                                    ::tracing_core::field::FieldSet::new(&["span", "def_id",
                                                    "scalable"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = meta.fields().iter();
                                meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&span)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&def_id)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&scalable)
                                                            as &dyn Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return: () = loop {};
            return __tracing_attr_fake_return;
        }
        {
            let ty = tcx.type_of(def_id).instantiate_identity();
            let ty::Adt(def, args) = ty.kind() else { return };
            if !def.is_struct() {
                tcx.dcx().delayed_bug("`rustc_scalable_vector` applied to non-struct");
                return;
            }
            let fields = &def.non_enum_variant().fields;
            match scalable {
                ScalableElt::ElementCount(..) if fields.is_empty() => {
                    let mut err =
                        tcx.dcx().struct_span_err(span,
                            "scalable vectors must have a single field");
                    err.help("scalable vector types' only field must be a primitive scalar type");
                    err.emit();
                    return;
                }
                ScalableElt::ElementCount(..) if fields.len() >= 2 => {
                    tcx.dcx().struct_span_err(span,
                            "scalable vectors cannot have multiple fields").emit();
                    return;
                }
                ScalableElt::Container if fields.is_empty() => {
                    let mut err =
                        tcx.dcx().struct_span_err(span,
                            "scalable vectors must have a single field");
                    err.help("tuples of scalable vectors can only contain multiple of the same scalable vector type");
                    err.emit();
                    return;
                }
                _ => {}
            }
            match scalable {
                ScalableElt::ElementCount(..) => {
                    let element_ty = &fields[FieldIdx::ZERO].ty(tcx, args);
                    match element_ty.kind() {
                        ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::Bool => (),
                        _ => {
                            let mut err =
                                tcx.dcx().struct_span_err(span,
                                    "element type of a scalable vector must be a primitive scalar");
                            err.help("only `u*`, `i*`, `f*` and `bool` types are accepted");
                            err.emit();
                        }
                    }
                }
                ScalableElt::Container => {
                    let mut prev_field_ty = None;
                    for field in fields.iter() {
                        let element_ty = field.ty(tcx, args);
                        if let ty::Adt(def, _) = element_ty.kind() &&
                                def.repr().scalable() {
                            match def.repr().scalable.expect("`repr().scalable.is_some()` != `repr().scalable()`")
                                {
                                ScalableElt::ElementCount(_) => {}
                                ScalableElt::Container => {
                                    tcx.dcx().span_err(tcx.def_span(field.did),
                                        "scalable vector structs cannot contain other scalable vector structs");
                                    break;
                                }
                            }
                        } else {
                            tcx.dcx().span_err(tcx.def_span(field.did),
                                "scalable vector structs can only have scalable vector fields");
                            break;
                        }
                        if let Some(prev_ty) = prev_field_ty.replace(element_ty) &&
                                prev_ty != element_ty {
                            tcx.dcx().span_err(tcx.def_span(field.did),
                                "all fields in a scalable vector struct must be the same type");
                            break;
                        }
                    }
                }
            }
        }
    }
}#[tracing::instrument(skip(tcx), level = "debug")]
1470fn check_scalable_vector(tcx: TyCtxt<'_>, span: Span, def_id: LocalDefId, scalable: ScalableElt) {
1471    let ty = tcx.type_of(def_id).instantiate_identity();
1472    let ty::Adt(def, args) = ty.kind() else { return };
1473    if !def.is_struct() {
1474        tcx.dcx().delayed_bug("`rustc_scalable_vector` applied to non-struct");
1475        return;
1476    }
1477
1478    let fields = &def.non_enum_variant().fields;
1479    match scalable {
1480        ScalableElt::ElementCount(..) if fields.is_empty() => {
1481            let mut err =
1482                tcx.dcx().struct_span_err(span, "scalable vectors must have a single field");
1483            err.help("scalable vector types' only field must be a primitive scalar type");
1484            err.emit();
1485            return;
1486        }
1487        ScalableElt::ElementCount(..) if fields.len() >= 2 => {
1488            tcx.dcx().struct_span_err(span, "scalable vectors cannot have multiple fields").emit();
1489            return;
1490        }
1491        ScalableElt::Container if fields.is_empty() => {
1492            let mut err =
1493                tcx.dcx().struct_span_err(span, "scalable vectors must have a single field");
1494            err.help("tuples of scalable vectors can only contain multiple of the same scalable vector type");
1495            err.emit();
1496            return;
1497        }
1498        _ => {}
1499    }
1500
1501    match scalable {
1502        ScalableElt::ElementCount(..) => {
1503            let element_ty = &fields[FieldIdx::ZERO].ty(tcx, args);
1504
1505            // Check that `element_ty` only uses types valid in the lanes of a scalable vector
1506            // register: scalar types which directly match a "machine" type - integers, floats and
1507            // bools
1508            match element_ty.kind() {
1509                ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::Bool => (),
1510                _ => {
1511                    let mut err = tcx.dcx().struct_span_err(
1512                        span,
1513                        "element type of a scalable vector must be a primitive scalar",
1514                    );
1515                    err.help("only `u*`, `i*`, `f*` and `bool` types are accepted");
1516                    err.emit();
1517                }
1518            }
1519        }
1520        ScalableElt::Container => {
1521            let mut prev_field_ty = None;
1522            for field in fields.iter() {
1523                let element_ty = field.ty(tcx, args);
1524                if let ty::Adt(def, _) = element_ty.kind()
1525                    && def.repr().scalable()
1526                {
1527                    match def
1528                        .repr()
1529                        .scalable
1530                        .expect("`repr().scalable.is_some()` != `repr().scalable()`")
1531                    {
1532                        ScalableElt::ElementCount(_) => { /* expected field */ }
1533                        ScalableElt::Container => {
1534                            tcx.dcx().span_err(
1535                                tcx.def_span(field.did),
1536                                "scalable vector structs cannot contain other scalable vector structs",
1537                            );
1538                            break;
1539                        }
1540                    }
1541                } else {
1542                    tcx.dcx().span_err(
1543                        tcx.def_span(field.did),
1544                        "scalable vector structs can only have scalable vector fields",
1545                    );
1546                    break;
1547                }
1548
1549                if let Some(prev_ty) = prev_field_ty.replace(element_ty)
1550                    && prev_ty != element_ty
1551                {
1552                    tcx.dcx().span_err(
1553                        tcx.def_span(field.did),
1554                        "all fields in a scalable vector struct must be the same type",
1555                    );
1556                    break;
1557                }
1558            }
1559        }
1560    }
1561}
1562
1563pub(super) fn check_packed(tcx: TyCtxt<'_>, sp: Span, def: ty::AdtDef<'_>) {
1564    let repr = def.repr();
1565    if repr.packed() {
1566        if let Some(reprs) = {

    #[allow(deprecated)]
    {
        {
            'done:
                {
                for i in tcx.get_all_attrs(def.did()) {
                    #[allow(unused_imports)]
                    use rustc_hir::attrs::AttributeKind::*;
                    let i: &rustc_hir::Attribute = i;
                    match i {
                        rustc_hir::Attribute::Parsed(Repr { reprs, .. }) => {
                            break 'done Some(reprs);
                        }
                        rustc_hir::Attribute::Unparsed(..) =>
                            {}
                            #[deny(unreachable_patterns)]
                            _ => {}
                    }
                }
                None
            }
        }
    }
}find_attr!(tcx, def.did(), Repr { reprs, .. } => reprs) {
1567            for (r, _) in reprs {
1568                if let ReprPacked(pack) = r
1569                    && let Some(repr_pack) = repr.pack
1570                    && pack != &repr_pack
1571                {
1572                    {
    tcx.dcx().struct_span_err(sp,
            ::alloc::__export::must_use({
                    ::alloc::fmt::format(format_args!("type has conflicting packed representation hints"))
                })).with_code(E0634)
}struct_span_code_err!(
1573                        tcx.dcx(),
1574                        sp,
1575                        E0634,
1576                        "type has conflicting packed representation hints"
1577                    )
1578                    .emit();
1579                }
1580            }
1581        }
1582        if repr.align.is_some() {
1583            {
    tcx.dcx().struct_span_err(sp,
            ::alloc::__export::must_use({
                    ::alloc::fmt::format(format_args!("type has conflicting packed and align representation hints"))
                })).with_code(E0587)
}struct_span_code_err!(
1584                tcx.dcx(),
1585                sp,
1586                E0587,
1587                "type has conflicting packed and align representation hints"
1588            )
1589            .emit();
1590        } else if let Some(def_spans) = check_packed_inner(tcx, def.did(), &mut ::alloc::vec::Vec::new()vec![]) {
1591            let mut err = {
    tcx.dcx().struct_span_err(sp,
            ::alloc::__export::must_use({
                    ::alloc::fmt::format(format_args!("packed type cannot transitively contain a `#[repr(align)]` type"))
                })).with_code(E0588)
}struct_span_code_err!(
1592                tcx.dcx(),
1593                sp,
1594                E0588,
1595                "packed type cannot transitively contain a `#[repr(align)]` type"
1596            );
1597
1598            err.span_note(
1599                tcx.def_span(def_spans[0].0),
1600                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("`{0}` has a `#[repr(align)]` attribute",
                tcx.item_name(def_spans[0].0)))
    })format!("`{}` has a `#[repr(align)]` attribute", tcx.item_name(def_spans[0].0)),
1601            );
1602
1603            if def_spans.len() > 2 {
1604                let mut first = true;
1605                for (adt_def, span) in def_spans.iter().skip(1).rev() {
1606                    let ident = tcx.item_name(*adt_def);
1607                    err.span_note(
1608                        *span,
1609                        if first {
1610                            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("`{0}` contains a field of type `{1}`",
                tcx.type_of(def.did()).instantiate_identity(), ident))
    })format!(
1611                                "`{}` contains a field of type `{}`",
1612                                tcx.type_of(def.did()).instantiate_identity(),
1613                                ident
1614                            )
1615                        } else {
1616                            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("...which contains a field of type `{0}`",
                ident))
    })format!("...which contains a field of type `{ident}`")
1617                        },
1618                    );
1619                    first = false;
1620                }
1621            }
1622
1623            err.emit();
1624        }
1625    }
1626}
1627
1628pub(super) fn check_packed_inner(
1629    tcx: TyCtxt<'_>,
1630    def_id: DefId,
1631    stack: &mut Vec<DefId>,
1632) -> Option<Vec<(DefId, Span)>> {
1633    if let ty::Adt(def, args) = tcx.type_of(def_id).instantiate_identity().kind() {
1634        if def.is_struct() || def.is_union() {
1635            if def.repr().align.is_some() {
1636                return Some(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(def.did(), DUMMY_SP)]))vec![(def.did(), DUMMY_SP)]);
1637            }
1638
1639            stack.push(def_id);
1640            for field in &def.non_enum_variant().fields {
1641                if let ty::Adt(def, _) = field.ty(tcx, args).kind()
1642                    && !stack.contains(&def.did())
1643                    && let Some(mut defs) = check_packed_inner(tcx, def.did(), stack)
1644                {
1645                    defs.push((def.did(), field.ident(tcx).span));
1646                    return Some(defs);
1647                }
1648            }
1649            stack.pop();
1650        }
1651    }
1652
1653    None
1654}
1655
1656pub(super) fn check_transparent<'tcx>(tcx: TyCtxt<'tcx>, adt: ty::AdtDef<'tcx>) {
1657    struct ZeroSizedFieldReprTransparentIncompatibility<'tcx> {
1658        unsuited: UnsuitedInfo<'tcx>,
1659    }
1660
1661    impl<'a, 'tcx> Diagnostic<'a, ()> for ZeroSizedFieldReprTransparentIncompatibility<'tcx> {
1662        fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, ()> {
1663            let Self { unsuited } = self;
1664            let (title, note) = match unsuited.reason {
1665                UnsuitedReason::NonExhaustive => (
1666                    "external non-exhaustive types",
1667                    "is marked with `#[non_exhaustive]`, so it could become non-zero-sized in the future.",
1668                ),
1669                UnsuitedReason::PrivateField => (
1670                    "external types with private fields",
1671                    "contains private fields, so it could become non-zero-sized in the future.",
1672                ),
1673                UnsuitedReason::ReprC => (
1674                    "`repr(C)` types",
1675                    "is a `#[repr(C)]` type, so it is not guaranteed to be zero-sized on all targets.",
1676                ),
1677            };
1678            Diag::new(
1679                dcx,
1680                level,
1681                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("zero-sized fields in `repr(transparent)` cannot contain {0}",
                title))
    })format!("zero-sized fields in `repr(transparent)` cannot contain {title}"),
1682            )
1683            .with_note(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("this field contains `{0}`, which {1}",
                unsuited.ty, note))
    })format!(
1684                "this field contains `{field_ty}`, which {note}",
1685                field_ty = unsuited.ty,
1686            ))
1687        }
1688    }
1689
1690    if !adt.repr().transparent() {
1691        return;
1692    }
1693
1694    if adt.is_union() && !tcx.features().transparent_unions() {
1695        feature_err(
1696            &tcx.sess,
1697            sym::transparent_unions,
1698            tcx.def_span(adt.did()),
1699            "transparent unions are unstable",
1700        )
1701        .emit();
1702    }
1703
1704    if adt.variants().len() != 1 {
1705        bad_variant_count(tcx, adt, tcx.def_span(adt.did()), adt.did());
1706        // Don't bother checking the fields.
1707        return;
1708    }
1709
1710    let typing_env = ty::TypingEnv::non_body_analysis(tcx, adt.did());
1711    // For each field, figure out if it has "trivial" layout (i.e., is a 1-ZST).
1712    struct FieldInfo<'tcx> {
1713        span: Span,
1714        trivial: bool,
1715        ty: Ty<'tcx>,
1716    }
1717
1718    let field_infos = adt.all_fields().map(|field| {
1719        let ty = field.ty(tcx, GenericArgs::identity_for_item(tcx, field.did));
1720        let layout = tcx.layout_of(typing_env.as_query_input(ty));
1721        // We are currently checking the type this field came from, so it must be local
1722        let span = tcx.hir_span_if_local(field.did).unwrap();
1723        let trivial = layout.is_ok_and(|layout| layout.is_1zst());
1724        FieldInfo { span, trivial, ty }
1725    });
1726
1727    let non_trivial_fields = field_infos
1728        .clone()
1729        .filter_map(|field| if !field.trivial { Some(field.span) } else { None });
1730    let non_trivial_count = non_trivial_fields.clone().count();
1731    if non_trivial_count >= 2 {
1732        bad_non_zero_sized_fields(
1733            tcx,
1734            adt,
1735            non_trivial_count,
1736            non_trivial_fields,
1737            tcx.def_span(adt.did()),
1738        );
1739        return;
1740    }
1741
1742    // Even some 1-ZST fields are not allowed though, if they have `non_exhaustive` or private
1743    // fields or `repr(C)`. We call those fields "unsuited".
1744    struct UnsuitedInfo<'tcx> {
1745        /// The source of the problem, a type that is found somewhere within the field type.
1746        ty: Ty<'tcx>,
1747        reason: UnsuitedReason,
1748    }
1749    enum UnsuitedReason {
1750        NonExhaustive,
1751        PrivateField,
1752        ReprC,
1753    }
1754
1755    fn check_unsuited<'tcx>(
1756        tcx: TyCtxt<'tcx>,
1757        typing_env: ty::TypingEnv<'tcx>,
1758        ty: Ty<'tcx>,
1759    ) -> ControlFlow<UnsuitedInfo<'tcx>> {
1760        // We can encounter projections during traversal, so ensure the type is normalized.
1761        let ty = tcx.try_normalize_erasing_regions(typing_env, ty).unwrap_or(ty);
1762        match ty.kind() {
1763            ty::Tuple(list) => list.iter().try_for_each(|t| check_unsuited(tcx, typing_env, t)),
1764            ty::Array(ty, _) => check_unsuited(tcx, typing_env, *ty),
1765            ty::Adt(def, args) => {
1766                if !def.did().is_local() && !{

        #[allow(deprecated)]
        {
            {
                'done:
                    {
                    for i in tcx.get_all_attrs(def.did()) {
                        #[allow(unused_imports)]
                        use rustc_hir::attrs::AttributeKind::*;
                        let i: &rustc_hir::Attribute = i;
                        match i {
                            rustc_hir::Attribute::Parsed(RustcPubTransparent(_)) => {
                                break 'done Some(());
                            }
                            rustc_hir::Attribute::Unparsed(..) =>
                                {}
                                #[deny(unreachable_patterns)]
                                _ => {}
                        }
                    }
                    None
                }
            }
        }
    }.is_some()find_attr!(tcx, def.did(), RustcPubTransparent(_)) {
1767                    let non_exhaustive = def.is_variant_list_non_exhaustive()
1768                        || def.variants().iter().any(ty::VariantDef::is_field_list_non_exhaustive);
1769                    let has_priv = def.all_fields().any(|f| !f.vis.is_public());
1770                    if non_exhaustive || has_priv {
1771                        return ControlFlow::Break(UnsuitedInfo {
1772                            ty,
1773                            reason: if non_exhaustive {
1774                                UnsuitedReason::NonExhaustive
1775                            } else {
1776                                UnsuitedReason::PrivateField
1777                            },
1778                        });
1779                    }
1780                }
1781                if def.repr().c() {
1782                    return ControlFlow::Break(UnsuitedInfo { ty, reason: UnsuitedReason::ReprC });
1783                }
1784                def.all_fields()
1785                    .map(|field| field.ty(tcx, args))
1786                    .try_for_each(|t| check_unsuited(tcx, typing_env, t))
1787            }
1788            _ => ControlFlow::Continue(()),
1789        }
1790    }
1791
1792    let mut prev_unsuited_1zst = false;
1793    for field in field_infos {
1794        if field.trivial
1795            && let Some(unsuited) = check_unsuited(tcx, typing_env, field.ty).break_value()
1796        {
1797            // If there are any non-trivial fields, then there can be no non-exhaustive 1-zsts.
1798            // Otherwise, it's only an issue if there's >1 non-exhaustive 1-zst.
1799            if non_trivial_count > 0 || prev_unsuited_1zst {
1800                tcx.emit_node_span_lint(
1801                    REPR_TRANSPARENT_NON_ZST_FIELDS,
1802                    tcx.local_def_id_to_hir_id(adt.did().expect_local()),
1803                    field.span,
1804                    ZeroSizedFieldReprTransparentIncompatibility { unsuited },
1805                );
1806            } else {
1807                prev_unsuited_1zst = true;
1808            }
1809        }
1810    }
1811}
1812
1813#[allow(trivial_numeric_casts)]
1814fn check_enum(tcx: TyCtxt<'_>, def_id: LocalDefId) {
1815    let def = tcx.adt_def(def_id);
1816    def.destructor(tcx); // force the destructor to be evaluated
1817
1818    if def.variants().is_empty() {
1819        {

    #[allow(deprecated)]
    {
        {
            'done:
                {
                for i in tcx.get_all_attrs(def_id) {
                    #[allow(unused_imports)]
                    use rustc_hir::attrs::AttributeKind::*;
                    let i: &rustc_hir::Attribute = i;
                    match i {
                        rustc_hir::Attribute::Parsed(Repr { reprs, first_span }) =>
                            {
                            break 'done
                                Some({
                                        {
                                                    tcx.dcx().struct_span_err(reprs.first().map(|repr|
                                                                        repr.1).unwrap_or(*first_span),
                                                            ::alloc::__export::must_use({
                                                                    ::alloc::fmt::format(format_args!("unsupported representation for zero-variant enum"))
                                                                })).with_code(E0084)
                                                }.with_span_label(tcx.def_span(def_id),
                                                "zero-variant enum").emit();
                                    });
                        }
                        rustc_hir::Attribute::Unparsed(..) =>
                            {}
                            #[deny(unreachable_patterns)]
                            _ => {}
                    }
                }
                None
            }
        }
    }
};find_attr!(tcx, def_id, Repr { reprs, first_span } => {
1820            struct_span_code_err!(
1821                tcx.dcx(),
1822                reprs.first().map(|repr| repr.1).unwrap_or(*first_span),
1823                E0084,
1824                "unsupported representation for zero-variant enum"
1825            )
1826            .with_span_label(tcx.def_span(def_id), "zero-variant enum")
1827            .emit();
1828        });
1829    }
1830
1831    for v in def.variants() {
1832        if let ty::VariantDiscr::Explicit(discr_def_id) = v.discr {
1833            tcx.ensure_ok().typeck(discr_def_id.expect_local());
1834        }
1835    }
1836
1837    if def.repr().int.is_none() {
1838        let is_unit = |var: &ty::VariantDef| #[allow(non_exhaustive_omitted_patterns)] match var.ctor_kind() {
    Some(CtorKind::Const) => true,
    _ => false,
}matches!(var.ctor_kind(), Some(CtorKind::Const));
1839        let get_disr = |var: &ty::VariantDef| match var.discr {
1840            ty::VariantDiscr::Explicit(disr) => Some(disr),
1841            ty::VariantDiscr::Relative(_) => None,
1842        };
1843
1844        let non_unit = def.variants().iter().find(|var| !is_unit(var));
1845        let disr_unit =
1846            def.variants().iter().filter(|var| is_unit(var)).find_map(|var| get_disr(var));
1847        let disr_non_unit =
1848            def.variants().iter().filter(|var| !is_unit(var)).find_map(|var| get_disr(var));
1849
1850        if disr_non_unit.is_some() || (disr_unit.is_some() && non_unit.is_some()) {
1851            let mut err = {
    tcx.dcx().struct_span_err(tcx.def_span(def_id),
            ::alloc::__export::must_use({
                    ::alloc::fmt::format(format_args!("`#[repr(inttype)]` must be specified for enums with explicit discriminants and non-unit variants"))
                })).with_code(E0732)
}struct_span_code_err!(
1852                tcx.dcx(),
1853                tcx.def_span(def_id),
1854                E0732,
1855                "`#[repr(inttype)]` must be specified for enums with explicit discriminants and non-unit variants"
1856            );
1857            if let Some(disr_non_unit) = disr_non_unit {
1858                err.span_label(
1859                    tcx.def_span(disr_non_unit),
1860                    "explicit discriminant on non-unit variant specified here",
1861                );
1862            } else {
1863                err.span_label(
1864                    tcx.def_span(disr_unit.unwrap()),
1865                    "explicit discriminant specified here",
1866                );
1867                err.span_label(
1868                    tcx.def_span(non_unit.unwrap().def_id),
1869                    "non-unit discriminant declared here",
1870                );
1871            }
1872            err.emit();
1873        }
1874    }
1875
1876    detect_discriminant_duplicate(tcx, def);
1877    check_transparent(tcx, def);
1878}
1879
1880/// Part of enum check. Given the discriminants of an enum, errors if two or more discriminants are equal
1881fn detect_discriminant_duplicate<'tcx>(tcx: TyCtxt<'tcx>, adt: ty::AdtDef<'tcx>) {
1882    // Helper closure to reduce duplicate code. This gets called everytime we detect a duplicate.
1883    // Here `idx` refers to the order of which the discriminant appears, and its index in `vs`
1884    let report = |dis: Discr<'tcx>, idx, err: &mut Diag<'_>| {
1885        let var = adt.variant(idx); // HIR for the duplicate discriminant
1886        let (span, display_discr) = match var.discr {
1887            ty::VariantDiscr::Explicit(discr_def_id) => {
1888                // In the case the discriminant is both a duplicate and overflowed, let the user know
1889                if let hir::Node::AnonConst(expr) =
1890                    tcx.hir_node_by_def_id(discr_def_id.expect_local())
1891                    && let hir::ExprKind::Lit(lit) = &tcx.hir_body(expr.body).value.kind
1892                    && let rustc_ast::LitKind::Int(lit_value, _int_kind) = &lit.node
1893                    && *lit_value != dis.val
1894                {
1895                    (tcx.def_span(discr_def_id), ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("`{0}` (overflowed from `{1}`)",
                dis, lit_value))
    })format!("`{dis}` (overflowed from `{lit_value}`)"))
1896                } else {
1897                    // Otherwise, format the value as-is
1898                    (tcx.def_span(discr_def_id), ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("`{0}`", dis))
    })format!("`{dis}`"))
1899                }
1900            }
1901            // This should not happen.
1902            ty::VariantDiscr::Relative(0) => (tcx.def_span(var.def_id), ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("`{0}`", dis))
    })format!("`{dis}`")),
1903            ty::VariantDiscr::Relative(distance_to_explicit) => {
1904                // At this point we know this discriminant is a duplicate, and was not explicitly
1905                // assigned by the user. Here we iterate backwards to fetch the HIR for the last
1906                // explicitly assigned discriminant, and letting the user know that this was the
1907                // increment startpoint, and how many steps from there leading to the duplicate
1908                if let Some(explicit_idx) =
1909                    idx.as_u32().checked_sub(distance_to_explicit).map(VariantIdx::from_u32)
1910                {
1911                    let explicit_variant = adt.variant(explicit_idx);
1912                    let ve_ident = var.name;
1913                    let ex_ident = explicit_variant.name;
1914                    let sp = if distance_to_explicit > 1 { "variants" } else { "variant" };
1915
1916                    err.span_label(
1917                        tcx.def_span(explicit_variant.def_id),
1918                        ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("discriminant for `{0}` incremented from this startpoint (`{1}` + {2} {3} later => `{0}` = {4})",
                ve_ident, ex_ident, distance_to_explicit, sp, dis))
    })format!(
1919                            "discriminant for `{ve_ident}` incremented from this startpoint \
1920                            (`{ex_ident}` + {distance_to_explicit} {sp} later \
1921                             => `{ve_ident}` = {dis})"
1922                        ),
1923                    );
1924                }
1925
1926                (tcx.def_span(var.def_id), ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("`{0}`", dis))
    })format!("`{dis}`"))
1927            }
1928        };
1929
1930        err.span_label(span, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0} assigned here", display_discr))
    })format!("{display_discr} assigned here"));
1931    };
1932
1933    let mut discrs = adt.discriminants(tcx).collect::<Vec<_>>();
1934
1935    // Here we loop through the discriminants, comparing each discriminant to another.
1936    // When a duplicate is detected, we instantiate an error and point to both
1937    // initial and duplicate value. The duplicate discriminant is then discarded by swapping
1938    // it with the last element and decrementing the `vec.len` (which is why we have to evaluate
1939    // `discrs.len()` anew every iteration, and why this could be tricky to do in a functional
1940    // style as we are mutating `discrs` on the fly).
1941    let mut i = 0;
1942    while i < discrs.len() {
1943        let var_i_idx = discrs[i].0;
1944        let mut error: Option<Diag<'_, _>> = None;
1945
1946        let mut o = i + 1;
1947        while o < discrs.len() {
1948            let var_o_idx = discrs[o].0;
1949
1950            if discrs[i].1.val == discrs[o].1.val {
1951                let err = error.get_or_insert_with(|| {
1952                    let mut ret = {
    tcx.dcx().struct_span_err(tcx.def_span(adt.did()),
            ::alloc::__export::must_use({
                    ::alloc::fmt::format(format_args!("discriminant value `{0}` assigned more than once",
                            discrs[i].1))
                })).with_code(E0081)
}struct_span_code_err!(
1953                        tcx.dcx(),
1954                        tcx.def_span(adt.did()),
1955                        E0081,
1956                        "discriminant value `{}` assigned more than once",
1957                        discrs[i].1,
1958                    );
1959
1960                    report(discrs[i].1, var_i_idx, &mut ret);
1961
1962                    ret
1963                });
1964
1965                report(discrs[o].1, var_o_idx, err);
1966
1967                // Safe to unwrap here, as we wouldn't reach this point if `discrs` was empty
1968                discrs[o] = *discrs.last().unwrap();
1969                discrs.pop();
1970            } else {
1971                o += 1;
1972            }
1973        }
1974
1975        if let Some(e) = error {
1976            e.emit();
1977        }
1978
1979        i += 1;
1980    }
1981}
1982
1983fn check_type_alias_type_params_are_used<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) {
1984    if tcx.type_alias_is_lazy(def_id) {
1985        // Since we compute the variances for lazy type aliases and already reject bivariant
1986        // parameters as unused, we can and should skip this check for lazy type aliases.
1987        return;
1988    }
1989
1990    let generics = tcx.generics_of(def_id);
1991    if generics.own_counts().types == 0 {
1992        return;
1993    }
1994
1995    let ty = tcx.type_of(def_id).instantiate_identity();
1996    if ty.references_error() {
1997        // If there is already another error, do not emit an error for not using a type parameter.
1998        return;
1999    }
2000
2001    // Lazily calculated because it is only needed in case of an error.
2002    let bounded_params = LazyCell::new(|| {
2003        tcx.explicit_predicates_of(def_id)
2004            .predicates
2005            .iter()
2006            .filter_map(|(predicate, span)| {
2007                let bounded_ty = match predicate.kind().skip_binder() {
2008                    ty::ClauseKind::Trait(pred) => pred.trait_ref.self_ty(),
2009                    ty::ClauseKind::TypeOutlives(pred) => pred.0,
2010                    _ => return None,
2011                };
2012                if let ty::Param(param) = bounded_ty.kind() {
2013                    Some((param.index, span))
2014                } else {
2015                    None
2016                }
2017            })
2018            // FIXME: This assumes that elaborated `Sized` bounds come first (which does hold at the
2019            // time of writing). This is a bit fragile since we later use the span to detect elaborated
2020            // `Sized` bounds. If they came last for example, this would break `Trait + /*elab*/Sized`
2021            // since it would overwrite the span of the user-written bound. This could be fixed by
2022            // folding the spans with `Span::to` which requires a bit of effort I think.
2023            .collect::<FxIndexMap<_, _>>()
2024    });
2025
2026    let mut params_used = DenseBitSet::new_empty(generics.own_params.len());
2027    for leaf in ty.walk() {
2028        if let GenericArgKind::Type(leaf_ty) = leaf.kind()
2029            && let ty::Param(param) = leaf_ty.kind()
2030        {
2031            {
    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/check/check.rs:2031",
                        "rustc_hir_analysis::check::check", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/check/check.rs"),
                        ::tracing_core::__macro_support::Option::Some(2031u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::check::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!("found use of ty param {0:?}",
                                                    param) as &dyn Value))])
            });
    } else { ; }
};debug!("found use of ty param {:?}", param);
2032            params_used.insert(param.index);
2033        }
2034    }
2035
2036    for param in &generics.own_params {
2037        if !params_used.contains(param.index)
2038            && let ty::GenericParamDefKind::Type { .. } = param.kind
2039        {
2040            let span = tcx.def_span(param.def_id);
2041            let param_name = Ident::new(param.name, span);
2042
2043            // The corresponding predicates are post-`Sized`-elaboration. Therefore we
2044            // * check for emptiness to detect lone user-written `?Sized` bounds
2045            // * compare the param span to the pred span to detect lone user-written `Sized` bounds
2046            let has_explicit_bounds = bounded_params.is_empty()
2047                || (*bounded_params).get(&param.index).is_some_and(|&&pred_sp| pred_sp != span);
2048            let const_param_help = !has_explicit_bounds;
2049
2050            let mut diag = tcx.dcx().create_err(errors::UnusedGenericParameter {
2051                span,
2052                param_name,
2053                param_def_kind: tcx.def_descr(param.def_id),
2054                help: errors::UnusedGenericParameterHelp::TyAlias { param_name },
2055                usage_spans: ::alloc::vec::Vec::new()vec![],
2056                const_param_help,
2057            });
2058            diag.code(E0091);
2059            diag.emit();
2060        }
2061    }
2062}
2063
2064/// Emit an error for recursive opaque types.
2065///
2066/// If this is a return `impl Trait`, find the item's return expressions and point at them. For
2067/// direct recursion this is enough, but for indirect recursion also point at the last intermediary
2068/// `impl Trait`.
2069///
2070/// If all the return expressions evaluate to `!`, then we explain that the error will go away
2071/// after changing it. This can happen when a user uses `panic!()` or similar as a placeholder.
2072fn opaque_type_cycle_error(tcx: TyCtxt<'_>, opaque_def_id: LocalDefId) -> ErrorGuaranteed {
2073    let span = tcx.def_span(opaque_def_id);
2074    let mut err = {
    tcx.dcx().struct_span_err(span,
            ::alloc::__export::must_use({
                    ::alloc::fmt::format(format_args!("cannot resolve opaque type"))
                })).with_code(E0720)
}struct_span_code_err!(tcx.dcx(), span, E0720, "cannot resolve opaque type");
2075
2076    let mut label = false;
2077    if let Some((def_id, visitor)) = get_owner_return_paths(tcx, opaque_def_id) {
2078        let typeck_results = tcx.typeck(def_id);
2079        if visitor
2080            .returns
2081            .iter()
2082            .filter_map(|expr| typeck_results.node_type_opt(expr.hir_id))
2083            .all(|ty| #[allow(non_exhaustive_omitted_patterns)] match ty.kind() {
    ty::Never => true,
    _ => false,
}matches!(ty.kind(), ty::Never))
2084        {
2085            let spans = visitor
2086                .returns
2087                .iter()
2088                .filter(|expr| typeck_results.node_type_opt(expr.hir_id).is_some())
2089                .map(|expr| expr.span)
2090                .collect::<Vec<Span>>();
2091            let span_len = spans.len();
2092            if span_len == 1 {
2093                err.span_label(spans[0], "this returned value is of `!` type");
2094            } else {
2095                let mut multispan: MultiSpan = spans.clone().into();
2096                for span in spans {
2097                    multispan.push_span_label(span, "this returned value is of `!` type");
2098                }
2099                err.span_note(multispan, "these returned values have a concrete \"never\" type");
2100            }
2101            err.help("this error will resolve once the item's body returns a concrete type");
2102        } else {
2103            let mut seen = FxHashSet::default();
2104            seen.insert(span);
2105            err.span_label(span, "recursive opaque type");
2106            label = true;
2107            for (sp, ty) in visitor
2108                .returns
2109                .iter()
2110                .filter_map(|e| typeck_results.node_type_opt(e.hir_id).map(|t| (e.span, t)))
2111                .filter(|(_, ty)| !#[allow(non_exhaustive_omitted_patterns)] match ty.kind() {
    ty::Never => true,
    _ => false,
}matches!(ty.kind(), ty::Never))
2112            {
2113                #[derive(#[automatically_derived]
impl ::core::default::Default for OpaqueTypeCollector {
    #[inline]
    fn default() -> OpaqueTypeCollector {
        OpaqueTypeCollector {
            opaques: ::core::default::Default::default(),
            closures: ::core::default::Default::default(),
        }
    }
}Default)]
2114                struct OpaqueTypeCollector {
2115                    opaques: Vec<DefId>,
2116                    closures: Vec<DefId>,
2117                }
2118                impl<'tcx> ty::TypeVisitor<TyCtxt<'tcx>> for OpaqueTypeCollector {
2119                    fn visit_ty(&mut self, t: Ty<'tcx>) {
2120                        match *t.kind() {
2121                            ty::Alias(ty::Opaque, ty::AliasTy { def_id: def, .. }) => {
2122                                self.opaques.push(def);
2123                            }
2124                            ty::Closure(def_id, ..) | ty::Coroutine(def_id, ..) => {
2125                                self.closures.push(def_id);
2126                                t.super_visit_with(self);
2127                            }
2128                            _ => t.super_visit_with(self),
2129                        }
2130                    }
2131                }
2132
2133                let mut visitor = OpaqueTypeCollector::default();
2134                ty.visit_with(&mut visitor);
2135                for def_id in visitor.opaques {
2136                    let ty_span = tcx.def_span(def_id);
2137                    if !seen.contains(&ty_span) {
2138                        let descr = if ty.is_impl_trait() { "opaque " } else { "" };
2139                        err.span_label(ty_span, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("returning this {0}type `{1}`",
                descr, ty))
    })format!("returning this {descr}type `{ty}`"));
2140                        seen.insert(ty_span);
2141                    }
2142                    err.span_label(sp, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("returning here with type `{0}`",
                ty))
    })format!("returning here with type `{ty}`"));
2143                }
2144
2145                for closure_def_id in visitor.closures {
2146                    let Some(closure_local_did) = closure_def_id.as_local() else {
2147                        continue;
2148                    };
2149                    let typeck_results = tcx.typeck(closure_local_did);
2150
2151                    let mut label_match = |ty: Ty<'_>, span| {
2152                        for arg in ty.walk() {
2153                            if let ty::GenericArgKind::Type(ty) = arg.kind()
2154                                && let ty::Alias(
2155                                    ty::Opaque,
2156                                    ty::AliasTy { def_id: captured_def_id, .. },
2157                                ) = *ty.kind()
2158                                && captured_def_id == opaque_def_id.to_def_id()
2159                            {
2160                                err.span_label(
2161                                    span,
2162                                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0} captures itself here",
                tcx.def_descr(closure_def_id)))
    })format!(
2163                                        "{} captures itself here",
2164                                        tcx.def_descr(closure_def_id)
2165                                    ),
2166                                );
2167                            }
2168                        }
2169                    };
2170
2171                    // Label any closure upvars that capture the opaque
2172                    for capture in typeck_results.closure_min_captures_flattened(closure_local_did)
2173                    {
2174                        label_match(capture.place.ty(), capture.get_path_span(tcx));
2175                    }
2176                    // Label any coroutine locals that capture the opaque
2177                    if tcx.is_coroutine(closure_def_id)
2178                        && let Some(coroutine_layout) = tcx.mir_coroutine_witnesses(closure_def_id)
2179                    {
2180                        for interior_ty in &coroutine_layout.field_tys {
2181                            label_match(interior_ty.ty, interior_ty.source_info.span);
2182                        }
2183                    }
2184                }
2185            }
2186        }
2187    }
2188    if !label {
2189        err.span_label(span, "cannot resolve opaque type");
2190    }
2191    err.emit()
2192}
2193
2194pub(super) fn check_coroutine_obligations(
2195    tcx: TyCtxt<'_>,
2196    def_id: LocalDefId,
2197) -> Result<(), ErrorGuaranteed> {
2198    if true {
    if !!tcx.is_typeck_child(def_id.to_def_id()) {
        ::core::panicking::panic("assertion failed: !tcx.is_typeck_child(def_id.to_def_id())")
    };
};debug_assert!(!tcx.is_typeck_child(def_id.to_def_id()));
2199
2200    let typeck_results = tcx.typeck(def_id);
2201    let param_env = tcx.param_env(def_id);
2202
2203    {
    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/check/check.rs:2203",
                        "rustc_hir_analysis::check::check", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/check/check.rs"),
                        ::tracing_core::__macro_support::Option::Some(2203u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::check::check"),
                        ::tracing_core::field::FieldSet::new(&["typeck_results.coroutine_stalled_predicates"],
                            ::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(&debug(&typeck_results.coroutine_stalled_predicates)
                                            as &dyn Value))])
            });
    } else { ; }
};debug!(?typeck_results.coroutine_stalled_predicates);
2204
2205    let mode = if tcx.next_trait_solver_globally() {
2206        // This query is conceptually between HIR typeck and
2207        // MIR borrowck. We use the opaque types defined by HIR
2208        // and ignore region constraints.
2209        TypingMode::borrowck(tcx, def_id)
2210    } else {
2211        TypingMode::analysis_in_body(tcx, def_id)
2212    };
2213
2214    // Typeck writeback gives us predicates with their regions erased.
2215    // We only need to check the goals while ignoring lifetimes to give good
2216    // error message and to avoid breaking the assumption of `mir_borrowck`
2217    // that all obligations already hold modulo regions.
2218    let infcx = tcx.infer_ctxt().ignoring_regions().build(mode);
2219
2220    let ocx = ObligationCtxt::new_with_diagnostics(&infcx);
2221    for (predicate, cause) in &typeck_results.coroutine_stalled_predicates {
2222        ocx.register_obligation(Obligation::new(tcx, cause.clone(), param_env, *predicate));
2223    }
2224
2225    let errors = ocx.evaluate_obligations_error_on_ambiguity();
2226    {
    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/check/check.rs:2226",
                        "rustc_hir_analysis::check::check", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/check/check.rs"),
                        ::tracing_core::__macro_support::Option::Some(2226u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::check::check"),
                        ::tracing_core::field::FieldSet::new(&["errors"],
                            ::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(&debug(&errors) as
                                            &dyn Value))])
            });
    } else { ; }
};debug!(?errors);
2227    if !errors.is_empty() {
2228        return Err(infcx.err_ctxt().report_fulfillment_errors(errors));
2229    }
2230
2231    if !tcx.next_trait_solver_globally() {
2232        // Check that any hidden types found when checking these stalled coroutine obligations
2233        // are valid.
2234        for (key, ty) in infcx.take_opaque_types() {
2235            let hidden_type = infcx.resolve_vars_if_possible(ty);
2236            let key = infcx.resolve_vars_if_possible(key);
2237            sanity_check_found_hidden_type(tcx, key, hidden_type)?;
2238        }
2239    } else {
2240        // We're not checking region constraints here, so we can simply drop the
2241        // added opaque type uses in `TypingMode::Borrowck`.
2242        let _ = infcx.take_opaque_types();
2243    }
2244
2245    Ok(())
2246}
2247
2248pub(super) fn check_potentially_region_dependent_goals<'tcx>(
2249    tcx: TyCtxt<'tcx>,
2250    def_id: LocalDefId,
2251) -> Result<(), ErrorGuaranteed> {
2252    if !tcx.next_trait_solver_globally() {
2253        return Ok(());
2254    }
2255    let typeck_results = tcx.typeck(def_id);
2256    let param_env = tcx.param_env(def_id);
2257
2258    // We use `TypingMode::Borrowck` as we want to use the opaque types computed by HIR typeck.
2259    let typing_mode = TypingMode::borrowck(tcx, def_id);
2260    let infcx = tcx.infer_ctxt().ignoring_regions().build(typing_mode);
2261    let ocx = ObligationCtxt::new_with_diagnostics(&infcx);
2262    for (predicate, cause) in &typeck_results.potentially_region_dependent_goals {
2263        let predicate = fold_regions(tcx, *predicate, |_, _| {
2264            infcx.next_region_var(RegionVariableOrigin::Misc(cause.span))
2265        });
2266        ocx.register_obligation(Obligation::new(tcx, cause.clone(), param_env, predicate));
2267    }
2268
2269    let errors = ocx.evaluate_obligations_error_on_ambiguity();
2270    {
    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/check/check.rs:2270",
                        "rustc_hir_analysis::check::check", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/check/check.rs"),
                        ::tracing_core::__macro_support::Option::Some(2270u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::check::check"),
                        ::tracing_core::field::FieldSet::new(&["errors"],
                            ::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(&debug(&errors) as
                                            &dyn Value))])
            });
    } else { ; }
};debug!(?errors);
2271    if errors.is_empty() { Ok(()) } else { Err(infcx.err_ctxt().report_fulfillment_errors(errors)) }
2272}