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

Generalize quote_spanned to accept DelimSpan as span #239

Merged
merged 1 commit into from
Mar 12, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -619,28 +619,28 @@ macro_rules! quote_spanned {
#[macro_export]
macro_rules! quote_spanned {
($span:expr=>) => {{
let _: $crate::__private::Span = $span;
let _ = $crate::__private::IntoSpan::into_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;
let _span = $crate::__private::IntoSpan::into_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 _: $crate::__private::Span = $span;
let _ = $crate::__private::IntoSpan::into_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;
let _span = $crate::__private::IntoSpan::into_span($span);
$crate::quote_token_spanned!{$tt1 _s _span}
$crate::quote_token_spanned!{$tt2 _s _span}
_s
Expand All @@ -649,7 +649,7 @@ macro_rules! quote_spanned {
// Rule for any other number of tokens.
($span:expr=> $($tt:tt)*) => {{
let mut _s = $crate::__private::TokenStream::new();
let _span: $crate::__private::Span = $span;
let _span = $crate::__private::IntoSpan::into_span($span);
$crate::quote_each_token_spanned!{_s _span $($tt)*}
_s
}};
Expand Down
19 changes: 19 additions & 0 deletions src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{IdentFragment, ToTokens, TokenStreamExt};
use core::fmt;
use core::iter;
use core::ops::BitOr;
use proc_macro2::extra::DelimSpan;
use proc_macro2::{Group, Ident, Punct, Spacing, TokenTree};

pub use core::option::Option;
Expand Down Expand Up @@ -165,6 +166,24 @@ impl<T: ToTokens> ToTokens for RepInterp<T> {
}
}

pub trait IntoSpan {
fn into_span(self) -> Span;
}

impl IntoSpan for Span {
#[inline]
fn into_span(self) -> Span {
self
}
}

impl IntoSpan for DelimSpan {
#[inline]
fn into_span(self) -> Span {
self.join()
}
}

pub fn push_group(tokens: &mut TokenStream, delimiter: Delimiter, inner: TokenStream) {
tokens.append(Group::new(delimiter, inner));
}
Expand Down
10 changes: 7 additions & 3 deletions tests/ui/wrong-type-span.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
error[E0308]: mismatched types
error[E0277]: the trait bound `&str: IntoSpan` is not satisfied
--> tests/ui/wrong-type-span.rs:6:20
|
6 | quote_spanned!(span=> #x);
| ---------------^^^^------
| | |
| | expected `Span`, found `&str`
| expected due to this
| | the trait `IntoSpan` is not implemented for `&str`
| required by a bound introduced by this call
|
= help: the following other types implement trait `IntoSpan`:
Span
proc_macro2::extra::DelimSpan