@@ -38,29 +38,14 @@ use std::mem;
38
38
use std:: ops:: { Deref , DerefMut } ;
39
39
use thin_vec:: thin_vec;
40
40
41
- use crate :: errors;
41
+ use crate :: errors:: { self , TildeConstReason } ;
42
42
43
43
/// Is `self` allowed semantically as the first parameter in an `FnDecl`?
44
44
enum SelfSemantic {
45
45
Yes ,
46
46
No ,
47
47
}
48
48
49
- /// What is the context that prevents using `~const`?
50
- // FIXME(effects): Consider getting rid of this in favor of `errors::TildeConstReason`, they're
51
- // almost identical. This gets rid of an abstraction layer which might be considered bad.
52
- enum DisallowTildeConstContext < ' a > {
53
- TraitObject ,
54
- Fn ( FnKind < ' a > ) ,
55
- Trait ( Span ) ,
56
- TraitImpl ( Span ) ,
57
- Impl ( Span ) ,
58
- TraitAssocTy ( Span ) ,
59
- TraitImplAssocTy ( Span ) ,
60
- InherentAssocTy ( Span ) ,
61
- Item ,
62
- }
63
-
64
49
enum TraitOrTraitImpl {
65
50
Trait { span : Span , constness : Option < Span > } ,
66
51
TraitImpl { constness : Const , polarity : ImplPolarity , trait_ref : Span } ,
@@ -92,7 +77,7 @@ struct AstValidator<'a> {
92
77
/// e.g., `impl Iterator<Item = impl Debug>`.
93
78
outer_impl_trait : Option < Span > ,
94
79
95
- disallow_tilde_const : Option < DisallowTildeConstContext < ' a > > ,
80
+ disallow_tilde_const : Option < TildeConstReason > ,
96
81
97
82
/// Used to ban `impl Trait` in path projections like `<impl Iterator>::Item`
98
83
/// or `Foo::Bar<impl Trait>`
@@ -145,7 +130,7 @@ impl<'a> AstValidator<'a> {
145
130
146
131
fn with_tilde_const (
147
132
& mut self ,
148
- disallowed : Option < DisallowTildeConstContext < ' a > > ,
133
+ disallowed : Option < TildeConstReason > ,
149
134
f : impl FnOnce ( & mut Self ) ,
150
135
) {
151
136
let old = mem:: replace ( & mut self . disallow_tilde_const , disallowed) ;
@@ -224,7 +209,7 @@ impl<'a> AstValidator<'a> {
224
209
}
225
210
}
226
211
TyKind :: TraitObject ( ..) => self
227
- . with_tilde_const ( Some ( DisallowTildeConstContext :: TraitObject ) , |this| {
212
+ . with_tilde_const ( Some ( TildeConstReason :: TraitObject ) , |this| {
228
213
visit:: walk_ty ( this, t)
229
214
} ) ,
230
215
TyKind :: Path ( qself, path) => {
@@ -980,7 +965,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
980
965
this. visit_vis ( & item. vis ) ;
981
966
this. visit_ident ( item. ident ) ;
982
967
let disallowed = matches ! ( constness, Const :: No )
983
- . then ( || DisallowTildeConstContext :: TraitImpl ( item. span ) ) ;
968
+ . then ( || TildeConstReason :: TraitImpl { span : item. span } ) ;
984
969
this. with_tilde_const ( disallowed, |this| this. visit_generics ( generics) ) ;
985
970
this. visit_trait_ref ( t) ;
986
971
this. visit_ty ( self_ty) ;
@@ -1035,7 +1020,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
1035
1020
this. visit_vis ( & item. vis ) ;
1036
1021
this. visit_ident ( item. ident ) ;
1037
1022
this. with_tilde_const (
1038
- Some ( DisallowTildeConstContext :: Impl ( item. span ) ) ,
1023
+ Some ( TildeConstReason :: Impl { span : item. span } ) ,
1039
1024
|this| this. visit_generics ( generics) ,
1040
1025
) ;
1041
1026
this. visit_ty ( self_ty) ;
@@ -1154,7 +1139,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
1154
1139
this. visit_ident ( item. ident ) ;
1155
1140
let disallowed = is_const_trait
1156
1141
. is_none ( )
1157
- . then ( || DisallowTildeConstContext :: Trait ( item. span ) ) ;
1142
+ . then ( || TildeConstReason :: Trait { span : item. span } ) ;
1158
1143
this. with_tilde_const ( disallowed, |this| {
1159
1144
this. visit_generics ( generics) ;
1160
1145
walk_list ! ( this, visit_param_bound, bounds, BoundKind :: SuperTraits )
@@ -1399,40 +1384,8 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
1399
1384
self . dcx ( ) . emit_err ( errors:: ConstBoundTraitObject { span : trait_ref. span } ) ;
1400
1385
}
1401
1386
( _, BoundConstness :: Maybe ( span) , BoundPolarity :: Positive )
1402
- if let Some ( reason) = & self . disallow_tilde_const =>
1387
+ if let Some ( reason) = self . disallow_tilde_const =>
1403
1388
{
1404
- let reason = match reason {
1405
- DisallowTildeConstContext :: Fn ( FnKind :: Closure ( ..) ) => {
1406
- errors:: TildeConstReason :: Closure
1407
- }
1408
- DisallowTildeConstContext :: Fn ( FnKind :: Fn ( _, ident, ..) ) => {
1409
- errors:: TildeConstReason :: Function { ident : ident. span }
1410
- }
1411
- & DisallowTildeConstContext :: Trait ( span) => {
1412
- errors:: TildeConstReason :: Trait { span }
1413
- }
1414
- & DisallowTildeConstContext :: TraitImpl ( span) => {
1415
- errors:: TildeConstReason :: TraitImpl { span }
1416
- }
1417
- & DisallowTildeConstContext :: Impl ( span) => {
1418
- // FIXME(effects): Consider providing a help message or even a structured
1419
- // suggestion for moving such bounds to the assoc const fns if available.
1420
- errors:: TildeConstReason :: Impl { span }
1421
- }
1422
- & DisallowTildeConstContext :: TraitAssocTy ( span) => {
1423
- errors:: TildeConstReason :: TraitAssocTy { span }
1424
- }
1425
- & DisallowTildeConstContext :: TraitImplAssocTy ( span) => {
1426
- errors:: TildeConstReason :: TraitImplAssocTy { span }
1427
- }
1428
- & DisallowTildeConstContext :: InherentAssocTy ( span) => {
1429
- errors:: TildeConstReason :: InherentAssocTy { span }
1430
- }
1431
- DisallowTildeConstContext :: TraitObject => {
1432
- errors:: TildeConstReason :: TraitObject
1433
- }
1434
- DisallowTildeConstContext :: Item => errors:: TildeConstReason :: Item ,
1435
- } ;
1436
1389
self . dcx ( ) . emit_err ( errors:: TildeConstDisallowed { span, reason } ) ;
1437
1390
}
1438
1391
(
@@ -1569,7 +1522,10 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
1569
1522
. and_then ( TraitOrTraitImpl :: constness)
1570
1523
. is_some ( ) ;
1571
1524
1572
- let disallowed = ( !tilde_const_allowed) . then ( || DisallowTildeConstContext :: Fn ( fk) ) ;
1525
+ let disallowed = ( !tilde_const_allowed) . then ( || match fk {
1526
+ FnKind :: Fn ( _, ident, _, _, _, _) => TildeConstReason :: Function { ident : ident. span } ,
1527
+ FnKind :: Closure ( _, _, _) => TildeConstReason :: Closure ,
1528
+ } ) ;
1573
1529
self . with_tilde_const ( disallowed, |this| visit:: walk_fn ( this, fk) ) ;
1574
1530
}
1575
1531
@@ -1664,12 +1620,12 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
1664
1620
AssocItemKind :: Type ( _) => {
1665
1621
let disallowed = ( !parent_is_const) . then ( || match self . outer_trait_or_trait_impl {
1666
1622
Some ( TraitOrTraitImpl :: Trait { .. } ) => {
1667
- DisallowTildeConstContext :: TraitAssocTy ( item. span )
1623
+ TildeConstReason :: TraitAssocTy { span : item. span }
1668
1624
}
1669
1625
Some ( TraitOrTraitImpl :: TraitImpl { .. } ) => {
1670
- DisallowTildeConstContext :: TraitImplAssocTy ( item. span )
1626
+ TildeConstReason :: TraitImplAssocTy { span : item. span }
1671
1627
}
1672
- None => DisallowTildeConstContext :: InherentAssocTy ( item. span ) ,
1628
+ None => TildeConstReason :: InherentAssocTy { span : item. span } ,
1673
1629
} ) ;
1674
1630
self . with_tilde_const ( disallowed, |this| {
1675
1631
this. with_in_trait_impl ( None , |this| visit:: walk_assoc_item ( this, item, ctxt) )
@@ -1852,7 +1808,7 @@ pub fn check_crate(
1852
1808
outer_trait_or_trait_impl : None ,
1853
1809
has_proc_macro_decls : false ,
1854
1810
outer_impl_trait : None ,
1855
- disallow_tilde_const : Some ( DisallowTildeConstContext :: Item ) ,
1811
+ disallow_tilde_const : Some ( TildeConstReason :: Item ) ,
1856
1812
is_impl_trait_banned : false ,
1857
1813
extern_mod_safety : None ,
1858
1814
lint_buffer : lints,
0 commit comments