@@ -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 ;
@@ -1684,7 +1695,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
1684
1695
// not used as part of the type system, this is far less surprising.
1685
1696
this. with_constant_rib (
1686
1697
IsRepeatExpr :: No ,
1687
- true ,
1698
+ HasGenericParams :: Yes ,
1688
1699
None ,
1689
1700
|this| this. visit_expr ( expr) ,
1690
1701
) ;
@@ -1763,7 +1774,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
1763
1774
// so it doesn't matter whether this is a trivial constant.
1764
1775
this. with_constant_rib (
1765
1776
IsRepeatExpr :: No ,
1766
- true ,
1777
+ HasGenericParams :: Yes ,
1767
1778
Some ( ( item. ident , constant_item_kind) ) ,
1768
1779
|this| this. visit_expr ( expr) ,
1769
1780
) ;
@@ -1909,20 +1920,23 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
1909
1920
// Note that we intentionally still forbid `[0; N + 1]` during
1910
1921
// name resolution so that we don't extend the future
1911
1922
// compat lint to new cases.
1923
+ #[ instrument( level = "debug" , skip( self , f) ) ]
1912
1924
fn with_constant_rib (
1913
1925
& mut self ,
1914
1926
is_repeat : IsRepeatExpr ,
1915
- is_trivial : bool ,
1927
+ may_use_generics : HasGenericParams ,
1916
1928
item : Option < ( Ident , ConstantItemKind ) > ,
1917
1929
f : impl FnOnce ( & mut Self ) ,
1918
1930
) {
1919
- debug ! ( "with_constant_rib: is_repeat={:?} is_trivial={}" , is_repeat, is_trivial) ;
1920
- self . with_rib ( ValueNS , ConstantItemRibKind ( is_trivial, item) , |this| {
1931
+ self . with_rib ( ValueNS , ConstantItemRibKind ( may_use_generics, item) , |this| {
1921
1932
this. with_rib (
1922
1933
TypeNS ,
1923
- ConstantItemRibKind ( is_repeat == IsRepeatExpr :: Yes || is_trivial, item) ,
1934
+ ConstantItemRibKind (
1935
+ may_use_generics. force_yes_if ( is_repeat == IsRepeatExpr :: Yes ) ,
1936
+ item,
1937
+ ) ,
1924
1938
|this| {
1925
- this. with_label_rib ( ConstantItemRibKind ( is_trivial , item) , f) ;
1939
+ this. with_label_rib ( ConstantItemRibKind ( may_use_generics , item) , f) ;
1926
1940
} ,
1927
1941
)
1928
1942
} ) ;
@@ -2064,7 +2078,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
2064
2078
// not used as part of the type system, this is far less surprising.
2065
2079
this. with_constant_rib (
2066
2080
IsRepeatExpr :: No ,
2067
- true ,
2081
+ HasGenericParams :: Yes ,
2068
2082
None ,
2069
2083
|this| {
2070
2084
visit:: walk_assoc_item (
@@ -3077,7 +3091,11 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
3077
3091
debug ! ( "resolve_anon_const {:?} is_repeat: {:?}" , constant, is_repeat) ;
3078
3092
self . with_constant_rib (
3079
3093
is_repeat,
3080
- constant. value . is_potential_trivial_const_param ( ) ,
3094
+ if constant. value . is_potential_trivial_const_param ( ) {
3095
+ HasGenericParams :: Yes
3096
+ } else {
3097
+ HasGenericParams :: No
3098
+ } ,
3081
3099
None ,
3082
3100
|this| visit:: walk_anon_const ( this, constant) ,
3083
3101
) ;
@@ -3180,7 +3198,11 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
3180
3198
if const_args. contains ( & idx) {
3181
3199
self . with_constant_rib (
3182
3200
IsRepeatExpr :: No ,
3183
- argument. is_potential_trivial_const_param ( ) ,
3201
+ if argument. is_potential_trivial_const_param ( ) {
3202
+ HasGenericParams :: Yes
3203
+ } else {
3204
+ HasGenericParams :: No
3205
+ } ,
3184
3206
None ,
3185
3207
|this| {
3186
3208
this. resolve_expr ( argument, None ) ;
0 commit comments