Skip to content

Commit

Permalink
Work around Rust hygiene bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
m-ou-se authored and de-vri-es committed Jul 4, 2020
1 parent 13953e9 commit f412269
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
20 changes: 20 additions & 0 deletions assert2-macros/src/hygiene_bug.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use proc_macro::{Delimiter, Group, TokenStream, TokenTree};

pub fn fix(ts: TokenStream) -> TokenStream {
ts.into_iter()
.map(|t| match t {
TokenTree::Group(g) => {
let mut fixed = Group::new(
match g.delimiter() {
Delimiter::None => Delimiter::Parenthesis,
d => d,
},
fix(g.stream()),
);
fixed.set_span(g.span());
TokenTree::Group(fixed)
}
t => t,
})
.collect()
}
6 changes: 4 additions & 2 deletions assert2-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,19 @@ type FormatArgs = Punctuated<syn::Expr, syn::token::Comma>;
#[proc_macro_hack]
#[doc(hidden)]
pub fn check_impl(tokens: TokenStream) -> TokenStream {
check_or_assert_impl(syn::parse_macro_input!(tokens)).into()
hygiene_bug::fix(check_or_assert_impl(syn::parse_macro_input!(tokens)).into())
}

mod hygiene_bug;

#[cfg(feature = "let-assert")]
mod let_assert;

#[cfg(feature = "let-assert")]
#[proc_macro]
#[doc(hidden)]
pub fn let_assert_impl(tokens: proc_macro::TokenStream) -> proc_macro::TokenStream {
let_assert::let_assert_impl(syn::parse_macro_input!(tokens)).into()
hygiene_bug::fix(let_assert::let_assert_impl(syn::parse_macro_input!(tokens)).into())
}

/// Real implementation for assert!() and check!().
Expand Down

0 comments on commit f412269

Please sign in to comment.