Skip to content

Commit

Permalink
Merge pull request rust-lang#722 from ehuss/unified-vis-items
Browse files Browse the repository at this point in the history
Update for visibility syntax changes.
  • Loading branch information
ehuss authored Dec 10, 2019
2 parents faac114 + d229cca commit 787e8d8
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
34 changes: 33 additions & 1 deletion src/items/enumerations.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
> &nbsp;&nbsp; _EnumItem_ ( `,` _EnumItem_ )<sup>\*</sup> `,`<sup>?</sup>
>
> _EnumItem_ :\
> &nbsp;&nbsp; _OuterAttribute_<sup>\*</sup>\
> &nbsp;&nbsp; _OuterAttribute_<sup>\*</sup> [_Visibility_]<sup>?</sup>\
> &nbsp;&nbsp; [IDENTIFIER]&nbsp;( _EnumItemTuple_ | _EnumItemStruct_
> | _EnumItemDiscriminant_ )<sup>?</sup>
>
Expand Down Expand Up @@ -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
Expand Down
39 changes: 38 additions & 1 deletion src/items/traits.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
> &nbsp;&nbsp; `}`
>
> _TraitItem_ :\
> &nbsp;&nbsp; [_OuterAttribute_]<sup>\*</sup> (\
> &nbsp;&nbsp; [_OuterAttribute_]<sup>\*</sup> [_Visibility_]<sup>?</sup> (\
> &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; _TraitFunc_\
> &nbsp;&nbsp; &nbsp;&nbsp; | _TraitMethod_\
> &nbsp;&nbsp; &nbsp;&nbsp; | _TraitConst_\
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 787e8d8

Please sign in to comment.