forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 ![image](https://github.com/bevyengine/bevy/assets/33934311/415247fa-ff5c-4513-8012-7a9ff77445fb) #### After ![image](https://github.com/bevyengine/bevy/assets/33934311/d124eeaa-9213-49e0-8860-539ad0218a40) --- ## Changelog - `#[derive(TypeUuid)]` provide better error messages.
- Loading branch information
1 parent
28afa2d
commit 066e6d2
Showing
5 changed files
with
75 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
crates/bevy_reflect_compile_fail_tests/tests/type_uuid_derive.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#[test] | ||
fn test() { | ||
let t = trybuild::TestCases::new(); | ||
t.compile_fail("tests/type_uuid_derive/*.rs"); | ||
} |
17 changes: 17 additions & 0 deletions
17
crates/bevy_reflect_compile_fail_tests/tests/type_uuid_derive/derive_type_uuid.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
use bevy_reflect::TypeUuid; | ||
|
||
fn main() {} | ||
|
||
// Missing #[uuid] attribute | ||
#[derive(TypeUuid)] | ||
struct A; | ||
|
||
// Malformed attribute | ||
#[derive(TypeUuid)] | ||
#[uuid = 42] | ||
struct B; | ||
|
||
// UUID parse fail | ||
#[derive(TypeUuid)] | ||
#[uuid = "000"] | ||
struct C; |
19 changes: 19 additions & 0 deletions
19
crates/bevy_reflect_compile_fail_tests/tests/type_uuid_derive/derive_type_uuid.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
error: No `#[uuid = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"]` attribute found. | ||
--> tests/type_uuid_derive/derive_type_uuid.rs:6:10 | ||
| | ||
6 | #[derive(TypeUuid)] | ||
| ^^^^^^^^ | ||
| | ||
= note: this error originates in the derive macro `TypeUuid` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error: `uuid` attribute must take the form `#[uuid = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"]`. | ||
--> tests/type_uuid_derive/derive_type_uuid.rs:11:1 | ||
| | ||
11 | #[uuid = 42] | ||
| ^^^^^^^^^^^^ | ||
|
||
error: Invalid UUID: invalid length: expected length 32 for simple format, found 3 | ||
--> tests/type_uuid_derive/derive_type_uuid.rs:16:10 | ||
| | ||
16 | #[uuid = "000"] | ||
| ^^^^^ |