1010>   ;  ; ` } `
1111>
1212> _ TraitItem_ :\
13- >   ;  ; [ _ OuterAttribute_ ] <sup >\* </sup > (\
13+ >   ;  ; [ _ OuterAttribute_ ] <sup >\* </sup > [ _ Visibility _ ] < sup >?</ sup > (\
1414>   ;  ;   ;  ;   ;  ; _ TraitFunc_ \
1515>   ;  ;   ;  ; | _ TraitMethod_ \
1616>   ;  ;   ;  ; | _ TraitConst_ \
@@ -204,6 +204,42 @@ trait T {
204204}
205205```
206206
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+
207243[ IDENTIFIER ] : ../identifiers.md
208244[ WildcardPattern ] : ../patterns.md#wildcard-pattern
209245[ _BlockExpression_ ] : ../expressions/block-expr.md
@@ -217,6 +253,7 @@ trait T {
217253[ _SelfParam_ ] : associated-items.md#methods
218254[ _TypeParamBounds_ ] : ../trait-bounds.md
219255[ _Type_ ] : ../types.md#type-expressions
256+ [ _Visibility_ ] : ../visibility-and-privacy.md
220257[ _WhereClause_ ] : generics.md#where-clauses
221258[ bounds ] : ../trait-bounds.md
222259[ trait object ] : ../types/trait-object.md
0 commit comments