Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Special case quote!/quote_spanned! for 1 and 2 tts. #217

Merged
merged 1 commit into from Apr 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/lib.rs
Expand Up @@ -473,6 +473,28 @@ macro_rules! quote {
() => {
$crate::__private::TokenStream::new()
};

// Special case rule for a single tt, for performance.
($tt:tt) => {{
let mut _s = $crate::__private::TokenStream::new();
$crate::quote_token!($tt _s);
_s
}};

// Special case rules for two tts, for performance.
(# $var:ident) => {{
let mut _s = $crate::__private::TokenStream::new();
$crate::ToTokens::to_tokens(&$var, &mut _s);
_s
}};
($tt1:tt $tt2:tt) => {{
let mut _s = $crate::__private::TokenStream::new();
$crate::quote_token!($tt1 _s);
$crate::quote_token!($tt2 _s);
_s
}};

// Catch-all rule for all remaining inputs.
($($tt:tt)*) => {{
let mut _s = $crate::__private::TokenStream::new();
$crate::quote_each_token!(_s $($tt)*);
Expand Down Expand Up @@ -582,6 +604,31 @@ macro_rules! quote_spanned {
let _: $crate::__private::Span = $span;
$crate::__private::TokenStream::new()
}};

// Special case rule for a single tt, for performance.
($span:expr=> $tt:tt) => {{
let mut _s = $crate::__private::TokenStream::new();
let _span: $crate::__private::Span = $span;
$crate::quote_token_spanned!($tt _s _span);
_s
}};

// Special case rules for two tts, for performance.
($span:expr=> # $var:ident) => {{
let mut _s = $crate::__private::TokenStream::new();
let _span: $crate::__private::Span = $span;
$crate::ToTokens::to_tokens(&$var, &mut _s);
_s
}};
($span:expr=> $tt1:tt $tt2:tt) => {{
let mut _s = $crate::__private::TokenStream::new();
let _span: $crate::__private::Span = $span;
$crate::quote_token_spanned!($tt1 _s _span);
$crate::quote_token_spanned!($tt2 _s _span);
_s
}};

// Catch-all rule for all remaining inputs.
($span:expr=> $($tt:tt)*) => {{
let mut _s = $crate::__private::TokenStream::new();
let _span: $crate::__private::Span = $span;
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/not-quotable.stderr
Expand Up @@ -14,4 +14,4 @@ error[E0277]: the trait bound `Ipv4Addr: ToTokens` is not satisfied
RepInterp<T>
String
and 23 others
= note: this error originates in the macro `$crate::quote_token_with_context` (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in the macro `quote` (in Nightly builds, run with -Z macro-backtrace for more info)