Skip to content

Commit c2df7bd

Browse files
fn_sig_for_fn_abi should return a ty::FnSig, no need for a binder
1 parent 46e8d20 commit c2df7bd

File tree

2 files changed

+52
-82
lines changed

2 files changed

+52
-82
lines changed

compiler/rustc_mir_transform/src/shim.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -747,8 +747,8 @@ fn build_call_shim<'tcx>(
747747
sig.inputs_and_output = tcx.mk_type_list(&inputs_and_output);
748748
}
749749

750-
// FIXME(eddyb) avoid having this snippet both here and in
751-
// `Instance::fn_sig` (introduce `InstanceKind::fn_sig`?).
750+
// FIXME: Avoid having to adjust the signature both here and in
751+
// `fn_sig_for_fn_abi`.
752752
if let ty::InstanceKind::VTableShim(..) = instance {
753753
// Modify fn(self, ...) to fn(self: *mut Self, ...)
754754
let mut inputs_and_output = sig.inputs_and_output.to_vec();

compiler/rustc_ty_utils/src/abi.rs

+50-80
Original file line numberDiff line numberDiff line change
@@ -31,90 +31,63 @@ fn fn_sig_for_fn_abi<'tcx>(
3131
tcx: TyCtxt<'tcx>,
3232
instance: ty::Instance<'tcx>,
3333
param_env: ty::ParamEnv<'tcx>,
34-
) -> ty::PolyFnSig<'tcx> {
34+
) -> ty::FnSig<'tcx> {
3535
if let InstanceKind::ThreadLocalShim(..) = instance.def {
36-
return ty::Binder::dummy(tcx.mk_fn_sig(
36+
return tcx.mk_fn_sig(
3737
[],
3838
tcx.thread_local_ptr_ty(instance.def_id()),
3939
false,
4040
hir::Safety::Safe,
4141
rustc_abi::ExternAbi::Unadjusted,
42-
));
42+
);
4343
}
4444

