Skip to content

Commit d229cca

Browse files
committed
Update for visibility syntax changes.
1 parent faac114 commit d229cca

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
>
@@ -140,12 +140,44 @@ let x: ZeroVariants = panic!();
140140
let y: u32 = x; // mismatched type error
141141
```
142142

143+
## Variant visibility
144+
145+
Enum variants syntactically allow a [_Visibility_] annotation, but this is
146+
rejected when the enum is validated. This allows items to be parsed with a
147+
unified syntax across different contexts where they are used.
148+
149+
```rust
150+
macro_rules! mac_variant {
151+
($vis:vis $name:ident) => {
152+
enum $name {
153+
$vis Unit,
154+
155+
$vis Tuple(u8, u16),
156+
157+
$vis Struct { f: u8 },
158+
}
159+
}
160+
}
161+
162+
// Empty `vis` is allowed.
163+
mac_variant! { E }
164+
165+
// This is allowed, since it is removed before being validated.
166+
#[cfg(FALSE)]
167+
enum E {
168+
pub U,
169+
pub(crate) T(u8),
170+
pub(super) T { f: String }
171+
}
172+
```
173+
143174
[IDENTIFIER]: ../identifiers.md
144175
[_Generics_]: generics.md
145176
[_WhereClause_]: generics.md#where-clauses
146177
[_Expression_]: ../expressions.md
147178
[_TupleFields_]: structs.md
148179
[_StructFields_]: structs.md
180+
[_Visibility_]: ../visibility-and-privacy.md
149181
[enumerated type]: ../types/enum.md
150182
[`mem::discriminant`]: ../../std/mem/fn.discriminant.html
151183
[never type]: ../types/never.md

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)