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, 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#[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::ThreadLocalShim(..) = 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::VTableShim(..) = 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::ConstructCoroutineInClosureShim {
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::ThreadLocalShim(..) = 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 if let ty::InstanceKind::VTableShim(..) = 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 let mut coroutine_kind = args.as_coroutine_closure().kind();
84
85 let env_ty =
86 if let InstanceKind::ConstructCoroutineInClosureShim { receiver_by_ref, .. } =
87 instance.def
88 {
89 coroutine_kind = ty::ClosureKind::FnOnce;
90
91 if receiver_by_ref {
94 Ty::new_imm_ref(tcx, tcx.lifetimes.re_erased, coroutine_ty)
95 } else {
96 coroutine_ty
97 }
98 } else {
99 tcx.closure_env_ty(coroutine_ty, coroutine_kind, tcx.lifetimes.re_erased)
100 };
101
102 let sig = tcx.instantiate_bound_regions_with_erased(sig);
103
104 tcx.mk_fn_sig(
105 iter::once(env_ty).chain([sig.tupled_inputs_ty]),
106 sig.to_coroutine_given_kind_and_upvars(
107 tcx,
108 args.as_coroutine_closure().parent_args(),
109 tcx.coroutine_for_closure(def_id),
110 coroutine_kind,
111 tcx.lifetimes.re_erased,
112 args.as_coroutine_closure().tupled_upvars_ty(),
113 args.as_coroutine_closure().coroutine_captures_by_ref_ty(),
114 ),
115 sig.fn_sig_kind,
116 )
117 }
118 ty::Coroutine(did, args) => {
119 let coroutine_kind = tcx.coroutine_kind(did).unwrap();
120 let sig = args.as_coroutine().sig();
121
122 let env_ty = Ty::new_mut_ref(tcx, tcx.lifetimes.re_erased, ty);
123
124 let pin_did = tcx.require_lang_item(LangItem::Pin, DUMMY_SP);
125 let pin_adt_ref = tcx.adt_def(pin_did);
126 let pin_args = tcx.mk_args(&[env_ty.into()]);
127 let env_ty = match coroutine_kind {
128 hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Gen, _) => {
129 env_ty
132 }
133 hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Async, _)
134 | hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::AsyncGen, _)
135 | hir::CoroutineKind::Coroutine(_) => Ty::new_adt(tcx, pin_adt_ref, pin_args),
136 };
137
138 let (resume_ty, ret_ty) = match coroutine_kind {
145 hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Async, _) => {
146 assert_eq!(sig.yield_ty, tcx.types.unit);
148
149 let poll_did = tcx.require_lang_item(LangItem::Poll, DUMMY_SP);
150 let poll_adt_ref = tcx.adt_def(poll_did);
151 let poll_args = tcx.mk_args(&[sig.return_ty.into()]);
152 let ret_ty = Ty::new_adt(tcx, poll_adt_ref, poll_args);
153
154 #[cfg(debug_assertions)]
157 {
158 if let ty::Adt(resume_ty_adt, _) = sig.resume_ty.kind() {
159 let expected_adt =
160 tcx.adt_def(tcx.require_lang_item(LangItem::ResumeTy, DUMMY_SP));
161 assert_eq!(*resume_ty_adt, expected_adt);
162 } else {
163 panic!("expected `ResumeTy`, found `{:?}`", sig.resume_ty);
164 };
165 }
166 let context_mut_ref = Ty::new_task_context(tcx);
167
168 (Some(context_mut_ref), ret_ty)
169 }
170 hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Gen, _) => {
171 let option_did = tcx.require_lang_item(LangItem::Option, DUMMY_SP);
173 let option_adt_ref = tcx.adt_def(option_did);
174 let option_args = tcx.mk_args(&[sig.yield_ty.into()]);
175 let ret_ty = Ty::new_adt(tcx, option_adt_ref, option_args);
176
177 assert_eq!(sig.return_ty, tcx.types.unit);
178 assert_eq!(sig.resume_ty, tcx.types.unit);
179
180 (None, ret_ty)
181 }
182 hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::AsyncGen, _) => {
183 assert_eq!(sig.return_ty, tcx.types.unit);
186
187 let ret_ty = sig.yield_ty;
189
190 #[cfg(debug_assertions)]
193 {
194 if let ty::Adt(resume_ty_adt, _) = sig.resume_ty.kind() {
195 let expected_adt =
196 tcx.adt_def(tcx.require_lang_item(LangItem::ResumeTy, DUMMY_SP));
197 assert_eq!(*resume_ty_adt, expected_adt);
198 } else {
199 panic!("expected `ResumeTy`, found `{:?}`", sig.resume_ty);
200 };
201 }
202 let context_mut_ref = Ty::new_task_context(tcx);
203
204 (Some(context_mut_ref), ret_ty)
205 }
206 hir::CoroutineKind::Coroutine(_) => {
207 let state_did = tcx.require_lang_item(LangItem::CoroutineState, DUMMY_SP);
209 let state_adt_ref = tcx.adt_def(state_did);
210 let state_args = tcx.mk_args(&[sig.yield_ty.into(), sig.return_ty.into()]);
211 let ret_ty = Ty::new_adt(tcx, state_adt_ref, state_args);
212
213 (Some(sig.resume_ty), ret_ty)
214 }
215 };
216
217 if let Some(resume_ty) = resume_ty {
218 tcx.mk_fn_sig_safe_rust_abi([env_ty, resume_ty], ret_ty)
219 } else {
220 tcx.mk_fn_sig_safe_rust_abi([env_ty], ret_ty)
222 }
223 }
224 _ => bug!("unexpected type {:?} in Instance::fn_sig", ty),
225 }
226}
227
228struct FnAbiDesc<'tcx> {
230 layout_cx: LayoutCx<'tcx>,
231 sig: ty::FnSig<'tcx>,
232
233 determined_fn_def_id: Option<DefId>,
235 caller_location: Option<Ty<'tcx>>,
236 is_virtual_call: bool,
237 extra_args: &'tcx [Ty<'tcx>],
238}
239
240impl<'tcx> FnAbiDesc<'tcx> {
241 fn for_fn_ptr(
242 tcx: TyCtxt<'tcx>,
243 query: ty::PseudoCanonicalInput<'tcx, (ty::PolyFnSig<'tcx>, &'tcx ty::List<Ty<'tcx>>)>,
244 ) -> Self {
245 let ty::PseudoCanonicalInput { typing_env, value: (sig, extra_args) } = query;
246 Self {
247 layout_cx: LayoutCx::new(tcx, typing_env),
248 sig: tcx.normalize_erasing_regions(
249 typing_env,
250 Unnormalized::new_wip(tcx.instantiate_bound_regions_with_erased(sig)),
251 ),
252 determined_fn_def_id: None,
255 caller_location: None,
256 is_virtual_call: false,
257 extra_args,
258 }
259 }
260
261 fn for_instance(
262 tcx: TyCtxt<'tcx>,
263 query: ty::PseudoCanonicalInput<'tcx, (ty::Instance<'tcx>, &'tcx ty::List<Ty<'tcx>>)>,
264 ) -> Self {
265 let ty::PseudoCanonicalInput { typing_env, value: (instance, extra_args) } = query;
266 let is_virtual_call = #[allow(non_exhaustive_omitted_patterns)] match instance.def {
ty::InstanceKind::Virtual(..) => true,
_ => false,
}matches!(instance.def, ty::InstanceKind::Virtual(..));
267 let is_tls_shim_call = #[allow(non_exhaustive_omitted_patterns)] match instance.def {
ty::InstanceKind::ThreadLocalShim(_) => true,
_ => false,
}matches!(instance.def, ty::InstanceKind::ThreadLocalShim(_));
268 Self {
269 layout_cx: LayoutCx::new(tcx, typing_env),
270 sig: tcx.normalize_erasing_regions(
271 typing_env,
272 Unnormalized::new_wip(fn_sig_for_fn_abi(tcx, instance, typing_env)),
273 ),
274 determined_fn_def_id: (!is_virtual_call && !is_tls_shim_call)
278 .then(|| instance.def_id()),
279 caller_location: instance
280 .def
281 .requires_caller_location(tcx)
282 .then(|| tcx.caller_location_ty()),
283 is_virtual_call,
284 extra_args,
285 }
286 }
287}
288
289fn fn_abi_of_fn_ptr<'tcx>(
290 tcx: TyCtxt<'tcx>,
291 query: ty::PseudoCanonicalInput<'tcx, (ty::PolyFnSig<'tcx>, &'tcx ty::List<Ty<'tcx>>)>,
292) -> Result<&'tcx FnAbi<'tcx, Ty<'tcx>>, &'tcx FnAbiError<'tcx>> {
293 let desc = FnAbiDesc::for_fn_ptr(tcx, query);
294 fn_abi_new_uncached(desc)
295}
296
297fn fn_abi_of_instance_no_deduced_attrs<'tcx>(
298 tcx: TyCtxt<'tcx>,
299 query: ty::PseudoCanonicalInput<'tcx, (ty::Instance<'tcx>, &'tcx ty::List<Ty<'tcx>>)>,
300) -> Result<&'tcx FnAbi<'tcx, Ty<'tcx>>, &'tcx FnAbiError<'tcx>> {
301 let desc = FnAbiDesc::for_instance(tcx, query);
302 fn_abi_new_uncached(desc)
303}
304
305fn fn_abi_of_instance_raw<'tcx>(
306 tcx: TyCtxt<'tcx>,
307 query: ty::PseudoCanonicalInput<'tcx, (ty::Instance<'tcx>, &'tcx ty::List<Ty<'tcx>>)>,
308) -> Result<&'tcx FnAbi<'tcx, Ty<'tcx>>, &'tcx FnAbiError<'tcx>> {
309 tcx.fn_abi_of_instance_no_deduced_attrs(query).map(|fn_abi| {
312 let params = FnAbiDesc::for_instance(tcx, query);
313 params.determined_fn_def_id.map_or(fn_abi, |fn_def_id| {
316 fn_abi_adjust_for_deduced_attrs(¶ms.layout_cx, fn_abi, params.sig.abi(), fn_def_id)
317 })
318 })
319}
320
321fn arg_attrs_for_rust_scalar<'tcx>(
323 cx: LayoutCx<'tcx>,
324 scalar: Scalar,
325 layout: TyAndLayout<'tcx>,
326 offset: Size,
327 is_return: bool,
328 determined_fn_def_id: Option<DefId>,
329) -> ArgAttributes {
330 let mut attrs = ArgAttributes::new();
331
332 if scalar.is_bool() {
334 attrs.ext(ArgExtension::Zext);
335 attrs.set(ArgAttribute::NoUndef);
336 return attrs;
337 }
338
339 if !scalar.is_uninit_valid() {
340 attrs.set(ArgAttribute::NoUndef);
341 }
342
343 let Scalar::Initialized { value: Pointer(_), valid_range } = scalar else { return attrs };
345
346 if !valid_range.contains(0) {
348 attrs.set(ArgAttribute::NonNull);
349 }
350
351 let tcx = cx.tcx();
352
353 if let Some(pointee) = layout.pointee_info_at(&cx, offset) {
354 if pointee.align > Align::ONE {
355 attrs.pointee_align =
356 Some(pointee.align.min(cx.tcx().sess.target.max_reliable_alignment()));
357 }
358
359 attrs.pointee_size = pointee.size;
360
361 if let Some(kind) = pointee.safe {
362 let noalias_for_box = tcx.sess.opts.unstable_opts.box_noalias;
366
367 let noalias_mut_ref = tcx.sess.opts.unstable_opts.mutable_noalias;
371
372 let no_alias = match kind {
379 PointerKind::SharedRef { frozen } => frozen,
380 PointerKind::MutableRef { unpin } => unpin && noalias_mut_ref,
381 PointerKind::Box { unpin, global } => unpin && global && noalias_for_box,
382 };
383 if no_alias && !is_return {
386 attrs.set(ArgAttribute::NoAlias);
387 }
388
389 if tcx.sess.opts.unstable_opts.llvm_writable
391 && #[allow(non_exhaustive_omitted_patterns)] match kind {
PointerKind::MutableRef { unpin: true } => true,
_ => false,
}matches!(kind, PointerKind::MutableRef { unpin: true })
392 && !is_return
393 {
394 let rustc_no_writable = match determined_fn_def_id {
395 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),
396 None => true, };
398
399 if !rustc_no_writable {
400 attrs.set(ArgAttribute::Writable);
401 }
402 }
403
404 let no_free = !is_return
408 && match kind {
409 PointerKind::SharedRef { frozen } => frozen,
413 PointerKind::MutableRef { unpin } => unpin,
417 PointerKind::Box { .. } => false,
419 };
420 if no_free {
421 attrs.set(ArgAttribute::NoFree);
422 }
423
424 if #[allow(non_exhaustive_omitted_patterns)] match kind {
PointerKind::SharedRef { frozen: true } => true,
_ => false,
}matches!(kind, PointerKind::SharedRef { frozen: true }) && !is_return {
425 attrs.set(ArgAttribute::ReadOnly);
426 attrs.set(ArgAttribute::CapturesReadOnly);
427 }
428 }
429 }
430
431 attrs
432}
433
434fn fn_abi_sanity_check<'tcx>(
436 cx: &LayoutCx<'tcx>,
437 fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
438 spec_abi: ExternAbi,
439) {
440 fn fn_arg_attrs_sanity_check(attrs: &ArgAttributes, is_ret: bool) {
441 if attrs.regular.contains(ArgAttribute::NoFree) {
442 if !!is_ret {
{
::core::panicking::panic_fmt(format_args!("NoFree not valid on return values"));
}
};assert!(!is_ret, "NoFree not valid on return values");
443 }
444 }
445
446 fn fn_arg_sanity_check<'tcx>(
447 cx: &LayoutCx<'tcx>,
448 fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
449 spec_abi: ExternAbi,
450 arg: &ArgAbi<'tcx, Ty<'tcx>>,
451 is_ret: bool,
452 ) {
453 let tcx = cx.tcx();
454
455 if spec_abi.is_rustic_abi() {
456 if arg.layout.is_zst() {
457 if !arg.is_ignore() {
::core::panicking::panic("assertion failed: arg.is_ignore()")
};assert!(arg.is_ignore());
460 }
461 if let PassMode::Indirect { on_stack, .. } = arg.mode {
462 if !!on_stack {
{
::core::panicking::panic_fmt(format_args!("rust abi shouldn\'t use on_stack"));
}
};assert!(!on_stack, "rust abi shouldn't use on_stack");
463 }
464 } else if arg.layout.pass_indirectly_in_non_rustic_abis(cx) {
465 {
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!(
466 arg.mode,
467 PassMode::Indirect { on_stack: false, .. },
468 "the {spec_abi} ABI does not implement `#[rustc_pass_indirectly_in_non_rustic_abis]`"
469 );
470 }
471
472 match &arg.mode {
473 PassMode::Ignore => {
474 if !arg.layout.is_zst() {
::core::panicking::panic("assertion failed: arg.layout.is_zst()")
};assert!(arg.layout.is_zst());
475 }
476 PassMode::Direct(attrs) => {
477 match arg.layout.backend_repr {
482 BackendRepr::Scalar(_)
483 | BackendRepr::SimdVector { .. }
484 | BackendRepr::SimdScalableVector { .. } => {}
485 BackendRepr::ScalarPair(..) => {
486 {
::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)
487 }
488 BackendRepr::Memory { sized } => {
489 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);
492
493 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!(
499 matches!(spec_abi, ExternAbi::Unadjusted),
500 "`PassMode::Direct` for aggregates only allowed for \"unadjusted\"\n\
501 Problematic type: {:#?}",
502 arg.layout,
503 );
504 }
505 }
506 fn_arg_attrs_sanity_check(attrs, is_ret);
507 }
508 PassMode::Pair(attrs1, attrs2) => {
509 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!(
512 matches!(arg.layout.backend_repr, BackendRepr::ScalarPair(..)),
513 "PassMode::Pair for type {}",
514 arg.layout.ty
515 );
516 fn_arg_attrs_sanity_check(attrs1, is_ret);
517 fn_arg_attrs_sanity_check(attrs2, is_ret);
518 }
519 PassMode::Cast { .. } => {
520 if !arg.layout.is_sized() {
::core::panicking::panic("assertion failed: arg.layout.is_sized()")
};assert!(arg.layout.is_sized());
522 }
523 PassMode::Indirect { meta_attrs: None, attrs, .. } => {
524 if !arg.layout.is_sized() {
::core::panicking::panic("assertion failed: arg.layout.is_sized()")
};assert!(arg.layout.is_sized());
529 fn_arg_attrs_sanity_check(attrs, false);
531 }
532 PassMode::Indirect { meta_attrs: Some(meta_attrs), attrs, on_stack } => {
533 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);
535 let tail = tcx.struct_tail_for_codegen(arg.layout.ty, cx.typing_env);
537 if #[allow(non_exhaustive_omitted_patterns)] match tail.kind() {
ty::Foreign(..) => true,
_ => false,
}matches!(tail.kind(), ty::Foreign(..)) {
538 {
::core::panicking::panic_fmt(format_args!("unsized arguments must not be `extern` types"));
};panic!("unsized arguments must not be `extern` types");
543 }
544 fn_arg_attrs_sanity_check(attrs, false);
546 fn_arg_attrs_sanity_check(meta_attrs, false);
547 }
548 }
549 }
550
551 for arg in fn_abi.args.iter() {
552 fn_arg_sanity_check(cx, fn_abi, spec_abi, arg, false);
553 }
554 fn_arg_sanity_check(cx, fn_abi, spec_abi, &fn_abi.ret, true);
555}
556
557#[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(557u32),
::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(597u32),
::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:638",
"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(638u32),
::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(
558 level = "debug",
559 skip(cx, caller_location, determined_fn_def_id, is_virtual_call)
560)]
561fn fn_abi_new_uncached<'tcx>(
562 FnAbiDesc {
563 layout_cx: ref cx,
564 sig,
565 determined_fn_def_id,
566 caller_location,
567 is_virtual_call,
568 extra_args,
569 }: FnAbiDesc<'tcx>,
570) -> Result<&'tcx FnAbi<'tcx, Ty<'tcx>>, &'tcx FnAbiError<'tcx>> {
571 let tcx = cx.tcx();
572
573 let abi_map = AbiMap::from_target(&tcx.sess.target);
574 let conv = abi_map.canonize_abi(sig.abi(), sig.c_variadic()).unwrap();
575
576 let mut inputs = sig.inputs();
577 let extra_args = if sig.abi() == ExternAbi::RustCall {
578 assert!(!sig.c_variadic() && extra_args.is_empty());
579
580 if let Some(input) = sig.inputs().last()
581 && let ty::Tuple(tupled_arguments) = input.kind()
582 {
583 inputs = &sig.inputs()[0..sig.inputs().len() - 1];
584 tupled_arguments
585 } else {
586 bug!(
587 "argument to function with \"rust-call\" ABI \
588 is not a tuple"
589 );
590 }
591 } else {
592 assert!(sig.c_variadic() || extra_args.is_empty());
593 extra_args
594 };
595
596 let arg_of = |ty: Ty<'tcx>, arg_idx: Option<usize>| -> Result<_, &'tcx FnAbiError<'tcx>> {
597 let span = tracing::debug_span!("arg_of");
598 let _entered = span.enter();
599 let is_return = arg_idx.is_none();
600
601 let layout = cx.layout_of(ty).map_err(|err| &*tcx.arena.alloc(FnAbiError::Layout(*err)))?;
602 let layout = if is_virtual_call && arg_idx == Some(0) {
603 make_thin_self_ptr(cx, layout)
607 } else {
608 layout
609 };
610
611 Ok(ArgAbi::new(cx, layout, |scalar, offset| {
612 arg_attrs_for_rust_scalar(*cx, scalar, layout, offset, is_return, determined_fn_def_id)
613 }))
614 };
615
616 let mut fn_abi = FnAbi {
617 ret: arg_of(sig.output(), None)?,
618 args: inputs
619 .iter()
620 .copied()
621 .chain(extra_args.iter().copied())
622 .chain(caller_location)
623 .enumerate()
624 .map(|(i, ty)| arg_of(ty, Some(i)))
625 .collect::<Result<_, _>>()?,
626 c_variadic: sig.c_variadic(),
627 fixed_count: inputs.len() as u32,
628 conv,
629 can_unwind: fn_can_unwind(
631 tcx,
632 determined_fn_def_id,
634 sig.abi(),
635 ),
636 };
637 fn_abi_adjust_for_abi(cx, &mut fn_abi, sig.abi());
638 debug!("fn_abi_new_uncached = {:?}", fn_abi);
639 fn_abi_sanity_check(cx, &fn_abi, sig.abi());
640 Ok(tcx.arena.alloc(fn_abi))
641}
642
643#[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(643u32),
::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))]
644fn fn_abi_adjust_for_abi<'tcx>(
645 cx: &LayoutCx<'tcx>,
646 fn_abi: &mut FnAbi<'tcx, Ty<'tcx>>,
647 abi: ExternAbi,
648) {
649 if abi == ExternAbi::Unadjusted {
650 fn unadjust<'tcx>(arg: &mut ArgAbi<'tcx, Ty<'tcx>>) {
653 if matches!(arg.layout.backend_repr, BackendRepr::Memory { .. }) {
656 assert!(
657 arg.layout.backend_repr.is_sized(),
658 "'unadjusted' ABI does not support unsized arguments"
659 );
660 }
661 arg.make_direct_deprecated();
662 }
663
664 unadjust(&mut fn_abi.ret);
665 for arg in fn_abi.args.iter_mut() {
666 unadjust(arg);
667 }
668 } else if abi.is_rustic_abi() {
669 fn_abi.adjust_for_rust_abi(cx);
670 } else {
671 fn_abi.adjust_for_foreign_abi(cx, abi);
672 }
673}
674
675#[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(675u32),
::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:694",
"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(694u32),
::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))]
676fn fn_abi_adjust_for_deduced_attrs<'tcx>(
677 cx: &LayoutCx<'tcx>,
678 fn_abi: &'tcx FnAbi<'tcx, Ty<'tcx>>,
679 abi: ExternAbi,
680 fn_def_id: DefId,
681) -> &'tcx FnAbi<'tcx, Ty<'tcx>> {
682 let tcx = cx.tcx();
683 let deduced = if abi.is_rustic_abi() { tcx.deduced_param_attrs(fn_def_id) } else { &[] };
686 if deduced.is_empty() {
687 fn_abi
688 } else {
689 let mut fn_abi = fn_abi.clone();
690 apply_deduced_attributes(cx, deduced, 0, &mut fn_abi.ret);
691 for (arg_idx, arg) in fn_abi.args.iter_mut().enumerate() {
692 apply_deduced_attributes(cx, deduced, arg_idx + 1, arg);
693 }
694 debug!("fn_abi_adjust_for_deduced_attrs = {:?}", fn_abi);
695 fn_abi_sanity_check(cx, &fn_abi, abi);
696 tcx.arena.alloc(fn_abi)
697 }
698}
699
700fn apply_deduced_attributes<'tcx>(
705 cx: &LayoutCx<'tcx>,
706 deduced: &[DeducedParamAttrs],
707 idx: usize,
708 arg: &mut ArgAbi<'tcx, Ty<'tcx>>,
709) {
710 let PassMode::Indirect { ref mut attrs, .. } = arg.mode else {
712 return;
713 };
714 let Some(deduced) = deduced.get(idx) else {
716 return;
717 };
718 if deduced.read_only(cx.tcx(), cx.typing_env, arg.layout.ty) {
719 {
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:719",
"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(719u32),
::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");
720 attrs.regular.insert(ArgAttribute::ReadOnly);
721 }
722 if deduced.captures_none(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 CapturesNone attribute")
as &dyn Value))])
});
} else { ; }
};debug!("added deduced CapturesNone attribute");
724 attrs.regular.insert(ArgAttribute::CapturesNone);
725 }
726}
727
728#[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(728u32),
::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))]
729fn make_thin_self_ptr<'tcx>(
730 cx: &(impl HasTyCtxt<'tcx> + HasTypingEnv<'tcx>),
731 layout: TyAndLayout<'tcx>,
732) -> TyAndLayout<'tcx> {
733 let tcx = cx.tcx();
734 let wide_pointer_ty = if layout.is_unsized() {
735 Ty::new_mut_ptr(tcx, layout.ty)
738 } else {
739 match layout.backend_repr {
740 BackendRepr::ScalarPair(..) | BackendRepr::Scalar(..) => (),
741 _ => bug!("receiver type has unsupported layout: {:?}", layout),
742 }
743
744 let mut wide_pointer_layout = layout;
750 while !wide_pointer_layout.ty.is_raw_ptr() && !wide_pointer_layout.ty.is_ref() {
751 wide_pointer_layout = wide_pointer_layout
752 .non_1zst_field(cx)
753 .expect("not exactly one non-1-ZST field in a `DispatchFromDyn` type")
754 .1
755 }
756
757 wide_pointer_layout.ty
758 };
759
760 let unit_ptr_ty = Ty::new_mut_ptr(tcx, tcx.types.unit);
764
765 TyAndLayout {
766 ty: wide_pointer_ty,
767
768 ..tcx.layout_of(ty::TypingEnv::fully_monomorphized().as_query_input(unit_ptr_ty)).unwrap()
771 }
772}