10
10
>   ;  ; ` } `
11
11
>
12
12
> _ TraitItem_ :\
13
- >   ;  ; [ _ OuterAttribute_ ] <sup >\* </sup > (\
13
+ >   ;  ; [ _ OuterAttribute_ ] <sup >\* </sup > [ _ Visibility _ ] < sup >?</ sup > (\
14
14
>   ;  ;   ;  ;   ;  ; _ TraitFunc_ \
15
15
>   ;  ;   ;  ; | _ TraitMethod_ \
16
16
>   ;  ;   ;  ; | _ TraitConst_ \
@@ -204,6 +204,42 @@ trait T {
204
204
}
205
205
```
206
206
207
+ ## Item visibility
208
+
209
+ Trait items syntactically allow a [ _ Visibility_ ] annotation, but this is
210
+ rejected when the trait is validated. This allows items to be parsed with a
211
+ unified syntax across different contexts where they are used. As an example,
212
+ an empty ` vis ` macro fragment specifier can be used for trait items, where the
213
+ macro rule may be used in other situations where visibility is allowed.
214
+
215
+ ``` rust
216
+ macro_rules! create_method {
217
+ ($ vis : vis $ name : ident ) => {
218
+ $ vis fn $ name (& self ) {}
219
+ };
220
+ }
221
+
222
+ trait T1 {
223
+ // Empty `vis` is allowed.
224
+ create_method! { method_of_t1 }
225
+ }
226
+
227
+ struct S ;
228
+
229
+ impl S {
230
+ // Visibility is allowed here.
231
+ create_method! { pub method_of_s }
232
+ }
233
+
234
+ impl T1 for S {}
235
+
236
+ fn main () {
237
+ let s = S ;
238
+ s . method_of_t1 ();
239
+ s . method_of_s ();
240
+ }
241
+ ```
242
+
207
243
[ IDENTIFIER ] : ../identifiers.md
208
244
[ WildcardPattern ] : ../patterns.md#wildcard-pattern
209
245
[ _BlockExpression_ ] : ../expressions/block-expr.md
@@ -217,6 +253,7 @@ trait T {
217
253
[ _SelfParam_ ] : associated-items.md#methods
218
254
[ _TypeParamBounds_ ] : ../trait-bounds.md
219
255
[ _Type_ ] : ../types.md#type-expressions
256
+ [ _Visibility_ ] : ../visibility-and-privacy.md
220
257
[ _WhereClause_ ] : generics.md#where-clauses
221
258
[ bounds ] : ../trait-bounds.md
222
259
[ trait object ] : ../types/trait-object.md
0 commit comments