@@ -6,7 +6,7 @@ use rustc_hir::def_id::DefId;
66use rustc_middle:: ty:: GenericParamDefKind ;
77use rustc_middle:: { bug, ty} ;
88use rustc_span:: symbol:: kw;
9- use rustc_span:: { Ident , Span } ;
9+ use rustc_span:: { Ident , Span , Symbol } ;
1010
1111use crate :: { LoweringContext , ResolverAstLoweringExt } ;
1212
@@ -25,22 +25,37 @@ pub(super) enum DelegationGenericsKind {
2525 TraitImpl ( bool /* Has user-specified args */ ) ,
2626}
2727
28+ #[ derive( Debug , Clone , Copy ) ]
29+ pub ( super ) enum GenericsPosition {
30+ Parent ,
31+ Child ,
32+ }
33+
2834pub ( super ) struct DelegationGenerics < T > {
2935 generics : T ,
3036 kind : DelegationGenericsKind ,
37+ pos : GenericsPosition ,
3138}
3239
3340impl < ' hir > DelegationGenerics < & ' hir [ ty:: GenericParamDef ] > {
34- fn default ( generics : & ' hir [ ty:: GenericParamDef ] ) -> Self {
35- DelegationGenerics { generics, kind : DelegationGenericsKind :: Default }
41+ fn default ( generics : & ' hir [ ty:: GenericParamDef ] , pos : GenericsPosition ) -> Self {
42+ DelegationGenerics { generics, pos , kind : DelegationGenericsKind :: Default }
3643 }
3744
38- fn user_specified ( generics : & ' hir [ ty:: GenericParamDef ] ) -> Self {
39- DelegationGenerics { generics, kind : DelegationGenericsKind :: UserSpecified }
45+ fn user_specified ( generics : & ' hir [ ty:: GenericParamDef ] , pos : GenericsPosition ) -> Self {
46+ DelegationGenerics { generics, pos , kind : DelegationGenericsKind :: UserSpecified }
4047 }
4148
42- fn trait_impl ( generics : & ' hir [ ty:: GenericParamDef ] , user_specified : bool ) -> Self {
43- DelegationGenerics { generics, kind : DelegationGenericsKind :: TraitImpl ( user_specified) }
49+ fn trait_impl (
50+ generics : & ' hir [ ty:: GenericParamDef ] ,
51+ user_specified : bool ,
52+ pos : GenericsPosition ,
53+ ) -> Self {
54+ DelegationGenerics {
55+ generics,
56+ pos,
57+ kind : DelegationGenericsKind :: TraitImpl ( user_specified) ,
58+ }
4459 }
4560}
4661
@@ -100,9 +115,16 @@ impl<'hir> HirOrTyGenerics<'hir> {
100115 ctx : & mut LoweringContext < ' _ , ' hir , impl ResolverAstLoweringExt < ' hir > > ,
101116 span : Span ,
102117 ) -> & mut HirOrTyGenerics < ' hir > {
118+ let rename_self = matches ! ( self . position( ) , GenericsPosition :: Child ) ;
119+
103120 if let HirOrTyGenerics :: Ty ( ty) = self {
104- let params = ctx. uplift_delegation_generic_params ( span, ty. generics ) ;
105- * self = HirOrTyGenerics :: Hir ( DelegationGenerics { generics : params, kind : ty. kind } ) ;
121+ let params = ctx. uplift_delegation_generic_params ( span, ty. generics , rename_self) ;
122+
123+ * self = HirOrTyGenerics :: Hir ( DelegationGenerics {
124+ generics : params,
125+ kind : ty. kind ,
126+ pos : ty. pos ,
127+ } ) ;
106128 }
107129
108130 self
@@ -118,14 +140,14 @@ impl<'hir> HirOrTyGenerics<'hir> {
118140 pub ( super ) fn into_generic_args (
119141 & self ,
120142 ctx : & mut LoweringContext < ' _ , ' hir , impl ResolverAstLoweringExt < ' hir > > ,
121- add_lifetimes : bool ,
122143 span : Span ,
123144 ) -> & ' hir hir:: GenericArgs < ' hir > {
124145 match self {
125146 HirOrTyGenerics :: Ty ( _) => {
126147 bug ! ( "Attempting to get generic args before uplifting to HIR" )
127148 }
128149 HirOrTyGenerics :: Hir ( hir) => {
150+ let add_lifetimes = matches ! ( self . position( ) , GenericsPosition :: Parent ) ;
129151 ctx. create_generics_args_from_params ( hir. generics . params , add_lifetimes, span)
130152 }
131153 }
@@ -137,6 +159,13 @@ impl<'hir> HirOrTyGenerics<'hir> {
137159 HirOrTyGenerics :: Hir ( hir) => hir. kind . args_propagation_details ( ) ,
138160 }
139161 }
162+
163+ pub ( super ) fn position ( & self ) -> GenericsPosition {
164+ match self {
165+ HirOrTyGenerics :: Ty ( ty) => ty. pos ,
166+ HirOrTyGenerics :: Hir ( hir) => hir. pos ,
167+ }
168+ }
140169}
141170
142171impl < ' hir > GenericsGenerationResult < ' hir > {
@@ -224,10 +253,15 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
224253 if matches ! ( delegation_parent_kind, DefKind :: Impl { of_trait: true } ) {
225254 // Considering parent generics, during signature inheritance
226255 // we will take those args that are in trait impl header trait ref.
227- let parent = DelegationGenerics :: trait_impl ( & [ ] , true ) ;
256+ let parent = DelegationGenerics :: trait_impl ( & [ ] , true , GenericsPosition :: Parent ) ;
228257 let parent = GenericsGenerationResult :: new ( parent) ;
229258
230- let child = DelegationGenerics :: trait_impl ( sig_params, child_user_specified) ;
259+ let child = DelegationGenerics :: trait_impl (
260+ sig_params,
261+ child_user_specified,
262+ GenericsPosition :: Child ,
263+ ) ;
264+
231265 let child = GenericsGenerationResult :: new ( child) ;
232266
233267 return GenericsGenerationResults { parent, child } ;
@@ -254,25 +288,32 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
254288 DelegationGenerics {
255289 kind : DelegationGenericsKind :: SelfAndUserSpecified ,
256290 generics : & sig_parent_params[ ..1 ] ,
291+ pos : GenericsPosition :: Parent ,
257292 }
258293 } else {
259- DelegationGenerics :: user_specified ( & [ ] )
294+ DelegationGenerics :: user_specified ( & [ ] , GenericsPosition :: Parent )
260295 }
261296 } else {
262297 let skip_self = usize:: from ( !generate_self) ;
263- DelegationGenerics :: default ( & sig_parent_params[ skip_self..] )
298+ DelegationGenerics :: default (
299+ & sig_parent_params[ skip_self..] ,
300+ GenericsPosition :: Parent ,
301+ )
264302 }
265303 } else {
266- DelegationGenerics :: default ( & [ ] )
304+ DelegationGenerics :: default ( & [ ] , GenericsPosition :: Parent )
267305 } ;
268306
269307 let child_generics = if child_user_specified {
270308 let synth_params_index =
271309 sig_params. iter ( ) . position ( |p| p. kind . is_synthetic ( ) ) . unwrap_or ( sig_params. len ( ) ) ;
272310
273- DelegationGenerics :: user_specified ( & sig_params[ synth_params_index..] )
311+ DelegationGenerics :: user_specified (
312+ & sig_params[ synth_params_index..] ,
313+ GenericsPosition :: Child ,
314+ )
274315 } else {
275- DelegationGenerics :: default ( sig_params)
316+ DelegationGenerics :: default ( sig_params, GenericsPosition :: Child )
276317 } ;
277318
278319 GenericsGenerationResults {
@@ -285,6 +326,7 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
285326 & mut self ,
286327 span : Span ,
287328 params : & ' hir [ ty:: GenericParamDef ] ,
329+ rename_self : bool ,
288330 ) -> & ' hir hir:: Generics < ' hir > {
289331 let params = self . arena . alloc_from_iter ( params. iter ( ) . map ( |p| {
290332 let def_kind = match p. kind {
@@ -293,7 +335,22 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
293335 GenericParamDefKind :: Const { .. } => DefKind :: ConstParam ,
294336 } ;
295337
296- let param_ident = Ident :: new ( p. name , span) ;
338+ // Rename Self generic param to This so it is properly propagated.
339+ // If the user will create a function `fn foo<Self>() {}` with generic
340+ // param "Self" then it will not be generated in HIR, the same thing
341+ // applies to traits, `trait Trait<Self> {}` will be represented as
342+ // `trait Trait {}` in HIR and "unexpected keyword `Self` in generic parameters"
343+ // error will be emitted.
344+ // Note that we do not rename `Self` to `This` after first reuse from Trait,
345+ // in this case the Self should not be propagated
346+ // and it is OK to have Self generic param generated during lowering.
347+ let param_name = if rename_self && p. name == kw:: SelfUpper {
348+ Symbol :: intern ( & format ! ( "This" ) )
349+ } else {
350+ p. name
351+ } ;
352+
353+ let param_ident = Ident :: new ( param_name, span) ;
297354 let def_name = Some ( param_ident. name ) ;
298355 let node_id = self . next_node_id ( ) ;
299356
0 commit comments