-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix
proc macro panicked
with Invalid type
in const_random!
in R…
…ust 1.77. With Rust nightly 1.77, any invocation of `const_random!` with a `u8` array causes a compile-time panic. This can be seen when running `cargo test`: ``` error: proc macro panicked --> tests/tests.rs:52:28 | 52 | const VALUE1: &[u8] = &const_random!([u8; 30]); | ^^^^^^^^^^^^^^^^^^^^^^^ | = help: message: Invalid type ``` This is because the proc macro starts by calling `to_string` on the input token stream, and then uses substring matching to "parse" it. In Rust 1.77 the `Display` impl for `TokenStream` has changed, and what used to be converted to the string `"[u8 ; 30]"` is now `"[u8; 30]"`. As a result, the `byte_array.starts_with("[u8 ; ")` call fails. Note that substring matching is inherently flawed because the whitespace in the output of `to_string` is not guaranteed. This commit rewrites the proc macro to be robust in the face of `to_string` whitespace changes, by iterating over the individual `TokenTrees`s. The commit also adds a comment explaining why `usize` and `isize` are handled differently, because it's subtle. Note: I ran `cargo fmt` within `macro/` to format the changes to `macro/src/lib.rs` and it made some minor changes to `macro/src/span.rs` as well.
- Loading branch information
1 parent
e9f560b
commit 025c9c5
Showing
2 changed files
with
86 additions
and
44 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