@@ -207,7 +207,7 @@ pub(crate) enum RibKind<'ra> {
207207 /// All bindings in this rib are generic parameters that can't be used
208208 /// from the default of a generic parameter because they're not declared
209209 /// before said generic parameter. Also see the `visit_generics` override.
210- ForwardGenericParamBan ,
210+ ForwardGenericParamBan ( ForwardGenericParamBanReason ) ,
211211
212212 /// We are inside of the type of a const parameter. Can't refer to any
213213 /// parameters.
@@ -218,6 +218,12 @@ pub(crate) enum RibKind<'ra> {
218218 InlineAsmSym ,
219219}
220220
221+ #[ derive( Copy , Clone , PartialEq , Eq , Debug ) ]
222+ pub ( crate ) enum ForwardGenericParamBanReason {
223+ Default ,
224+ ConstParamTy ,
225+ }
226+
221227impl RibKind < ' _ > {
222228 /// Whether this rib kind contains generic parameters, as opposed to local
223229 /// variables.
@@ -232,7 +238,7 @@ impl RibKind<'_> {
232238 RibKind :: ConstParamTy
233239 | RibKind :: AssocItem
234240 | RibKind :: Item ( ..)
235- | RibKind :: ForwardGenericParamBan => true ,
241+ | RibKind :: ForwardGenericParamBan ( _ ) => true ,
236242 }
237243 }
238244
@@ -246,7 +252,7 @@ impl RibKind<'_> {
246252 | RibKind :: Item ( ..)
247253 | RibKind :: ConstantItem ( ..)
248254 | RibKind :: Module ( ..)
249- | RibKind :: ForwardGenericParamBan
255+ | RibKind :: ForwardGenericParamBan ( _ )
250256 | RibKind :: ConstParamTy
251257 | RibKind :: InlineAsmSym => true ,
252258 }
@@ -1541,8 +1547,10 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
15411547 // provide previous type parameters as they're built. We
15421548 // put all the parameters on the ban list and then remove
15431549 // them one by one as they are processed and become available.
1544- let mut forward_ty_ban_rib = Rib :: new ( RibKind :: ForwardGenericParamBan ) ;
1545- let mut forward_const_ban_rib = Rib :: new ( RibKind :: ForwardGenericParamBan ) ;
1550+ let mut forward_ty_ban_rib =
1551+ Rib :: new ( RibKind :: ForwardGenericParamBan ( ForwardGenericParamBanReason :: Default ) ) ;
1552+ let mut forward_const_ban_rib =
1553+ Rib :: new ( RibKind :: ForwardGenericParamBan ( ForwardGenericParamBanReason :: Default ) ) ;
15461554 for param in params. iter ( ) {
15471555 match param. kind {
15481556 GenericParamKind :: Type { .. } => {
@@ -1573,16 +1581,24 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
15731581 forward_ty_ban_rib. bindings . insert ( Ident :: with_dummy_span ( kw:: SelfUpper ) , Res :: Err ) ;
15741582 }
15751583
1584+ // NOTE: We use different ribs here not for a technical reason, but just
1585+ // for better diagnostics.
15761586 let mut forward_ty_ban_rib_const_param_ty = Rib {
15771587 bindings : forward_ty_ban_rib. bindings . clone ( ) ,
15781588 patterns_with_skipped_bindings : FxHashMap :: default ( ) ,
1579- kind : RibKind :: ConstParamTy ,
1589+ kind : RibKind :: ForwardGenericParamBan ( ForwardGenericParamBanReason :: ConstParamTy ) ,
15801590 } ;
15811591 let mut forward_const_ban_rib_const_param_ty = Rib {
15821592 bindings : forward_const_ban_rib. bindings . clone ( ) ,
15831593 patterns_with_skipped_bindings : FxHashMap :: default ( ) ,
1584- kind : RibKind :: ConstParamTy ,
1594+ kind : RibKind :: ForwardGenericParamBan ( ForwardGenericParamBanReason :: ConstParamTy ) ,
15851595 } ;
1596+ // We'll ban these with a `ConstParamTy` rib, so just clear these ribs for better
1597+ // diagnostics, so we don't mention anything about const param tys having generics at all.
1598+ if !self . r . tcx . features ( ) . generic_const_parameter_types ( ) {
1599+ forward_ty_ban_rib_const_param_ty. bindings . clear ( ) ;
1600+ forward_const_ban_rib_const_param_ty. bindings . clear ( ) ;
1601+ }
15861602
15871603 self . with_lifetime_rib ( LifetimeRibKind :: AnonymousReportError , |this| {
15881604 for param in params {
@@ -1608,9 +1624,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
16081624 // Allow all following defaults to refer to this type parameter.
16091625 let i = & Ident :: with_dummy_span ( param. ident . name ) ;
16101626 forward_ty_ban_rib. bindings . remove ( i) ;
1611- if this. r . tcx . features ( ) . generic_const_parameter_types ( ) {
1612- forward_ty_ban_rib_const_param_ty. bindings . remove ( i) ;
1613- }
1627+ forward_ty_ban_rib_const_param_ty. bindings . remove ( i) ;
16141628 }
16151629 GenericParamKind :: Const { ref ty, kw_span : _, ref default } => {
16161630 // Const parameters can't have param bounds.
@@ -1621,9 +1635,13 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
16211635 if this. r . tcx . features ( ) . generic_const_parameter_types ( ) {
16221636 this. visit_ty ( ty)
16231637 } else {
1638+ this. ribs [ TypeNS ] . push ( Rib :: new ( RibKind :: ConstParamTy ) ) ;
1639+ this. ribs [ ValueNS ] . push ( Rib :: new ( RibKind :: ConstParamTy ) ) ;
16241640 this. with_lifetime_rib ( LifetimeRibKind :: ConstParamTy , |this| {
16251641 this. visit_ty ( ty)
16261642 } ) ;
1643+ this. ribs [ TypeNS ] . pop ( ) . unwrap ( ) ;
1644+ this. ribs [ ValueNS ] . pop ( ) . unwrap ( ) ;
16271645 }
16281646 forward_const_ban_rib_const_param_ty = this. ribs [ ValueNS ] . pop ( ) . unwrap ( ) ;
16291647 forward_ty_ban_rib_const_param_ty = this. ribs [ TypeNS ] . pop ( ) . unwrap ( ) ;
@@ -1642,9 +1660,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
16421660 // Allow all following defaults to refer to this const parameter.
16431661 let i = & Ident :: with_dummy_span ( param. ident . name ) ;
16441662 forward_const_ban_rib. bindings . remove ( i) ;
1645- if this. r . tcx . features ( ) . generic_const_parameter_types ( ) {
1646- forward_const_ban_rib_const_param_ty. bindings . remove ( i) ;
1647- }
1663+ forward_const_ban_rib_const_param_ty. bindings . remove ( i) ;
16481664 }
16491665 }
16501666 }
0 commit comments