Skip to content
/ rust Public
forked from rust-lang/rust

Commit 47d0c6b

Browse files
authored
Rollup merge of rust-lang#138586 - jyn514:doc-register-tool, r=jieyouxu
Document `#![register_tool]` cc rust-lang#66079
2 parents 72f95eb + 10bc5ac commit 47d0c6b

File tree

5 files changed

+79
-0
lines changed

5 files changed

+79
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
```
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//@ check-pass
2+
//@ compile-flags: -Z crate-attr=feature(register_tool) -Z crate-attr=register_tool(foo)
3+
4+
#[allow(foo::bar)]
5+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//@ check-pass
2+
3+
#![feature(register_tool)]
4+
#![register_tool(foo, bar, baz)]
5+
6+
#[allow(foo::a, bar::b, baz::c)]
7+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#![feature(register_tool)]
2+
#![register_tool(foo::bar)] //~ ERROR only accepts identifiers
3+
4+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: `register_tool` only accepts identifiers
2+
--> $DIR/nested-disallowed.rs:2:18
3+
|
4+
LL | #![register_tool(foo::bar)]
5+
| ^^^^^^^^ not an identifier
6+
7+
error: aborting due to 1 previous error
8+

0 commit comments

Comments
 (0)