Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 849b4c4

Browse files
committed
ty::layout: replicate layout_of setup for fn_abi_of_{fn_ptr,instance}.
1 parent 1b8e830 commit 849b4c4

File tree

5 files changed

+65
-16
lines changed

5 files changed

+65
-16
lines changed

src/abi/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ mod pass_mode;
55
mod returning;
66

77
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
8-
use rustc_middle::ty::layout::FnAbiExt;
8+
use rustc_middle::ty::layout::FnAbiOf;
99
use rustc_target::abi::call::{Conv, FnAbi};
1010
use rustc_target::spec::abi::Abi;
1111

@@ -53,7 +53,7 @@ pub(crate) fn get_function_sig<'tcx>(
5353
inst: Instance<'tcx>,
5454
) -> Signature {
5555
assert!(!inst.substs.needs_infer());
56-
clif_sig_from_fn_abi(tcx, triple, &FnAbi::of_instance(&RevealAllLayoutCx(tcx), inst, &[]))
56+
clif_sig_from_fn_abi(tcx, triple, &RevealAllLayoutCx(tcx).fn_abi_of_instance(inst, &[]))
5757
}
5858

5959
/// Instance must be monomorphized
@@ -355,9 +355,9 @@ pub(crate) fn codegen_terminator_call<'tcx>(
355355
.map(|op_arg| fx.monomorphize(op_arg.ty(fx.mir, fx.tcx)))
356356
.collect::<Vec<_>>();
357357
let fn_abi = if let Some(instance) = instance {
358-
FnAbi::of_instance(&RevealAllLayoutCx(fx.tcx), instance, &extra_args)
358+
RevealAllLayoutCx(fx.tcx).fn_abi_of_instance(instance, &extra_args)
359359
} else {
360-
FnAbi::of_fn_ptr(&RevealAllLayoutCx(fx.tcx), fn_ty.fn_sig(fx.tcx), &extra_args)
360+
RevealAllLayoutCx(fx.tcx).fn_abi_of_fn_ptr(fn_ty.fn_sig(fx.tcx), &extra_args)
361361
};
362362

363363
let is_cold = instance
@@ -525,7 +525,7 @@ pub(crate) fn codegen_drop<'tcx>(
525525
def: ty::InstanceDef::Virtual(drop_instance.def_id(), 0),
526526
substs: drop_instance.substs,
527527
};
528-
let fn_abi = FnAbi::of_instance(&RevealAllLayoutCx(fx.tcx), virtual_drop, &[]);
528+
let fn_abi = RevealAllLayoutCx(fx.tcx).fn_abi_of_instance(virtual_drop, &[]);
529529

530530
let sig = clif_sig_from_fn_abi(fx.tcx, fx.triple(), &fn_abi);
531531
let sig = fx.bcx.import_signature(sig);
@@ -534,7 +534,7 @@ pub(crate) fn codegen_drop<'tcx>(
534534
_ => {
535535
assert!(!matches!(drop_instance.def, InstanceDef::Virtual(_, _)));
536536

537-
let fn_abi = FnAbi::of_instance(&RevealAllLayoutCx(fx.tcx), drop_instance, &[]);
537+
let fn_abi = RevealAllLayoutCx(fx.tcx).fn_abi_of_instance(drop_instance, &[]);
538538

539539
let arg_value = drop_place.place_ref(
540540
fx,

src/base.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
use cranelift_codegen::binemit::{NullStackMapSink, NullTrapSink};
44
use rustc_index::vec::IndexVec;
55
use rustc_middle::ty::adjustment::PointerCast;
6-
use rustc_middle::ty::layout::FnAbiExt;
7-
use rustc_target::abi::call::FnAbi;
6+
use rustc_middle::ty::layout::FnAbiOf;
87

98
use crate::constant::ConstantCx;
109
use crate::prelude::*;
@@ -62,7 +61,7 @@ pub(crate) fn codegen_fn<'tcx>(
6261
instance,
6362
symbol_name,
6463
mir,
65-
fn_abi: Some(FnAbi::of_instance(&RevealAllLayoutCx(tcx), instance, &[])),
64+
fn_abi: Some(RevealAllLayoutCx(tcx).fn_abi_of_instance(instance, &[])),
6665

6766
bcx,
6867
block_map,

src/common.rs

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use rustc_index::vec::IndexVec;
2-
use rustc_middle::ty::layout::{LayoutError, LayoutOfHelpers};
2+
use rustc_middle::ty::layout::{
3+
FnAbiError, FnAbiOfHelpers, FnAbiRequest, LayoutError, LayoutOfHelpers,
4+
};
35
use rustc_middle::ty::SymbolName;
46
use rustc_target::abi::call::FnAbi;
57
use rustc_target::abi::{Integer, Primitive};
@@ -266,6 +268,20 @@ impl<'tcx> LayoutOfHelpers<'tcx> for FunctionCx<'_, '_, 'tcx> {
266268
}
267269
}
268270

