Skip to content

Commit

Permalink
Stabilize param_attrs in Rust 1.39.0
Browse files Browse the repository at this point in the history
  • Loading branch information
c410-f3r committed Aug 26, 2019
1 parent 2b29a56 commit 37c69ce
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 10 deletions.
4 changes: 4 additions & 0 deletions src/attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ Attributes may be applied to many things in the language:
* [Generic lifetime or type parameter][generics] accept outer attributes.
* Expressions accept outer attributes in limited situations, see [Expression
Attributes] for details.
* [Function][function], [closure][closure] and [function pointer][function pointer]
parameters accept outer attributes.

Some examples of attributes:

Expand Down Expand Up @@ -306,3 +308,5 @@ The following is an index of all built-in attributes.
[statements]: statements.md
[struct]: items/structs.md
[union]: items/unions.md
[closure]: expressions/closure-expr.md
[function pointer]: types/function-pointer.md
3 changes: 2 additions & 1 deletion src/expressions/closure-expr.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
> &nbsp;&nbsp; _ClosureParam_ (`,` _ClosureParam_)<sup>\*</sup> `,`<sup>?</sup>
>
> _ClosureParam_ :\
> &nbsp;&nbsp; [_Pattern_]&nbsp;( `:` [_Type_] )<sup>?</sup>
> &nbsp;&nbsp; [_OuterAttribute_]<sup>\*</sup>\ [_Pattern_]&nbsp;( `:` [_Type_] )<sup>?</sup>
A _closure expression_ defines a closure and denotes it as a value, in a single
expression. A closure expression is a pipe-symbol-delimited (`|`) list of
Expand Down Expand Up @@ -77,3 +77,4 @@ ten_times(move |j| println!("{}, {}", word, j));
[_Pattern_]: ../patterns.md
[_Type_]: ../types.md#type-expressions
[`let` binding]: ../statements.md#let-statements
[_OuterAttribute_]: ../attributes.md
8 changes: 4 additions & 4 deletions src/items/external-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
> _NamedFunctionParameters_ :\
> &nbsp;&nbsp; _NamedFunctionParam_ ( `,` _NamedFunctionParam_ )<sup>\*</sup> `,`<sup>?</sup>
>
> _NamedFunctionParam_ :\
> &nbsp;&nbsp; ( [IDENTIFIER] | `_` ) `:` [_Type_]
>
> _NamedFunctionParametersWithVariadics_ :\
> &nbsp;&nbsp; ( _NamedFunctionParam_ `,` )<sup>\*</sup> _NamedFunctionParam_ `,` `...`
> &nbsp;&nbsp; ( _NamedFunctionParam_ `,` )<sup>\*</sup> _NamedFunctionParam_ `,` [_OuterAttribute_]<sup>\*</sup>\ `...`
>
> _NamedFunctionParam_ :\
> &nbsp;&nbsp; ( [_OuterAttribute_]<sup>\*</sup>\ [IDENTIFIER] | `_` ) `:` [_Type_]
External blocks provide _declarations_ of items that are not _defined_ in the
current crate and are the basis of Rust's foreign function interface. These are
Expand Down
18 changes: 17 additions & 1 deletion src/items/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
> &nbsp;&nbsp; _FunctionParam_ (`,` _FunctionParam_)<sup>\*</sup> `,`<sup>?</sup>
>
> _FunctionParam_ :\
> &nbsp;&nbsp; [_Pattern_] `:` [_Type_]
> &nbsp;&nbsp; [_OuterAttribute_]<sup>\*</sup>\ [_Pattern_] `:` [_Type_]
>
> _FunctionReturnType_ :\
> &nbsp;&nbsp; `->` [_Type_]
Expand Down Expand Up @@ -250,6 +250,21 @@ attributes], [`must_use`], [the procedural macro attributes], [the testing
attributes], and [the optimization hint attributes]. Functions also accept
attributes macros.

## Attributes on function parameters

Outer Attributes on function parameters are possible and enable finer
conditional compilation with `#[cfg(..)]`/`#[cfg_attr(..)]` and linting
control of variables. For example:

```rust
fn len(
#[cfg(windows)] slice: &[u16],
#[cfg(not(windows))] slice: &[u8],
) -> usize {
slice.len()
}
```

[IDENTIFIER]: ../identifiers.md
[RAW_STRING_LITERAL]: ../tokens.md#raw-string-literals
[STRING_LITERAL]: ../tokens.md#string-literals
Expand Down Expand Up @@ -282,3 +297,4 @@ attributes macros.
[`link_section`]: ../abi.md#the-link_section-attribute
[`no_mangle`]: ../abi.md#the-no_mangle-attribute
[external_block_abi]: external-blocks.md#abi
[_OuterAttribute_]: ../attributes.md
9 changes: 5 additions & 4 deletions src/types/function-pointer.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
> _MaybeNamedFunctionParameters_ :\
> &nbsp;&nbsp; _MaybeNamedParam_ ( `,` _MaybeNamedParam_ )<sup>\*</sup> `,`<sup>?</sup>
>
> _MaybeNamedParam_ :\
> &nbsp;&nbsp; ( ( [IDENTIFIER] | `_` ) `:` )<sup>?</sup> [_Type_]
>
> _MaybeNamedFunctionParametersVariadic_ :\
> &nbsp;&nbsp; ( _MaybeNamedParam_ `,` )<sup>\*</sup> _MaybeNamedParam_ `,` `...`
> &nbsp;&nbsp; ( _MaybeNamedParam_ `,` )<sup>\*</sup> _MaybeNamedParam_ `,` [_OuterAttribute_]<sup>\*</sup>\ `...`
>
> _MaybeNamedParam_ :\
> &nbsp;&nbsp; ( [_OuterAttribute_]<sup>\*</sup>\ ( [IDENTIFIER] | `_` ) `:` )<sup>?</sup> [_Type_]
Function pointer types, written using the `fn` keyword, refer to a function
whose identity is not necessarily known at compile-time. They can be created
Expand Down Expand Up @@ -54,3 +54,4 @@ x = bo(5,7);
[extern function]: ../items/functions.md#extern-function-qualifier
[function items]: function-item.md
[unsafe function]: ../unsafe-functions.md
[_OuterAttribute_]: ../attributes.md

0 comments on commit 37c69ce

Please sign in to comment.