1- use super :: { AnonymousLifetimeMode , LoweringContext , ParamMode } ;
21use super :: { AstOwner , ImplTraitContext , ImplTraitPosition , ResolverAstLowering } ;
2+ use super :: { LoweringContext , ParamMode } ;
33use crate :: { Arena , FnDeclKind } ;
44
55use rustc_ast:: ptr:: P ;
66use rustc_ast:: visit:: AssocCtxt ;
77use rustc_ast:: * ;
8- use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
8+ use rustc_data_structures:: fx:: FxHashMap ;
99use rustc_data_structures:: sorted_map:: SortedMap ;
1010use rustc_errors:: struct_span_err;
1111use rustc_hir as hir;
@@ -81,13 +81,10 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
8181 is_in_loop_condition : false ,
8282 is_in_trait_impl : false ,
8383 is_in_dyn_type : false ,
84- anonymous_lifetime_mode : AnonymousLifetimeMode :: PassThrough ,
8584 generator_kind : None ,
8685 task_context : None ,
8786 current_item : None ,
88- lifetimes_to_define : Vec :: new ( ) ,
89- is_collecting_anonymous_lifetimes : None ,
90- in_scope_lifetimes : Vec :: new ( ) ,
87+ captured_lifetimes : None ,
9188 allow_try_trait : Some ( [ sym:: try_trait_v2] [ ..] . into ( ) ) ,
9289 allow_gen_future : Some ( [ sym:: gen_future] [ ..] . into ( ) ) ,
9390 allow_into_future : Some ( [ sym:: into_future] [ ..] . into ( ) ) ,
@@ -143,32 +140,14 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
143140 LocalDefId { local_def_index }
144141 } ;
145142
146- let parent_hir = self . lower_node ( parent_id) . unwrap ( ) . node ( ) . expect_item ( ) ;
143+ let parent_hir = self . lower_node ( parent_id) . unwrap ( ) ;
147144 self . with_lctx ( item. id , |lctx| {
148145 // Evaluate with the lifetimes in `params` in-scope.
149146 // This is used to track which lifetimes have already been defined,
150147 // and which need to be replicated when lowering an async fn.
151- match parent_hir. kind {
152- hir:: ItemKind :: Impl ( hir:: Impl { ref of_trait, ref generics , .. } ) => {
148+ match parent_hir. node ( ) . expect_item ( ) . kind {
149+ hir:: ItemKind :: Impl ( hir:: Impl { ref of_trait, .. } ) => {
153150 lctx. is_in_trait_impl = of_trait. is_some ( ) ;
154- lctx. in_scope_lifetimes = generics
155- . params
156- . iter ( )
157- . filter ( |param| {
158- matches ! ( param. kind, hir:: GenericParamKind :: Lifetime { .. } )
159- } )
160- . map ( |param| param. name )
161- . collect ( ) ;
162- }
163- hir:: ItemKind :: Trait ( _, _, ref generics, ..) => {
164- lctx. in_scope_lifetimes = generics
165- . params
166- . iter ( )
167- . filter ( |param| {
168- matches ! ( param. kind, hir:: GenericParamKind :: Lifetime { .. } )
169- } )
170- . map ( |param| param. name )
171- . collect ( ) ;
172151 }
173152 _ => { }
174153 } ;
@@ -276,7 +255,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
276255 ref body,
277256 ..
278257 } ) => {
279- let fn_def_id = self . resolver . local_def_id ( id) ;
280258 self . with_new_scopes ( |this| {
281259 this. current_item = Some ( ident. span ) ;
282260
@@ -288,20 +266,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
288266 let body_id =
289267 this. lower_maybe_async_body ( span, & decl, asyncness, body. as_deref ( ) ) ;
290268
291- let ( generics, decl) = this. add_in_band_defs (
292- generics,
293- fn_def_id,
294- AnonymousLifetimeMode :: PassThrough ,
295- |this, idty| {
269+ let ( generics, decl) =
270+ this. add_implicit_generics ( generics, id, |this, idty| {
296271 let ret_id = asyncness. opt_return_id ( ) ;
297- this. lower_fn_decl (
298- & decl,
299- Some ( ( fn_def_id, idty) ) ,
300- FnDeclKind :: Fn ,
301- ret_id,
302- )
303- } ,
304- ) ;
272+ this. lower_fn_decl ( & decl, Some ( ( id, idty) ) , FnDeclKind :: Fn , ret_id)
273+ } ) ;
305274 let sig = hir:: FnSig {
306275 decl,
307276 header : this. lower_fn_header ( header) ,
@@ -339,12 +308,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
339308 //
340309 // type Foo = Foo1
341310 // opaque type Foo1: Trait
342- let ty = self . lower_ty (
343- ty,
344- ImplTraitContext :: TypeAliasesOpaqueTy {
345- capturable_lifetimes : & mut FxHashSet :: default ( ) ,
346- } ,
347- ) ;
311+ let ty = self . lower_ty ( ty, ImplTraitContext :: TypeAliasesOpaqueTy ) ;
348312 let mut generics = generics. clone ( ) ;
349313 add_ty_alias_where_clause ( & mut generics, where_clauses, true ) ;
350314 let generics = self . lower_generics (
@@ -419,12 +383,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
419383 // method, it will not be considered an in-band
420384 // lifetime to be added, but rather a reference to a
421385 // parent lifetime.
422- let lowered_trait_def_id = hir_id. expect_owner ( ) ;
423- let ( generics, ( trait_ref, lowered_ty) ) = self . add_in_band_defs (
424- ast_generics,
425- lowered_trait_def_id,
426- AnonymousLifetimeMode :: CreateParameter ,
427- |this, _| {
386+ let ( generics, ( trait_ref, lowered_ty) ) =
387+ self . add_implicit_generics ( ast_generics, id, |this, _| {
428388 let trait_ref = trait_ref. as_ref ( ) . map ( |trait_ref| {
429389 this. lower_trait_ref (
430390 trait_ref,
@@ -436,16 +396,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
436396 . lower_ty ( ty, ImplTraitContext :: Disallowed ( ImplTraitPosition :: Type ) ) ;
437397
438398 ( trait_ref, lowered_ty)
439- } ,
440- ) ;
441-
442- let new_impl_items =
443- self . with_in_scope_lifetime_defs ( & ast_generics. params , |this| {
444- this. arena . alloc_from_iter (
445- impl_items. iter ( ) . map ( |item| this. lower_impl_item_ref ( item) ) ,
446- )
447399 } ) ;
448400
401+ let new_impl_items = self
402+ . arena
403+ . alloc_from_iter ( impl_items. iter ( ) . map ( |item| self . lower_impl_item_ref ( item) ) ) ;
404+
449405 // `defaultness.has_value()` is never called for an `impl`, always `true` in order
450406 // to not cause an assertion failure inside the `lower_defaultness` function.
451407 let has_val = true ;
@@ -692,18 +648,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
692648 kind : match i. kind {
693649 ForeignItemKind :: Fn ( box Fn { ref sig, ref generics, .. } ) => {
694650 let fdec = & sig. decl ;
695- let ( generics, ( fn_dec, fn_args) ) = self . add_in_band_defs (
696- generics,
697- def_id,
698- AnonymousLifetimeMode :: PassThrough ,
699- |this, _| {
651+ let ( generics, ( fn_dec, fn_args) ) =
652+ self . add_implicit_generics ( generics, i. id , |this, _| {
700653 (
701654 // Disallow `impl Trait` in foreign items.
702655 this. lower_fn_decl ( fdec, None , FnDeclKind :: ExternFn , None ) ,
703656 this. lower_fn_params_to_names ( fdec) ,
704657 )
705- } ,
706- ) ;
658+ } ) ;
707659
708660 hir:: ForeignItemKind :: Fn ( fn_dec, fn_args, generics)
709661 }
@@ -810,13 +762,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
810762 }
811763 AssocItemKind :: Fn ( box Fn { ref sig, ref generics, body : None , .. } ) => {
812764 let names = self . lower_fn_params_to_names ( & sig. decl ) ;
813- let ( generics, sig) = self . lower_method_sig (
814- generics,
815- sig,
816- trait_item_def_id,
817- FnDeclKind :: Trait ,
818- None ,
819- ) ;
765+ let ( generics, sig) =
766+ self . lower_method_sig ( generics, sig, i. id , FnDeclKind :: Trait , None ) ;
820767 ( generics, hir:: TraitItemKind :: Fn ( sig, hir:: TraitFn :: Required ( names) ) )
821768 }
822769 AssocItemKind :: Fn ( box Fn { ref sig, ref generics, body : Some ( ref body) , .. } ) => {
@@ -826,7 +773,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
826773 let ( generics, sig) = self . lower_method_sig (
827774 generics,
828775 sig,
829- trait_item_def_id ,
776+ i . id ,
830777 FnDeclKind :: Trait ,
831778 asyncness. opt_return_id ( ) ,
832779 ) ;
@@ -900,8 +847,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
900847 }
901848
902849 fn lower_impl_item ( & mut self , i : & AssocItem ) -> & ' hir hir:: ImplItem < ' hir > {
903- let impl_item_def_id = self . resolver . local_def_id ( i. id ) ;
904-
905850 let ( generics, kind) = match & i. kind {
906851 AssocItemKind :: Const ( _, ty, expr) => {
907852 let ty = self . lower_ty ( ty, ImplTraitContext :: Disallowed ( ImplTraitPosition :: Type ) ) ;
@@ -918,7 +863,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
918863 let ( generics, sig) = self . lower_method_sig (
919864 generics,
920865 sig,
921- impl_item_def_id ,
866+ i . id ,
922867 if self . is_in_trait_impl { FnDeclKind :: Impl } else { FnDeclKind :: Inherent } ,
923868 asyncness. opt_return_id ( ) ,
924869 ) ;
@@ -938,12 +883,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
938883 hir:: ImplItemKind :: TyAlias ( ty)
939884 }
940885 Some ( ty) => {
941- let ty = self . lower_ty (
942- ty,
943- ImplTraitContext :: TypeAliasesOpaqueTy {
944- capturable_lifetimes : & mut FxHashSet :: default ( ) ,
945- } ,
946- ) ;
886+ let ty = self . lower_ty ( ty, ImplTraitContext :: TypeAliasesOpaqueTy ) ;
947887 hir:: ImplItemKind :: TyAlias ( ty)
948888 }
949889 } ;
@@ -1283,17 +1223,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
12831223 & mut self ,
12841224 generics : & Generics ,
12851225 sig : & FnSig ,
1286- fn_def_id : LocalDefId ,
1226+ id : NodeId ,
12871227 kind : FnDeclKind ,
12881228 is_async : Option < NodeId > ,
12891229 ) -> ( hir:: Generics < ' hir > , hir:: FnSig < ' hir > ) {
12901230 let header = self . lower_fn_header ( sig. header ) ;
1291- let ( generics, decl) = self . add_in_band_defs (
1292- generics,
1293- fn_def_id,
1294- AnonymousLifetimeMode :: PassThrough ,
1295- |this, idty| this. lower_fn_decl ( & sig. decl , Some ( ( fn_def_id, idty) ) , kind, is_async) ,
1296- ) ;
1231+ let ( generics, decl) = self . add_implicit_generics ( generics, id, |this, idty| {
1232+ this. lower_fn_decl ( & sig. decl , Some ( ( id, idty) ) , kind, is_async)
1233+ } ) ;
12971234 ( generics, hir:: FnSig { header, decl, span : self . lower_span ( sig. span ) } )
12981235 }
12991236
@@ -1418,14 +1355,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
14181355 }
14191356
14201357 fn lower_where_clause ( & mut self , wc : & WhereClause ) -> hir:: WhereClause < ' hir > {
1421- self . with_anonymous_lifetime_mode ( AnonymousLifetimeMode :: ReportError , |this| {
1422- hir:: WhereClause {
1423- predicates : this. arena . alloc_from_iter (
1424- wc. predicates . iter ( ) . map ( |predicate| this. lower_where_predicate ( predicate) ) ,
1425- ) ,
1426- span : this. lower_span ( wc. span ) ,
1427- }
1428- } )
1358+ hir:: WhereClause {
1359+ predicates : self . arena . alloc_from_iter (
1360+ wc. predicates . iter ( ) . map ( |predicate| self . lower_where_predicate ( predicate) ) ,
1361+ ) ,
1362+ span : self . lower_span ( wc. span ) ,
1363+ }
14291364 }
14301365
14311366 fn lower_where_predicate ( & mut self , pred : & WherePredicate ) -> hir:: WherePredicate < ' hir > {
@@ -1435,24 +1370,20 @@ impl<'hir> LoweringContext<'_, 'hir> {
14351370 ref bounded_ty,
14361371 ref bounds,
14371372 span,
1438- } ) => self . with_in_scope_lifetime_defs ( & bound_generic_params, |this| {
1439- hir:: WherePredicate :: BoundPredicate ( hir:: WhereBoundPredicate {
1440- bound_generic_params : this. lower_generic_params (
1441- bound_generic_params,
1442- ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
1443- ) ,
1444- bounded_ty : this. lower_ty (
1445- bounded_ty,
1446- ImplTraitContext :: Disallowed ( ImplTraitPosition :: Type ) ,
1447- ) ,
1448- bounds : this. arena . alloc_from_iter ( bounds. iter ( ) . map ( |bound| {
1449- this. lower_param_bound (
1450- bound,
1451- ImplTraitContext :: Disallowed ( ImplTraitPosition :: Bound ) ,
1452- )
1453- } ) ) ,
1454- span : this. lower_span ( span) ,
1455- } )
1373+ } ) => hir:: WherePredicate :: BoundPredicate ( hir:: WhereBoundPredicate {
1374+ bound_generic_params : self . lower_generic_params (
1375+ bound_generic_params,
1376+ ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
1377+ ) ,
1378+ bounded_ty : self
1379+ . lower_ty ( bounded_ty, ImplTraitContext :: Disallowed ( ImplTraitPosition :: Type ) ) ,
1380+ bounds : self . arena . alloc_from_iter ( bounds. iter ( ) . map ( |bound| {
1381+ self . lower_param_bound (
1382+ bound,
1383+ ImplTraitContext :: Disallowed ( ImplTraitPosition :: Bound ) ,
1384+ )
1385+ } ) ) ,
1386+ span : self . lower_span ( span) ,
14561387 } ) ,
14571388 WherePredicate :: RegionPredicate ( WhereRegionPredicate {
14581389 ref lifetime,
0 commit comments