From d229cca2dd3ec7006461e94f4e4287a68329ecbd Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Sun, 24 Nov 2019 07:50:04 -0800 Subject: [PATCH] Update for visibility syntax changes. --- src/items/enumerations.md | 34 +++++++++++++++++++++++++++++++++- src/items/traits.md | 39 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 71 insertions(+), 2 deletions(-) diff --git a/src/items/enumerations.md b/src/items/enumerations.md index ec4003f5338ab..25dfecb656452 100644 --- a/src/items/enumerations.md +++ b/src/items/enumerations.md @@ -12,7 +12,7 @@ >    _EnumItem_ ( `,` _EnumItem_ )\* `,`? > > _EnumItem_ :\ ->    _OuterAttribute_\*\ +>    _OuterAttribute_\* [_Visibility_]?\ >    [IDENTIFIER] ( _EnumItemTuple_ | _EnumItemStruct_ > | _EnumItemDiscriminant_ )? > @@ -140,12 +140,44 @@ let x: ZeroVariants = panic!(); let y: u32 = x; // mismatched type error ``` +## Variant visibility + +Enum variants syntactically allow a [_Visibility_] annotation, but this is +rejected when the enum is validated. This allows items to be parsed with a +unified syntax across different contexts where they are used. + +```rust +macro_rules! mac_variant { + ($vis:vis $name:ident) => { + enum $name { + $vis Unit, + + $vis Tuple(u8, u16), + + $vis Struct { f: u8 }, + } + } +} + +// Empty `vis` is allowed. +mac_variant! { E } + +// This is allowed, since it is removed before being validated. +#[cfg(FALSE)] +enum E { + pub U, + pub(crate) T(u8), + pub(super) T { f: String } +} +``` + [IDENTIFIER]: ../identifiers.md [_Generics_]: generics.md [_WhereClause_]: generics.md#where-clauses [_Expression_]: ../expressions.md [_TupleFields_]: structs.md [_StructFields_]: structs.md +[_Visibility_]: ../visibility-and-privacy.md [enumerated type]: ../types/enum.md [`mem::discriminant`]: ../../std/mem/fn.discriminant.html [never type]: ../types/never.md diff --git a/src/items/traits.md b/src/items/traits.md index dea8ecad05ffc..5427dbe652e06 100644 --- a/src/items/traits.md +++ b/src/items/traits.md @@ -10,7 +10,7 @@ >    `}` > > _TraitItem_ :\ ->    [_OuterAttribute_]\* (\ +>    [_OuterAttribute_]\* [_Visibility_]? (\ >          _TraitFunc_\ >       | _TraitMethod_\ >       | _TraitConst_\ @@ -204,6 +204,42 @@ trait T { } ``` +## Item visibility + +Trait items syntactically allow a [_Visibility_] annotation, but this is +rejected when the trait is validated. This allows items to be parsed with a +unified syntax across different contexts where they are used. As an example, +an empty `vis` macro fragment specifier can be used for trait items, where the +macro rule may be used in other situations where visibility is allowed. + +```rust +macro_rules! create_method { + ($vis:vis $name:ident) => { + $vis fn $name(&self) {} + }; +} + +trait T1 { + // Empty `vis` is allowed. + create_method! { method_of_t1 } +} + +struct S; + +impl S { + // Visibility is allowed here. + create_method! { pub method_of_s } +} + +impl T1 for S {} + +fn main() { + let s = S; + s.method_of_t1(); + s.method_of_s(); +} +``` + [IDENTIFIER]: ../identifiers.md [WildcardPattern]: ../patterns.md#wildcard-pattern [_BlockExpression_]: ../expressions/block-expr.md @@ -217,6 +253,7 @@ trait T { [_SelfParam_]: associated-items.md#methods [_TypeParamBounds_]: ../trait-bounds.md [_Type_]: ../types.md#type-expressions +[_Visibility_]: ../visibility-and-privacy.md [_WhereClause_]: generics.md#where-clauses [bounds]: ../trait-bounds.md [trait object]: ../types/trait-object.md