4545
let ty = instance.ty(tcx, param_env);
4646
match *ty.kind() {
47-
ty::FnDef(..) => {
47+
ty::FnDef(def_id, args) => {
4848
// HACK(davidtwco,eddyb): This is a workaround for polymorphization considering
4949
// parameters unused if they show up in the signature, but not in the `mir::Body`
5050
// (i.e. due to being inside a projection that got normalized, see
5151
// `tests/ui/polymorphization/normalized_sig_types.rs`), and codegen not keeping
5252
// track of a polymorphization `ParamEnv` to allow normalizing later.
5353
//
5454
// We normalize the `fn_sig` again after instantiating at a later point.
55-
let mut sig = match *ty.kind() {
56-
ty::FnDef(def_id, args) => tcx
57-
.fn_sig(def_id)
55+
let mut sig = tcx.instantiate_bound_regions_with_erased(
56+
tcx.fn_sig(def_id)
5857
.map_bound(|fn_sig| {
5958
tcx.normalize_erasing_regions(tcx.param_env(def_id), fn_sig)
6059
})
6160
.instantiate(tcx, args),
62-
_ => unreachable!(),
63-
};
61+
);
6462

6563
if let ty::InstanceKind::VTableShim(..) = instance.def {
66-
// Modify `fn(self, ...)` to `fn(self: *mut Self, ...)`.
67-
sig = sig.map_bound(|mut sig| {
68-
let mut inputs_and_output = sig.inputs_and_output.to_vec();
69-
inputs_and_output[0] = Ty::new_mut_ptr(tcx, inputs_and_output[0]);
70-
sig.inputs_and_output = tcx.mk_type_list(&inputs_and_output);
71-
sig
72-
});
64+
let mut inputs_and_output = sig.inputs_and_output.to_vec();
65+
inputs_and_output[0] = Ty::new_mut_ptr(tcx, inputs_and_output[0]);
66+
sig.inputs_and_output = tcx.mk_type_list(&inputs_and_output);
7367
}
68+
7469
sig
7570
}
7671
ty::Closure(def_id, args) => {
77-
let sig = args.as_closure().sig();
78-
79-
let bound_vars =
80-
tcx.mk_bound_variable_kinds_from_iter(sig.bound_vars().iter().chain(iter::once(
81-
ty::BoundVariableKind::Region(ty::BoundRegionKind::ClosureEnv),
82-
)));
83-
let br = ty::BoundRegion {
84-
var: ty::BoundVar::from_usize(bound_vars.len() - 1),
85-
kind: ty::BoundRegionKind::ClosureEnv,
86-
};
87-
let env_region = ty::Region::new_bound(tcx, ty::INNERMOST, br);
72+
let sig = tcx.instantiate_bound_regions_with_erased(args.as_closure().sig());
8873
let env_ty = tcx.closure_env_ty(
8974
Ty::new_closure(tcx, def_id, args),
9075
args.as_closure().kind(),
91-
env_region,
76+
tcx.lifetimes.re_erased,
9277
);
9378

94-
let sig = sig.skip_binder();
95-
ty::Binder::bind_with_vars(
96-
tcx.mk_fn_sig(
97-
iter::once(env_ty).chain(sig.inputs().iter().cloned()),
98-
sig.output(),
99-
sig.c_variadic,
100-
sig.safety,
101-
sig.abi,
102-
),
103-
bound_vars,
79+
tcx.mk_fn_sig(
80+
iter::once(env_ty).chain(sig.inputs().iter().cloned()),
81+
sig.output(),
82+
sig.c_variadic,
83+
sig.safety,
84+
sig.abi,
10485
)
10586
}
10687
ty::CoroutineClosure(def_id, args) => {
10788
let coroutine_ty = Ty::new_coroutine_closure(tcx, def_id, args);
10889
let sig = args.as_coroutine_closure().coroutine_closure_sig();
109-
let bound_vars =
110-
tcx.mk_bound_variable_kinds_from_iter(sig.bound_vars().iter().chain(iter::once(
111-
ty::BoundVariableKind::Region(ty::BoundRegionKind::ClosureEnv),
112-
)));
113-
let br = ty::BoundRegion {
114-
var: ty::BoundVar::from_usize(bound_vars.len() - 1),
115-
kind: ty::BoundRegionKind::ClosureEnv,
116-
};
117-
let env_region = ty::Region::new_bound(tcx, ty::INNERMOST, br);
90+
11891
// When this `CoroutineClosure` comes from a `ConstructCoroutineInClosureShim`,
11992
// make sure we respect the `target_kind` in that shim.
12093
// FIXME(async_closures): This shouldn't be needed, and we should be populating
@@ -135,42 +108,32 @@ fn fn_sig_for_fn_abi<'tcx>(
135108
coroutine_ty
136109
}
137110
} else {
138-
tcx.closure_env_ty(coroutine_ty, coroutine_kind, env_region)
111+
tcx.closure_env_ty(coroutine_ty, coroutine_kind, tcx.lifetimes.re_erased)
139112
};
140113

141-
let sig = sig.skip_binder();
142-
ty::Binder::bind_with_vars(
143-
tcx.mk_fn_sig(
144-
iter::once(env_ty).chain([sig.tupled_inputs_ty]),
145-
sig.to_coroutine_given_kind_and_upvars(
146-
tcx,
147-
args.as_coroutine_closure().parent_args(),
148-
tcx.coroutine_for_closure(def_id),
149-
coroutine_kind,
150-
env_region,
151-
args.as_coroutine_closure().tupled_upvars_ty(),
152-
args.as_coroutine_closure().coroutine_captures_by_ref_ty(),
153-
),
154-
sig.c_variadic,
155-
sig.safety,
156-
sig.abi,
114+
let sig = tcx.instantiate_bound_regions_with_erased(sig);
115+
116+
tcx.mk_fn_sig(
117+
iter::once(env_ty).chain([sig.tupled_inputs_ty]),
118+
sig.to_coroutine_given_kind_and_upvars(
119+
tcx,
120+
args.as_coroutine_closure().parent_args(),
121+
tcx.coroutine_for_closure(def_id),
122+
coroutine_kind,
123+
tcx.lifetimes.re_erased,
124+
args.as_coroutine_closure().tupled_upvars_ty(),
125+
args.as_coroutine_closure().coroutine_captures_by_ref_ty(),
157126
),
158-
bound_vars,
127+
sig.c_variadic,
128+
sig.safety,
129+
sig.abi,
159130
)
160131
}
161132
ty::Coroutine(did, args) => {
162133
let coroutine_kind = tcx.coroutine_kind(did).unwrap();
163134
let sig = args.as_coroutine().sig();
164135

165-
let bound_vars = tcx.mk_bound_variable_kinds_from_iter(iter::once(
166-
ty::BoundVariableKind::Region(ty::BoundRegionKind::ClosureEnv),
167-
));
168-
let br = ty::BoundRegion {
169-
var: ty::BoundVar::from_usize(bound_vars.len() - 1),
170-
kind: ty::BoundRegionKind::ClosureEnv,
171-
};
172-
173-
let env_ty = Ty::new_mut_ref(tcx, ty::Region::new_bound(tcx, ty::INNERMOST, br), ty);
136+
let env_ty = Ty::new_mut_ref(tcx, tcx.lifetimes.re_erased, ty);
174137

175138
let pin_did = tcx.require_lang_item(LangItem::Pin, None);
176139
let pin_adt_ref = tcx.adt_def(pin_did);
@@ -265,7 +228,7 @@ fn fn_sig_for_fn_abi<'tcx>(
265228
}
266229
};
267230

268-
let fn_sig = if let Some(resume_ty) = resume_ty {
231+
if let Some(resume_ty) = resume_ty {
269232
tcx.mk_fn_sig(
270233
[env_ty, resume_ty],
271234
ret_ty,
@@ -282,8 +245,7 @@ fn fn_sig_for_fn_abi<'tcx>(
282245
hir::Safety::Safe,
283246
rustc_abi::ExternAbi::Rust,
284247
)
285-
};
286-
ty::Binder::bind_with_vars(fn_sig, bound_vars)
248+
}
287249
}
288250
_ => bug!("unexpected type {:?} in Instance::fn_sig", ty),
289251
}
@@ -334,7 +296,14 @@ fn fn_abi_of_fn_ptr<'tcx>(
334296
let (param_env, (sig, extra_args)) = query.into_parts();
335297

