Skip to content
This repository was archived by the owner on Dec 17, 2025. It is now read-only.

Fixed an issue where creating a type in a macro input wouldn't parse correctly#4

Merged
VictorKoenders merged 1 commit intotrunkfrom
fix_macro_input
Jan 8, 2022
Merged

Fixed an issue where creating a type in a macro input wouldn't parse correctly#4
VictorKoenders merged 1 commit intotrunkfrom
fix_macro_input

Conversation

@VictorKoenders
Copy link
Contributor

@VictorKoenders VictorKoenders commented Jan 8, 2022

In bincode, the following code fails:

macro_rules! macro_newtype {
    ($name:ident) => {
        #[derive(bincode::Encode, bincode::Decode)]
        #[derive(PartialEq, Eq)]
        pub struct $name(pub usize);
    }
}
macro_newtype!(MacroNewtype);

with the error:

error: Invalid rust syntax, expected ident, got Some(Group { delimiter: None, stream: TokenStream [Ident { ident: "MacroNewtype", span: #0 bytes(6253..6265) }], span: #109 bytes(6212..6217) })
   --> tests\derive.rs:222:20
    |
222 |         pub struct $name(pub usize);
    |                    ^^^^^
...
225 | macro_newtype!(MacroNewtype);
    | ---------------------------- in this macro invocation
    |
    = note: this error originates in the macro `macro_newtype` (in Nightly builds, run with -Z macro-backtrace for more info)

It looks like rust does not actually insert an TokenTree::Ident when doing macro expansion, but instead adds a Group(delimiter: None, TokenStream: [Ident]). This PR adds a helper function to check for such a case.

Tested with bincode

@codecov-commenter
Copy link

Codecov Report

Merging #4 (2b42218) into trunk (5ff82e0) will increase coverage by 0.20%.
The diff coverage is 76.47%.

Impacted file tree graph

@@            Coverage Diff             @@
##            trunk       #4      +/-   ##
==========================================
+ Coverage   43.07%   43.27%   +0.20%     
==========================================
  Files          15       15              
  Lines        1047     1056       +9     
==========================================
+ Hits          451      457       +6     
- Misses        596      599       +3     
Impacted Files Coverage Δ
src/parse/body.rs 65.70% <66.66%> (ø)
src/parse/utils.rs 87.69% <72.72%> (-3.05%) ⬇️
src/parse/data_type.rs 89.28% <100.00%> (-0.72%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5ff82e0...2b42218. Read the comment docs.

@VictorKoenders VictorKoenders merged commit ac553e2 into trunk Jan 8, 2022
@VictorKoenders VictorKoenders deleted the fix_macro_input branch January 8, 2022 09:49
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants