20
20
>   ;  ; _ FunctionParam_ (` , ` _ FunctionParam_ )<sup >\* </sup > ` , ` <sup >?</sup >
21
21
>
22
22
> _ FunctionParam_ :\
23
- >   ;  ; [ _ Pattern_ ] ` : ` [ _ Type_ ]
23
+ >   ;  ; [ _ OuterAttribute _ ] < sup > \* </ sup > [ _ Pattern_ ] ` : ` [ _ Type_ ]
24
24
>
25
25
> _ FunctionReturnType_ :\
26
26
>   ;  ; ` -> ` [ _ Type_ ]
@@ -345,12 +345,40 @@ fn test_only() {
345
345
> Note: Except for lints, it is idiomatic to only use outer attributes on
346
346
> function items.
347
347
348
- The attributes that have meaning on a function are [ ` cfg ` ] , [ ` deprecated ` ] ,
348
+ The attributes that have meaning on a function are [ ` cfg ` ] , [ ` cfg_attr ` ] , [ ` deprecated ` ] ,
349
349
[ ` doc ` ] , [ ` export_name ` ] , [ ` link_section ` ] , [ ` no_mangle ` ] , [ the lint check
350
350
attributes] , [ ` must_use ` ] , [ the procedural macro attributes] , [ the testing
351
351
attributes] , and [ the optimization hint attributes] . Functions also accept
352
352
attributes macros.
353
353
354
+ ## Attributes on function parameters
355
+
356
+ [ Outer attributes] [ attributes ] are allowed on function parameters and the
357
+ permitted [ built-in attributes] are restricted to ` cfg ` , ` cfg_attr ` , ` allow ` ,
358
+ ` warn ` , ` deny ` , and ` forbid ` .
359
+
360
+ ``` rust
361
+ fn len (
362
+ #[cfg (windows )] slice : & [u16 ],
363
+ #[cfg (not (windows ))] slice : & [u8 ],
364
+ ) -> usize {
365
+ slice . len ()
366
+ }
367
+ ```
368
+
369
+ Inert helper attributes used by procedural macro attributes applied to items are also
370
+ allowed but be careful to not include these inert attributes in your final ` TokenStream ` .
371
+
372
+ For example, the following code defines an inert ` some_inert_attribute ` attribute that
373
+ is not formally defined anywhere and the ` some_proc_macro_attribute ` procedural macro is
374
+ responsible for detecting its presence and removing it from the output token stream.
375
+
376
+ ``` rust,ignore
377
+ #[some_proc_macro_attribute]
378
+ fn foo_oof(#[some_inert_attribute] arg: u8) {
379
+ }
380
+ ```
381
+
354
382
[ IDENTIFIER ] : ../identifiers.md
355
383
[ RAW_STRING_LITERAL ] : ../tokens.md#raw-string-literals
356
384
[ STRING_LITERAL ] : ../tokens.md#string-literals
@@ -359,6 +387,7 @@ attributes macros.
359
387
[ _Pattern_ ] : ../patterns.md
360
388
[ _Type_ ] : ../types.md#type-expressions
361
389
[ _WhereClause_ ] : generics.md#where-clauses
390
+ [ _OuterAttribute_ ] : ../attributes.md
362
391
[ const context ] : ../const_eval.md#const-context
363
392
[ external block ] : external-blocks.md
364
393
[ path ] : ../paths.md
@@ -368,7 +397,8 @@ attributes macros.
368
397
[ *function item type* ] : ../types/function-item.md
369
398
[ Trait ] : traits.md
370
399
[ attributes ] : ../attributes.md
371
- [ `cfg` ] : ../conditional-compilation.md
400
+ [ `cfg` ] : ../conditional-compilation.md#the-cfg-attribute
401
+ [ `cfg_attr` ] : ../conditional-compilation.md#the-cfg_attr-attribute
372
402
[ the lint check attributes ] : ../attributes/diagnostics.md#lint-check-attributes
373
403
[ the procedural macro attributes ] : ../procedural-macros.md
374
404
[ the testing attributes ] : ../attributes/testing.md
@@ -383,3 +413,4 @@ attributes macros.
383
413
[ `link_section` ] : ../abi.md#the-link_section-attribute
384
414
[ `no_mangle` ] : ../abi.md#the-no_mangle-attribute
385
415
[ external_block_abi ] : external-blocks.md#abi
416
+ [ built-in attributes ] : ../attributes.html#built-in-attributes-index
0 commit comments