|
| 1 | +# Attributes |
| 2 | + |
| 3 | +Any item declaration may have an _attribute_ applied to it. Attributes in Rust |
| 4 | +are modeled on Attributes in ECMA-335, with the syntax coming from ECMA-334 |
| 5 | +(C#). An attribute is a general, free-form metadatum that is interpreted |
| 6 | +according to name, convention, and language and compiler version. Attributes |
| 7 | +may appear as any of: |
| 8 | + |
| 9 | +* A single identifier, the attribute name |
| 10 | +* An identifier followed by the equals sign '=' and a literal, providing a |
| 11 | + key/value pair |
| 12 | +* An identifier followed by a parenthesized list of sub-attribute arguments |
| 13 | + |
| 14 | +Attributes with a bang ("!") after the hash ("#") apply to the item that the |
| 15 | +attribute is declared within. Attributes that do not have a bang after the hash |
| 16 | +apply to the item that follows the attribute. |
| 17 | + |
| 18 | +An example of attributes: |
| 19 | + |
| 20 | +```{.rust} |
| 21 | +// General metadata applied to the enclosing module or crate. |
| 22 | +#![crate_type = "lib"] |
| 23 | +
|
| 24 | +// A function marked as a unit test |
| 25 | +#[test] |
| 26 | +fn test_foo() { |
| 27 | + /* ... */ |
| 28 | +} |
| 29 | +
|
| 30 | +// A conditionally-compiled module |
| 31 | +#[cfg(target_os="linux")] |
| 32 | +mod bar { |
| 33 | + /* ... */ |
| 34 | +} |
| 35 | +
|
| 36 | +// A lint attribute used to suppress a warning/error |
| 37 | +#[allow(non_camel_case_types)] |
| 38 | +type int8_t = i8; |
| 39 | +``` |
| 40 | + |
| 41 | +> **Note:** At some point in the future, the compiler will distinguish between |
| 42 | +> language-reserved and user-available attributes. Until then, there is |
| 43 | +> effectively no difference between an attribute handled by a loadable syntax |
| 44 | +> extension and the compiler. |
| 45 | +
|
| 46 | +## Crate-only attributes |
| 47 | + |
| 48 | +- `crate_name` - specify the crate's crate name. |
| 49 | +- `crate_type` - see [linkage](#linkage). |
| 50 | +- `feature` - see [compiler features](#compiler-features). |
| 51 | +- `no_builtins` - disable optimizing certain code patterns to invocations of |
| 52 | + library functions that are assumed to exist |
| 53 | +- `no_main` - disable emitting the `main` symbol. Useful when some other |
| 54 | + object being linked to defines `main`. |
| 55 | +- `no_start` - disable linking to the `native` crate, which specifies the |
| 56 | + "start" language item. |
| 57 | +- `no_std` - disable linking to the `std` crate. |
| 58 | +- `plugin` - load a list of named crates as compiler plugins, e.g. |
| 59 | + `#![plugin(foo, bar)]`. Optional arguments for each plugin, |
| 60 | + i.e. `#![plugin(foo(... args ...))]`, are provided to the plugin's |
| 61 | + registrar function. The `plugin` feature gate is required to use |
| 62 | + this attribute. |
| 63 | +- `recursion_limit` - Sets the maximum depth for potentially |
| 64 | + infinitely-recursive compile-time operations like |
| 65 | + auto-dereference or macro expansion. The default is |
| 66 | + `#![recursion_limit="64"]`. |
| 67 | + |
| 68 | +### Module-only attributes |
| 69 | + |
| 70 | +- `no_implicit_prelude` - disable injecting `use std::prelude::*` in this |
| 71 | + module. |
| 72 | +- `path` - specifies the file to load the module from. `#[path="foo.rs"] mod |
| 73 | + bar;` is equivalent to `mod bar { /* contents of foo.rs */ }`. The path is |
| 74 | + taken relative to the directory that the current module is in. |
| 75 | + |
| 76 | +## Function-only attributes |
| 77 | + |
| 78 | +- `main` - indicates that this function should be passed to the entry point, |
| 79 | + rather than the function in the crate root named `main`. |
| 80 | +- `plugin_registrar` - mark this function as the registration point for |
| 81 | + [compiler plugins][plugin], such as loadable syntax extensions. |
| 82 | +- `start` - indicates that this function should be used as the entry point, |
| 83 | + overriding the "start" language item. See the "start" [language |
| 84 | + item](#language-items) for more details. |
| 85 | +- `test` - indicates that this function is a test function, to only be compiled |
| 86 | + in case of `--test`. |
| 87 | +- `should_panic` - indicates that this test function should panic, inverting the success condition. |
| 88 | +- `cold` - The function is unlikely to be executed, so optimize it (and calls |
| 89 | + to it) differently. |
| 90 | +- `naked` - The function utilizes a custom ABI or custom inline ASM that requires |
| 91 | + epilogue and prologue to be skipped. |
| 92 | + |
| 93 | +## Static-only attributes |
| 94 | + |
| 95 | +- `thread_local` - on a `static mut`, this signals that the value of this |
| 96 | + static may change depending on the current thread. The exact consequences of |
| 97 | + this are implementation-defined. |
| 98 | + |
| 99 | +## FFI attributes |
| 100 | + |
| 101 | +On an `extern` block, the following attributes are interpreted: |
| 102 | + |
| 103 | +- `link_args` - specify arguments to the linker, rather than just the library |
| 104 | + name and type. This is feature gated and the exact behavior is |
| 105 | + implementation-defined (due to variety of linker invocation syntax). |
| 106 | +- `link` - indicate that a native library should be linked to for the |
| 107 | + declarations in this block to be linked correctly. `link` supports an optional |
| 108 | + `kind` key with three possible values: `dylib`, `static`, and `framework`. See |
| 109 | + [external blocks](#external-blocks) for more about external blocks. Two |
| 110 | + examples: `#[link(name = "readline")]` and |
| 111 | + `#[link(name = "CoreFoundation", kind = "framework")]`. |
| 112 | +- `linked_from` - indicates what native library this block of FFI items is |
| 113 | + coming from. This attribute is of the form `#[linked_from = "foo"]` where |
| 114 | + `foo` is the name of a library in either `#[link]` or a `-l` flag. This |
| 115 | + attribute is currently required to export symbols from a Rust dynamic library |
| 116 | + on Windows, and it is feature gated behind the `linked_from` feature. |
| 117 | + |
| 118 | +On declarations inside an `extern` block, the following attributes are |
| 119 | +interpreted: |
| 120 | + |
| 121 | +- `link_name` - the name of the symbol that this function or static should be |
| 122 | + imported as. |
| 123 | +- `linkage` - on a static, this specifies the [linkage |
| 124 | + type](http://llvm.org/docs/LangRef.html#linkage-types). |
| 125 | + |
| 126 | +On `enum`s: |
| 127 | + |
| 128 | +- `repr` - on C-like enums, this sets the underlying type used for |
| 129 | + representation. Takes one argument, which is the primitive |
| 130 | + type this enum should be represented for, or `C`, which specifies that it |
| 131 | + should be the default `enum` size of the C ABI for that platform. Note that |
| 132 | + enum representation in C is undefined, and this may be incorrect when the C |
| 133 | + code is compiled with certain flags. |
| 134 | + |
| 135 | +On `struct`s: |
| 136 | + |
| 137 | +- `repr` - specifies the representation to use for this struct. Takes a list |
| 138 | + of options. The currently accepted ones are `C` and `packed`, which may be |
| 139 | + combined. `C` will use a C ABI compatible struct layout, and `packed` will |
| 140 | + remove any padding between fields (note that this is very fragile and may |
| 141 | + break platforms which require aligned access). |
| 142 | + |
| 143 | +## Macro-related attributes |
| 144 | + |
| 145 | +- `macro_use` on a `mod` — macros defined in this module will be visible in the |
| 146 | + module's parent, after this module has been included. |
| 147 | + |
| 148 | +- `macro_use` on an `extern crate` — load macros from this crate. An optional |
| 149 | + list of names `#[macro_use(foo, bar)]` restricts the import to just those |
| 150 | + macros named. The `extern crate` must appear at the crate root, not inside |
| 151 | + `mod`, which ensures proper function of the [`$crate` macro |
| 152 | + variable](book/macros.html#The%20variable%20%24crate). |
| 153 | + |
| 154 | +- `macro_reexport` on an `extern crate` — re-export the named macros. |
| 155 | + |
| 156 | +- `macro_export` - export a macro for cross-crate usage. |
| 157 | + |
| 158 | +- `no_link` on an `extern crate` — even if we load this crate for macros, don't |
| 159 | + link it into the output. |
| 160 | + |
| 161 | +See the [macros section of the |
| 162 | +book](book/macros.html#Scoping%20and%20macro%20import%2Fexport) for more information on |
| 163 | +macro scope. |
| 164 | + |
| 165 | +## Miscellaneous attributes |
| 166 | + |
| 167 | +- `deprecated` - mark the item as deprecated; the full attribute is |
| 168 | + `#[deprecated(since = "crate version", note = "...")`, where both arguments |
| 169 | + are optional. |
| 170 | +- `export_name` - on statics and functions, this determines the name of the |
| 171 | + exported symbol. |
| 172 | +- `link_section` - on statics and functions, this specifies the section of the |
| 173 | + object file that this item's contents will be placed into. |
| 174 | +- `no_mangle` - on any item, do not apply the standard name mangling. Set the |
| 175 | + symbol for this item to its identifier. |
| 176 | +- `simd` - on certain tuple structs, derive the arithmetic operators, which |
| 177 | + lower to the target's SIMD instructions, if any; the `simd` feature gate |
| 178 | + is necessary to use this attribute. |
| 179 | +- `unsafe_destructor_blind_to_params` - on `Drop::drop` method, asserts that the |
| 180 | + destructor code (and all potential specializations of that code) will |
| 181 | + never attempt to read from nor write to any references with lifetimes |
| 182 | + that come in via generic parameters. This is a constraint we cannot |
| 183 | + currently express via the type system, and therefore we rely on the |
| 184 | + programmer to assert that it holds. Adding this to a Drop impl causes |
| 185 | + the associated destructor to be considered "uninteresting" by the |
| 186 | + Drop-Check rule, and thus it can help sidestep data ordering |
| 187 | + constraints that would otherwise be introduced by the Drop-Check |
| 188 | + rule. Such sidestepping of the constraints, if done incorrectly, can |
| 189 | + lead to undefined behavior (in the form of reading or writing to data |
| 190 | + outside of its dynamic extent), and thus this attribute has the word |
| 191 | + "unsafe" in its name. To use this, the |
| 192 | + `unsafe_destructor_blind_to_params` feature gate must be enabled. |
| 193 | +- `doc` - Doc comments such as `/// foo` are equivalent to `#[doc = "foo"]`. |
| 194 | +- `rustc_on_unimplemented` - Write a custom note to be shown along with the error |
| 195 | + when the trait is found to be unimplemented on a type. |
| 196 | + You may use format arguments like `{T}`, `{A}` to correspond to the |
| 197 | + types at the point of use corresponding to the type parameters of the |
| 198 | + trait of the same name. `{Self}` will be replaced with the type that is supposed |
| 199 | + to implement the trait but doesn't. To use this, the `on_unimplemented` feature gate |
| 200 | + must be enabled. |
| 201 | +- `must_use` - on structs and enums, will warn if a value of this type isn't used or |
| 202 | + assigned to a variable. You may also include an optional message by using |
| 203 | + `#[must_use = "message"]` which will be given alongside the warning. |
0 commit comments