Skip to content

Commit 7e9d4de

Browse files
committed
Update for visibility syntax changes.
1 parent 45558c4 commit 7e9d4de

File tree

2 files changed

+71
-2
lines changed

2 files changed

+71
-2
lines changed

src/items/enumerations.md

+33-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
> &nbsp;&nbsp; _EnumItem_ ( `,` _EnumItem_ )<sup>\*</sup> `,`<sup>?</sup>
1313
>
1414
> _EnumItem_ :\
15-
> &nbsp;&nbsp; _OuterAttribute_<sup>\*</sup>\
15+
> &nbsp;&nbsp; _OuterAttribute_<sup>\*</sup> [_Visibility_]<sup>?</sup>\
1616
> &nbsp;&nbsp; [IDENTIFIER]&nbsp;( _EnumItemTuple_ | _EnumItemStruct_
1717
> | _EnumItemDiscriminant_ )<sup>?</sup>
1818
>
@@ -131,12 +131,44 @@ no valid values, they cannot be instantiated.
131131
enum ZeroVariants {}
132132
```
133133

134+
## Variant visibility
135+
136+
Enum variants syntactically allow a [_Visibility_] annotation, but this is
137+
rejected when the enum is validated. This allows items to be parsed with a
138+
unified syntax across different contexts where they are used.
139+
140+
```rust
141+
macro_rules! mac_variant {
142+
($vis:vis $name:ident) => {
143+
enum $name {
144+
$vis Unit,
145+
146+
$vis Tuple(u8, u16),
147+
148+
$vis Struct { f: u8 },
149+
}
150+
}
151+
}
152+
153+
// Empty `vis` is allowed.
154+
mac_variant! { E }
155+
156+
// This is allowed, since it is removed before being validated.
157+
#[cfg(FALSE)]
158+
enum E {
159+
pub U,
160+
pub(crate) T(u8),
161+
pub(super) T { f: String }
162+
}
163+
```
164+
134165
[IDENTIFIER]: ../identifiers.md
135166
[_Generics_]: generics.md
136167
[_WhereClause_]: generics.md#where-clauses
137168
[_Expression_]: ../expressions.md
138169
[_TupleFields_]: structs.md
139170
[_StructFields_]: structs.md
171+
[_Visibility_]: ../visibility-and-privacy.md
140172
[enumerated type]: ../types/enum.md
141173
[`mem::discriminant`]: ../../std/mem/fn.discriminant.html
142174
[numeric cast]: ../expressions/operator-expr.md#semantics

src/items/traits.md

+38-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
> &nbsp;&nbsp; `}`
1111
>
1212
> _TraitItem_ :\
13-
> &nbsp;&nbsp; [_OuterAttribute_]<sup>\*</sup> (\
13+
> &nbsp;&nbsp; [_OuterAttribute_]<sup>\*</sup> [_Visibility_]<sup>?</sup> (\
1414
> &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; _TraitFunc_\
1515
> &nbsp;&nbsp; &nbsp;&nbsp; | _TraitMethod_\
1616
> &nbsp;&nbsp; &nbsp;&nbsp; | _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

Comments
 (0)