@@ -337,6 +337,7 @@ enum LifetimeBinderKind {
337
337
PolyTrait ,
338
338
WhereBound ,
339
339
Item ,
340
+ ConstItem ,
340
341
Function ,
341
342
Closure ,
342
343
ImplBlock ,
@@ -349,7 +350,7 @@ impl LifetimeBinderKind {
349
350
BareFnType => "type" ,
350
351
PolyTrait => "bound" ,
351
352
WhereBound => "bound" ,
352
- Item => "item" ,
353
+ Item | ConstItem => "item" ,
353
354
ImplBlock => "impl block" ,
354
355
Function => "function" ,
355
356
Closure => "closure" ,
@@ -2404,30 +2405,44 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
2404
2405
} ) ;
2405
2406
}
2406
2407
2407
- ItemKind :: Static ( box ast:: StaticItem { ref ty, ref expr, .. } )
2408
- | ItemKind :: Const ( box ast:: ConstItem { ref ty, ref expr, .. } ) => {
2408
+ ItemKind :: Static ( box ast:: StaticItem { ref ty, ref expr, .. } ) => {
2409
2409
self . with_static_rib ( |this| {
2410
2410
this. with_lifetime_rib ( LifetimeRibKind :: Elided ( LifetimeRes :: Static ) , |this| {
2411
2411
this. visit_ty ( ty) ;
2412
2412
} ) ;
2413
- this. with_lifetime_rib ( LifetimeRibKind :: Elided ( LifetimeRes :: Infer ) , |this| {
2413
+ if let Some ( expr) = expr {
2414
+ // We already forbid generic params because of the above item rib,
2415
+ // so it doesn't matter whether this is a trivial constant.
2416
+ this. resolve_const_body ( expr, Some ( ( item. ident , ConstantItemKind :: Static ) ) ) ;
2417
+ }
2418
+ } ) ;
2419
+ }
2420
+
2421
+ ItemKind :: Const ( box ast:: ConstItem { ref generics, ref ty, ref expr, .. } ) => {
2422
+ self . with_generic_param_rib (
2423
+ & generics. params ,
2424
+ RibKind :: Item ( HasGenericParams :: Yes ( generics. span ) ) ,
2425
+ LifetimeRibKind :: Generics {
2426
+ binder : item. id ,
2427
+ kind : LifetimeBinderKind :: ConstItem ,
2428
+ span : generics. span ,
2429
+ } ,
2430
+ |this| {
2431
+ this. visit_generics ( generics) ;
2432
+
2433
+ this. with_lifetime_rib (
2434
+ LifetimeRibKind :: Elided ( LifetimeRes :: Static ) ,
2435
+ |this| this. visit_ty ( ty) ,
2436
+ ) ;
2437
+
2414
2438
if let Some ( expr) = expr {
2415
- let constant_item_kind = match item. kind {
2416
- ItemKind :: Const ( ..) => ConstantItemKind :: Const ,
2417
- ItemKind :: Static ( ..) => ConstantItemKind :: Static ,
2418
- _ => unreachable ! ( ) ,
2419
- } ;
2420
- // We already forbid generic params because of the above item rib,
2421
- // so it doesn't matter whether this is a trivial constant.
2422
- this. with_constant_rib (
2423
- IsRepeatExpr :: No ,
2424
- ConstantHasGenerics :: Yes ,
2425
- Some ( ( item. ident , constant_item_kind) ) ,
2426
- |this| this. visit_expr ( expr) ,
2439
+ this. resolve_const_body (
2440
+ expr,
2441
+ Some ( ( item. ident , ConstantItemKind :: Const ) ) ,
2427
2442
) ;
2428
2443
}
2429
- } ) ;
2430
- } ) ;
2444
+ } ,
2445
+ ) ;
2431
2446
}
2432
2447
2433
2448
ItemKind :: Use ( ref use_tree) => {
@@ -2700,28 +2715,31 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
2700
2715
for item in trait_items {
2701
2716
self . resolve_doc_links ( & item. attrs , MaybeExported :: Ok ( item. id ) ) ;
2702
2717
match & item. kind {
2703
- AssocItemKind :: Const ( box ast:: ConstItem { ty, expr, .. } ) => {
2704
- self . visit_ty ( ty) ;
2705
- // Only impose the restrictions of `ConstRibKind` for an
2706
- // actual constant expression in a provided default.
2707
- if let Some ( expr) = expr {
2708
- // We allow arbitrary const expressions inside of associated consts,
2709
- // even if they are potentially not const evaluatable.
2710
- //
2711
- // Type parameters can already be used and as associated consts are
2712
- // not used as part of the type system, this is far less surprising.
2713
- self . with_lifetime_rib (
2714
- LifetimeRibKind :: Elided ( LifetimeRes :: Infer ) ,
2715
- |this| {
2716
- this. with_constant_rib (
2717
- IsRepeatExpr :: No ,
2718
- ConstantHasGenerics :: Yes ,
2719
- None ,
2720
- |this| this. visit_expr ( expr) ,
2721
- )
2722
- } ,
2723
- ) ;
2724
- }
2718
+ AssocItemKind :: Const ( box ast:: ConstItem { generics, ty, expr, .. } ) => {
2719
+ self . with_generic_param_rib (
2720
+ & generics. params ,
2721
+ RibKind :: AssocItem ,
2722
+ LifetimeRibKind :: Generics {
2723
+ binder : item. id ,
2724
+ span : generics. span ,
2725
+ kind : LifetimeBinderKind :: ConstItem ,
2726
+ } ,
2727
+ |this| {
2728
+ this. visit_generics ( generics) ;
2729
+ this. visit_ty ( ty) ;
2730
+
2731
+ // Only impose the restrictions of `ConstRibKind` for an
2732
+ // actual constant expression in a provided default.
2733
+ if let Some ( expr) = expr {
2734
+ // We allow arbitrary const expressions inside of associated consts,
2735
+ // even if they are potentially not const evaluatable.
2736
+ //
2737
+ // Type parameters can already be used and as associated consts are
2738
+ // not used as part of the type system, this is far less surprising.
2739
+ this. resolve_const_body ( expr, None ) ;
2740
+ }
2741
+ } ,
2742
+ ) ;
2725
2743
}
2726
2744
AssocItemKind :: Fn ( box Fn { generics, .. } ) => {
2727
2745
walk_assoc_item ( self , generics, LifetimeBinderKind :: Function , item) ;
@@ -2876,36 +2894,42 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
2876
2894
use crate :: ResolutionError :: * ;
2877
2895
self . resolve_doc_links ( & item. attrs , MaybeExported :: ImplItem ( trait_id. ok_or ( & item. vis ) ) ) ;
2878
2896
match & item. kind {
2879
- AssocItemKind :: Const ( box ast:: ConstItem { ty, expr, .. } ) => {
2897
+ AssocItemKind :: Const ( box ast:: ConstItem { generics , ty, expr, .. } ) => {
2880
2898
debug ! ( "resolve_implementation AssocItemKind::Const" ) ;
2881
- // If this is a trait impl, ensure the const
2882
- // exists in trait
2883
- self . check_trait_item (
2884
- item. id ,
2885
- item. ident ,
2886
- & item. kind ,
2887
- ValueNS ,
2888
- item. span ,
2889
- seen_trait_items,
2890
- |i, s, c| ConstNotMemberOfTrait ( i, s, c) ,
2891
- ) ;
2892
2899
2893
- self . visit_ty ( ty) ;
2894
- if let Some ( expr) = expr {
2895
- // We allow arbitrary const expressions inside of associated consts,
2896
- // even if they are potentially not const evaluatable.
2897
- //
2898
- // Type parameters can already be used and as associated consts are
2899
- // not used as part of the type system, this is far less surprising.
2900
- self . with_lifetime_rib ( LifetimeRibKind :: Elided ( LifetimeRes :: Infer ) , |this| {
2901
- this. with_constant_rib (
2902
- IsRepeatExpr :: No ,
2903
- ConstantHasGenerics :: Yes ,
2904
- None ,
2905
- |this| this. visit_expr ( expr) ,
2906
- )
2907
- } ) ;
2908
- }
2900
+ self . with_generic_param_rib (
2901
+ & generics. params ,
2902
+ RibKind :: AssocItem ,
2903
+ LifetimeRibKind :: Generics {
2904
+ binder : item. id ,
2905
+ span : generics. span ,
2906
+ kind : LifetimeBinderKind :: ConstItem ,
2907
+ } ,
2908
+ |this| {
2909
+ // If this is a trait impl, ensure the const
2910
+ // exists in trait
2911
+ this. check_trait_item (
2912
+ item. id ,
2913
+ item. ident ,
2914
+ & item. kind ,
2915
+ ValueNS ,
2916
+ item. span ,
2917
+ seen_trait_items,
2918
+ |i, s, c| ConstNotMemberOfTrait ( i, s, c) ,
2919
+ ) ;
2920
+
2921
+ this. visit_generics ( generics) ;
2922
+ this. visit_ty ( ty) ;
2923
+ if let Some ( expr) = expr {
2924
+ // We allow arbitrary const expressions inside of associated consts,
2925
+ // even if they are potentially not const evaluatable.
2926
+ //
2927
+ // Type parameters can already be used and as associated consts are
2928
+ // not used as part of the type system, this is far less surprising.
2929
+ this. resolve_const_body ( expr, None ) ;
2930
+ }
2931
+ } ,
2932
+ ) ;
2909
2933
}
2910
2934
AssocItemKind :: Fn ( box Fn { generics, .. } ) => {
2911
2935
debug ! ( "resolve_implementation AssocItemKind::Fn" ) ;
@@ -3063,6 +3087,14 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
3063
3087
) ;
3064
3088
}
3065
3089
3090
+ fn resolve_const_body ( & mut self , expr : & ' ast Expr , item : Option < ( Ident , ConstantItemKind ) > ) {
3091
+ self . with_lifetime_rib ( LifetimeRibKind :: Elided ( LifetimeRes :: Infer ) , |this| {
3092
+ this. with_constant_rib ( IsRepeatExpr :: No , ConstantHasGenerics :: Yes , item, |this| {
3093
+ this. visit_expr ( expr)
3094
+ } ) ;
3095
+ } )
3096
+ }
3097
+
3066
3098
fn resolve_params ( & mut self , params : & ' ast [ Param ] ) {
3067
3099
let mut bindings = smallvec ! [ ( PatBoundCtx :: Product , Default :: default ( ) ) ] ;
3068
3100
self . with_lifetime_rib ( LifetimeRibKind :: Elided ( LifetimeRes :: Infer ) , |this| {
@@ -4448,6 +4480,7 @@ impl<'ast> Visitor<'ast> for LifetimeCountVisitor<'_, '_, '_> {
4448
4480
fn visit_item ( & mut self , item : & ' ast Item ) {
4449
4481
match & item. kind {
4450
4482
ItemKind :: TyAlias ( box TyAlias { ref generics, .. } )
4483
+ | ItemKind :: Const ( box ConstItem { ref generics, .. } )
4451
4484
| ItemKind :: Fn ( box Fn { ref generics, .. } )
4452
4485
| ItemKind :: Enum ( _, ref generics)
4453
4486
| ItemKind :: Struct ( _, ref generics)
@@ -4467,7 +4500,6 @@ impl<'ast> Visitor<'ast> for LifetimeCountVisitor<'_, '_, '_> {
4467
4500
ItemKind :: Mod ( ..)
4468
4501
| ItemKind :: ForeignMod ( ..)
4469
4502
| ItemKind :: Static ( ..)
4470
- | ItemKind :: Const ( ..)
4471
4503
| ItemKind :: Use ( ..)
4472
4504
| ItemKind :: ExternCrate ( ..)
4473
4505
| ItemKind :: MacroDef ( ..)
0 commit comments