336298
let cx = LayoutCx::new(tcx, param_env);
337-
fn_abi_new_uncached(&cx, sig, extra_args, None, None, false)
299+
fn_abi_new_uncached(
300+
&cx,
301+
tcx.instantiate_bound_regions_with_erased(sig),
302+
extra_args,
303+
None,
304+
None,
305+
false,
306+
)
338307
}
339308

340309
fn fn_abi_of_instance<'tcx>(
@@ -565,15 +534,16 @@ fn fn_abi_sanity_check<'tcx>(
565534
#[tracing::instrument(level = "debug", skip(cx, caller_location, fn_def_id, force_thin_self_ptr))]
566535
fn fn_abi_new_uncached<'tcx>(
567536
cx: &LayoutCx<'tcx>,
568-
sig: ty::PolyFnSig<'tcx>,
537+
sig: ty::FnSig<'tcx>,
569538
extra_args: &[Ty<'tcx>],
570539
caller_location: Option<Ty<'tcx>>,
571540
fn_def_id: Option<DefId>,
572541
// FIXME(eddyb) replace this with something typed, like an `enum`.
573542
force_thin_self_ptr: bool,
574543
) -> Result<&'tcx FnAbi<'tcx, Ty<'tcx>>, &'tcx FnAbiError<'tcx>> {
575544
let tcx = cx.tcx();
576-
let sig = tcx.normalize_erasing_late_bound_regions(cx.param_env, sig);
545+
// FIXME: is this needed?
546+
let sig = tcx.normalize_erasing_regions(cx.param_env, sig);
577547

578548
let conv = conv_from_spec_abi(cx.tcx(), sig.abi, sig.c_variadic);
579549

0 commit comments

Comments
 (0)