Skip to content

Commit c6177bc

Browse files
committed
Attribute resolution, types of attrs, inert/dynamic attrs
1 parent 34f5a1e commit c6177bc

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

src/attributes.md

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ Attributes may appear as any of:
3535

3636
_Inner attributes_, written with a bang ("!") after the hash ("#"), apply to the
3737
item that the attribute is declared within. _Outer attributes_, written without
38-
the bang after the hash, apply to the item or generic parameter that follow the
39-
attribute.
38+
the bang after the hash, apply to the thing that follows the attribute.
4039

4140
Attributes may be applied to many things in the language:
4241

@@ -82,6 +81,47 @@ fn some_unused_variables() {
8281
}
8382
```
8483

84+
There are three kinds of attributes:
85+
86+
* Built-in attributes
87+
* Macro attributes
88+
* Derive mode helper attributes
89+
90+
## Dynamic and inert attributes
91+
92+
An attribute is either dynamic or inert. During attribute processing, *dynamic
93+
attributes* remove themselves from the thing they are on while *inert attriutes*
94+
stay on.
95+
96+
The `cfg` and `cfg_attr` attributes are dynamic. The `test` attribute is inert
97+
when compiling for tests and dynamic otherwise. Attribute macros are dynamic.
98+
All other attributes are inert.
99+
100+
## Attribute resolution
101+
102+
On all things except for items, attribute resolution is straightforward. The
103+
`cfg` and `cfg_attr` attributes are applied and only inert attributes are
104+
allowed. Each of those inert attributes then resolve to either a built-in
105+
attribute or a derive mode helper attribute. If the attribute cannot resolve to
106+
either, it is an error.
107+
108+
For items, attribute resolution is a bit more involved. First off, if there are
109+
any `cfg`, `cfg_attr`, or `test` attributes when not compiling tests, they are
110+
resolved first, and removed from the item. Then, each attribute is checked in
111+
the order they are written. If the attribute resolves to an inert attribute,
112+
check the next attribute. If the attribute resolves to a dynamic attribute, then
113+
perform its dynamic behavior. This will effectively replace the item with a new
114+
set of items that must go through attribute resolution from the beginning. If
115+
the attribute resolves to a macro that is not an attribute macro, it is an
116+
error. Otherwise, the attriute is not current resolveable. In this case, wait
117+
until another item brings the attribute into scope, and then recheck it. If all
118+
other items have their attributes resolved or are also waiting for attribute or
119+
derive mode resolution, it is an error. If all of the attributes are inert, then
120+
the item is finalized. If the item defines a new path for a macro, it is now
121+
available for other items.
122+
123+
---
124+
85125
The rest of this page describes or links to descriptions of which attribute
86126
names have meaning.
87127

0 commit comments

Comments
 (0)