Skip to main content

rustc_ty_utils/
abi.rs

1use std::{assert_matches, iter};
2
3use rustc_abi::Primitive::Pointer;
4use rustc_abi::{Align, BackendRepr, ExternAbi, PointerKind, Scalar, Size};
5use rustc_hir::lang_items::LangItem;
6use rustc_hir::{self as hir, find_attr};
7use rustc_middle::bug;
8use rustc_middle::middle::deduced_param_attrs::DeducedParamAttrs;
9use rustc_middle::query::Providers;
10use rustc_middle::ty::layout::{
11    FnAbiError, HasTyCtxt, HasTypingEnv, LayoutCx, LayoutOf, TyAndLayout, fn_can_unwind,
12};
13use rustc_middle::ty::{self, InstanceKind, ShimKind, Ty, TyCtxt, Unnormalized};
14use rustc_span::DUMMY_SP;
15use rustc_span::def_id::DefId;
16use rustc_target::callconv::{
17    AbiMap, ArgAbi, ArgAttribute, ArgAttributes, ArgExtension, FnAbi, PassMode,
18};
19use tracing::debug;
20
21pub(crate) fn provide(providers: &mut Providers) {
22    *providers = Providers {
23        fn_abi_of_fn_ptr,
24        fn_abi_of_instance_no_deduced_attrs,
25        fn_abi_of_instance_raw,
26        ..*providers
27    };
28}
29
30// NOTE(eddyb) this is private to avoid using it from outside of
31// `fn_abi_of_instance` - any other uses are either too high-level
32// for `Instance` (e.g. typeck would use `Ty::fn_sig` instead),
33// or should go through `FnAbi` instead, to avoid losing any
34// adjustments `fn_abi_of_instance` might be performing.
35#[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("fn_sig_for_fn_abi",
                                    "rustc_ty_utils::abi", ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_ty_utils/src/abi.rs"),
                                    ::tracing_core::__macro_support::Option::Some(35u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_ty_utils::abi"),
                                    ::tracing_core::field::FieldSet::new(&["instance"],
                                        ::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(&instance)
                                                            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: ty::FnSig<'tcx> = loop {};
            return __tracing_attr_fake_return;
        }
        {
            if let InstanceKind::Shim(ShimKind::ThreadLocal(..)) =
                    instance.def {
                return tcx.mk_fn_sig_safe_rust_abi([],
                        tcx.thread_local_ptr_ty(instance.def_id()));
            }
            let ty = instance.ty(tcx, typing_env);
            match *ty.kind() {
                ty::FnDef(def_id, args) => {
                    let mut sig =
                        tcx.instantiate_bound_regions_with_erased(tcx.fn_sig(def_id).instantiate(tcx,
                                    args).skip_norm_wip());
                    if let ty::InstanceKind::Shim(ty::ShimKind::VTable(..)) =
                            instance.def {
                        let mut inputs_and_output = sig.inputs_and_output.to_vec();
                        inputs_and_output[0] =
                            Ty::new_mut_ptr(tcx, inputs_and_output[0]);
                        sig.inputs_and_output =
                            tcx.mk_type_list(&inputs_and_output);
                    }
                    sig
                }
                ty::Closure(def_id, args) => {
                    let sig =
                        tcx.instantiate_bound_regions_with_erased(args.as_closure().sig());
                    let env_ty =
                        tcx.closure_env_ty(Ty::new_closure(tcx, def_id, args),
                            args.as_closure().kind(), tcx.lifetimes.re_erased);
                    tcx.mk_fn_sig(iter::once(env_ty).chain(sig.inputs().iter().cloned()),
                        sig.output(), sig.fn_sig_kind)
                }
                ty::CoroutineClosure(def_id, args) => {
                    let coroutine_ty =
                        Ty::new_coroutine_closure(tcx, def_id, args);
                    let sig =
                        args.as_coroutine_closure().coroutine_closure_sig();
                    let mut coroutine_kind = args.as_coroutine_closure().kind();
                    let env_ty =
                        if let InstanceKind::Shim(ShimKind::ConstructCoroutineInClosure {
                                receiver_by_ref, .. }) = instance.def {
                            coroutine_kind = ty::ClosureKind::FnOnce;
                            if receiver_by_ref {
                                Ty::new_imm_ref(tcx, tcx.lifetimes.re_erased, coroutine_ty)
                            } else { coroutine_ty }
                        } else {
                            tcx.closure_env_ty(coroutine_ty, coroutine_kind,
                                tcx.lifetimes.re_erased)
                        };
                    let sig = tcx.instantiate_bound_regions_with_erased(sig);
                    tcx.mk_fn_sig(iter::once(env_ty).chain([sig.tupled_inputs_ty]),
                        sig.to_coroutine_given_kind_and_upvars(tcx,
                            args.as_coroutine_closure().parent_args(),
                            tcx.coroutine_for_closure(def_id), coroutine_kind,
                            tcx.lifetimes.re_erased,
                            args.as_coroutine_closure().tupled_upvars_ty(),
                            args.as_coroutine_closure().coroutine_captures_by_ref_ty()),
                        sig.fn_sig_kind)
                }
                ty::Coroutine(did, args) => {
                    let coroutine_kind = tcx.coroutine_kind(did).unwrap();
                    let sig = args.as_coroutine().sig();
                    let env_ty =
                        Ty::new_mut_ref(tcx, tcx.lifetimes.re_erased, ty);
                    let pin_did =
                        tcx.require_lang_item(LangItem::Pin, DUMMY_SP);
                    let pin_adt_ref = tcx.adt_def(pin_did);
                    let pin_args = tcx.mk_args(&[env_ty.into()]);
                    let env_ty =
                        match coroutine_kind {
                            hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Gen,
                                _) => {
                                env_ty
                            }
                            hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Async,
                                _) |
                                hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::AsyncGen,
                                _) | hir::CoroutineKind::Coroutine(_) =>
                                Ty::new_adt(tcx, pin_adt_ref, pin_args),
                        };
                    let (resume_ty, ret_ty) =
                        match coroutine_kind {
                            hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Async,
                                _) => {
                                {
                                    match (&sig.yield_ty, &tcx.types.unit) {
                                        (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::None);
                                            }
                                        }
                                    }
                                };
                                let poll_did =
                                    tcx.require_lang_item(LangItem::Poll, DUMMY_SP);
                                let poll_adt_ref = tcx.adt_def(poll_did);
                                let poll_args = tcx.mk_args(&[sig.return_ty.into()]);
                                let ret_ty = Ty::new_adt(tcx, poll_adt_ref, poll_args);
                                {
                                    if let ty::Adt(resume_ty_adt, _) = sig.resume_ty.kind() {
                                        let expected_adt =
                                            tcx.adt_def(tcx.require_lang_item(LangItem::ResumeTy,
                                                    DUMMY_SP));
                                        {
                                            match (&*resume_ty_adt, &expected_adt) {
                                                (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::None);
                                                    }
                                                }
                                            }
                                        };
                                    } else {
                                        {
                                            ::core::panicking::panic_fmt(format_args!("expected `ResumeTy`, found `{0:?}`",
                                                    sig.resume_ty));
                                        };
                                    };
                                }
                                let context_mut_ref = Ty::new_task_context(tcx);
                                (Some(context_mut_ref), ret_ty)
                            }
                            hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Gen,
                                _) => {
                                let option_did =
                                    tcx.require_lang_item(LangItem::Option, DUMMY_SP);
                                let option_adt_ref = tcx.adt_def(option_did);
                                let option_args = tcx.mk_args(&[sig.yield_ty.into()]);
                                let ret_ty = Ty::new_adt(tcx, option_adt_ref, option_args);
                                {
                                    match (&sig.return_ty, &tcx.types.unit) {
                                        (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::None);
                                            }
                                        }
                                    }
                                };
                                {
                                    match (&sig.resume_ty, &tcx.types.unit) {
                                        (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::None);
                                            }
                                        }
                                    }
                                };
                                (None, ret_ty)
                            }
                            hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::AsyncGen,
                                _) => {
                                {
                                    match (&sig.return_ty, &tcx.types.unit) {
                                        (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::None);
                                            }
                                        }
                                    }
                                };
                                let ret_ty = sig.yield_ty;
                                {
                                    if let ty::Adt(resume_ty_adt, _) = sig.resume_ty.kind() {
                                        let expected_adt =
                                            tcx.adt_def(tcx.require_lang_item(LangItem::ResumeTy,
                                                    DUMMY_SP));
                                        {
                                            match (&*resume_ty_adt, &expected_adt) {
                                                (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::None);
                                                    }
                                                }
                                            }
                                        };
                                    } else {
                                        {
                                            ::core::panicking::panic_fmt(format_args!("expected `ResumeTy`, found `{0:?}`",
                                                    sig.resume_ty));
                                        };
                                    };
                                }
                                let context_mut_ref = Ty::new_task_context(tcx);
                                (Some(context_mut_ref), ret_ty)
                            }
                            hir::CoroutineKind::Coroutine(_) => {
                                let state_did =
                                    tcx.require_lang_item(LangItem::CoroutineState, DUMMY_SP);
                                let state_adt_ref = tcx.adt_def(state_did);
                                let state_args =
                                    tcx.mk_args(&[sig.yield_ty.into(), sig.return_ty.into()]);
                                let ret_ty = Ty::new_adt(tcx, state_adt_ref, state_args);
                                (Some(sig.resume_ty), ret_ty)
                            }
                        };
                    if let Some(resume_ty) = resume_ty {
                        tcx.mk_fn_sig_safe_rust_abi([env_ty, resume_ty], ret_ty)
                    } else { tcx.mk_fn_sig_safe_rust_abi([env_ty], ret_ty) }
                }
                _ =>
                    ::rustc_middle::util::bug::bug_fmt(format_args!("unexpected type {0:?} in Instance::fn_sig",
                            ty)),
            }
        }
    }
}#[tracing::instrument(level = "debug", skip(tcx, typing_env))]
36fn fn_sig_for_fn_abi<'tcx>(
37    tcx: TyCtxt<'tcx>,
38    instance: ty::Instance<'tcx>,
39    typing_env: ty::TypingEnv<'tcx>,
40) -> ty::FnSig<'tcx> {
41    if let InstanceKind::Shim(ShimKind::ThreadLocal(..)) = instance.def {
42        return tcx.mk_fn_sig_safe_rust_abi([], tcx.thread_local_ptr_ty(instance.def_id()));
43    }
44
45    let ty = instance.ty(tcx, typing_env);
46    match *ty.kind() {
47        ty::FnDef(def_id, args) => {
48            let mut sig = tcx.instantiate_bound_regions_with_erased(
49                tcx.fn_sig(def_id).instantiate(tcx, args).skip_norm_wip(),
50            );
51
52            // Modify `fn(self, ...)` to `fn(self: *mut Self, ...)`.
53            if let ty::InstanceKind::Shim(ty::ShimKind::VTable(..)) = instance.def {
54                let mut inputs_and_output = sig.inputs_and_output.to_vec();
55                inputs_and_output[0] = Ty::new_mut_ptr(tcx, inputs_and_output[0]);
56                sig.inputs_and_output = tcx.mk_type_list(&inputs_and_output);
57            }
58
59            sig
60        }
61        ty::Closure(def_id, args) => {
62            let sig = tcx.instantiate_bound_regions_with_erased(args.as_closure().sig());
63            let env_ty = tcx.closure_env_ty(
64                Ty::new_closure(tcx, def_id, args),
65                args.as_closure().kind(),
66                tcx.lifetimes.re_erased,
67            );
68
69            tcx.mk_fn_sig(
70                iter::once(env_ty).chain(sig.inputs().iter().cloned()),
71                sig.output(),
72                sig.fn_sig_kind,
73            )
74        }
75        ty::CoroutineClosure(def_id, args) => {
76            let coroutine_ty = Ty::new_coroutine_closure(tcx, def_id, args);
77            let sig = args.as_coroutine_closure().coroutine_closure_sig();
78
79            // When this `CoroutineClosure` comes from a `ConstructCoroutineInClosureShim`,
80            // make sure we respect the `target_kind` in that shim.
81            // FIXME(async_closures): This shouldn't be needed, and we should be populating
82            // a separate def-id for these bodies.
83            let mut coroutine_kind = args.as_coroutine_closure().kind();
84
85            let env_ty = if let InstanceKind::Shim(ShimKind::ConstructCoroutineInClosure {
86                receiver_by_ref,
87                ..
88            }) = instance.def
89            {
90                coroutine_kind = ty::ClosureKind::FnOnce;
91
92                // Implementations of `FnMut` and `Fn` for coroutine-closures
93                // still take their receiver by ref.
94                if receiver_by_ref {
95                    Ty::new_imm_ref(tcx, tcx.lifetimes.re_erased, coroutine_ty)
96                } else {
97                    coroutine_ty
98                }
99            } else {
100                tcx.closure_env_ty(coroutine_ty, coroutine_kind, tcx.lifetimes.re_erased)
101            };
102
103            let sig = tcx.instantiate_bound_regions_with_erased(sig);
104
105            tcx.mk_fn_sig(
106                iter::once(env_ty).chain([sig.tupled_inputs_ty]),
107                sig.to_coroutine_given_kind_and_upvars(
108                    tcx,
109                    args.as_coroutine_closure().parent_args(),
110                    tcx.coroutine_for_closure(def_id),
111                    coroutine_kind,
112                    tcx.lifetimes.re_erased,
113                    args.as_coroutine_closure().tupled_upvars_ty(),
114                    args.as_coroutine_closure().coroutine_captures_by_ref_ty(),
115                ),
116                sig.fn_sig_kind,
117            )
118        }
119        ty::Coroutine(did, args) => {
120            let coroutine_kind = tcx.coroutine_kind(did).unwrap();
121            let sig = args.as_coroutine().sig();
122
123            let env_ty = Ty::new_mut_ref(tcx, tcx.lifetimes.re_erased, ty);
124
125            let pin_did = tcx.require_lang_item(LangItem::Pin, DUMMY_SP);
126            let pin_adt_ref = tcx.adt_def(pin_did);
127            let pin_args = tcx.mk_args(&[env_ty.into()]);
128            let env_ty = match coroutine_kind {
129                hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Gen, _) => {
130                    // Iterator::next doesn't accept a pinned argument,
131                    // unlike for all other coroutine kinds.
132                    env_ty
133                }
134                hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Async, _)
135                | hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::AsyncGen, _)
136                | hir::CoroutineKind::Coroutine(_) => Ty::new_adt(tcx, pin_adt_ref, pin_args),
137            };
138
139            // The `FnSig` and the `ret_ty` here is for a coroutines main
140            // `Coroutine::resume(...) -> CoroutineState` function in case we
141            // have an ordinary coroutine, the `Future::poll(...) -> Poll`
142            // function in case this is a special coroutine backing an async construct
143            // or the `Iterator::next(...) -> Option` function in case this is a
144            // special coroutine backing a gen construct.
145            let (resume_ty, ret_ty) = match coroutine_kind {
146                hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Async, _) => {
147                    // The signature should be `Future::poll(_, &mut Context<'_>) -> Poll<Output>`
148                    assert_eq!(sig.yield_ty, tcx.types.unit);
149
150                    let poll_did = tcx.require_lang_item(LangItem::Poll, DUMMY_SP);
151                    let poll_adt_ref = tcx.adt_def(poll_did);
152                    let poll_args = tcx.mk_args(&[sig.return_ty.into()]);
153                    let ret_ty = Ty::new_adt(tcx, poll_adt_ref, poll_args);
154
155                    // We have to replace the `ResumeTy` that is used for type and borrow checking
156                    // with `&mut Context<'_>` which is used in codegen.
157                    #[cfg(debug_assertions)]
158                    {
159                        if let ty::Adt(resume_ty_adt, _) = sig.resume_ty.kind() {
160                            let expected_adt =
161                                tcx.adt_def(tcx.require_lang_item(LangItem::ResumeTy, DUMMY_SP));
162                            assert_eq!(*resume_ty_adt, expected_adt);
163                        } else {
164                            panic!("expected `ResumeTy`, found `{:?}`", sig.resume_ty);
165                        };
166                    }
167                    let context_mut_ref = Ty::new_task_context(tcx);
168
169                    (Some(context_mut_ref), ret_ty)
170                }
171                hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Gen, _) => {
172                    // The signature should be `Iterator::next(_) -> Option<Yield>`
173                    let option_did = tcx.require_lang_item(LangItem::Option, DUMMY_SP);
174                    let option_adt_ref = tcx.adt_def(option_did);
175                    let option_args = tcx.mk_args(&[sig.yield_ty.into()]);
176                    let ret_ty = Ty::new_adt(tcx, option_adt_ref, option_args);
177
178                    assert_eq!(sig.return_ty, tcx.types.unit);
179                    assert_eq!(sig.resume_ty, tcx.types.unit);
180
181                    (None, ret_ty)
182                }
183                hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::AsyncGen, _) => {
184                    // The signature should be
185                    // `AsyncIterator::poll_next(_, &mut Context<'_>) -> Poll<Option<Output>>`
186                    assert_eq!(sig.return_ty, tcx.types.unit);
187
188                    // Yield type is already `Poll<Option<yield_ty>>`
189                    let ret_ty = sig.yield_ty;
190
191                    // We have to replace the `ResumeTy` that is used for type and borrow checking
192                    // with `&mut Context<'_>` which is used in codegen.
193                    #[cfg(debug_assertions)]
194                    {
195                        if let ty::Adt(resume_ty_adt, _) = sig.resume_ty.kind() {
196                            let expected_adt =
197                                tcx.adt_def(tcx.require_lang_item(LangItem::ResumeTy, DUMMY_SP));
198                            assert_eq!(*resume_ty_adt, expected_adt);
199                        } else {
200                            panic!("expected `ResumeTy`, found `{:?}`", sig.resume_ty);
201                        };
202                    }
203                    let context_mut_ref = Ty::new_task_context(tcx);
204
205                    (Some(context_mut_ref), ret_ty)
206                }
207                hir::CoroutineKind::Coroutine(_) => {
208                    // The signature should be `Coroutine::resume(_, Resume) -> CoroutineState<Yield, Return>`
209                    let state_did = tcx.require_lang_item(LangItem::CoroutineState, DUMMY_SP);
210                    let state_adt_ref = tcx.adt_def(state_did);
211                    let state_args = tcx.mk_args(&[sig.yield_ty.into(), sig.return_ty.into()]);
212                    let ret_ty = Ty::new_adt(tcx, state_adt_ref, state_args);
213
214                    (Some(sig.resume_ty), ret_ty)
215                }
216            };
217
218            if let Some(resume_ty) = resume_ty {
219                tcx.mk_fn_sig_safe_rust_abi([env_ty, resume_ty], ret_ty)
220            } else {
221                // `Iterator::next` doesn't have a `resume` argument.
222                tcx.mk_fn_sig_safe_rust_abi([env_ty], ret_ty)
223            }
224        }
225        _ => bug!("unexpected type {:?} in Instance::fn_sig", ty),
226    }
227}
228
229/// Describes a function for determination of its ABI.
230struct FnAbiDesc<'tcx> {
231    layout_cx: LayoutCx<'tcx>,
232    sig: ty::FnSig<'tcx>,
233
234    /// The function's definition, if its body can be used to deduce parameter attributes.
235    determined_fn_def_id: Option<DefId>,
236    caller_location: Option<Ty<'tcx>>,
237    is_virtual_call: bool,
238    extra_args: &'tcx [Ty<'tcx>],
239}
240
241impl<'tcx> FnAbiDesc<'tcx> {
242    fn for_fn_ptr(
243        tcx: TyCtxt<'tcx>,
244        query: ty::PseudoCanonicalInput<'tcx, (ty::PolyFnSig<'tcx>, &'tcx ty::List<Ty<'tcx>>)>,
245    ) -> Self {
246        let ty::PseudoCanonicalInput { typing_env, value: (sig, extra_args) } = query;
247        Self {
248            layout_cx: LayoutCx::new(tcx, typing_env),
249            sig: tcx.normalize_erasing_regions(
250                typing_env,
251                Unnormalized::new_wip(tcx.instantiate_bound_regions_with_erased(sig)),
252            ),
253            // Parameter attributes can never be deduced for indirect calls, as there is no
254            // function body available to use.
255            determined_fn_def_id: None,
256            caller_location: None,
257            is_virtual_call: false,
258            extra_args,
259        }
260    }
261
262    fn for_instance(
263        tcx: TyCtxt<'tcx>,
264        query: ty::PseudoCanonicalInput<'tcx, (ty::Instance<'tcx>, &'tcx ty::List<Ty<'tcx>>)>,
265    ) -> Self {
266        let ty::PseudoCanonicalInput { typing_env, value: (instance, extra_args) } = query;
267        let is_virtual_call = #[allow(non_exhaustive_omitted_patterns)] match instance.def {
    ty::InstanceKind::Virtual(..) => true,
    _ => false,
}matches!(instance.def, ty::InstanceKind::Virtual(..));
268        let is_tls_shim_call =
269            #[allow(non_exhaustive_omitted_patterns)] match instance.def {
    ty::InstanceKind::Shim(ty::ShimKind::ThreadLocal(_)) => true,
    _ => false,
}matches!(instance.def, ty::InstanceKind::Shim(ty::ShimKind::ThreadLocal(_)));
270        Self {
271            layout_cx: LayoutCx::new(tcx, typing_env),
272            sig: tcx.normalize_erasing_regions(
273                typing_env,
274                Unnormalized::new_wip(fn_sig_for_fn_abi(tcx, instance, typing_env)),
275            ),
276            // Parameter attributes can be deduced from the bodies of neither:
277            // - virtual calls, as they might call other functions from the vtable; nor
278            // - TLS shims, as they would refer to the underlying static.
279            determined_fn_def_id: (!is_virtual_call && !is_tls_shim_call)
280                .then(|| instance.def_id()),
281            caller_location: instance
282                .def
283                .requires_caller_location(tcx)
284                .then(|| tcx.caller_location_ty()),
285            is_virtual_call,
286            extra_args,
287        }
288    }
289}
290
291fn fn_abi_of_fn_ptr<'tcx>(
292    tcx: TyCtxt<'tcx>,
293    query: ty::PseudoCanonicalInput<'tcx, (ty::PolyFnSig<'tcx>, &'tcx ty::List<Ty<'tcx>>)>,
294) -> Result<&'tcx FnAbi<'tcx, Ty<'tcx>>, &'tcx FnAbiError<'tcx>> {
295    let desc = FnAbiDesc::for_fn_ptr(tcx, query);
296    fn_abi_new_uncached(desc)
297}
298
299fn fn_abi_of_instance_no_deduced_attrs<'tcx>(
300    tcx: TyCtxt<'tcx>,
301    query: ty::PseudoCanonicalInput<'tcx, (ty::Instance<'tcx>, &'tcx ty::List<Ty<'tcx>>)>,
302) -> Result<&'tcx FnAbi<'tcx, Ty<'tcx>>, &'tcx FnAbiError<'tcx>> {
303    let desc = FnAbiDesc::for_instance(tcx, query);
304    fn_abi_new_uncached(desc)
305}
306
307fn fn_abi_of_instance_raw<'tcx>(
308    tcx: TyCtxt<'tcx>,
309    query: ty::PseudoCanonicalInput<'tcx, (ty::Instance<'tcx>, &'tcx ty::List<Ty<'tcx>>)>,
310) -> Result<&'tcx FnAbi<'tcx, Ty<'tcx>>, &'tcx FnAbiError<'tcx>> {
311    // The `fn_abi_of_instance_no_deduced_attrs` query may have been called during CTFE, so we
312    // delegate to it here in order to reuse (and, if necessary, augment) its result.
313    tcx.fn_abi_of_instance_no_deduced_attrs(query).map(|fn_abi| {
314        let params = FnAbiDesc::for_instance(tcx, query);
315        // If the function's body can be used to deduce parameter attributes, then adjust such
316        // "no deduced attrs" ABI; otherwise, return that ABI unadjusted.
317        params.determined_fn_def_id.map_or(fn_abi, |fn_def_id| {
318            fn_abi_adjust_for_deduced_attrs(&params.layout_cx, fn_abi, params.sig.abi(), fn_def_id)
319        })
320    })
321}
322
323/// Returns argument attributes for a scalar argument.
324fn arg_attrs_for_rust_scalar<'tcx>(
325    cx: LayoutCx<'tcx>,
326    scalar: Scalar,
327    layout: TyAndLayout<'tcx>,
328    offset: Size,
329    is_return: bool,
330    determined_fn_def_id: Option<DefId>,
331) -> ArgAttributes {
332    let mut attrs = ArgAttributes::new();
333
334    // Booleans are always a noundef i1 that needs to be zero-extended.
335    if scalar.is_bool() {
336        attrs.ext(ArgExtension::Zext);
337        attrs.set(ArgAttribute::NoUndef);
338        return attrs;
339    }
340
341    if !scalar.is_uninit_valid() {
342        attrs.set(ArgAttribute::NoUndef);
343    }
344
345    // Only pointer types handled below.
346    let Scalar::Initialized { value: Pointer(_), valid_range } = scalar else { return attrs };
347
348    // Set `nonnull` if the validity range excludes zero.
349    if !valid_range.contains(0) {
350        attrs.set(ArgAttribute::NonNull);
351    }
352
353    let tcx = cx.tcx();
354
355    if let Some(pointee) = layout.pointee_info_at(&cx, offset) {
356        if pointee.align > Align::ONE {
357            attrs.pointee_align =
358                Some(pointee.align.min(cx.tcx().sess.target.max_reliable_alignment()));
359        }
360
361        attrs.pointee_size = pointee.size;
362
363        if let Some(kind) = pointee.safe {
364            // The aliasing rules for `Box<T>` are still not decided, but currently we emit
365            // `noalias` for it. This can be turned off using an unstable flag.
366            // See https://github.com/rust-lang/unsafe-code-guidelines/issues/326
367            let noalias_for_box = tcx.sess.opts.unstable_opts.box_noalias;
368
369            // LLVM prior to version 12 had known miscompiles in the presence of noalias attributes
370            // (see #54878), so it was conditionally disabled, but we don't support earlier
371            // versions at all anymore. We still support turning it off using -Zmutable-noalias.
372            let noalias_mut_ref = tcx.sess.opts.unstable_opts.mutable_noalias;
373
374            // `&T` where `T` contains no `UnsafeCell<U>` is immutable, and can be marked as both
375            // `readonly` and `noalias`, as LLVM's definition of `noalias` is based solely on memory
376            // dependencies rather than pointer equality. However this only applies to arguments,
377            // not return values.
378            //
379            // `&mut T` and `Box<T>` where `T: Unpin` are unique and hence `noalias`.
380            let no_alias = match kind {
381                PointerKind::SharedRef { frozen } => frozen,
382                PointerKind::MutableRef { unpin } => unpin && noalias_mut_ref,
383                PointerKind::Box { unpin, global } => unpin && global && noalias_for_box,
384            };
385            // We can never add `noalias` in return position; that LLVM attribute has some very surprising semantics
386            // (see <https://github.com/rust-lang/unsafe-code-guidelines/issues/385#issuecomment-1368055745>).
387            if no_alias && !is_return {
388                attrs.set(ArgAttribute::NoAlias);
389            }
390
391            // Set writable if no_alias is set, it's a mutable reference and the feature is enabled.
392            if tcx.sess.opts.unstable_opts.llvm_writable
393                && #[allow(non_exhaustive_omitted_patterns)] match kind {
    PointerKind::MutableRef { unpin: true } => true,
    _ => false,
}matches!(kind, PointerKind::MutableRef { unpin: true })
394                && !is_return
395            {
396                let rustc_no_writable = match determined_fn_def_id {
397                    Some(def_id) => {
        {
            'done:
                {
                for i in ::rustc_hir::attrs::HasAttrs::get_attrs(def_id, &tcx)
                    {
                    #[allow(unused_imports)]
                    use rustc_hir::attrs::AttributeKind::*;
                    let i: &rustc_hir::Attribute = i;
                    match i {
                        rustc_hir::Attribute::Parsed(RustcNoWritable) => {
                            break 'done Some(());
                        }
                        rustc_hir::Attribute::Unparsed(..) =>
                            {}
                            #[deny(unreachable_patterns)]
                            _ => {}
                    }
                }
                None
            }
        }
    }.is_some()find_attr!(tcx, def_id, RustcNoWritable),
398                    None => true, // If no def_id exists, we make the conservative choice and disable the feature.
399                };
400
401                if !rustc_no_writable {
402                    attrs.set(ArgAttribute::Writable);
403                }
404            }
405
406            // NoFree is not valid on return values. If it were, it would mean something like
407            // "will not be freed until the end of the program", which is generally not valid for
408            // references.
409            let no_free = !is_return
410                && match kind {
411                    // Non-frozen shared references are not necessarily dereferenceable for the
412                    // entire duration of the function
413                    // (see <https://github.com/rust-lang/rust/pull/98017>).
414                    PointerKind::SharedRef { frozen } => frozen,
415                    // Mutable references to potentially self-referential types are not necessarily
416                    // dereferenceable for the entire duration of the function
417                    // (see <https://github.com/rust-lang/unsafe-code-guidelines/issues/381>).
418                    PointerKind::MutableRef { unpin } => unpin,
419                    // Box may be deallocated during execution of the function.
420                    PointerKind::Box { .. } => false,
421                };
422            if no_free {
423                attrs.set(ArgAttribute::NoFree);
424            }
425
426            if #[allow(non_exhaustive_omitted_patterns)] match kind {
    PointerKind::SharedRef { frozen: true } => true,
    _ => false,
}matches!(kind, PointerKind::SharedRef { frozen: true }) && !is_return {
427                attrs.set(ArgAttribute::ReadOnly);
428                attrs.set(ArgAttribute::CapturesReadOnly);
429            }
430        }
431    }
432
433    attrs
434}
435
436/// Ensure that the ABI makes basic sense.
437fn fn_abi_sanity_check<'tcx>(
438    cx: &LayoutCx<'tcx>,
439    fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
440    spec_abi: ExternAbi,
441) {
442    fn fn_arg_attrs_sanity_check(attrs: &ArgAttributes, is_ret: bool) {
443        if attrs.regular.contains(ArgAttribute::NoFree) {
444            if !!is_ret {
    {
        ::core::panicking::panic_fmt(format_args!("NoFree not valid on return values"));
    }
};assert!(!is_ret, "NoFree not valid on return values");
445        }
446    }
447
448    fn fn_arg_sanity_check<'tcx>(
449        cx: &LayoutCx<'tcx>,
450        fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
451        spec_abi: ExternAbi,
452        arg: &ArgAbi<'tcx, Ty<'tcx>>,
453        is_ret: bool,
454    ) {
455        let tcx = cx.tcx();
456
457        if spec_abi.is_rustic_abi() {
458            if arg.layout.is_zst() {
459                // Casting closures to function pointers depends on ZST closure types being
460                // omitted entirely in the calling convention.
461                if !arg.is_ignore() {
    ::core::panicking::panic("assertion failed: arg.is_ignore()")
};assert!(arg.is_ignore());
462            }
463            if let PassMode::Indirect { on_stack, .. } = arg.mode
464                && spec_abi != ExternAbi::RustTail
465            {
466                if !!on_stack {
    {
        ::core::panicking::panic_fmt(format_args!("rustic abi {0:?} shouldn\'t use on_stack",
                spec_abi));
    }
};assert!(!on_stack, "rustic abi {spec_abi:?} shouldn't use on_stack");
467            }
468        } else if arg.layout.pass_indirectly_in_non_rustic_abis(cx) {
469            {
    match arg.mode {
        PassMode::Indirect { on_stack: false, .. } => {}
        ref left_val => {
            ::core::panicking::assert_matches_failed(left_val,
                "PassMode::Indirect { on_stack: false, .. }",
                ::core::option::Option::Some(format_args!("the {0} ABI does not implement `#[rustc_pass_indirectly_in_non_rustic_abis]`",
                        spec_abi)));
        }
    }
};assert_matches!(
470                arg.mode,
471                PassMode::Indirect { on_stack: false, .. },
472                "the {spec_abi} ABI does not implement `#[rustc_pass_indirectly_in_non_rustic_abis]`"
473            );
474        }
475
476        match &arg.mode {
477            PassMode::Ignore => {
478                if !arg.layout.is_zst() {
    ::core::panicking::panic("assertion failed: arg.layout.is_zst()")
};assert!(arg.layout.is_zst());
479            }
480            PassMode::Direct(attrs) => {
481                // Here the Rust type is used to determine the actual ABI, so we have to be very
482                // careful. Scalar/Vector is fine, since backends will generally use
483                // `layout.backend_repr` and ignore everything else. We should just reject
484                //`Aggregate` entirely here, but some targets need to be fixed first.
485                match arg.layout.backend_repr {
486                    BackendRepr::Scalar(_)
487                    | BackendRepr::SimdVector { .. }
488                    | BackendRepr::SimdScalableVector { .. } => {}
489                    BackendRepr::ScalarPair(..) => {
490                        {
    ::core::panicking::panic_fmt(format_args!("`PassMode::Direct` used for ScalarPair type {0}",
            arg.layout.ty));
}panic!("`PassMode::Direct` used for ScalarPair type {}", arg.layout.ty)
491                    }
492                    BackendRepr::Memory { sized } => {
493                        // For an unsized type we'd only pass the sized prefix, so there is no universe
494                        // in which we ever want to allow this.
495                        if !sized {
    {
        ::core::panicking::panic_fmt(format_args!("`PassMode::Direct` for unsized type in ABI: {0:#?}",
                fn_abi));
    }
};assert!(sized, "`PassMode::Direct` for unsized type in ABI: {:#?}", fn_abi);
496
497                        // This really shouldn't happen even for sized aggregates, since
498                        // `immediate_llvm_type` will use `layout.fields` to turn this Rust type into an
499                        // LLVM type. This means all sorts of Rust type details leak into the ABI.
500                        // The unadjusted ABI however uses Direct for all args. It is ill-specified,
501                        // but unfortunately we need it for calling certain LLVM intrinsics.
502                        if !#[allow(non_exhaustive_omitted_patterns)] match spec_abi {
            ExternAbi::Unadjusted => true,
            _ => false,
        } {
    {
        ::core::panicking::panic_fmt(format_args!("`PassMode::Direct` for aggregates only allowed for \"unadjusted\"\nProblematic type: {0:#?}",
                arg.layout));
    }
};assert!(
503                            matches!(spec_abi, ExternAbi::Unadjusted),
504                            "`PassMode::Direct` for aggregates only allowed for \"unadjusted\"\n\
505                             Problematic type: {:#?}",
506                            arg.layout,
507                        );
508                    }
509                }
510                fn_arg_attrs_sanity_check(attrs, is_ret);
511            }
512            PassMode::Pair(attrs1, attrs2) => {
513                // Similar to `Direct`, we need to make sure that backends use `layout.backend_repr`
514                // and ignore the rest of the layout.
515                if !#[allow(non_exhaustive_omitted_patterns)] match arg.layout.backend_repr {
            BackendRepr::ScalarPair(..) => true,
            _ => false,
        } {
    {
        ::core::panicking::panic_fmt(format_args!("PassMode::Pair for type {0}",
                arg.layout.ty));
    }
};assert!(
516                    matches!(arg.layout.backend_repr, BackendRepr::ScalarPair(..)),
517                    "PassMode::Pair for type {}",
518                    arg.layout.ty
519                );
520                fn_arg_attrs_sanity_check(attrs1, is_ret);
521                fn_arg_attrs_sanity_check(attrs2, is_ret);
522            }
523            PassMode::Cast { .. } => {
524                // `Cast` means "transmute to `CastType`"; that only makes sense for sized types.
525                if !arg.layout.is_sized() {
    ::core::panicking::panic("assertion failed: arg.layout.is_sized()")
};assert!(arg.layout.is_sized());
526            }
527            PassMode::Indirect { meta_attrs: None, attrs, .. } => {
528                // No metadata, must be sized.
529                // Conceptually, unsized arguments must be copied around, which requires dynamically
530                // determining their size, which we cannot do without metadata. Consult
531                // t-opsem before removing this check.
532                if !arg.layout.is_sized() {
    ::core::panicking::panic("assertion failed: arg.layout.is_sized()")
};assert!(arg.layout.is_sized());
533                // Indirect returns are arguments from an ABI perspective.
534                fn_arg_attrs_sanity_check(attrs, false);
535            }
536            PassMode::Indirect { meta_attrs: Some(meta_attrs), attrs, on_stack } => {
537                // With metadata. Must be unsized and not on the stack.
538                if !(arg.layout.is_unsized() && !on_stack) {
    ::core::panicking::panic("assertion failed: arg.layout.is_unsized() && !on_stack")
};assert!(arg.layout.is_unsized() && !on_stack);
539                // Also, must not be `extern` type.
540                let tail = tcx.struct_tail_for_codegen(arg.layout.ty, cx.typing_env);
541                if #[allow(non_exhaustive_omitted_patterns)] match tail.kind() {
    ty::Foreign(..) => true,
    _ => false,
}matches!(tail.kind(), ty::Foreign(..)) {
542                    // These types do not have metadata, so having `meta_attrs` is bogus.
543                    // Conceptually, unsized arguments must be copied around, which requires dynamically
544                    // determining their size. Therefore, we cannot allow `extern` types here. Consult
545                    // t-opsem before removing this check.
546                    {
    ::core::panicking::panic_fmt(format_args!("unsized arguments must not be `extern` types"));
};panic!("unsized arguments must not be `extern` types");
547                }
548                // Indirect returns are arguments from an ABI perspective.
549                fn_arg_attrs_sanity_check(attrs, false);
550                fn_arg_attrs_sanity_check(meta_attrs, false);
551            }
552        }
553    }
554
555    for arg in fn_abi.args.iter() {
556        fn_arg_sanity_check(cx, fn_abi, spec_abi, arg, false);
557    }
558    fn_arg_sanity_check(cx, fn_abi, spec_abi, &fn_abi.ret, true);
559}
560
561#[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("fn_abi_new_uncached",
                                    "rustc_ty_utils::abi", ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_ty_utils/src/abi.rs"),
                                    ::tracing_core::__macro_support::Option::Some(561u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_ty_utils::abi"),
                                    ::tracing_core::field::FieldSet::new(&["sig", "extra_args"],
                                        ::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(&sig)
                                                            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(&extra_args)
                                                            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<&'tcx FnAbi<'tcx, Ty<'tcx>>,
                    &'tcx FnAbiError<'tcx>> = loop {};
            return __tracing_attr_fake_return;
        }
        {
            let tcx = cx.tcx();
            let abi_map = AbiMap::from_target(&tcx.sess.target);
            let conv =
                abi_map.canonize_abi(sig.abi(), sig.c_variadic()).unwrap();
            let mut inputs = sig.inputs();
            let extra_args =
                if sig.abi() == ExternAbi::RustCall {
                    if !(!sig.c_variadic() && extra_args.is_empty()) {
                        ::core::panicking::panic("assertion failed: !sig.c_variadic() && extra_args.is_empty()")
                    };
                    if let Some(input) = sig.inputs().last() &&
                            let ty::Tuple(tupled_arguments) = input.kind() {
                        inputs = &sig.inputs()[0..sig.inputs().len() - 1];
                        tupled_arguments
                    } else {
                        ::rustc_middle::util::bug::bug_fmt(format_args!("argument to function with \"rust-call\" ABI is not a tuple"));
                    }
                } else {
                    if !(sig.c_variadic() || extra_args.is_empty()) {
                        ::core::panicking::panic("assertion failed: sig.c_variadic() || extra_args.is_empty()")
                    };
                    extra_args
                };
            let arg_of =
                |ty: Ty<'tcx>, arg_idx: Option<usize>|
                    -> Result<_, &'tcx FnAbiError<'tcx>>
                    {
                        let span =
                            {
                                use ::tracing::__macro_support::Callsite as _;
                                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                                    {
                                        static META: ::tracing::Metadata<'static> =
                                            {
                                                ::tracing_core::metadata::Metadata::new("arg_of",
                                                    "rustc_ty_utils::abi", ::tracing::Level::DEBUG,
                                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_ty_utils/src/abi.rs"),
                                                    ::tracing_core::__macro_support::Option::Some(601u32),
                                                    ::tracing_core::__macro_support::Option::Some("rustc_ty_utils::abi"),
                                                    ::tracing_core::field::FieldSet::new(&[],
                                                        ::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,
                                        &{ meta.fields().value_set(&[]) })
                                } else {
                                    let span =
                                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                                    {};
                                    span
                                }
                            };
                        let _entered = span.enter();
                        let is_return = arg_idx.is_none();
                        let layout =
                            cx.layout_of(ty).map_err(|err|
                                        &*tcx.arena.alloc(FnAbiError::Layout(*err)))?;
                        let layout =
                            if is_virtual_call && arg_idx == Some(0) {
                                make_thin_self_ptr(cx, layout)
                            } else { layout };
                        Ok(ArgAbi::new(cx, layout,
                                |scalar, offset|
                                    {
                                        arg_attrs_for_rust_scalar(*cx, scalar, layout, offset,
                                            is_return, determined_fn_def_id)
                                    }))
                    };
            let mut fn_abi =
                FnAbi {
                    ret: arg_of(sig.output(), None)?,
                    args: inputs.iter().copied().chain(extra_args.iter().copied()).chain(caller_location).enumerate().map(|(i,
                                        ty)| arg_of(ty, Some(i))).collect::<Result<_, _>>()?,
                    c_variadic: sig.c_variadic(),
                    fixed_count: inputs.len() as u32,
                    conv,
                    can_unwind: fn_can_unwind(tcx, determined_fn_def_id,
                        sig.abi()),
                };
            fn_abi_adjust_for_abi(cx, &mut fn_abi, sig.abi());
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("event compiler/rustc_ty_utils/src/abi.rs:642",
                                    "rustc_ty_utils::abi", ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_ty_utils/src/abi.rs"),
                                    ::tracing_core::__macro_support::Option::Some(642u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_ty_utils::abi"),
                                    ::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!("fn_abi_new_uncached = {0:?}",
                                                                fn_abi) as &dyn Value))])
                        });
                } else { ; }
            };
            fn_abi_sanity_check(cx, &fn_abi, sig.abi());
            Ok(tcx.arena.alloc(fn_abi))
        }
    }
}#[tracing::instrument(
562    level = "debug",
563    skip(cx, caller_location, determined_fn_def_id, is_virtual_call)
564)]
565fn fn_abi_new_uncached<'tcx>(
566    FnAbiDesc {
567        layout_cx: ref cx,
568        sig,
569        determined_fn_def_id,
570        caller_location,
571        is_virtual_call,
572        extra_args,
573    }: FnAbiDesc<'tcx>,
574) -> Result<&'tcx FnAbi<'tcx, Ty<'tcx>>, &'tcx FnAbiError<'tcx>> {
575    let tcx = cx.tcx();
576
577    let abi_map = AbiMap::from_target(&tcx.sess.target);
578    let conv = abi_map.canonize_abi(sig.abi(), sig.c_variadic()).unwrap();
579
580    let mut inputs = sig.inputs();
581    let extra_args = if sig.abi() == ExternAbi::RustCall {
582        assert!(!sig.c_variadic() && extra_args.is_empty());
583
584        if let Some(input) = sig.inputs().last()
585            && let ty::Tuple(tupled_arguments) = input.kind()
586        {
587            inputs = &sig.inputs()[0..sig.inputs().len() - 1];
588            tupled_arguments
589        } else {
590            bug!(
591                "argument to function with \"rust-call\" ABI \
592                    is not a tuple"
593            );
594        }
595    } else {
596        assert!(sig.c_variadic() || extra_args.is_empty());
597        extra_args
598    };
599
600    let arg_of = |ty: Ty<'tcx>, arg_idx: Option<usize>| -> Result<_, &'tcx FnAbiError<'tcx>> {
601        let span = tracing::debug_span!("arg_of");
602        let _entered = span.enter();
603        let is_return = arg_idx.is_none();
604
605        let layout = cx.layout_of(ty).map_err(|err| &*tcx.arena.alloc(FnAbiError::Layout(*err)))?;
606        let layout = if is_virtual_call && arg_idx == Some(0) {
607            // Don't pass the vtable, it's not an argument of the virtual fn.
608            // Instead, pass just the data pointer, but give it the type `*const/mut dyn Trait`
609            // or `&/&mut dyn Trait` because this is special-cased elsewhere in codegen
610            make_thin_self_ptr(cx, layout)
611        } else {
612            layout
613        };
614
615        Ok(ArgAbi::new(cx, layout, |scalar, offset| {
616            arg_attrs_for_rust_scalar(*cx, scalar, layout, offset, is_return, determined_fn_def_id)
617        }))
618    };
619
620    let mut fn_abi = FnAbi {
621        ret: arg_of(sig.output(), None)?,
622        args: inputs
623            .iter()
624            .copied()
625            .chain(extra_args.iter().copied())
626            .chain(caller_location)
627            .enumerate()
628            .map(|(i, ty)| arg_of(ty, Some(i)))
629            .collect::<Result<_, _>>()?,
630        c_variadic: sig.c_variadic(),
631        fixed_count: inputs.len() as u32,
632        conv,
633        // FIXME return false for tls shim
634        can_unwind: fn_can_unwind(
635            tcx,
636            // Since `#[rustc_nounwind]` can change unwinding, we cannot infer unwinding by `fn_def_id` for a virtual call.
637            determined_fn_def_id,
638            sig.abi(),
639        ),
640    };
641    fn_abi_adjust_for_abi(cx, &mut fn_abi, sig.abi());
642    debug!("fn_abi_new_uncached = {:?}", fn_abi);
643    fn_abi_sanity_check(cx, &fn_abi, sig.abi());
644    Ok(tcx.arena.alloc(fn_abi))
645}
646
647#[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::TRACE <=
                    ::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("fn_abi_adjust_for_abi",
                                    "rustc_ty_utils::abi", ::tracing::Level::TRACE,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_ty_utils/src/abi.rs"),
                                    ::tracing_core::__macro_support::Option::Some(647u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_ty_utils::abi"),
                                    ::tracing_core::field::FieldSet::new(&["fn_abi", "abi"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::TRACE <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::TRACE <=
                                    ::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(&fn_abi)
                                                            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(&abi)
                                                            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;
        }
        {
            if abi == ExternAbi::Unadjusted {
                fn unadjust<'tcx>(arg: &mut ArgAbi<'tcx, Ty<'tcx>>) {
                    if #[allow(non_exhaustive_omitted_patterns)] match arg.layout.backend_repr
                            {
                            BackendRepr::Memory { .. } => true,
                            _ => false,
                        } {
                        if !arg.layout.backend_repr.is_sized() {
                            {
                                ::core::panicking::panic_fmt(format_args!("\'unadjusted\' ABI does not support unsized arguments"));
                            }
                        };
                    }
                    arg.make_direct_deprecated();
                }
                unadjust(&mut fn_abi.ret);
                for arg in fn_abi.args.iter_mut() { unadjust(arg); }
            } else if abi.is_rustic_abi() {
                fn_abi.adjust_for_rust_abi(cx);
            } else { fn_abi.adjust_for_foreign_abi(cx, abi); }
        }
    }
}#[tracing::instrument(level = "trace", skip(cx))]
648fn fn_abi_adjust_for_abi<'tcx>(
649    cx: &LayoutCx<'tcx>,
650    fn_abi: &mut FnAbi<'tcx, Ty<'tcx>>,
651    abi: ExternAbi,
652) {
653    if abi == ExternAbi::Unadjusted {
654        // The "unadjusted" ABI passes aggregates in "direct" mode. That's fragile but needed for
655        // some LLVM intrinsics.
656        fn unadjust<'tcx>(arg: &mut ArgAbi<'tcx, Ty<'tcx>>) {
657            // This still uses `PassMode::Pair` for ScalarPair types. That's unlikely to be intended,
658            // but who knows what breaks if we change this now.
659            if matches!(arg.layout.backend_repr, BackendRepr::Memory { .. }) {
660                assert!(
661                    arg.layout.backend_repr.is_sized(),
662                    "'unadjusted' ABI does not support unsized arguments"
663                );
664            }
665            arg.make_direct_deprecated();
666        }
667
668        unadjust(&mut fn_abi.ret);
669        for arg in fn_abi.args.iter_mut() {
670            unadjust(arg);
671        }
672    } else if abi.is_rustic_abi() {
673        fn_abi.adjust_for_rust_abi(cx);
674    } else {
675        fn_abi.adjust_for_foreign_abi(cx, abi);
676    }
677}
678
679#[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::TRACE <=
                    ::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("fn_abi_adjust_for_deduced_attrs",
                                    "rustc_ty_utils::abi", ::tracing::Level::TRACE,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_ty_utils/src/abi.rs"),
                                    ::tracing_core::__macro_support::Option::Some(679u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_ty_utils::abi"),
                                    ::tracing_core::field::FieldSet::new(&["fn_abi", "abi",
                                                    "fn_def_id"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::TRACE <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::TRACE <=
                                    ::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(&fn_abi)
                                                            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(&abi)
                                                            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(&fn_def_id)
                                                            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: &'tcx FnAbi<'tcx, Ty<'tcx>> =
                loop {};
            return __tracing_attr_fake_return;
        }
        {
            let tcx = cx.tcx();
            let deduced =
                if abi.is_rustic_abi() {
                    tcx.deduced_param_attrs(fn_def_id)
                } else { &[] };
            if deduced.is_empty() {
                fn_abi
            } else {
                let mut fn_abi = fn_abi.clone();
                apply_deduced_attributes(cx, deduced, 0, &mut fn_abi.ret);
                for (arg_idx, arg) in fn_abi.args.iter_mut().enumerate() {
                    apply_deduced_attributes(cx, deduced, arg_idx + 1, arg);
                }
                {
                    use ::tracing::__macro_support::Callsite as _;
                    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                        {
                            static META: ::tracing::Metadata<'static> =
                                {
                                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_ty_utils/src/abi.rs:698",
                                        "rustc_ty_utils::abi", ::tracing::Level::DEBUG,
                                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_ty_utils/src/abi.rs"),
                                        ::tracing_core::__macro_support::Option::Some(698u32),
                                        ::tracing_core::__macro_support::Option::Some("rustc_ty_utils::abi"),
                                        ::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!("fn_abi_adjust_for_deduced_attrs = {0:?}",
                                                                    fn_abi) as &dyn Value))])
                            });
                    } else { ; }
                };
                fn_abi_sanity_check(cx, &fn_abi, abi);
                tcx.arena.alloc(fn_abi)
            }
        }
    }
}#[tracing::instrument(level = "trace", skip(cx))]
680fn fn_abi_adjust_for_deduced_attrs<'tcx>(
681    cx: &LayoutCx<'tcx>,
682    fn_abi: &'tcx FnAbi<'tcx, Ty<'tcx>>,
683    abi: ExternAbi,
684    fn_def_id: DefId,
685) -> &'tcx FnAbi<'tcx, Ty<'tcx>> {
686    let tcx = cx.tcx();
687    // Look up the deduced parameter attributes for this function, if we have its def ID.
688    // We'll tag its parameters with those attributes as appropriate.
689    let deduced = if abi.is_rustic_abi() { tcx.deduced_param_attrs(fn_def_id) } else { &[] };
690    if deduced.is_empty() {
691        fn_abi
692    } else {
693        let mut fn_abi = fn_abi.clone();
694        apply_deduced_attributes(cx, deduced, 0, &mut fn_abi.ret);
695        for (arg_idx, arg) in fn_abi.args.iter_mut().enumerate() {
696            apply_deduced_attributes(cx, deduced, arg_idx + 1, arg);
697        }
698        debug!("fn_abi_adjust_for_deduced_attrs = {:?}", fn_abi);
699        fn_abi_sanity_check(cx, &fn_abi, abi);
700        tcx.arena.alloc(fn_abi)
701    }
702}
703
704/// Apply deduced optimization attributes to a parameter using an indirect pass mode.
705///
706/// `deduced` is a possibly truncated list of deduced attributes for a return place and arguments.
707/// `idx` the index of the parameter on the list (0 for a return place, and 1.. for arguments).
708fn apply_deduced_attributes<'tcx>(
709    cx: &LayoutCx<'tcx>,
710    deduced: &[DeducedParamAttrs],
711    idx: usize,
712    arg: &mut ArgAbi<'tcx, Ty<'tcx>>,
713) {
714    // Deduction is performed under the assumption of the indirection pass mode.
715    let PassMode::Indirect { ref mut attrs, .. } = arg.mode else {
716        return;
717    };
718    // The default values at the tail of the list are not encoded.
719    let Some(deduced) = deduced.get(idx) else {
720        return;
721    };
722    if deduced.read_only(cx.tcx(), cx.typing_env, arg.layout.ty) {
723        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_ty_utils/src/abi.rs:723",
                        "rustc_ty_utils::abi", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_ty_utils/src/abi.rs"),
                        ::tracing_core::__macro_support::Option::Some(723u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_ty_utils::abi"),
                        ::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!("added deduced ReadOnly attribute")
                                            as &dyn Value))])
            });
    } else { ; }
};debug!("added deduced ReadOnly attribute");
724        attrs.regular.insert(ArgAttribute::ReadOnly);
725    }
726    if deduced.captures_none(cx.tcx(), cx.typing_env, arg.layout.ty) {
727        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_ty_utils/src/abi.rs:727",
                        "rustc_ty_utils::abi", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_ty_utils/src/abi.rs"),
                        ::tracing_core::__macro_support::Option::Some(727u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_ty_utils::abi"),
                        ::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!("added deduced CapturesNone attribute")
                                            as &dyn Value))])
            });
    } else { ; }
};debug!("added deduced CapturesNone attribute");
728        attrs.regular.insert(ArgAttribute::CapturesNone);
729    }
730}
731
732#[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("make_thin_self_ptr",
                                    "rustc_ty_utils::abi", ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_ty_utils/src/abi.rs"),
                                    ::tracing_core::__macro_support::Option::Some(732u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_ty_utils::abi"),
                                    ::tracing_core::field::FieldSet::new(&["layout"],
                                        ::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(&layout)
                                                            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: TyAndLayout<'tcx> = loop {};
            return __tracing_attr_fake_return;
        }
        {
            let tcx = cx.tcx();
            let wide_pointer_ty =
                if layout.is_unsized() {
                    Ty::new_mut_ptr(tcx, layout.ty)
                } else {
                    match layout.backend_repr {
                        BackendRepr::ScalarPair(..) | BackendRepr::Scalar(..) => (),
                        _ =>
                            ::rustc_middle::util::bug::bug_fmt(format_args!("receiver type has unsupported layout: {0:?}",
                                    layout)),
                    }
                    let mut wide_pointer_layout = layout;
                    while !wide_pointer_layout.ty.is_raw_ptr() &&
                            !wide_pointer_layout.ty.is_ref() {
                        wide_pointer_layout =
                            wide_pointer_layout.non_1zst_field(cx).expect("not exactly one non-1-ZST field in a `DispatchFromDyn` type").1
                    }
                    wide_pointer_layout.ty
                };
            let unit_ptr_ty = Ty::new_mut_ptr(tcx, tcx.types.unit);
            TyAndLayout {
                ty: wide_pointer_ty,
                ..tcx.layout_of(ty::TypingEnv::fully_monomorphized().as_query_input(unit_ptr_ty)).unwrap()
            }
        }
    }
}#[tracing::instrument(level = "debug", skip(cx))]
733fn make_thin_self_ptr<'tcx>(
734    cx: &(impl HasTyCtxt<'tcx> + HasTypingEnv<'tcx>),
735    layout: TyAndLayout<'tcx>,
736) -> TyAndLayout<'tcx> {
737    let tcx = cx.tcx();
738    let wide_pointer_ty = if layout.is_unsized() {
739        // unsized `self` is passed as a pointer to `self`
740        // FIXME (mikeyhew) change this to use &own if it is ever added to the language
741        Ty::new_mut_ptr(tcx, layout.ty)
742    } else {
743        match layout.backend_repr {
744            BackendRepr::ScalarPair(..) | BackendRepr::Scalar(..) => (),
745            _ => bug!("receiver type has unsupported layout: {:?}", layout),
746        }
747
748        // In the case of Rc<Self>, we need to explicitly pass a *mut RcInner<Self>
749        // with a Scalar (not ScalarPair) ABI. This is a hack that is understood
750        // elsewhere in the compiler as a method on a `dyn Trait`.
751        // To get the type `*mut RcInner<Self>`, we just keep unwrapping newtypes until we
752        // get a built-in pointer type
753        let mut wide_pointer_layout = layout;
754        while !wide_pointer_layout.ty.is_raw_ptr() && !wide_pointer_layout.ty.is_ref() {
755            wide_pointer_layout = wide_pointer_layout
756                .non_1zst_field(cx)
757                .expect("not exactly one non-1-ZST field in a `DispatchFromDyn` type")
758                .1
759        }
760
761        wide_pointer_layout.ty
762    };
763
764    // we now have a type like `*mut RcInner<dyn Trait>`
765    // change its layout to that of `*mut ()`, a thin pointer, but keep the same type
766    // this is understood as a special case elsewhere in the compiler
767    let unit_ptr_ty = Ty::new_mut_ptr(tcx, tcx.types.unit);
768
769    TyAndLayout {
770        ty: wide_pointer_ty,
771
772        // NOTE(eddyb) using an empty `ParamEnv`, and `unwrap`-ing the `Result`
773        // should always work because the type is always `*mut ()`.
774        ..tcx.layout_of(ty::TypingEnv::fully_monomorphized().as_query_input(unit_ptr_ty)).unwrap()
775    }
776}