271+
impl<'tcx> FnAbiOfHelpers<'tcx> for FunctionCx<'_, '_, 'tcx> {
272+
type FnAbiOfResult = &'tcx FnAbi<'tcx, Ty<'tcx>>;
273+
274+
#[inline]
275+
fn handle_fn_abi_err(
276+
&self,
277+
err: FnAbiError<'tcx>,
278+
span: Span,
279+
fn_abi_request: FnAbiRequest<'_, 'tcx>,
280+
) -> ! {
281+
RevealAllLayoutCx(self.tcx).handle_fn_abi_err(err, span, fn_abi_request)
282+
}
283+
}
284+
269285
impl<'tcx> layout::HasTyCtxt<'tcx> for FunctionCx<'_, '_, 'tcx> {
270286
fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
271287
self.tcx
@@ -378,6 +394,43 @@ impl<'tcx> LayoutOfHelpers<'tcx> for RevealAllLayoutCx<'tcx> {
378394
}
379395
}
380396

397+
impl<'tcx> FnAbiOfHelpers<'tcx> for RevealAllLayoutCx<'tcx> {
398+
type FnAbiOfResult = &'tcx FnAbi<'tcx, Ty<'tcx>>;
399+
400+
#[inline]
401+
fn handle_fn_abi_err(
402+
&self,
403+
err: FnAbiError<'tcx>,
404+
span: Span,
405+
fn_abi_request: FnAbiRequest<'_, 'tcx>,
406+
) -> ! {
407+
if let FnAbiError::Layout(LayoutError::SizeOverflow(_)) = err {
408+
self.0.sess.span_fatal(span, &err.to_string())
409+
} else {
410+
match fn_abi_request {
411+
FnAbiRequest::OfFnPtr { sig, extra_args } => {
412+
span_bug!(
413+
span,
414+
"`fn_abi_of_fn_ptr({}, {:?})` failed: {}",
415+
sig,
416+
extra_args,
417+
err
418+
);
419+
}
420+
FnAbiRequest::OfInstance { instance, extra_args } => {
421+
span_bug!(
422+
span,
423+
"`fn_abi_of_instance({}, {:?})` failed: {}",
424+
instance,
425+
extra_args,
426+
err
427+
);
428+
}
429+
}
430+
}
431+
}
432+
}
433+
381434
impl<'tcx> layout::HasTyCtxt<'tcx> for RevealAllLayoutCx<'tcx> {
382435
fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
383436
self.0

src/constant.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,7 @@ pub(crate) fn codegen_constant<'tcx>(
129129
};
130130
let const_val = match const_.val {
131131
ConstKind::Value(const_val) => const_val,
132-
ConstKind::Unevaluated(uv)
133-
if fx.tcx.is_static(uv.def.did) =>
134-
{
132+
ConstKind::Unevaluated(uv) if fx.tcx.is_static(uv.def.did) => {
135133
assert!(uv.substs(fx.tcx).is_empty());
136134
assert!(uv.promoted.is_none());
137135

src/pretty_clif.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,8 @@ use cranelift_codegen::{
6161
write::{FuncWriter, PlainWriter},
6262
};
6363

64-
use rustc_middle::ty::layout::FnAbiExt;
64+
use rustc_middle::ty::layout::FnAbiOf;
6565
use rustc_session::config::OutputType;
66-
use rustc_target::abi::call::FnAbi;
6766

6867
use crate::prelude::*;
6968

@@ -81,7 +80,7 @@ impl CommentWriter {
8180
vec![
8281
format!("symbol {}", tcx.symbol_name(instance).name),
8382
format!("instance {:?}", instance),
84-
format!("abi {:?}", FnAbi::of_instance(&RevealAllLayoutCx(tcx), instance, &[])),
83+
format!("abi {:?}", RevealAllLayoutCx(tcx).fn_abi_of_instance(instance, &[])),
8584
String::new(),
8685
]
8786
} else {

0 commit comments

Comments
 (0)