Skip to content

Commit 4be8477

Browse files
committed
Put FnAbiError behind reference to shrink result
The `FnAbi` is just a pointer, so the error type should not be bigger.
1 parent 3019c1c commit 4be8477

File tree

4 files changed

+20
-25
lines changed

4 files changed

+20
-25
lines changed

compiler/rustc_middle/src/query/erase.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ impl<T> EraseType for Result<&'_ T, traits::CodegenObligationError> {
7171
type Result = [u8; size_of::<Result<&'static (), traits::CodegenObligationError>>()];
7272
}
7373

74-
impl<T> EraseType for Result<&'_ T, ty::layout::FnAbiError<'_>> {
75-
type Result = [u8; size_of::<Result<&'static (), ty::layout::FnAbiError<'static>>>()];
74+
impl<T> EraseType for Result<&'_ T, &'_ ty::layout::FnAbiError<'_>> {
75+
type Result = [u8; size_of::<Result<&'static (), &'static ty::layout::FnAbiError<'static>>>()];
7676
}
7777

7878
impl<T> EraseType for Result<(&'_ T, rustc_middle::thir::ExprId), rustc_errors::ErrorGuaranteed> {

compiler/rustc_middle/src/query/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1394,7 +1394,7 @@ rustc_queries! {
13941394
/// instead, where the instance is an `InstanceDef::Virtual`.
13951395
query fn_abi_of_fn_ptr(
13961396
key: ty::ParamEnvAnd<'tcx, (ty::PolyFnSig<'tcx>, &'tcx ty::List<Ty<'tcx>>)>
1397-
) -> Result<&'tcx abi::call::FnAbi<'tcx, Ty<'tcx>>, ty::layout::FnAbiError<'tcx>> {
1397+
) -> Result<&'tcx abi::call::FnAbi<'tcx, Ty<'tcx>>, &'tcx ty::layout::FnAbiError<'tcx>> {
13981398
desc { "computing call ABI of `{}` function pointers", key.value.0 }
13991399
}
14001400

@@ -1405,7 +1405,7 @@ rustc_queries! {
14051405
/// to an `InstanceDef::Virtual` instance (of `<dyn Trait as Trait>::fn`).
14061406
query fn_abi_of_instance(
14071407
key: ty::ParamEnvAnd<'tcx, (ty::Instance<'tcx>, &'tcx ty::List<Ty<'tcx>>)>
1408-
) -> Result<&'tcx abi::call::FnAbi<'tcx, Ty<'tcx>>, ty::layout::FnAbiError<'tcx>> {
1408+
) -> Result<&'tcx abi::call::FnAbi<'tcx, Ty<'tcx>>, &'tcx ty::layout::FnAbiError<'tcx>> {
14091409
desc { "computing call ABI of `{}`", key.value.0 }
14101410
}
14111411

compiler/rustc_middle/src/ty/layout.rs

+6-14
Original file line numberDiff line numberDiff line change
@@ -1260,18 +1260,6 @@ pub enum FnAbiError<'tcx> {
12601260
AdjustForForeignAbi(call::AdjustForForeignAbiError),
12611261
}
12621262

1263-
impl<'tcx> From<LayoutError<'tcx>> for FnAbiError<'tcx> {
1264-
fn from(err: LayoutError<'tcx>) -> Self {
1265-
Self::Layout(err)
1266-
}
1267-
}
1268-
1269-
impl From<call::AdjustForForeignAbiError> for FnAbiError<'_> {
1270-
fn from(err: call::AdjustForForeignAbiError) -> Self {
1271-
Self::AdjustForForeignAbi(err)
1272-
}
1273-
}
1274-
12751263
impl<'a, 'b> IntoDiagnostic<'a, !> for FnAbiError<'b> {
12761264
fn into_diagnostic(self, handler: &'a Handler) -> DiagnosticBuilder<'a, !> {
12771265
match self {
@@ -1331,7 +1319,7 @@ pub trait FnAbiOf<'tcx>: FnAbiOfHelpers<'tcx> {
13311319
let tcx = self.tcx().at(span);
13321320

13331321
MaybeResult::from(tcx.fn_abi_of_fn_ptr(self.param_env().and((sig, extra_args))).map_err(
1334-
|err| self.handle_fn_abi_err(err, span, FnAbiRequest::OfFnPtr { sig, extra_args }),
1322+
|err| self.handle_fn_abi_err(*err, span, FnAbiRequest::OfFnPtr { sig, extra_args }),
13351323
))
13361324
}
13371325

@@ -1358,7 +1346,11 @@ pub trait FnAbiOf<'tcx>: FnAbiOfHelpers<'tcx> {
13581346
// However, we don't do this early in order to avoid calling
13591347
// `def_span` unconditionally (which may have a perf penalty).
13601348
let span = if !span.is_dummy() { span } else { tcx.def_span(instance.def_id()) };
1361-
self.handle_fn_abi_err(err, span, FnAbiRequest::OfInstance { instance, extra_args })
1349+
self.handle_fn_abi_err(
1350+
*err,
1351+
span,
1352+
FnAbiRequest::OfInstance { instance, extra_args },
1353+
)
13621354
}),
13631355
)
13641356
}

compiler/rustc_ty_utils/src/abi.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ fn conv_from_spec_abi(tcx: TyCtxt<'_>, abi: SpecAbi) -> Conv {
202202
fn fn_abi_of_fn_ptr<'tcx>(
203203
tcx: TyCtxt<'tcx>,
204204
query: ty::ParamEnvAnd<'tcx, (ty::PolyFnSig<'tcx>, &'tcx ty::List<Ty<'tcx>>)>,
205-
) -> Result<&'tcx FnAbi<'tcx, Ty<'tcx>>, FnAbiError<'tcx>> {
205+
) -> Result<&'tcx FnAbi<'tcx, Ty<'tcx>>, &'tcx FnAbiError<'tcx>> {
206206
let (param_env, (sig, extra_args)) = query.into_parts();
207207

