@@ -94,6 +94,12 @@ crate enum HasGenericParams {
94
94
No ,
95
95
}
96
96
97
+ impl HasGenericParams {
98
+ fn force_yes_if ( self , b : bool ) -> Self {
99
+ if b { Self :: Yes } else { self }
100
+ }
101
+ }
102
+
97
103
#[ derive( Copy , Clone , Debug , Eq , PartialEq ) ]
98
104
crate enum ConstantItemKind {
99
105
Const ,
@@ -125,9 +131,9 @@ crate enum RibKind<'a> {
125
131
126
132
/// We're in a constant item. Can't refer to dynamic stuff.
127
133
///
128
- /// The `bool` indicates if this constant may reference generic parameters
129
- /// and is used to only allow generic parameters to be used in trivial constant expressions .
130
- ConstantItemRibKind ( bool , Option < ( Ident , ConstantItemKind ) > ) ,
134
+ /// The item may reference generic parameters in trivial constant expressions.
135
+ /// All other constants aren't allowed to use generic params at all .
136
+ ConstantItemRibKind ( HasGenericParams , Option < ( Ident , ConstantItemKind ) > ) ,
131
137
132
138
/// We passed through a module.
133
139
ModuleRibKind ( Module < ' a > ) ,
@@ -826,19 +832,24 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
826
832
// Note that we might not be inside of an repeat expression here,
827
833
// but considering that `IsRepeatExpr` is only relevant for
828
834
// non-trivial constants this is doesn't matter.
829
- self . with_constant_rib ( IsRepeatExpr :: No , true , None , |this| {
830
- this. smart_resolve_path (
831
- ty. id ,
832
- qself. as_ref ( ) ,
833
- path,
834
- PathSource :: Expr ( None ) ,
835
- ) ;
836
-
837
- if let Some ( ref qself) = * qself {
838
- this. visit_ty ( & qself. ty ) ;
839
- }
840
- this. visit_path ( path, ty. id ) ;
841
- } ) ;
835
+ self . with_constant_rib (
836
+ IsRepeatExpr :: No ,
837
+ HasGenericParams :: Yes ,
838
+ None ,
839
+ |this| {
840
+ this. smart_resolve_path (
841
+ ty. id ,
842
+ qself. as_ref ( ) ,
843
+ path,
844
+ PathSource :: Expr ( None ) ,
845
+ ) ;
846
+
847
+ if let Some ( ref qself) = * qself {
848
+ this. visit_ty ( & qself. ty ) ;
849
+ }
850
+ this. visit_path ( path, ty. id ) ;
851
+ } ,
852
+ ) ;
842
853
843
854
self . diagnostic_metadata . currently_processing_generics = prev;
844
855
return ;
@@ -1688,7 +1699,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
1688
1699
// not used as part of the type system, this is far less surprising.
1689
1700
this. with_constant_rib (
1690
1701
IsRepeatExpr :: No ,
1691
- true ,
1702
+ HasGenericParams :: Yes ,
1692
1703
None ,
1693
1704
|this| this. visit_expr ( expr) ,
1694
1705
) ;
@@ -1767,7 +1778,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
1767
1778
// so it doesn't matter whether this is a trivial constant.
1768
1779
this. with_constant_rib (
1769
1780
IsRepeatExpr :: No ,
1770
- true ,
1781
+ HasGenericParams :: Yes ,
1771
1782
Some ( ( item. ident , constant_item_kind) ) ,
1772
1783
|this| this. visit_expr ( expr) ,
1773
1784
) ;
@@ -1913,20 +1924,23 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
1913
1924
// Note that we intentionally still forbid `[0; N + 1]` during
1914
1925
// name resolution so that we don't extend the future
1915
1926
// compat lint to new cases.
1927
+ #[ instrument( level = "debug" , skip( self , f) ) ]
1916
1928
fn with_constant_rib (
1917
1929
& mut self ,
1918
1930
is_repeat : IsRepeatExpr ,
1919
- is_trivial : bool ,
1931
+ may_use_generics : HasGenericParams ,
1920
1932
item : Option < ( Ident , ConstantItemKind ) > ,
1921
1933
f : impl FnOnce ( & mut Self ) ,
1922
1934
) {
1923
- debug ! ( "with_constant_rib: is_repeat={:?} is_trivial={}" , is_repeat, is_trivial) ;
1924
- self . with_rib ( ValueNS , ConstantItemRibKind ( is_trivial, item) , |this| {
1935
+ self . with_rib ( ValueNS , ConstantItemRibKind ( may_use_generics, item) , |this| {
1925
1936
this. with_rib (
1926
1937
TypeNS ,
1927
- ConstantItemRibKind ( is_repeat == IsRepeatExpr :: Yes || is_trivial, item) ,
1938
+ ConstantItemRibKind (
1939
+ may_use_generics. force_yes_if ( is_repeat == IsRepeatExpr :: Yes ) ,
1940
+ item,
1941
+ ) ,
1928
1942
|this| {
1929
- this. with_label_rib ( ConstantItemRibKind ( is_trivial , item) , f) ;
1943
+ this. with_label_rib ( ConstantItemRibKind ( may_use_generics , item) , f) ;
1930
1944
} ,
1931
1945
)
1932
1946
} ) ;
@@ -2068,7 +2082,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
2068
2082
// not used as part of the type system, this is far less surprising.
2069
2083
this. with_constant_rib (
2070
2084
IsRepeatExpr :: No ,
2071
- true ,
2085
+ HasGenericParams :: Yes ,
2072
2086
None ,
2073
2087
|this| {
2074
2088
visit:: walk_assoc_item (
@@ -3081,7 +3095,11 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
3081
3095
debug ! ( "resolve_anon_const {:?} is_repeat: {:?}" , constant, is_repeat) ;
3082
3096
self . with_constant_rib (
3083
3097
is_repeat,
3084
- constant. value . is_potential_trivial_const_param ( ) ,
3098
+ if constant. value . is_potential_trivial_const_param ( ) {
3099
+ HasGenericParams :: Yes
3100
+ } else {
3101
+ HasGenericParams :: No
3102
+ } ,
3085
3103
None ,
3086
3104
|this| visit:: walk_anon_const ( this, constant) ,
3087
3105
) ;
@@ -3184,7 +3202,11 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
3184
3202
if const_args. contains ( & idx) {
3185
3203
self . with_constant_rib (
3186
3204
IsRepeatExpr :: No ,
3187
- argument. is_potential_trivial_const_param ( ) ,
3205
+ if argument. is_potential_trivial_const_param ( ) {
3206
+ HasGenericParams :: Yes
3207
+ } else {
3208
+ HasGenericParams :: No
3209
+ } ,
3188
3210
None ,
3189
3211
|this| {
3190
3212
this. resolve_expr ( argument, None ) ;
0 commit comments