@@ -6,16 +6,19 @@ use rustc_apfloat::Float;
6
6
use rustc_data_structures:: fx:: FxHashMap ;
7
7
use rustc_errors:: ErrorGuaranteed ;
8
8
use rustc_hir as hir;
9
+ use rustc_hir:: def:: DefKind ;
9
10
use rustc_hir:: def_id:: { DefId , LocalDefId } ;
10
- use rustc_hir:: { GeneratorKind , ImplicitSelfKind , Node } ;
11
+ use rustc_hir:: { GeneratorKind , Node } ;
11
12
use rustc_index:: vec:: { Idx , IndexVec } ;
12
13
use rustc_infer:: infer:: { InferCtxt , TyCtxtInferExt } ;
13
14
use rustc_middle:: hir:: place:: PlaceBase as HirPlaceBase ;
14
15
use rustc_middle:: middle:: region;
15
16
use rustc_middle:: mir:: interpret:: ConstValue ;
16
17
use rustc_middle:: mir:: interpret:: Scalar ;
17
18
use rustc_middle:: mir:: * ;
18
- use rustc_middle:: thir:: { BindingMode , Expr , ExprId , LintLevel , LocalVarId , Param , PatKind , Thir } ;
19
+ use rustc_middle:: thir:: {
20
+ self , BindingMode , Expr , ExprId , LintLevel , LocalVarId , Param , ParamId , PatKind , Thir ,
21
+ } ;
19
22
use rustc_middle:: ty:: { self , Ty , TyCtxt , TypeVisitable , TypeckResults } ;
20
23
use rustc_span:: symbol:: sym;
21
24
use rustc_span:: Span ;
@@ -96,26 +99,6 @@ fn mir_build(tcx: TyCtxt<'_>, def: ty::WithOptConstParam<LocalDefId>) -> Body<'_
96
99
///////////////////////////////////////////////////////////////////////////
97
100
// BuildMir -- walks a crate, looking for fn items and methods to build MIR from
98
101
99
- fn liberated_closure_env_ty (
100
- tcx : TyCtxt < ' _ > ,
101
- closure_expr_id : hir:: HirId ,
102
- body_id : hir:: BodyId ,
103
- ) -> Ty < ' _ > {
104
- let closure_ty = tcx. typeck_body ( body_id) . node_type ( closure_expr_id) ;
105
-
106
- let ty:: Closure ( closure_def_id, closure_substs) = * closure_ty. kind ( ) else {
107
- bug ! ( "closure expr does not have closure type: {:?}" , closure_ty) ;
108
- } ;
109
-
110
- let bound_vars =
111
- tcx. mk_bound_variable_kinds ( std:: iter:: once ( ty:: BoundVariableKind :: Region ( ty:: BrEnv ) ) ) ;
112
- let br =
113
- ty:: BoundRegion { var : ty:: BoundVar :: from_usize ( bound_vars. len ( ) - 1 ) , kind : ty:: BrEnv } ;
114
- let env_region = ty:: ReLateBound ( ty:: INNERMOST , br) ;
115
- let closure_env_ty = tcx. closure_env_ty ( closure_def_id, closure_substs, env_region) . unwrap ( ) ;
116
- tcx. erase_late_bound_regions ( ty:: Binder :: bind_with_vars ( closure_env_ty, bound_vars) )
117
- }
118
-
119
102
#[ derive( Debug , PartialEq , Eq ) ]
120
103
enum BlockFrame {
121
104
/// Evaluation is currently within a statement.
@@ -436,13 +419,6 @@ macro_rules! unpack {
436
419
///////////////////////////////////////////////////////////////////////////
437
420
/// the main entry point for building MIR for a function
438
421
439
- struct ArgInfo < ' thir , ' tcx > (
440
- Ty < ' tcx > ,
441
- Option < Span > ,
442
- Option < & ' thir Param < ' tcx > > ,
443
- Option < ImplicitSelfKind > ,
444
- ) ;
445
-
446
422
fn construct_fn < ' tcx > (
447
423
tcx : TyCtxt < ' tcx > ,
448
424
fn_def : ty:: WithOptConstParam < LocalDefId > ,
@@ -473,50 +449,28 @@ fn construct_fn<'tcx>(
473
449
hir:: Unsafety :: Unsafe => Safety :: FnUnsafe ,
474
450
} ;
475
451
476
- let body = tcx. hir ( ) . body ( body_id) ;
477
- let ty = tcx. type_of ( fn_def. did ) ;
478
452
let mut abi = fn_sig. abi ;
479
- let implicit_argument = match ty. kind ( ) {
480
- ty:: Closure ( ..) => {
481
- // HACK(eddyb) Avoid having RustCall on closures,
482
- // as it adds unnecessary (and wrong) auto-tupling.
483
- abi = Abi :: Rust ;
484
- vec ! [ ArgInfo ( liberated_closure_env_ty( tcx, fn_id, body_id) , None , None , None ) ]
485
- }
486
- ty:: Generator ( ..) => {
487
- let gen_ty = typeck_results. node_type ( fn_id) ;
488
-
489
- // The resume argument may be missing, in that case we need to provide it here.
490
- // It will always be `()` in this case.
491
- if body. params . is_empty ( ) {
492
- vec ! [ ArgInfo ( gen_ty, None , None , None ) , ArgInfo ( tcx. mk_unit( ) , None , None , None ) ]
493
- } else {
494
- vec ! [ ArgInfo ( gen_ty, None , None , None ) ]
495
- }
496
- }
497
- _ => vec ! [ ] ,
498
- } ;
499
-
500
- let explicit_arguments =
501
- thir. params . iter ( ) . map ( |arg| ArgInfo ( arg. ty , arg. ty_span , Some ( & arg) , arg. self_kind ) ) ;
453
+ if let DefKind :: Closure = tcx. def_kind ( fn_def. did ) {
454
+ // HACK(eddyb) Avoid having RustCall on closures,
455
+ // as it adds unnecessary (and wrong) auto-tupling.
456
+ abi = Abi :: Rust ;
457
+ }
502
458
503
- let arguments = implicit_argument . into_iter ( ) . chain ( explicit_arguments ) ;
459
+ let arguments = & thir . params ;
504
460
505
461
let ( yield_ty, return_ty) = if generator_kind. is_some ( ) {
506
- let gen_ty = typeck_results . node_type ( fn_id ) ;
462
+ let gen_ty = arguments [ thir :: UPVAR_ENV_PARAM ] . ty ;
507
463
let gen_sig = match gen_ty. kind ( ) {
508
464
ty:: Generator ( _, gen_substs, ..) => gen_substs. as_generator ( ) . sig ( ) ,
509
465
_ => {
510
- span_bug ! ( span, "generator w/o generator type: {:?}" , ty )
466
+ span_bug ! ( span, "generator w/o generator type: {:?}" , gen_ty )
511
467
}
512
468
} ;
513
469
( Some ( gen_sig. yield_ty ) , gen_sig. return_ty )
514
470
} else {
515
471
( None , fn_sig. output ( ) )
516
472
} ;
517
473
518
- let arguments: Vec < _ > = arguments. collect ( ) ;
519
-
520
474
let mut body = tcx. infer_ctxt ( ) . enter ( |infcx| {
521
475
let mut builder = Builder :: new (
522
476
thir,
@@ -532,9 +486,9 @@ fn construct_fn<'tcx>(
532
486
) ;
533
487
534
488
let call_site_scope =
535
- region:: Scope { id : body . value . hir_id . local_id , data : region:: ScopeData :: CallSite } ;
489
+ region:: Scope { id : body_id . hir_id . local_id , data : region:: ScopeData :: CallSite } ;
536
490
let arg_scope =
537
- region:: Scope { id : body . value . hir_id . local_id , data : region:: ScopeData :: Arguments } ;
491
+ region:: Scope { id : body_id . hir_id . local_id , data : region:: ScopeData :: Arguments } ;
538
492
let source_info = builder. source_info ( span) ;
539
493
let call_site_s = ( call_site_scope, source_info) ;
540
494
unpack ! ( builder. in_scope( call_site_s, LintLevel :: Inherited , |builder| {
@@ -550,7 +504,7 @@ fn construct_fn<'tcx>(
550
504
builder. args_and_body(
551
505
START_BLOCK ,
552
506
fn_def. did,
553
- & arguments,
507
+ arguments,
554
508
arg_scope,
555
509
& thir[ expr] ,
556
510
)
@@ -809,18 +763,19 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
809
763
& mut self ,
810
764
mut block : BasicBlock ,
811
765
fn_def_id : LocalDefId ,
812
- arguments : & [ ArgInfo < ' _ , ' tcx > ] ,
766
+ arguments : & IndexVec < ParamId , Param < ' tcx > > ,
813
767
argument_scope : region:: Scope ,
814
768
expr : & Expr < ' tcx > ,
815
769
) -> BlockAnd < ( ) > {
816
770
// Allocate locals for the function arguments
817
- for & ArgInfo ( ty , _ , arg_opt , _ ) in arguments. iter ( ) {
771
+ for param in arguments. iter ( ) {
818
772
let source_info =
819
- SourceInfo :: outermost ( arg_opt. map_or ( self . fn_span , |arg| arg. pat . span ) ) ;
820
- let arg_local = self . local_decls . push ( LocalDecl :: with_source_info ( ty, source_info) ) ;
773
+ SourceInfo :: outermost ( param. pat . as_ref ( ) . map_or ( self . fn_span , |pat| pat. span ) ) ;
774
+ let arg_local =
775
+ self . local_decls . push ( LocalDecl :: with_source_info ( param. ty , source_info) ) ;
821
776
822
777
// If this is a simple binding pattern, give debuginfo a nice name.
823
- if let Some ( arg ) = arg_opt && let Some ( name) = arg . pat . simple_ident ( ) {
778
+ if let Some ( ref pat ) = param . pat && let Some ( name) = pat. simple_ident ( ) {
824
779
self . var_debug_info . push ( VarDebugInfo {
825
780
name,
826
781
source_info,
@@ -893,27 +848,28 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
893
848
894
849
let mut scope = None ;
895
850
// Bind the argument patterns
896
- for ( index, arg_info ) in arguments. iter ( ) . enumerate ( ) {
851
+ for ( index, param ) in arguments. iter ( ) . enumerate ( ) {
897
852
// Function arguments always get the first Local indices after the return place
898
853
let local = Local :: new ( index + 1 ) ;
899
854
let place = Place :: from ( local) ;
900
- let & ArgInfo ( _, opt_ty_info, arg_opt, ref self_binding) = arg_info;
901
855
902
856
// Make sure we drop (parts of) the argument even when not matched on.
903
857
self . schedule_drop (
904
- arg_opt . as_ref ( ) . map_or ( expr. span , |arg| arg . pat . span ) ,
858
+ param . pat . as_ref ( ) . map_or ( expr. span , |pat| pat. span ) ,
905
859
argument_scope,
906
860
local,
907
861
DropKind :: Value ,
908
862
) ;
909
863
910
- let Some ( arg ) = arg_opt else {
864
+ let Some ( ref pat ) = param . pat else {
911
865
continue ;
912
866
} ;
913
867
let original_source_scope = self . source_scope ;
914
- let span = arg. pat . span ;
915
- self . set_correct_source_scope_for_arg ( arg. hir_id , original_source_scope, span) ;
916
- match * arg. pat . kind {
868
+ let span = pat. span ;
869
+ if let Some ( arg_hir_id) = param. hir_id {
870
+ self . set_correct_source_scope_for_arg ( arg_hir_id, original_source_scope, span) ;
871
+ }
872
+ match * pat. kind {
917
873
// Don't introduce extra copies for simple bindings
918
874
PatKind :: Binding {
919
875
mutability,
@@ -924,16 +880,16 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
924
880
} => {
925
881
self . local_decls [ local] . mutability = mutability;
926
882
self . local_decls [ local] . source_info . scope = self . source_scope ;
927
- self . local_decls [ local] . local_info = if let Some ( kind) = self_binding {
883
+ self . local_decls [ local] . local_info = if let Some ( kind) = param . self_kind {
928
884
Some ( Box :: new ( LocalInfo :: User ( ClearCrossCrate :: Set (
929
- BindingForm :: ImplicitSelf ( * kind) ,
885
+ BindingForm :: ImplicitSelf ( kind) ,
930
886
) ) ) )
931
887
} else {
932
888
let binding_mode = ty:: BindingMode :: BindByValue ( mutability) ;
933
889
Some ( Box :: new ( LocalInfo :: User ( ClearCrossCrate :: Set ( BindingForm :: Var (
934
890
VarBindingForm {
935
891
binding_mode,
936
- opt_ty_info,
892
+ opt_ty_info : param . ty_span ,
937
893
opt_match_place : Some ( ( Some ( place) , span) ) ,
938
894
pat_span : span,
939
895
} ,
@@ -945,12 +901,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
945
901
scope = self . declare_bindings (
946
902
scope,
947
903
expr. span ,
948
- & arg . pat ,
904
+ & pat,
949
905
matches:: ArmHasGuard ( false ) ,
950
906
Some ( ( Some ( & place) , span) ) ,
951
907
) ;
952
908
let place_builder = PlaceBuilder :: from ( local) ;
953
- unpack ! ( block = self . place_into_pattern( block, & arg . pat, place_builder, false ) ) ;
909
+ unpack ! ( block = self . place_into_pattern( block, & pat, place_builder, false ) ) ;
954
910
}
955
911
}
956
912
self . source_scope = original_source_scope;
0 commit comments