Skip to content

Commit 05b0a98

Browse files
committed
Attributes in generics position and general attributes improvements
1 parent 25e2c77 commit 05b0a98

File tree

2 files changed

+41
-16
lines changed

2 files changed

+41
-16
lines changed

src/attributes.md

+20-14
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
>    | _MetaItem_
2222
>    | _MetaItem_ `,` _MetaSeq_
2323
24-
Any item declaration may have an _attribute_ applied to it. Attributes in Rust
25-
are modeled on Attributes in ECMA-335, with the syntax coming from ECMA-334
26-
(C#). An attribute is a general, free-form metadatum that is interpreted
27-
according to name, convention, and language and compiler version. Attributes
28-
may appear as any of:
24+
Any [item declaration] or [generic lifetime or type parameter][generics] may
25+
have an attribute applied to it. Attributes are modeled on Attributes in
26+
[ECMA-335], with the syntax coming from [ECMA-334]\ (C#). An _attribute_ is a
27+
general, free-form metadatum that is interpreted according to name, convention,
28+
and language and compiler version. Attributes may appear as any of:
2929

3030
* A single identifier, the attribute name
3131
* An identifier followed by the equals sign '=' and a literal, providing a
@@ -34,9 +34,10 @@ may appear as any of:
3434
key/value pair
3535
* An identifier followed by a parenthesized list of sub-attribute arguments
3636

37-
Attributes with a bang ("!") after the hash ("#") apply to the item that the
38-
attribute is declared within. Attributes that do not have a bang after the hash
39-
apply to the item that follows the attribute.
37+
_Inner attributes_, written with a bang ("!") after the hash ("#"), apply to the
38+
item that the attribute is declared within. _Outer attributes_, written without
39+
the bang after the hash, apply to the item or generic parameter that follow the
40+
attribute.
4041

4142
An example of attributes:
4243

@@ -101,7 +102,8 @@ type int8_t = i8;
101102
- `test` - indicates that this function is a test function, to only be compiled
102103
in case of `--test`.
103104
- `ignore` - indicates that this test function is disabled.
104-
- `should_panic` - indicates that this test function should panic, inverting the success condition.
105+
- `should_panic` - indicates that this test function should panic, inverting the
106+
success condition.
105107
- `cold` - The function is unlikely to be executed, so optimize it (and calls
106108
to it) differently.
107109

@@ -153,9 +155,9 @@ which can be used to control type layout.
153155
- `no_link` on an `extern crate` — even if we load this crate for macros, don't
154156
link it into the output.
155157

156-
See the [macros section of the
157-
book](../book/first-edition/macros.html#scoping-and-macro-importexport) for more information on
158-
macro scope.
158+
See the [macros section of the first edition of the
159+
book](../book/first-edition/macros.html#scoping-and-macro-importexport) for more
160+
information on macro scope.
159161

160162
## Miscellaneous attributes
161163

@@ -213,8 +215,8 @@ release builds.
213215
Configuration options are boolean (on or off) and are named either with a
214216
single identifier (e.g. `foo`) or an identifier and a string (e.g. `foo = "bar"`;
215217
the quotes are required and spaces around the `=` are unimportant). Note that
216-
similarly-named options, such as `foo`, `foo="bar"` and `foo="baz"` may each be set
217-
or unset independently.
218+
similarly-named options, such as `foo`, `foo="bar"` and `foo="baz"` may each be
219+
set or unset independently.
218220

219221
Configuration options are either provided by the compiler or passed in on the
220222
command line using `--cfg` (e.g. `rustc main.rs --cfg foo --cfg 'bar="baz"'`).
@@ -537,3 +539,7 @@ You can implement `derive` for your own type through [procedural macros].
537539
[let statement]: statements.html#let-statements
538540
[unstable book plugin]: ../unstable-book/language-features/plugin.html#lint-plugins
539541
[zero-variant enum]: items/enumerations.html#zero-variant-enums
542+
[ECMA-334]: https://www.ecma-international.org/publications/standards/Ecma-334.htm
543+
[ECMA-335]: https://www.ecma-international.org/publications/standards/Ecma-335.htm
544+
[item declaration]: items.html
545+
[generics]: generics.html

src/items/generics.md

+21-2
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
> &nbsp;&nbsp; ( _LifetimeParam_ `,` )<sup>\*</sup> _LifetimeParam_<sup>?</sup>
1313
>
1414
> _LifetimeParam_ :
15-
> &nbsp;&nbsp; [LIFETIME_OR_LABEL] `:` [_LifetimeBounds_]<sup>?</sup>
15+
> &nbsp;&nbsp; [_OuterAttribute_]<sup>?</sup> [LIFETIME_OR_LABEL] `:` [_LifetimeBounds_]<sup>?</sup>
1616
>
1717
> _TypeParams_:
1818
> &nbsp;&nbsp; ( _TypeParam_ `,` )<sup>\*</sup> _TypeParam_ <sup>?</sup>
1919
>
2020
> _TypeParam_ :
21-
> &nbsp;&nbsp; [IDENTIFIER] ( `:` [_TypeParamBounds_] )<sup>?</sup> ( `=` [_Type_] )<sup>?</sup>
21+
> &nbsp;&nbsp; [_OuterAttribute_]<sup>?</sup> [IDENTIFIER] ( `:` [_TypeParamBounds_] )<sup>?</sup> ( `=` [_Type_] )<sup>?</sup>
2222
2323
Functions, type aliases, structs, enumerations, unions, traits and
2424
implementations may be *parameterized* by types and lifetimes. These parameters
@@ -83,11 +83,29 @@ where
8383
}
8484
```
8585

86+
## Attributes
87+
88+
Generic lifetime and type parameters allow [attributes] on them. There are no
89+
built-in attributes that do anything in this position, although custom derive
90+
attributes may give meaning to it.
91+
92+
This example shows using a custom derive attribute to modify the meaning of a
93+
generic parameter.
94+
95+
```ignore
96+
// Assume that the derive for MyFlexibleClone declared `my_flexible_clone` as
97+
// an attribute it understands.
98+
#[derive(MyFlexibleClone)] struct Foo<#[my_flexible_clone(unbounded)] H> {
99+
a: *const H
100+
}
101+
```
102+
86103
[IDENTIFIER]: identifiers.html
87104
[LIFETIME_OR_LABEL]: tokens.html#lifetimes-and-loop-labels
88105

89106
[_LifetimeBounds_]: trait-bounds.html
90107
[_Lifetime_]: trait-bounds.html
108+
[_OuterAttribute_]: attributes.html
91109
[_Type_]: types.html
92110
[_TypeParamBounds_]: trait-bounds.html
93111

@@ -100,6 +118,7 @@ where
100118
[`Sized`]: special-types-and-traits.html#sized
101119
[tuples]: types.html#tuple-types
102120
[trait object]: types.html#trait-objects
121+
[attributes]: attributes.html
103122

104123
[path]: ../paths.html
105124
[Trait]: traits.html#trait-bounds

0 commit comments

Comments
 (0)