@@ -13,14 +13,26 @@ use crate::prelude::*;
13
13
14
14
pub ( crate ) use self :: returning:: { can_return_to_ssa_var, codegen_return} ;
15
15
16
- // Copied from https://github.com/rust-lang/rust/blob/b2c1a606feb1fbdb0ac0acba76f881ef172ed474 /src/librustc_middle/ty/layout.rs#L2287
16
+ // Copied from https://github.com/rust-lang/rust/blob/f52c72948aa1dd718cc1f168d21c91c584c0a662 /src/librustc_middle/ty/layout.rs#L2301
17
17
pub ( crate ) fn fn_sig_for_fn_abi < ' tcx > ( tcx : TyCtxt < ' tcx > , instance : Instance < ' tcx > ) -> ty:: PolyFnSig < ' tcx > {
18
- let ty = instance. ty ( tcx, ParamEnv :: reveal_all ( ) ) ;
18
+ use rustc_middle:: ty:: subst:: Subst ;
19
+
20
+ // FIXME(davidtwco,eddyb): A `ParamEnv` should be passed through to this function.
21
+ let ty = instance. ty ( tcx, ty:: ParamEnv :: reveal_all ( ) ) ;
19
22
match ty. kind {
20
- ty:: FnDef ( ..) |
21
- // Shims currently have type FnPtr. Not sure this should remain.
22
- ty:: FnPtr ( _) => {
23
- let mut sig = ty. fn_sig ( tcx) ;
23
+ ty:: FnDef ( ..) => {
24
+ // HACK(davidtwco,eddyb): This is a workaround for polymorphization considering
25
+ // parameters unused if they show up in the signature, but not in the `mir::Body`
26
+ // (i.e. due to being inside a projection that got normalized, see
27
+ // `src/test/ui/polymorphization/normalized_sig_types.rs`), and codegen not keeping
28
+ // track of a polymorphization `ParamEnv` to allow normalizing later.
29
+ let mut sig = match ty. kind {
30
+ ty:: FnDef ( def_id, substs) => tcx
31
+ . normalize_erasing_regions ( tcx. param_env ( def_id) , tcx. fn_sig ( def_id) )
32
+ . subst ( tcx, substs) ,
33
+ _ => unreachable ! ( ) ,
34
+ } ;
35
+
24
36
if let ty:: InstanceDef :: VtableShim ( ..) = instance. def {
25
37
// Modify `fn(self, ...)` to `fn(self: *mut Self, ...)`.
26
38
sig = sig. map_bound ( |mut sig| {
@@ -36,44 +48,44 @@ pub(crate) fn fn_sig_for_fn_abi<'tcx>(tcx: TyCtxt<'tcx>, instance: Instance<'tcx
36
48
let sig = substs. as_closure ( ) . sig ( ) ;
37
49
38
50
let env_ty = tcx. closure_env_ty ( def_id, substs) . unwrap ( ) ;
39
- sig. map_bound ( |sig| tcx. mk_fn_sig (
40
- std:: iter:: once ( env_ty. skip_binder ( ) ) . chain ( sig. inputs ( ) . iter ( ) . cloned ( ) ) ,
41
- sig. output ( ) ,
42
- sig. c_variadic ,
43
- sig. unsafety ,
44
- sig. abi
45
- ) )
51
+ sig. map_bound ( |sig| {
52
+ tcx. mk_fn_sig (
53
+ std:: iter:: once ( env_ty. skip_binder ( ) ) . chain ( sig. inputs ( ) . iter ( ) . cloned ( ) ) ,
54
+ sig. output ( ) ,
55
+ sig. c_variadic ,
56
+ sig. unsafety ,
57
+ sig. abi ,
58
+ )
59
+ } )
46
60
}
47
61
ty:: Generator ( _, substs, _) => {
48
62
let sig = substs. as_generator ( ) . poly_sig ( ) ;
49
63
50
64
let env_region = ty:: ReLateBound ( ty:: INNERMOST , ty:: BrEnv ) ;
51
65
let env_ty = tcx. mk_mut_ref ( tcx. mk_region ( env_region) , ty) ;
52
66
53
- let pin_did = tcx. lang_items ( ) . pin_type ( ) . unwrap ( ) ;
67
+ let pin_did = tcx. require_lang_item ( rustc_hir :: LangItem :: PinTypeLangItem , None ) ;
54
68
let pin_adt_ref = tcx. adt_def ( pin_did) ;
55
69
let pin_substs = tcx. intern_substs ( & [ env_ty. into ( ) ] ) ;
56
70
let env_ty = tcx. mk_adt ( pin_adt_ref, pin_substs) ;
57
71
58
72
sig. map_bound ( |sig| {
59
- let state_did = tcx. lang_items ( ) . gen_state ( ) . unwrap ( ) ;
73
+ let state_did = tcx. require_lang_item ( rustc_hir :: LangItem :: GeneratorStateLangItem , None ) ;
60
74
let state_adt_ref = tcx. adt_def ( state_did) ;
61
- let state_substs = tcx. intern_substs ( & [
62
- sig. yield_ty . into ( ) ,
63
- sig. return_ty . into ( ) ,
64
- ] ) ;
75
+ let state_substs =
76
+ tcx. intern_substs ( & [ sig. yield_ty . into ( ) , sig. return_ty . into ( ) ] ) ;
65
77
let ret_ty = tcx. mk_adt ( state_adt_ref, state_substs) ;
66
78
67
79
tcx. mk_fn_sig (
68
80
[ env_ty, sig. resume_ty ] . iter ( ) ,
69
81
& ret_ty,
70
82
false ,
71
83
rustc_hir:: Unsafety :: Normal ,
72
- rustc_target:: spec:: abi:: Abi :: Rust
84
+ rustc_target:: spec:: abi:: Abi :: Rust ,
73
85
)
74
86
} )
75
87
}
76
- _ => bug ! ( "unexpected type {:?} in Instance::fn_sig" , ty)
88
+ _ => bug ! ( "unexpected type {:?} in Instance::fn_sig" , ty) ,
77
89
}
78
90
}
79
91
@@ -464,7 +476,8 @@ pub(crate) fn codegen_terminator_call<'tcx>(
464
476
let instance = if let ty:: FnDef ( def_id, substs) = fn_ty. kind {
465
477
let instance = ty:: Instance :: resolve ( fx. tcx , ty:: ParamEnv :: reveal_all ( ) , def_id, substs)
466
478
. unwrap ( )
467
- . unwrap ( ) ;
479
+ . unwrap ( )
480
+ . polymorphize ( fx. tcx ) ;
468
481
469
482
if fx. tcx . symbol_name ( instance) . name . starts_with ( "llvm." ) {
470
483
crate :: intrinsics:: codegen_llvm_intrinsic_call (
@@ -655,7 +668,7 @@ pub(crate) fn codegen_drop<'tcx>(
655
668
drop_place : CPlace < ' tcx > ,
656
669
) {
657
670
let ty = drop_place. layout ( ) . ty ;
658
- let drop_fn = Instance :: resolve_drop_in_place ( fx. tcx , ty) ;
671
+ let drop_fn = Instance :: resolve_drop_in_place ( fx. tcx , ty) . polymorphize ( fx . tcx ) ;
659
672
660
673
if let ty:: InstanceDef :: DropGlue ( _, None ) = drop_fn. def {
661
674
// we don't actually need to drop anything
@@ -685,16 +698,7 @@ pub(crate) fn codegen_drop<'tcx>(
685
698
fx. bcx . ins ( ) . call_indirect ( sig, drop_fn, & [ ptr] ) ;
686
699
}
687
700
_ => {
688
- let instance = match drop_fn_ty. kind {
689
- ty:: FnDef ( def_id, substs) => {
690
- Instance :: resolve ( fx. tcx , ParamEnv :: reveal_all ( ) , def_id, substs)
691
- . unwrap ( )
692
- . unwrap ( )
693
- }
694
- _ => unreachable ! ( "{:?}" , drop_fn_ty) ,
695
- } ;
696
-
697
- assert ! ( !matches!( instance. def, InstanceDef :: Virtual ( _, _) ) ) ;
701
+ assert ! ( !matches!( drop_fn. def, InstanceDef :: Virtual ( _, _) ) ) ;
698
702
699
703
let arg_place = CPlace :: new_stack_slot (
700
704
fx,
@@ -712,13 +716,13 @@ pub(crate) fn codegen_drop<'tcx>(
712
716
713
717
let mut call_args: Vec < Value > = arg_value. into_iter ( ) . collect :: < Vec < _ > > ( ) ;
714
718
715
- if instance . def . requires_caller_location ( fx. tcx ) {
719
+ if drop_fn . def . requires_caller_location ( fx. tcx ) {
716
720
// Pass the caller location for `#[track_caller]`.
717
721
let caller_location = fx. get_caller_location ( span) ;
718
722
call_args. extend ( adjust_arg_for_abi ( fx, caller_location) . into_iter ( ) ) ;
719
723
}
720
724
721
- let func_ref = fx. get_function_ref ( instance ) ;
725
+ let func_ref = fx. get_function_ref ( drop_fn ) ;
722
726
fx. bcx . ins ( ) . call ( func_ref, & call_args) ;
723
727
}
724
728
}
0 commit comments