Skip to content

Commit 21da765

Browse files
committed
Add reference for attributes in function parameters
1 parent 2b29a56 commit 21da765

File tree

7 files changed

+70
-14
lines changed

7 files changed

+70
-14
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

+8-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
@@ -67,13 +67,20 @@ let word = "konnichiwa".to_owned();
6767
ten_times(move |j| println!("{}, {}", word, j));
6868
```
6969

70+
## Attributes on closure parameters
71+
72+
Attributes on closure parameters follow the same rules and restrictions as
73+
[regular function parameters].
74+
7075
[block]: block-expr.md
7176
[function definitions]: ../items/functions.md
7277
[patterns]: ../patterns.md
78+
[regular function parameters]: ../items/functions.md#attributes-on-function-parameters
7379

7480
[_Expression_]: ../expressions.md
7581
[_BlockExpression_]: block-expr.md
7682
[_TypeNoBounds_]: ../types.md#type-expressions
7783
[_Pattern_]: ../patterns.md
7884
[_Type_]: ../types.md#type-expressions
7985
[`let` binding]: ../statements.md#let-statements
86+
[_OuterAttribute_]: ../attributes.md

src/items/associated-items.md

+14-2
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,13 @@ let _: f64 = f64::from_i32(42);
8686
> &nbsp;&nbsp; &nbsp;&nbsp; [_BlockExpression_]
8787
>
8888
> _SelfParam_ :\
89-
> &nbsp;&nbsp; &nbsp;&nbsp; (`&` | `&` [_Lifetime_])<sup>?</sup> `mut`<sup>?</sup> `self`\
90-
> &nbsp;&nbsp; | `mut`<sup>?</sup> `self` (`:` [_Type_])<sup>?</sup>
89+
> &nbsp;&nbsp; [_OuterAttribute_]<sup>\*</sup> ( _ShorthandSelf_ | _TypedSelf_ )
90+
>
91+
> _ShorthandSelf_ :\
92+
> &nbsp;&nbsp; (`&` | `&` [_Lifetime_])<sup>?</sup> `mut`<sup>?</sup> `self`
93+
>
94+
> _TypedSelf_ :\
95+
> &nbsp;&nbsp; `mut`<sup>?</sup> `self` `:` [_Type_]
9196
9297
Associated functions whose first parameter is named `self` are called *methods*
9398
and may be invoked using the [method call operator], for example, `x.foo()`, as
@@ -190,6 +195,11 @@ let bounding_box = circle_shape.bounding_box();
190195
> methods with anonymous parameters (e.g. `fn foo(u8)`). This is deprecated and
191196
> an error as of the 2018 edition. All parameters must have an argument name.
192197
198+
#### Attributes on method parameters
199+
200+
Attributes on method parameters follow the same rules and restrictions as
201+
[regular function parameters].
202+
193203
## Associated Types
194204

195205
*Associated types* are [type aliases] associated with another type. Associated
@@ -336,6 +346,7 @@ fn main() {
336346
[`Box<Self>`]: ../special-types-and-traits.md#boxt
337347
[`Pin<P>`]: ../special-types-and-traits.md#pinp
338348
[`Rc<Self>`]: ../special-types-and-traits.md#rct
349+
[_OuterAttribute_]: ../attributes.md
339350
[traits]: traits.md
340351
[type aliases]: type-aliases.md
341352
[inherent implementations]: implementations.md#inherent-implementations
@@ -349,3 +360,4 @@ fn main() {
349360
[function item]: ../types/function-item.md
350361
[method call operator]: ../expressions/method-call-expr.md
351362
[path]: ../paths.md
363+
[regular function parameters]: functions.md#attributes-on-function-parameters

src/items/external-blocks.md

+11-5
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@
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
34-
akin to unchecked imports.
34+
akin to unchecked imports.
3535

3636
Two kind of item _declarations_ are allowed in external blocks: [functions] and
3737
[statics]. Calling functions or accessing statics that are declared in external
@@ -162,6 +162,11 @@ extern {
162162
}
163163
```
164164

