@@ -45,7 +45,7 @@ use rustc_ast::{self as ast, *};
45
45
use rustc_ast_pretty:: pprust;
46
46
use rustc_data_structures:: captures:: Captures ;
47
47
use rustc_data_structures:: fingerprint:: Fingerprint ;
48
- use rustc_data_structures:: fx:: { FxHashMap , FxHashSet , FxIndexMap } ;
48
+ use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
49
49
use rustc_data_structures:: sorted_map:: SortedMap ;
50
50
use rustc_data_structures:: stable_hasher:: { HashStable , StableHasher } ;
51
51
use rustc_data_structures:: sync:: Lrc ;
@@ -122,20 +122,6 @@ struct LoweringContext<'a, 'hir: 'a> {
122
122
is_in_trait_impl : bool ,
123
123
is_in_dyn_type : bool ,
124
124
125
- /// Used to create lifetime definitions for anonymous lifetimes.
126
- /// When an anonymous lifetime is encountered in a function or impl header and
127
- /// requires to create a fresh lifetime parameter, it is added
128
- /// to this list. The results of this list are then added to the list of
129
- /// lifetime definitions in the corresponding impl or function generics.
130
- lifetimes_to_define : FxIndexMap < NodeId , Span > ,
131
-
132
- /// If anonymous lifetimes are being collected, this field holds the parent
133
- /// `LocalDefId` to create the fresh lifetime parameters' `LocalDefId`.
134
- is_collecting_anonymous_lifetimes : Option < LocalDefId > ,
135
-
136
- /// Currently in-scope lifetimes defined in impl headers, fn headers, or HRTB.
137
- in_scope_lifetimes : Vec < ( ParamName , LocalDefId ) > ,
138
-
139
125
/// Used to handle lifetimes appearing in impl-traits.
140
126
captured_lifetimes : Option < LifetimeCaptureContext > ,
141
127
@@ -173,8 +159,6 @@ pub enum LifetimeRes {
173
159
Fresh {
174
160
/// Id of the generic parameter that introduced it.
175
161
param : LocalDefId ,
176
- /// Id to create the HirId. This is used when creating the `Fresh` lifetime parameters.
177
- introducer : Option < NodeId > ,
178
162
/// Id of the introducing place. See `Param`.
179
163
binder : NodeId ,
180
164
} ,
@@ -237,6 +221,9 @@ pub trait ResolverAstLowering {
237
221
/// Obtains resolution for a lifetime with the given `NodeId`.
238
222
fn get_lifetime_res ( & self , id : NodeId ) -> Option < LifetimeRes > ;
239
223
224
+ /// Obtain the list of lifetimes parameters to add to an item.
225
+ fn take_extra_lifetime_params ( & mut self , id : NodeId ) -> Vec < ( Ident , NodeId , LifetimeRes ) > ;
226
+
240
227
fn create_stable_hashing_context ( & self ) -> StableHashingContext < ' _ > ;
241
228
242
229
fn definitions ( & self ) -> & Definitions ;
@@ -694,46 +681,34 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
694
681
}
695
682
696
683
/// Converts a lifetime into a new generic parameter.
697
- fn fresh_lifetime_to_generic_param (
684
+ fn lifetime_res_to_generic_param (
698
685
& mut self ,
699
- span : Span ,
686
+ ident : Ident ,
700
687
node_id : NodeId ,
701
- ) -> hir:: GenericParam < ' hir > {
688
+ res : LifetimeRes ,
689
+ ) -> Option < hir:: GenericParam < ' hir > > {
690
+ let ( name, kind) = match res {
691
+ LifetimeRes :: Param { .. } => {
692
+ ( hir:: ParamName :: Plain ( ident) , hir:: LifetimeParamKind :: Explicit )
693
+ }
694
+ LifetimeRes :: Fresh { param, .. } => {
695
+ ( hir:: ParamName :: Fresh ( param) , hir:: LifetimeParamKind :: Elided )
696
+ }
697
+ LifetimeRes :: Static | LifetimeRes :: Error => return None ,
698
+ res => panic ! (
699
+ "Unexpected lifetime resolution {:?} for {:?} at {:?}" ,
700
+ res, ident, ident. span
701
+ ) ,
702
+ } ;
702
703
let hir_id = self . lower_node_id ( node_id) ;
703
- let def_id = self . resolver . local_def_id ( node_id) ;
704
- hir:: GenericParam {
704
+ Some ( hir:: GenericParam {
705
705
hir_id,
706
- name : hir :: ParamName :: Fresh ( def_id ) ,
706
+ name,
707
707
bounds : & [ ] ,
708
- span : self . lower_span ( span) ,
708
+ span : self . lower_span ( ident . span ) ,
709
709
pure_wrt_drop : false ,
710
- kind : hir:: GenericParamKind :: Lifetime { kind : hir:: LifetimeParamKind :: Elided } ,
711
- }
712
- }
713
-
714
- /// Evaluates `f` with the lifetimes in `params` in-scope.
715
- /// This is used to track which lifetimes have already been defined,
716
- /// which need to be duplicated for async fns.
717
- fn with_in_scope_lifetime_defs < T > (
718
- & mut self ,
719
- params : & [ GenericParam ] ,
720
- f : impl FnOnce ( & mut Self ) -> T ,
721
- ) -> T {
722
- let old_len = self . in_scope_lifetimes . len ( ) ;
723
- let lt_def_names = params. iter ( ) . filter_map ( |param| match param. kind {
724
- GenericParamKind :: Lifetime { .. } => {
725
- let def_id = self . resolver . local_def_id ( param. id ) ;
726
- let name = ParamName :: Plain ( param. ident ) ;
727
- Some ( ( name, def_id) )
728
- }
729
- _ => None ,
730
- } ) ;
731
- self . in_scope_lifetimes . extend ( lt_def_names) ;
732
-
733
- let res = f ( self ) ;
734
-
735
- self . in_scope_lifetimes . truncate ( old_len) ;
736
- res
710
+ kind : hir:: GenericParamKind :: Lifetime { kind } ,
711
+ } )
737
712
}
738
713
739
714
/// Creates a new `hir::GenericParam` for every new `Fresh` lifetime and
@@ -742,39 +717,23 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
742
717
fn add_implicit_generics < T > (
743
718
& mut self ,
744
719
generics : & Generics ,
745
- parent_def_id : LocalDefId ,
720
+ parent_node_id : NodeId ,
746
721
f : impl FnOnce ( & mut Self , & mut Vec < hir:: GenericParam < ' hir > > ) -> T ,
747
722
) -> ( hir:: Generics < ' hir > , T ) {
748
- let lifetime_stash = std:: mem:: take ( & mut self . lifetimes_to_define ) ;
749
- let was_collecting =
750
- std:: mem:: replace ( & mut self . is_collecting_anonymous_lifetimes , Some ( parent_def_id) ) ;
751
-
752
723
let mut impl_trait_defs = Vec :: new ( ) ;
724
+ let mut lowered_generics = self . lower_generics_mut (
725
+ generics,
726
+ ImplTraitContext :: Universal ( & mut impl_trait_defs, self . current_hir_id_owner ) ,
727
+ ) ;
728
+ let res = f ( self , & mut impl_trait_defs) ;
753
729
754
- let ( mut lowered_generics, res) =
755
- self . with_in_scope_lifetime_defs ( & generics. params , |this| {
756
- // Note: it is necessary to lower generics *before* calling `f`.
757
- // When lowering `async fn`, there's a final step when lowering
758
- // the return type that assumes that all in-scope lifetimes have
759
- // already been added to either `in_scope_lifetimes` or
760
- // `lifetimes_to_define`. If we swapped the order of these two,
761
- // fresh lifetimes introduced by generics or where-clauses
762
- // wouldn't have been added yet.
763
- let generics = this. lower_generics_mut (
764
- generics,
765
- ImplTraitContext :: Universal ( & mut impl_trait_defs, this. current_hir_id_owner ) ,
766
- ) ;
767
- let res = f ( this, & mut impl_trait_defs) ;
768
- ( generics, res)
769
- } ) ;
770
-
771
- self . is_collecting_anonymous_lifetimes = was_collecting;
772
- let lifetimes_to_define = std:: mem:: replace ( & mut self . lifetimes_to_define , lifetime_stash) ;
773
-
730
+ let extra_lifetimes = self . resolver . take_extra_lifetime_params ( parent_node_id) ;
774
731
lowered_generics. params . extend (
775
- lifetimes_to_define
732
+ extra_lifetimes
776
733
. into_iter ( )
777
- . map ( |( node_id, span) | self . fresh_lifetime_to_generic_param ( span, node_id) )
734
+ . filter_map ( |( ident, node_id, res) | {
735
+ self . lifetime_res_to_generic_param ( ident, node_id, res)
736
+ } )
778
737
. chain ( impl_trait_defs) ,
779
738
) ;
780
739
@@ -1227,19 +1186,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1227
1186
let lifetime = self . lower_lifetime ( & region) ;
1228
1187
hir:: TyKind :: Rptr ( lifetime, self . lower_mt ( mt, itctx) )
1229
1188
}
1230
- TyKind :: BareFn ( ref f) => self . with_in_scope_lifetime_defs ( & f. generic_params , |this| {
1231
- this. with_lifetime_binder ( t. id , |this| {
1232
- hir:: TyKind :: BareFn ( this. arena . alloc ( hir:: BareFnTy {
1233
- generic_params : this. lower_generic_params (
1234
- & f. generic_params ,
1235
- ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
1236
- ) ,
1237
- unsafety : this. lower_unsafety ( f. unsafety ) ,
1238
- abi : this. lower_extern ( f. ext ) ,
1239
- decl : this. lower_fn_decl ( & f. decl , None , FnDeclKind :: Pointer , None ) ,
1240
- param_names : this. lower_fn_params_to_names ( & f. decl ) ,
1241
- } ) )
1242
- } )
1189
+ TyKind :: BareFn ( ref f) => self . with_lifetime_binder ( t. id , |this| {
1190
+ hir:: TyKind :: BareFn ( this. arena . alloc ( hir:: BareFnTy {
1191
+ generic_params : this. lower_generic_params (
1192
+ & f. generic_params ,
1193
+ ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
1194
+ ) ,
1195
+ unsafety : this. lower_unsafety ( f. unsafety ) ,
1196
+ abi : this. lower_extern ( f. ext ) ,
1197
+ decl : this. lower_fn_decl ( & f. decl , None , FnDeclKind :: Pointer , None ) ,
1198
+ param_names : this. lower_fn_params_to_names ( & f. decl ) ,
1199
+ } ) )
1243
1200
} ) ,
1244
1201
TyKind :: Never => hir:: TyKind :: Never ,
1245
1202
TyKind :: Tup ( ref tys) => {
@@ -1676,70 +1633,50 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1676
1633
// should be figured out using the ordinary elision rules, and
1677
1634
// this desugaring achieves that.
1678
1635
1679
- debug ! ( "lower_async_fn_ret_ty: in_scope_lifetimes={:#?}" , self . in_scope_lifetimes) ;
1680
- debug ! ( "lower_async_fn_ret_ty: lifetimes_to_define={:#?}" , self . lifetimes_to_define) ;
1681
-
1682
1636
// Calculate all the lifetimes that should be captured
1683
1637
// by the opaque type. This should include all in-scope
1684
1638
// lifetime parameters, including those defined in-band.
1685
1639
1686
- // Input lifetime like `'a`:
1687
1640
let mut captures = FxHashMap :: default ( ) ;
1688
- for & ( p_name, def_id) in & self . in_scope_lifetimes {
1689
- let Ident { name, span } = p_name. ident ( ) ;
1690
- let node_id = self . resolver . next_node_id ( ) ;
1641
+
1642
+ let extra_lifetime_params = self . resolver . take_extra_lifetime_params ( opaque_ty_node_id) ;
1643
+ debug ! ( ?extra_lifetime_params) ;
1644
+ for ( ident, outer_node_id, outer_res) in extra_lifetime_params {
1645
+ let Ident { name, span } = ident;
1646
+ let outer_def_id = self . resolver . local_def_id ( outer_node_id) ;
1647
+ let inner_node_id = self . resolver . next_node_id ( ) ;
1691
1648
1692
1649
// Add a definition for the in scope lifetime def.
1693
1650
self . resolver . create_def (
1694
1651
opaque_ty_def_id,
1695
- node_id ,
1652
+ inner_node_id ,
1696
1653
DefPathData :: LifetimeNs ( name) ,
1697
1654
ExpnId :: root ( ) ,
1698
1655
span. with_parent ( None ) ,
1699
1656
) ;
1700
1657
1701
- let res = match p_name {
1702
- hir:: ParamName :: Plain ( _) => {
1703
- LifetimeRes :: Param { param : def_id, binder : fn_node_id }
1658
+ let ( p_name, inner_res) = match outer_res {
1659
+ // Input lifetime like `'a`:
1660
+ LifetimeRes :: Param { param, .. } => {
1661
+ ( hir:: ParamName :: Plain ( ident) , LifetimeRes :: Param { param, binder : fn_node_id } )
1704
1662
}
1705
- hir:: ParamName :: Fresh ( _) => {
1706
- LifetimeRes :: Fresh { param : def_id, introducer : None , binder : fn_node_id }
1663
+ // Input lifetime like `'1`:
1664
+ LifetimeRes :: Fresh { param, .. } => (
1665
+ hir:: ParamName :: Fresh ( outer_def_id) ,
1666
+ LifetimeRes :: Fresh { param, binder : fn_node_id } ,
1667
+ ) ,
1668
+ LifetimeRes :: Static | LifetimeRes :: Error => continue ,
1669
+ res => {
1670
+ panic ! ( "Unexpected lifetime resolution {:?} for {:?} at {:?}" , res, ident, span)
1707
1671
}
1708
- hir:: ParamName :: Error => LifetimeRes :: Error ,
1709
1672
} ;
1710
1673
1711
- captures. insert ( def_id, ( span, node_id, p_name, res) ) ;
1712
- }
1713
-
1714
- // Input lifetime like `'1`:
1715
- for ( & node_id, & span) in & self . lifetimes_to_define {
1716
- let def_id = self . resolver . local_def_id ( node_id) ;
1717
- let new_node_id = self . resolver . next_node_id ( ) ;
1718
-
1719
- // Add a definition for the `Fresh` lifetime def.
1720
- let new_def_id = self . resolver . create_def (
1721
- opaque_ty_def_id,
1722
- new_node_id,
1723
- DefPathData :: LifetimeNs ( kw:: UnderscoreLifetime ) ,
1724
- ExpnId :: root ( ) ,
1725
- span. with_parent ( None ) ,
1726
- ) ;
1727
-
1728
- captures. insert (
1729
- def_id,
1730
- (
1731
- span,
1732
- new_node_id,
1733
- hir:: ParamName :: Fresh ( new_def_id) ,
1734
- LifetimeRes :: Fresh { param : def_id, introducer : None , binder : fn_node_id } ,
1735
- ) ,
1736
- ) ;
1674
+ captures. insert ( outer_def_id, ( span, inner_node_id, p_name, inner_res) ) ;
1737
1675
}
1738
1676
1739
1677
debug ! ( ?captures) ;
1740
1678
1741
1679
self . with_hir_id_owner ( opaque_ty_node_id, |this| {
1742
- debug ! ( "lower_async_fn_ret_ty: lifetimes_to_define={:#?}" , this. lifetimes_to_define) ;
1743
1680
let future_bound =
1744
1681
this. while_capturing_lifetimes ( opaque_ty_def_id, & mut captures, |this| {
1745
1682
// We have to be careful to get elision right here. The
@@ -1923,11 +1860,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1923
1860
}
1924
1861
hir:: LifetimeName :: Param ( p_name)
1925
1862
}
1926
- LifetimeRes :: Fresh { mut param, introducer , binder } => {
1863
+ LifetimeRes :: Fresh { mut param, binder } => {
1927
1864
debug_assert_eq ! ( ident. name, kw:: UnderscoreLifetime ) ;
1928
- // Only items are allowed to introduce fresh lifetimes,
1929
- // so we know `binder` has a `LocalDefId`.
1930
- let binder_def_id = self . resolver . local_def_id ( binder) ;
1931
1865
if let Some ( LifetimeCaptureContext { parent_def_id, captures, binders_to_ignore } ) =
1932
1866
& mut self . captured_lifetimes
1933
1867
&& !binders_to_ignore. contains ( & binder)
@@ -1949,16 +1883,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1949
1883
param = p_def_id;
1950
1884
}
1951
1885
}
1952
- } else if let Some ( introducer) = introducer {
1953
- if self . is_collecting_anonymous_lifetimes == Some ( binder_def_id)
1954
- && self . resolver . opt_local_def_id ( introducer) == Some ( param)
1955
- {
1956
- debug ! (
1957
- "lifetime_to_define += id={:?} span={:?} res={:?}" ,
1958
- introducer, span, res
1959
- ) ;
1960
- self . lifetimes_to_define . insert ( introducer, span) ;
1961
- }
1962
1886
}
1963
1887
let p_name = ParamName :: Fresh ( param) ;
1964
1888
hir:: LifetimeName :: Param ( p_name)
@@ -2091,10 +2015,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
2091
2015
let bound_generic_params =
2092
2016
self . lower_generic_params ( & p. bound_generic_params , itctx. reborrow ( ) ) ;
2093
2017
2094
- let trait_ref = self . with_in_scope_lifetime_defs ( & p. bound_generic_params , |this| {
2095
- this. with_lifetime_binder ( p. trait_ref . ref_id , |this| {
2096
- this. lower_trait_ref ( & p. trait_ref , itctx. reborrow ( ) )
2097
- } )
2018
+ let trait_ref = self . with_lifetime_binder ( p. trait_ref . ref_id , |this| {
2019
+ this. lower_trait_ref ( & p. trait_ref , itctx. reborrow ( ) )
2098
2020
} ) ;
2099
2021
2100
2022
hir:: PolyTraitRef { bound_generic_params, trait_ref, span : self . lower_span ( p. span ) }
0 commit comments