Commit 74a6efe
Improve TypeUuid's derive macro error messages (bevyengine#9315)
# Objective
- Better error message
- More idiomatic code
## Solution
Refactorize `TypeUuid` macros to use `syn::Result` instead of panic.
## Before/After error messages
### Missing `#[uuid]` attribtue
#### Before
```
error: proc-macro derive panicked
--> src\main.rs:1:10
|
1 | #[derive(TypeUuid)]
| ^^^^^^^^
|
= help: message: No `#[uuid = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"` attribute found.
```
#### After
```
error: No `#[uuid = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"]` attribute found.
--> src\main.rs:3:10
|
3 | #[derive(TypeUuid)]
| ^^^^^^^^
|
= note: this error originates in the derive macro `TypeUuid` (in Nightly builds, run with -Z macro-backtrace for more info)
```
### Malformed attribute
#### Before
```
error: proc-macro derive panicked
--> src\main.rs:3:10
|
3 | #[derive(TypeUuid)]
| ^^^^^^^^
|
= help: message: `uuid` attribute must take the form `#[uuid = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"`.
```
#### After
```
error: `uuid` attribute must take the form `#[uuid = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"]`.
--> src\main.rs:4:1
|
4 | #[uuid = 42]
| ^^^^^^^^^^^^
```
### UUID parse fail
#### Before
```
error: proc-macro derive panicked
--> src\main.rs:3:10
|
3 | #[derive(TypeUuid)]
| ^^^^^^^^
|
= help: message: Value specified to `#[uuid]` attribute is not a valid UUID.: Error(SimpleLength { len: 3 })
```
#### After
```
error: Invalid UUID: invalid length: expected length 32 for simple format, found 3
--> src\main.rs:4:10
|
4 | #[uuid = "000"]
| ^^^^^
```
### With [Error
Lens](https://marketplace.visualstudio.com/items?itemName=usernamehw.errorlens)
#### Before

#### After

---
## Changelog
- `#[derive(TypeUuid)]` provide better error messages.1 parent 41b9fdc commit 74a6efe
File tree
5 files changed
+75
-33
lines changed- crates
- bevy_reflect_compile_fail_tests/tests
- type_uuid_derive
- bevy_reflect/bevy_reflect_derive/src
5 files changed
+75
-33
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
33 | | - | |
34 | 33 | | |
35 | 34 | | |
36 | 35 | | |
| |||
39 | 38 | | |
40 | 39 | | |
41 | 40 | | |
42 | | - | |
43 | 41 | | |
44 | 42 | | |
45 | 43 | | |
| |||
288 | 286 | | |
289 | 287 | | |
290 | 288 | | |
| 289 | + | |
291 | 290 | | |
| 291 | + | |
| 292 | + | |
292 | 293 | | |
293 | 294 | | |
294 | 295 | | |
| |||
588 | 589 | | |
589 | 590 | | |
590 | 591 | | |
591 | | - | |
592 | | - | |
| 592 | + | |
| 593 | + | |
593 | 594 | | |
Lines changed: 29 additions & 29 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | 1 | | |
| 2 | + | |
4 | 3 | | |
5 | 4 | | |
6 | 5 | | |
7 | | - | |
| 6 | + | |
8 | 7 | | |
9 | 8 | | |
10 | | - | |
11 | | - | |
12 | | - | |
13 | | - | |
14 | | - | |
15 | | - | |
16 | | - | |
17 | | - | |
| 9 | + | |
18 | 10 | | |
19 | | - | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
20 | 16 | | |
21 | 17 | | |
22 | 18 | | |
23 | 19 | | |
24 | 20 | | |
25 | 21 | | |
26 | | - | |
| 22 | + | |
27 | 23 | | |
28 | 24 | | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
33 | 29 | | |
34 | 30 | | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
40 | 41 | | |
41 | | - | |
| 42 | + | |
42 | 43 | | |
43 | 44 | | |
44 | 45 | | |
45 | | - | |
| 46 | + | |
46 | 47 | | |
47 | 48 | | |
48 | 49 | | |
49 | 50 | | |
50 | | - | |
| 51 | + | |
51 | 52 | | |
52 | 53 | | |
53 | 54 | | |
| |||
74 | 75 | | |
75 | 76 | | |
76 | 77 | | |
77 | | - | |
| 78 | + | |
78 | 79 | | |
79 | 80 | | |
80 | 81 | | |
81 | | - | |
82 | | - | |
| 82 | + | |
83 | 83 | | |
84 | 84 | | |
85 | 85 | | |
| |||
90 | 90 | | |
91 | 91 | | |
92 | 92 | | |
93 | | - | |
| 93 | + | |
94 | 94 | | |
95 | 95 | | |
96 | 96 | | |
| |||
Lines changed: 5 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
Lines changed: 17 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
Lines changed: 19 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
0 commit comments