165+
### Attributes on function parameters
166+
167+
Attributes on extern function parameters follow the same rules and
168+
restrictions as [regular function parameters].
169+
165170
[IDENTIFIER]: ../identifiers.md
166171
[WebAssembly module]: https://webassembly.github.io/spec/core/syntax/modules.html
167172
[functions]: functions.md
@@ -177,3 +182,4 @@ extern {
177182
[_Visibility_]: ../visibility-and-privacy.md
178183
[_WhereClause_]: generics.md#where-clauses
179184
[attributes]: ../attributes.md
185+
[regular function parameters]: functions.md#attributes-on-function-parameters

src/items/functions.md

+21-3
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_]
@@ -244,12 +244,27 @@ fn test_only() {
244244
> Note: Except for lints, it is idiomatic to only use outer attributes on
245245
> function items.
246246
247-
The attributes that have meaning on a function are [`cfg`], [`deprecated`],
247+
The attributes that have meaning on a function are [`cfg`], [`cfg_attr`], [`deprecated`],
248248
[`doc`], [`export_name`], [`link_section`], [`no_mangle`], [the lint check
249249
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
@@ -258,6 +273,7 @@ attributes macros.
258273
[_Pattern_]: ../patterns.md
259274
[_Type_]: ../types.md#type-expressions
260275
[_WhereClause_]: generics.md#where-clauses
276+
[_OuterAttribute_]: ../attributes.md
261277
[const context]: ../const_eval.md#const-context
262278
[external blocks]: external-blocks.md
263279
[path]: ../paths.md
@@ -267,7 +283,8 @@ attributes macros.
267283
[*function item type*]: ../types/function-item.md
268284
[Trait]: traits.md
269285
[attributes]: ../attributes.md
270-
[`cfg`]: ../conditional-compilation.md
286+
[`cfg`]: ../conditional-compilation.md#the-cfg-attribute
287+
[`cfg_attr`]: ../conditional-compilation.md#the-cfg_attr-attribute
271288
[the lint check attributes]: ../attributes/diagnostics.md#lint-check-attributes
272289
[the procedural macro attributes]: ../procedural-macros.md
273290
[the testing attributes]: ../attributes/testing.md
@@ -282,3 +299,4 @@ attributes macros.
282299
[`link_section`]: ../abi.md#the-link_section-attribute
283300
[`no_mangle`]: ../abi.md#the-no_mangle-attribute
284301
[external_block_abi]: external-blocks.md#abi
302+
[built-in attributes]: ../attributes.html#built-in-attributes-index

src/items/traits.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
> &nbsp;&nbsp; _TraitFunctionParam_ (`,` _TraitFunctionParam_)<sup>\*</sup> `,`<sup>?</sup>
3939
>
4040
> _TraitFunctionParam_<sup>[](#parameter-patterns)</sup> :\
41-
> &nbsp;&nbsp; ( [_Pattern_] `:` )<sup>?</sup> [_Type_]
41+
> &nbsp;&nbsp; [_OuterAttribute_]<sup>\*</sup> ( [_Pattern_] `:` )<sup>?</sup> [_Type_]
4242
>
4343
> _TraitConst_ :\
4444
> &nbsp;&nbsp; `const` [IDENTIFIER] `:` [_Type_]&nbsp;( `=` [_Expression_] )<sup>?</sup> `;`

src/types/function-pointer.md

+9-2
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
> &nbsp;&nbsp; _MaybeNamedParam_ ( `,` _MaybeNamedParam_ )<sup>\*</sup> `,`<sup>?</sup>
1616
>
1717
> _MaybeNamedParam_ :\
18-
> &nbsp;&nbsp; ( ( [IDENTIFIER] | `_` ) `:` )<sup>?</sup> [_Type_]
18+
> &nbsp;&nbsp; [_OuterAttribute_]<sup>\*</sup> ( ( [IDENTIFIER] | `_` ) `:` )<sup>?</sup> [_Type_]
1919
>
2020
> _MaybeNamedFunctionParametersVariadic_ :\
21-
> &nbsp;&nbsp; ( _MaybeNamedParam_ `,` )<sup>\*</sup> _MaybeNamedParam_ `,` `...`
21+
> &nbsp;&nbsp; ( _MaybeNamedParam_ `,` )<sup>\*</sup> _MaybeNamedParam_ `,` [_OuterAttribute_]<sup>\*</sup> `...`
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
@@ -44,13 +44,20 @@ let bo: Binop = add;
4444
x = bo(5,7);
4545
```
4646

47+
## Attributes on function pointer parameters
48+
49+
Attributes on function pointer parameters follow the same rules and
50+
restrictions as [regular function parameters].
51+
4752
[IDENTIFIER]: ../identifiers.md
4853
[_ForLifetimes_]: ../items/generics.md#where-clauses
4954
[_FunctionQualifiers_]: ../items/functions.md
5055
[_TypeNoBounds_]: ../types.md#type-expressions
5156
[_Type_]: ../types.md#type-expressions
57+
[_OuterAttribute_]: ../attributes.md
5258
[`extern`]: ../items/external-blocks.md
5359
[closures]: closure.md
5460
[extern function]: ../items/functions.md#extern-function-qualifier
5561
[function items]: function-item.md
5662
[unsafe function]: ../unsafe-functions.md
63+
[regular function parameters]: ../items/functions.md#attributes-on-function-parameters

0 commit comments

Comments
 (0)