@@ -207,7 +207,7 @@ pub(crate) enum RibKind<'ra> {
207
207
/// All bindings in this rib are generic parameters that can't be used
208
208
/// from the default of a generic parameter because they're not declared
209
209
/// before said generic parameter. Also see the `visit_generics` override.
210
- ForwardGenericParamBan ,
210
+ ForwardGenericParamBan ( ForwardGenericParamBanReason ) ,
211
211
212
212
/// We are inside of the type of a const parameter. Can't refer to any
213
213
/// parameters.
@@ -218,6 +218,12 @@ pub(crate) enum RibKind<'ra> {
218
218
InlineAsmSym ,
219
219
}
220
220
221
+ #[ derive( Copy , Clone , PartialEq , Eq , Debug ) ]
222
+ pub ( crate ) enum ForwardGenericParamBanReason {
223
+ Default ,
224
+ ConstParamTy ,
225
+ }
226
+
221
227
impl RibKind < ' _ > {
222
228
/// Whether this rib kind contains generic parameters, as opposed to local
223
229
/// variables.
@@ -232,7 +238,7 @@ impl RibKind<'_> {
232
238
RibKind :: ConstParamTy
233
239
| RibKind :: AssocItem
234
240
| RibKind :: Item ( ..)
235
- | RibKind :: ForwardGenericParamBan => true ,
241
+ | RibKind :: ForwardGenericParamBan ( _ ) => true ,
236
242
}
237
243
}
238
244
@@ -246,7 +252,7 @@ impl RibKind<'_> {
246
252
| RibKind :: Item ( ..)
247
253
| RibKind :: ConstantItem ( ..)
248
254
| RibKind :: Module ( ..)
249
- | RibKind :: ForwardGenericParamBan
255
+ | RibKind :: ForwardGenericParamBan ( _ )
250
256
| RibKind :: ConstParamTy
251
257
| RibKind :: InlineAsmSym => true ,
252
258
}
@@ -1541,8 +1547,10 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
1541
1547
// provide previous type parameters as they're built. We
1542
1548
// put all the parameters on the ban list and then remove
1543
1549
// 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 ) ) ;
1546
1554
for param in params. iter ( ) {
1547
1555
match param. kind {
1548
1556
GenericParamKind :: Type { .. } => {
@@ -1573,16 +1581,24 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
1573
1581
forward_ty_ban_rib. bindings . insert ( Ident :: with_dummy_span ( kw:: SelfUpper ) , Res :: Err ) ;
1574
1582
}
1575
1583
1584
+ // NOTE: We use different ribs here not for a technical reason, but just
1585
+ // for better diagnostics.
1576
1586
let mut forward_ty_ban_rib_const_param_ty = Rib {
1577
1587
bindings : forward_ty_ban_rib. bindings . clone ( ) ,
1578
1588
patterns_with_skipped_bindings : FxHashMap :: default ( ) ,
1579
- kind : RibKind :: ConstParamTy ,
1589
+ kind : RibKind :: ForwardGenericParamBan ( ForwardGenericParamBanReason :: ConstParamTy ) ,
1580
1590
} ;
1581
1591
let mut forward_const_ban_rib_const_param_ty = Rib {
1582
1592
bindings : forward_const_ban_rib. bindings . clone ( ) ,
1583
1593
patterns_with_skipped_bindings : FxHashMap :: default ( ) ,
1584
- kind : RibKind :: ConstParamTy ,
1594
+ kind : RibKind :: ForwardGenericParamBan ( ForwardGenericParamBanReason :: ConstParamTy ) ,
1585
1595
} ;
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
+ }
1586
1602
1587
1603
self . with_lifetime_rib ( LifetimeRibKind :: AnonymousReportError , |this| {
1588
1604
for param in params {
@@ -1608,9 +1624,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
1608
1624
// Allow all following defaults to refer to this type parameter.
1609
1625
let i = & Ident :: with_dummy_span ( param. ident . name ) ;
1610
1626
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) ;
1614
1628
}
1615
1629
GenericParamKind :: Const { ref ty, kw_span : _, ref default } => {
1616
1630
// Const parameters can't have param bounds.
@@ -1621,9 +1635,13 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
1621
1635
if this. r . tcx . features ( ) . generic_const_parameter_types ( ) {
1622
1636
this. visit_ty ( ty)
1623
1637
} else {
1638
+ this. ribs [ TypeNS ] . push ( Rib :: new ( RibKind :: ConstParamTy ) ) ;
1639
+ this. ribs [ ValueNS ] . push ( Rib :: new ( RibKind :: ConstParamTy ) ) ;
1624
1640
this. with_lifetime_rib ( LifetimeRibKind :: ConstParamTy , |this| {
1625
1641
this. visit_ty ( ty)
1626
1642
} ) ;
1643
+ this. ribs [ TypeNS ] . pop ( ) . unwrap ( ) ;
1644
+ this. ribs [ ValueNS ] . pop ( ) . unwrap ( ) ;
1627
1645
}
1628
1646
forward_const_ban_rib_const_param_ty = this. ribs [ ValueNS ] . pop ( ) . unwrap ( ) ;
1629
1647
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> {
1642
1660
// Allow all following defaults to refer to this const parameter.
1643
1661
let i = & Ident :: with_dummy_span ( param. ident . name ) ;
1644
1662
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) ;
1648
1664
}
1649
1665
}
1650
1666
}
0 commit comments