Skip to content

Commit 8966b50

Browse files
authored
Rollup merge of rust-lang#120139 - compiler-errors:fnonce-shim, r=BoxyUwU
Do not normalize closure signature when building `FnOnce` shim It is not necessary to normalize the closure signature when building an `FnOnce` shim for an `Fn`/`FnMut` closure. That closure shim is just calling `FnMut::call_mut(&mut self)` anyways. It's also somewhat sketchy that we were ever doing this to begin with, since we're normalizing with a `ParamEnv::reveal_all()` param-env, which is definitely not right with possibly polymorphic substs. This cuts out a tiny bit of unnecessary work in `Instance::resolve` and simplifies the signature because now we can unconditionally return an `Instance`.
2 parents 5edd432 + f700ee4 commit 8966b50

File tree

7 files changed

+20
-18
lines changed

7 files changed

+20
-18
lines changed

compiler/rustc_codegen_cranelift/src/base.rs

-1
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,6 @@ fn codegen_stmt<'tcx>(
682682
args,
683683
ty::ClosureKind::FnOnce,
684684
)
685-
.expect("failed to normalize and resolve closure during codegen")
686685
.polymorphize(fx.tcx);
687686
let func_ref = fx.get_function_ref(instance);
688687
let func_addr = fx.bcx.ins().func_addr(fx.pointer_type, func_ref);

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

-1
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,6 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
435435
args,
436436
ty::ClosureKind::FnOnce,
437437
)
438-
.expect("failed to normalize and resolve closure during codegen")
439438
.polymorphize(bx.cx().tcx());
440439
OperandValue::Immediate(bx.cx().get_fn_addr(instance))
441440
}

compiler/rustc_const_eval/src/interpret/cast.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
117117
def_id,
118118
args,
119119
ty::ClosureKind::FnOnce,
120-
)
121-
.ok_or_else(|| err_inval!(TooGeneric))?;
120+
);
122121
let fn_ptr = self.fn_ptr(FnVal::Instance(instance));
123122
self.write_pointer(fn_ptr, dest)?;
124123
}

compiler/rustc_middle/src/ty/instance.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -530,12 +530,12 @@ impl<'tcx> Instance<'tcx> {
530530
def_id: DefId,
531531
args: ty::GenericArgsRef<'tcx>,
532532
requested_kind: ty::ClosureKind,
533-
) -> Option<Instance<'tcx>> {
533+
) -> Instance<'tcx> {
534534
let actual_kind = args.as_closure().kind();
535535

536536
match needs_fn_once_adapter_shim(actual_kind, requested_kind) {
537537
Ok(true) => Instance::fn_once_adapter_instance(tcx, def_id, args),
538-
_ => Some(Instance::new(def_id, args)),
538+
_ => Instance::new(def_id, args),
539539
}
540540
}
541541

@@ -550,7 +550,7 @@ impl<'tcx> Instance<'tcx> {
550550
tcx: TyCtxt<'tcx>,
551551
closure_did: DefId,
552552
args: ty::GenericArgsRef<'tcx>,
553-
) -> Option<Instance<'tcx>> {
553+
) -> Instance<'tcx> {
554554
let fn_once = tcx.require_lang_item(LangItem::FnOnce, None);
555555
let call_once = tcx
556556
.associated_items(fn_once)
@@ -564,14 +564,12 @@ impl<'tcx> Instance<'tcx> {
564564

565565
let self_ty = Ty::new_closure(tcx, closure_did, args);
566566

567-
let sig = args.as_closure().sig();
568-
let sig =
569-
tcx.try_normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), sig).ok()?;
570-
assert_eq!(sig.inputs().len(), 1);
571-
let args = tcx.mk_args_trait(self_ty, [sig.inputs()[0].into()]);
567+
let tupled_inputs_ty = args.as_closure().sig().map_bound(|sig| sig.inputs()[0]);
568+
let tupled_inputs_ty = tcx.instantiate_bound_regions_with_erased(tupled_inputs_ty);
569+
let args = tcx.mk_args_trait(self_ty, [tupled_inputs_ty.into()]);
572570

573-
debug!(?self_ty, ?sig);
574-
Some(Instance { def, args })
571+
debug!(?self_ty, args=?tupled_inputs_ty.tuple_fields());
572+
Instance { def, args }
575573
}
576574

577575
pub fn try_resolve_item_for_coroutine(

compiler/rustc_monomorphize/src/collector.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -783,8 +783,7 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirUsedCollector<'a, 'tcx> {
783783
def_id,
784784
args,
785785
ty::ClosureKind::FnOnce,
786-
)
787-
.expect("failed to normalize and resolve closure during codegen");
786+
);
788787
if should_codegen_locally(self.tcx, &instance) {
789788
self.output.push(create_fn_mono_item(self.tcx, instance, span));
790789
}

compiler/rustc_smir/src/rustc_smir/context.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,10 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
464464
let def_id = def.0.internal(&mut *tables, tcx);
465465
let args_ref = args.internal(&mut *tables, tcx);
466466
let closure_kind = kind.internal(&mut *tables, tcx);
467-
Instance::resolve_closure(tables.tcx, def_id, args_ref, closure_kind).stable(&mut *tables)
467+
Some(
468+
Instance::resolve_closure(tables.tcx, def_id, args_ref, closure_kind)
469+
.stable(&mut *tables),
470+
)
468471
}
469472

470473
fn eval_instance(&self, def: InstanceDef, const_ty: Ty) -> Result<Allocation, Error> {

compiler/rustc_ty_utils/src/instance.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,12 @@ fn resolve_associated_item<'tcx>(
265265
match *rcvr_args.type_at(0).kind() {
266266
ty::Closure(closure_def_id, args) => {
267267
let trait_closure_kind = tcx.fn_trait_kind_from_def_id(trait_id).unwrap();
268-
Instance::resolve_closure(tcx, closure_def_id, args, trait_closure_kind)
268+
Some(Instance::resolve_closure(
269+
tcx,
270+
closure_def_id,
271+
args,
272+
trait_closure_kind,
273+
))
269274
}
270275
ty::FnDef(..) | ty::FnPtr(..) => Some(Instance {
271276
def: ty::InstanceDef::FnPtrShim(trait_item_id, rcvr_args.type_at(0)),

0 commit comments

Comments
 (0)