Skip to content

Commit ce880a4

Browse files
committed
Document #[diagnostic::on_move] in the unstable book
1 parent 70cf3f4 commit ce880a4

1 file changed

Lines changed: 92 additions & 0 deletions

File tree

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# `diagnostic_on_move`
2+
3+
The tracking issue for this feature is: [#154181]
4+
5+
------------------------
6+
7+
The `diagnostic_on_move` feature allows use of the `#[diagnostic::on_move]` attribute. It should be
8+
placed on struct, enum and union declarations, though it is not an error to be located in other
9+
positions. This attribute is a hint to the compiler to supplement the error message when the
10+
annotated type is involved in a borrowcheck error.
11+
12+
For example, [`File`] is annotated as such:
13+
```rust
14+
#![feature(diagnostic_on_move)]
15+
16+
#[diagnostic::on_move(note = "you can use `File::try_clone` \
17+
to duplicate a `File` instance")]
18+
pub struct File {
19+
// ...
20+
}
21+
```
22+
23+
When you try to use a `File` after it's already been moved, it will helpfully tell you about `try_clone`.
24+
25+
The message and label can also be customized:
26+
27+
```rust
28+
#![feature(diagnostic_on_move)]
29+
30+
use std::marker::PhantomData;
31+
32+
#[diagnostic::on_move(
33+
message = "`{Self}` cannot be used multiple times",
34+
label = "this token may only be used once",
35+
note = "you can create a new `Token` with `Token::conjure()`"
36+
)]
37+
pub struct Token<'brand> {
38+
spooky: PhantomData<&'brand ()>,
39+
}
40+
41+
impl Token<'_> {
42+
pub fn conjure<'u>() -> Token<'u> {
43+
Token {
44+
spooky: PhantomData,
45+
}
46+
}
47+
}
48+
```
49+
The user may try to use it like this:
50+
```rust,compile_fail,E0382
51+
# #![feature(diagnostic_on_move)]
52+
#
53+
# use std::marker::PhantomData;
54+
#
55+
# #[diagnostic::on_move(
56+
# message = "`{Self}` cannot be used multiple times",
57+
# label = "this token may only be used once",
58+
# note = "you can create a new `Token` with `Token::conjure()`"
59+
# )]
60+
# pub struct Token<'brand> {
61+
# spooky: PhantomData<&'brand ()>,
62+
# }
63+
#
64+
# impl Token<'_> {
65+
# pub fn conjure<'u>() -> Token<'u> {
66+
# Token {
67+
# spooky: PhantomData,
68+
# }
69+
# }
70+
# }
71+
# fn main() {
72+
let token = Token::conjure();
73+
let _ = (token, token);
74+
# }
75+
```
76+
This will result in the following error:
77+
```text
78+
error[E0382]: `Token` cannot be used multiple times
79+
--> src/main.rs:24:21
80+
|
81+
1 | let token = Token::conjure();
82+
| ----- this token may only be used once
83+
2 | let _ = (token, token);
84+
| ----- ^^^^^ value used here after move
85+
| |
86+
| value moved here
87+
|
88+
= note: you can create a new `Token` with `Token::conjure()`
89+
```
90+
91+
[`File`]: https://doc.rust-lang.org/nightly/std/fs/struct.File.html "File in std::fs"
92+
[#154181]: https://github.com/rust-lang/rust/issues/154181 "Tracking Issue for #[diagnostic::on_move]"

0 commit comments

Comments
 (0)