diff --git a/assert2-macros/src/hygiene_bug.rs b/assert2-macros/src/hygiene_bug.rs new file mode 100644 index 0000000..81aa2aa --- /dev/null +++ b/assert2-macros/src/hygiene_bug.rs @@ -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() +} diff --git a/assert2-macros/src/lib.rs b/assert2-macros/src/lib.rs index 7e9f02f..ed208e1 100644 --- a/assert2-macros/src/lib.rs +++ b/assert2-macros/src/lib.rs @@ -15,9 +15,11 @@ type FormatArgs = Punctuated; #[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; @@ -25,7 +27,7 @@ mod 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!().