Skip to content

Commit 36457ce

Browse files
Rollup merge of rust-lang#150454 - tiif:move_variadic, r=oli-obk
cleanup: move c-variadic arguments handling into compute_inputs_and_output Previously, ``unnormalized_input_tys`` needs to be mutable because the c_variadic arguments are added to ``unnormalized_input_tys`` outside of ``compute_inputs_and_output``. This could have been done together in ``compute_inputs_and_output``.
2 parents 6e48b44 + 791fa37 commit 36457ce

File tree

2 files changed

+39
-35
lines changed

2 files changed

+39
-35
lines changed

compiler/rustc_borrowck/src/universal_regions.rs

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -543,38 +543,9 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
543543
&indices,
544544
);
545545

546-
let (unnormalized_output_ty, mut unnormalized_input_tys) =
546+
let (unnormalized_output_ty, unnormalized_input_tys) =
547547
inputs_and_output.split_last().unwrap();
548548

549-
// C-variadic fns also have a `VaList` input that's not listed in the signature
550-
// (as it's created inside the body itself, not passed in from outside).
551-
if let DefiningTy::FnDef(def_id, _) = defining_ty {
552-
if self.infcx.tcx.fn_sig(def_id).skip_binder().c_variadic() {
553-
let va_list_did = self
554-
.infcx
555-
.tcx
556-
.require_lang_item(LangItem::VaList, self.infcx.tcx.def_span(self.mir_def));
557-
558-
let reg_vid = self
559-
.infcx
560-
.next_nll_region_var(NllRegionVariableOrigin::FreeRegion, || {
561-
RegionCtxt::Free(sym::c_dash_variadic)
562-
})
563-
.as_var();
564-
565-
let region = ty::Region::new_var(self.infcx.tcx, reg_vid);
566-
let va_list_ty = self
567-
.infcx
568-
.tcx
569-
.type_of(va_list_did)
570-
.instantiate(self.infcx.tcx, &[region.into()]);
571-
572-
unnormalized_input_tys = self.infcx.tcx.mk_type_list_from_iter(
573-
unnormalized_input_tys.iter().copied().chain(iter::once(va_list_ty)),
574-
);
575-
}
576-
}
577-
578549
let fr_fn_body = self
579550
.infcx
580551
.next_nll_region_var(NllRegionVariableOrigin::FreeRegion, || {
@@ -816,7 +787,40 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
816787
DefiningTy::FnDef(def_id, _) => {
817788
let sig = tcx.fn_sig(def_id).instantiate_identity();
818789
let sig = indices.fold_to_region_vids(tcx, sig);
819-
sig.inputs_and_output()
790+
let inputs_and_output = sig.inputs_and_output();
791+
792+
// C-variadic fns also have a `VaList` input that's not listed in the signature
793+
// (as it's created inside the body itself, not passed in from outside).
794+
if self.infcx.tcx.fn_sig(def_id).skip_binder().c_variadic() {
795+
let va_list_did = self
796+
.infcx
797+
.tcx
798+
.require_lang_item(LangItem::VaList, self.infcx.tcx.def_span(self.mir_def));
799+
800+
let reg_vid = self
801+
.infcx
802+
.next_nll_region_var(NllRegionVariableOrigin::FreeRegion, || {
803+
RegionCtxt::Free(sym::c_dash_variadic)
804+
})
805+
.as_var();
806+
807+
let region = ty::Region::new_var(self.infcx.tcx, reg_vid);
808+
let va_list_ty = self
809+
.infcx
810+
.tcx
811+
.type_of(va_list_did)
812+
.instantiate(self.infcx.tcx, &[region.into()]);
813+
814+
// The signature needs to follow the order [input_tys, va_list_ty, output_ty]
815+
return inputs_and_output.map_bound(|tys| {
816+
let (output_ty, input_tys) = tys.split_last().unwrap();
817+
tcx.mk_type_list_from_iter(
818+
input_tys.iter().copied().chain([va_list_ty, *output_ty]),
819+
)
820+
});
821+
}
822+
823+
inputs_and_output
820824
}
821825

822826
DefiningTy::Const(def_id, _) => {

tests/ui/c-variadic/variadic-ffi-4.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ error: lifetime may not live long enough
3030
--> $DIR/variadic-ffi-4.rs:22:5
3131
|
3232
LL | pub unsafe extern "C" fn no_escape4(_: usize, mut ap0: &mut VaList, mut ap1: ...) {
33-
| ------- ------- has type `VaList<'2>`
33+
| ------- ------- has type `VaList<'1>`
3434
| |
35-
| has type `&mut VaList<'1>`
35+
| has type `&mut VaList<'2>`
3636
LL | ap0 = &mut ap1;
3737
| ^^^^^^^^^^^^^^ assignment requires that `'1` must outlive `'2`
3838
|
@@ -44,9 +44,9 @@ error: lifetime may not live long enough
4444
--> $DIR/variadic-ffi-4.rs:22:5
4545
|
4646
LL | pub unsafe extern "C" fn no_escape4(_: usize, mut ap0: &mut VaList, mut ap1: ...) {
47-
| ------- ------- has type `VaList<'2>`
47+
| ------- ------- has type `VaList<'1>`
4848
| |
49-
| has type `&mut VaList<'1>`
49+
| has type `&mut VaList<'2>`
5050
LL | ap0 = &mut ap1;
5151
| ^^^^^^^^^^^^^^ assignment requires that `'2` must outlive `'1`
5252
|

0 commit comments

Comments
 (0)