diff --git a/src/attributes.md b/src/attributes.md index df2bb460f..f3eae35f1 100644 --- a/src/attributes.md +++ b/src/attributes.md @@ -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: @@ -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 diff --git a/src/expressions/closure-expr.md b/src/expressions/closure-expr.md index aa9299bd6..fff27c846 100644 --- a/src/expressions/closure-expr.md +++ b/src/expressions/closure-expr.md @@ -10,7 +10,7 @@ >    _ClosureParam_ (`,` _ClosureParam_)\* `,`? > > _ClosureParam_ :\ ->    [_Pattern_] ( `:` [_Type_] )? +>    [_OuterAttribute_]\*\ [_Pattern_] ( `:` [_Type_] )? 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 @@ -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 diff --git a/src/items/external-blocks.md b/src/items/external-blocks.md index f3e692ac9..8a7a61003 100644 --- a/src/items/external-blocks.md +++ b/src/items/external-blocks.md @@ -23,11 +23,11 @@ > _NamedFunctionParameters_ :\ >    _NamedFunctionParam_ ( `,` _NamedFunctionParam_ )\* `,`? > -> _NamedFunctionParam_ :\ ->    ( [IDENTIFIER] | `_` ) `:` [_Type_] -> > _NamedFunctionParametersWithVariadics_ :\ ->    ( _NamedFunctionParam_ `,` )\* _NamedFunctionParam_ `,` `...` +>    ( _NamedFunctionParam_ `,` )\* _NamedFunctionParam_ `,` [_OuterAttribute_]\*\ `...` +> +> _NamedFunctionParam_ :\ +>    ( [_OuterAttribute_]\*\ [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 diff --git a/src/items/functions.md b/src/items/functions.md index 7c93dc423..70912ed5b 100644 --- a/src/items/functions.md +++ b/src/items/functions.md @@ -17,7 +17,7 @@ >    _FunctionParam_ (`,` _FunctionParam_)\* `,`? > > _FunctionParam_ :\ ->    [_Pattern_] `:` [_Type_] +>    [_OuterAttribute_]\*\ [_Pattern_] `:` [_Type_] > > _FunctionReturnType_ :\ >    `->` [_Type_] @@ -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 @@ -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 diff --git a/src/types/function-pointer.md b/src/types/function-pointer.md index 88ef50c24..fd06e8329 100644 --- a/src/types/function-pointer.md +++ b/src/types/function-pointer.md @@ -14,11 +14,11 @@ > _MaybeNamedFunctionParameters_ :\ >    _MaybeNamedParam_ ( `,` _MaybeNamedParam_ )\* `,`? > -> _MaybeNamedParam_ :\ ->    ( ( [IDENTIFIER] | `_` ) `:` )? [_Type_] -> > _MaybeNamedFunctionParametersVariadic_ :\ ->    ( _MaybeNamedParam_ `,` )\* _MaybeNamedParam_ `,` `...` +>    ( _MaybeNamedParam_ `,` )\* _MaybeNamedParam_ `,` [_OuterAttribute_]\*\ `...` +> +> _MaybeNamedParam_ :\ +>    ( [_OuterAttribute_]\*\ ( [IDENTIFIER] | `_` ) `:` )? [_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 @@ -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