Skip to content

Commit 2218f38

Browse files
committed
Add reference for attributes in function parameters
1 parent 2b29a56 commit 2218f38

File tree

5 files changed

+35
-10
lines changed

5 files changed

+35
-10
lines changed

src/attributes.md

+6
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ Attributes may be applied to many things in the language:
5050
* [Generic lifetime or type parameter][generics] accept outer attributes.
5151
* Expressions accept outer attributes in limited situations, see [Expression
5252
Attributes] for details.
53+
* [Function][function], [closure][closure] and [function pointer][function pointer]
54+
parameters accept outer attributes. This includes attributes on variadic parameters
55+
denoted with `...` in function pointers and [external blocks][variadic functions].
5356

5457
Some examples of attributes:
5558

@@ -306,3 +309,6 @@ The following is an index of all built-in attributes.
306309
[statements]: statements.md
307310
[struct]: items/structs.md
308311
[union]: items/unions.md
312+
[closure]: expressions/closure-expr.md
313+
[function pointer]: types/function-pointer.md
314+
[variadic functions]: items/external-blocks.html#variadic-functions

src/expressions/closure-expr.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
> &nbsp;&nbsp; _ClosureParam_ (`,` _ClosureParam_)<sup>\*</sup> `,`<sup>?</sup>
1111
>
1212
> _ClosureParam_ :\
13-
> &nbsp;&nbsp; [_Pattern_]&nbsp;( `:` [_Type_] )<sup>?</sup>
13+
> &nbsp;&nbsp; [_OuterAttribute_]<sup>\*</sup> [_Pattern_]&nbsp;( `:` [_Type_] )<sup>?</sup>
1414
1515
A _closure expression_ defines a closure and denotes it as a value, in a single
1616
expression. A closure expression is a pipe-symbol-delimited (`|`) list of
@@ -77,3 +77,4 @@ ten_times(move |j| println!("{}, {}", word, j));
7777
[_Pattern_]: ../patterns.md
7878
[_Type_]: ../types.md#type-expressions
7979
[`let` binding]: ../statements.md#let-statements
80+
[_OuterAttribute_]: ../attributes.md

src/items/external-blocks.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323
> _NamedFunctionParameters_ :\
2424
> &nbsp;&nbsp; _NamedFunctionParam_ ( `,` _NamedFunctionParam_ )<sup>\*</sup> `,`<sup>?</sup>
2525
>
26-
> _NamedFunctionParam_ :\
27-
> &nbsp;&nbsp; ( [IDENTIFIER] | `_` ) `:` [_Type_]
28-
>
2926
> _NamedFunctionParametersWithVariadics_ :\
30-
> &nbsp;&nbsp; ( _NamedFunctionParam_ `,` )<sup>\*</sup> _NamedFunctionParam_ `,` `...`
27+
> &nbsp;&nbsp; ( _NamedFunctionParam_ `,` )<sup>\*</sup> _NamedFunctionParam_ `,` [_OuterAttribute_]<sup>\*</sup> `...`
28+
>
29+
> _NamedFunctionParam_ :\
30+
> &nbsp;&nbsp; [_OuterAttribute_]<sup>\*</sup> ( [IDENTIFIER] | `_` ) `:` [_Type_]
3131
3232
External blocks provide _declarations_ of items that are not _defined_ in the
3333
current crate and are the basis of Rust's foreign function interface. These are

src/items/functions.md

+18-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
> &nbsp;&nbsp; _FunctionParam_ (`,` _FunctionParam_)<sup>\*</sup> `,`<sup>?</sup>
1818
>
1919
> _FunctionParam_ :\
20-
> &nbsp;&nbsp; [_Pattern_] `:` [_Type_]
20+
> &nbsp;&nbsp; [_OuterAttribute_]<sup>\*</sup>\ [_Pattern_] `:` [_Type_]
2121
>
2222
> _FunctionReturnType_ :\
2323
> &nbsp;&nbsp; `->` [_Type_]
@@ -250,6 +250,21 @@ attributes], [`must_use`], [the procedural macro attributes], [the testing
250250
attributes], and [the optimization hint attributes]. Functions also accept
251251
attributes macros.
252252

253+
## Attributes on function parameters
254+
255+
[Outer attributes][attributes] are allowed on function parameters and the
256+
permitted [built-in attributes] are restricted to `cfg`, `cfg_attr`, `allow`,
257+
`warn`, `deny`, and `forbid`. For example:
258+
259+
```rust
260+
fn len(
261+
#[cfg(windows)] slice: &[u16],
262+
#[cfg(not(windows))] slice: &[u8],
263+
) -> usize {
264+
slice.len()
265+
}
266+
```
267+
253268
[IDENTIFIER]: ../identifiers.md
254269
[RAW_STRING_LITERAL]: ../tokens.md#raw-string-literals
255270
[STRING_LITERAL]: ../tokens.md#string-literals
@@ -282,3 +297,5 @@ attributes macros.
282297
[`link_section`]: ../abi.md#the-link_section-attribute
283298
[`no_mangle`]: ../abi.md#the-no_mangle-attribute
284299
[external_block_abi]: external-blocks.md#abi
300+
[_OuterAttribute_]: ../attributes.md
301+
[built-in attributes]: ../attributes.html#built-in-attributes-index

src/types/function-pointer.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
> _MaybeNamedFunctionParameters_ :\
1515
> &nbsp;&nbsp; _MaybeNamedParam_ ( `,` _MaybeNamedParam_ )<sup>\*</sup> `,`<sup>?</sup>
1616
>
17-
> _MaybeNamedParam_ :\
18-
> &nbsp;&nbsp; ( ( [IDENTIFIER] | `_` ) `:` )<sup>?</sup> [_Type_]
19-
>
2017
> _MaybeNamedFunctionParametersVariadic_ :\
21-
> &nbsp;&nbsp; ( _MaybeNamedParam_ `,` )<sup>\*</sup> _MaybeNamedParam_ `,` `...`
18+
> &nbsp;&nbsp; ( _MaybeNamedParam_ `,` )<sup>\*</sup> _MaybeNamedParam_ `,` [_OuterAttribute_]<sup>\*</sup> `...`
19+
>
20+
> _MaybeNamedParam_ :\
21+
> &nbsp;&nbsp; [_OuterAttribute_]<sup>\*</sup> ( ( [IDENTIFIER] | `_` ) `:` )<sup>?</sup> [_Type_]
2222
2323
Function pointer types, written using the `fn` keyword, refer to a function
2424
whose identity is not necessarily known at compile-time. They can be created
@@ -54,3 +54,4 @@ x = bo(5,7);
5454
[extern function]: ../items/functions.md#extern-function-qualifier
5555
[function items]: function-item.md
5656
[unsafe function]: ../unsafe-functions.md
57+
[_OuterAttribute_]: ../attributes.md

0 commit comments

Comments
 (0)