@@ -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
}
@@ -1561,8 +1567,10 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
1561
1567
// provide previous type parameters as they're built. We
1562
1568
// put all the parameters on the ban list and then remove
1563
1569
// 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 ) ) ;
1566
1574
for param in params. iter ( ) {
1567
1575
match param. kind {
1568
1576
GenericParamKind :: Type { .. } => {
@@ -1593,16 +1601,24 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
1593
1601
forward_ty_ban_rib. bindings . insert ( Ident :: with_dummy_span ( kw:: SelfUpper ) , Res :: Err ) ;
1594
1602
}
1595
1603
1604
+ // NOTE: We use different ribs here not for a technical reason, but just
1605
+ // for better diagnostics.
1596
1606
let mut forward_ty_ban_rib_const_param_ty = Rib {
1597
1607
bindings : forward_ty_ban_rib. bindings . clone ( ) ,
1598
1608
patterns_with_skipped_bindings : FxHashMap :: default ( ) ,
1599
- kind : RibKind :: ConstParamTy ,
1609
+ kind : RibKind :: ForwardGenericParamBan ( ForwardGenericParamBanReason :: ConstParamTy ) ,
1600
1610
} ;
1601
1611
let mut forward_const_ban_rib_const_param_ty = Rib {
1602
1612
bindings : forward_const_ban_rib. bindings . clone ( ) ,
1603
1613
patterns_with_skipped_bindings : FxHashMap :: default ( ) ,
1604
- kind : RibKind :: ConstParamTy ,
1614
+ kind : RibKind :: ForwardGenericParamBan ( ForwardGenericParamBanReason :: ConstParamTy ) ,
1605
1615
} ;
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
+ }
1606
1622
1607
1623
self . with_lifetime_rib ( LifetimeRibKind :: AnonymousReportError , |this| {
1608
1624
for param in params {
@@ -1628,9 +1644,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
1628
1644
// Allow all following defaults to refer to this type parameter.
1629
1645
let i = & Ident :: with_dummy_span ( param. ident . name ) ;
1630
1646
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) ;
1634
1648
}
1635
1649
GenericParamKind :: Const { ref ty, kw_span : _, ref default } => {
1636
1650
// Const parameters can't have param bounds.
@@ -1641,9 +1655,13 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
1641
1655
if this. r . tcx . features ( ) . generic_const_parameter_types ( ) {
1642
1656
this. visit_ty ( ty)
1643
1657
} else {
1658
+ this. ribs [ TypeNS ] . push ( Rib :: new ( RibKind :: ConstParamTy ) ) ;
1659
+ this. ribs [ ValueNS ] . push ( Rib :: new ( RibKind :: ConstParamTy ) ) ;
1644
1660
this. with_lifetime_rib ( LifetimeRibKind :: ConstParamTy , |this| {
1645
1661
this. visit_ty ( ty)
1646
1662
} ) ;
1663
+ this. ribs [ TypeNS ] . pop ( ) . unwrap ( ) ;
1664
+ this. ribs [ ValueNS ] . pop ( ) . unwrap ( ) ;
1647
1665
}
1648
1666
forward_const_ban_rib_const_param_ty = this. ribs [ ValueNS ] . pop ( ) . unwrap ( ) ;
1649
1667
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> {
1662
1680
// Allow all following defaults to refer to this const parameter.
1663
1681
let i = & Ident :: with_dummy_span ( param. ident . name ) ;
1664
1682
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) ;
1668
1684
}
1669
1685
}
1670
1686
}
0 commit comments