|
| 1 | +# `register_tool` |
| 2 | + |
| 3 | +The tracking issue for this feature is: [#66079] |
| 4 | + |
| 5 | +[#66079]: https://github.com/rust-lang/rust/issues/66079 |
| 6 | + |
| 7 | +------------------------ |
| 8 | + |
| 9 | +The `register_tool` language feature informs the compiler that attributes in your code are meant to be used with tools other than the compiler itself. This can be useful if your code has semantic meaning without the external tool, but enables additional features when the tool is present. |
| 10 | + |
| 11 | +`register_tool` also allows configuring lint levels for external tools. |
| 12 | + |
| 13 | +Tool attributes are only meant for ignorable attributes. If your code *changes* meaning when the attribute is present, it should not use a tool attribute (because it cannot be compiled with anything other than the external tool, and in a sense is a fork of the language). |
| 14 | + |
| 15 | +------------------------ |
| 16 | + |
| 17 | +`#![register_tool(tool)]` is an attribute, and is only valid at the crate root. |
| 18 | +Attributes using the registered tool are checked for valid syntax, and lint attributes are checked to be in a valid format. However, the compiler cannot validate the semantics of the attribute, nor can it tell whether the configured lint is present in the external tool. |
| 19 | + |
| 20 | +Semantically, `clippy::*`, `rustdoc::*`, and `rustfmt::*` lints and attributes all behave as if `#![register_tool(clippy, rustdoc, rustfmt)]` were injected into the crate root, except that the `rustdoc` namespace can only be used for lints, not for attributes. |
| 21 | +When compiling with `-Z unstable-features`, `rustc::*` lints can also be used. Like `rustdoc`, the `rustc` namespace can only be used with lints, not attributes. |
| 22 | + |
| 23 | +The compiler will emit an error if it encounters a lint/attribute whose namespace isn't a registered tool. |
| 24 | + |
| 25 | +Tool namespaces cannot be nested; `register_tool(main_tool::subtool)` is an error. |
| 26 | + |
| 27 | +## Examples |
| 28 | + |
| 29 | +Tool attributes: |
| 30 | + |
| 31 | +```rust |
| 32 | +#![feature(register_tool)] |
| 33 | +#![register_tool(c2rust)] |
| 34 | + |
| 35 | +// Mark which C header file this module was generated from. |
| 36 | +#[c2rust::header_src = "operations.h"] |
| 37 | +pub mod operations_h { |
| 38 | + use std::ffi::c_int; |
| 39 | + |
| 40 | + // Mark which source line this struct was generated from. |
| 41 | + #[c2rust::src_loc = "11:0"] |
| 42 | + pub struct Point { |
| 43 | + pub x: c_int, |
| 44 | + pub y: c_int, |
| 45 | + } |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +Tool lints: |
| 50 | + |
| 51 | +``` |
| 52 | +#![feature(register_tool)] |
| 53 | +#![register_tool(bevy)] |
| 54 | +#![deny(bevy::duplicate_bevy_dependencies)] |
| 55 | +``` |
0 commit comments