@@ -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> {
218218InlineAsmSym ,
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<'_> {
232238RibKind :: 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}
@@ -1561,8 +1567,10 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
15611567// provide previous type parameters as they're built. We
15621568// put all the parameters on the ban list and then remove
15631569// them one by one as they are processed and become available.
1564- let mut forward_ty_ban_rib = Rib :: new ( RibKind :: ForwardGenericParamBan ) ;
1565- let mut forward_const_ban_rib = Rib :: new ( RibKind :: ForwardGenericParamBan ) ;
1570+ let mut forward_ty_ban_rib =
1571+ Rib :: new ( RibKind :: ForwardGenericParamBan ( ForwardGenericParamBanReason :: Default ) ) ;
1572+ let mut forward_const_ban_rib =
1573+ Rib :: new ( RibKind :: ForwardGenericParamBan ( ForwardGenericParamBanReason :: Default ) ) ;
15661574for param in params. iter ( ) {
15671575match param. kind {
15681576GenericParamKind :: Type { .. } => {
@@ -1593,16 +1601,24 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
15931601 forward_ty_ban_rib. bindings . insert ( Ident :: with_dummy_span ( kw:: SelfUpper ) , Res :: Err ) ;
15941602}
15951603
1604+ // NOTE: We use different ribs here not for a technical reason, but just
1605+ // for better diagnostics.
15961606let mut forward_ty_ban_rib_const_param_ty = Rib {
15971607bindings : forward_ty_ban_rib. bindings . clone ( ) ,
15981608patterns_with_skipped_bindings : FxHashMap :: default ( ) ,
1599- kind : RibKind :: ConstParamTy ,
1609+ kind : RibKind :: ForwardGenericParamBan ( ForwardGenericParamBanReason :: ConstParamTy ) ,
16001610} ;
16011611let mut forward_const_ban_rib_const_param_ty = Rib {
16021612bindings : forward_const_ban_rib. bindings . clone ( ) ,
16031613patterns_with_skipped_bindings : FxHashMap :: default ( ) ,
1604- kind : RibKind :: ConstParamTy ,
1614+ kind : RibKind :: ForwardGenericParamBan ( ForwardGenericParamBanReason :: ConstParamTy ) ,
16051615} ;
1616+ // We'll ban these with a `ConstParamTy` rib, so just clear these ribs for better
1617+ // diagnostics, so we don't mention anything about const param tys having generics at all.
1618+ if !self . r . tcx . features ( ) . generic_const_parameter_types ( ) {
1619+ forward_ty_ban_rib_const_param_ty. bindings . clear ( ) ;
1620+ forward_const_ban_rib_const_param_ty. bindings . clear ( ) ;
1621+ }
16061622
16071623self . with_lifetime_rib ( LifetimeRibKind :: AnonymousReportError , |this| {
16081624for param in params {
@@ -1628,9 +1644,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
16281644// Allow all following defaults to refer to this type parameter.
16291645let i = & Ident :: with_dummy_span ( param. ident . name ) ;
16301646 forward_ty_ban_rib. bindings . remove ( i) ;
1631- if this. r . tcx . features ( ) . generic_const_parameter_types ( ) {
1632- forward_ty_ban_rib_const_param_ty. bindings . remove ( i) ;
1633- }
1647+ forward_ty_ban_rib_const_param_ty. bindings . remove ( i) ;
16341648}
16351649GenericParamKind :: Const { ref ty, kw_span : _, ref default } => {
16361650// Const parameters can't have param bounds.
@@ -1641,9 +1655,13 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
16411655if this. r . tcx . features ( ) . generic_const_parameter_types ( ) {
16421656 this. visit_ty ( ty)
16431657} else {
1658+ this. ribs [ TypeNS ] . push ( Rib :: new ( RibKind :: ConstParamTy ) ) ;
1659+ this. ribs [ ValueNS ] . push ( Rib :: new ( RibKind :: ConstParamTy ) ) ;
16441660 this. with_lifetime_rib ( LifetimeRibKind :: ConstParamTy , |this| {
16451661 this. visit_ty ( ty)
16461662} ) ;
1663+ this. ribs [ TypeNS ] . pop ( ) . unwrap ( ) ;
1664+ this. ribs [ ValueNS ] . pop ( ) . unwrap ( ) ;
16471665}
16481666 forward_const_ban_rib_const_param_ty = this. ribs [ ValueNS ] . pop ( ) . unwrap ( ) ;
16491667 forward_ty_ban_rib_const_param_ty = this. ribs [ TypeNS ] . pop ( ) . unwrap ( ) ;
@@ -1662,9 +1680,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
16621680// Allow all following defaults to refer to this const parameter.
16631681let i = & Ident :: with_dummy_span ( param. ident . name ) ;
16641682 forward_const_ban_rib. bindings . remove ( i) ;
1665- if this. r . tcx . features ( ) . generic_const_parameter_types ( ) {
1666- forward_const_ban_rib_const_param_ty. bindings . remove ( i) ;
1667- }
1683+ forward_const_ban_rib_const_param_ty. bindings . remove ( i) ;
16681684}
16691685}
16701686}
0 commit comments