208208
let cx = LayoutCx { tcx, param_env };
@@ -212,7 +212,7 @@ fn fn_abi_of_fn_ptr<'tcx>(
212212
fn fn_abi_of_instance<'tcx>(
213213
tcx: TyCtxt<'tcx>,
214214
query: ty::ParamEnvAnd<'tcx, (ty::Instance<'tcx>, &'tcx ty::List<Ty<'tcx>>)>,
215-
) -> Result<&'tcx FnAbi<'tcx, Ty<'tcx>>, FnAbiError<'tcx>> {
215+
) -> Result<&'tcx FnAbi<'tcx, Ty<'tcx>>, &'tcx FnAbiError<'tcx>> {
216216
let (param_env, (instance, extra_args)) = query.into_parts();
217217

218218
let sig = fn_sig_for_fn_abi(tcx, instance, param_env);
@@ -331,7 +331,7 @@ fn fn_abi_new_uncached<'tcx>(
331331
fn_def_id: Option<DefId>,
332332
// FIXME(eddyb) replace this with something typed, like an `enum`.
333333
force_thin_self_ptr: bool,
334-
) -> Result<&'tcx FnAbi<'tcx, Ty<'tcx>>, FnAbiError<'tcx>> {
334+
) -> Result<&'tcx FnAbi<'tcx, Ty<'tcx>>, &'tcx FnAbiError<'tcx>> {
335335
let sig = cx.tcx.normalize_erasing_late_bound_regions(cx.param_env, sig);
336336

337337
let conv = conv_from_spec_abi(cx.tcx(), sig.abi);
@@ -376,7 +376,7 @@ fn fn_abi_new_uncached<'tcx>(
376376
let is_drop_in_place =
377377
fn_def_id.is_some() && fn_def_id == cx.tcx.lang_items().drop_in_place_fn();
378378

379-
let arg_of = |ty: Ty<'tcx>, arg_idx: Option<usize>| -> Result<_, FnAbiError<'tcx>> {
379+
let arg_of = |ty: Ty<'tcx>, arg_idx: Option<usize>| -> Result<_, &'tcx FnAbiError<'tcx>> {
380380
let span = tracing::debug_span!("arg_of");
381381
let _entered = span.enter();
382382
let is_return = arg_idx.is_none();
@@ -386,7 +386,8 @@ fn fn_abi_new_uncached<'tcx>(
386386
_ => bug!("argument to drop_in_place is not a raw ptr: {:?}", ty),
387387
});
388388

389-
let layout = cx.layout_of(ty).map_err(|err| *err)?;
389+
let layout =
390+
cx.layout_of(ty).map_err(|err| &*cx.tcx.arena.alloc(FnAbiError::Layout(*err)))?;
390391
let layout = if force_thin_self_ptr && arg_idx == Some(0) {
391392
// Don't pass the vtable, it's not an argument of the virtual fn.
392393
// Instead, pass just the data pointer, but give it the type `*const/mut dyn Trait`
@@ -454,7 +455,7 @@ fn fn_abi_adjust_for_abi<'tcx>(
454455
fn_abi: &mut FnAbi<'tcx, Ty<'tcx>>,
455456
abi: SpecAbi,
456457
fn_def_id: Option<DefId>,
457-
) -> Result<(), FnAbiError<'tcx>> {
458+
) -> Result<(), &'tcx FnAbiError<'tcx>> {
458459
if abi == SpecAbi::Unadjusted {
459460
return Ok(());
460461
}
@@ -548,7 +549,9 @@ fn fn_abi_adjust_for_abi<'tcx>(
548549
fixup(arg, Some(arg_idx));
549550
}
550551
} else {
551-
fn_abi.adjust_for_foreign_abi(cx, abi)?;
552+
fn_abi
553+
.adjust_for_foreign_abi(cx, abi)
554+
.map_err(|err| &*cx.tcx.arena.alloc(FnAbiError::AdjustForForeignAbi(err)))?;
552555
}
553556

554557
Ok(())

0 commit comments

Comments
 (0)