diff --git a/src/lib.rs b/src/lib.rs index 8b6afa2..54390bc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -619,14 +619,14 @@ macro_rules! quote_spanned { #[macro_export] macro_rules! quote_spanned { ($span:expr=>) => {{ - let _ = $crate::__private::GetSpan($span).__into_span(); + let _ = $crate::__private::get_span($span).__into_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::GetSpan($span).__into_span(); + let _span = $crate::__private::get_span($span).__into_span(); $crate::quote_token_spanned!{$tt _s _span} _s }}; @@ -634,13 +634,13 @@ macro_rules! quote_spanned { // Special case rules for two tts, for performance. ($span:expr=> # $var:ident) => {{ let mut _s = $crate::__private::TokenStream::new(); - let _ = $crate::__private::GetSpan($span).__into_span(); + let _ = $crate::__private::get_span($span).__into_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::GetSpan($span).__into_span(); + let _span = $crate::__private::get_span($span).__into_span(); $crate::quote_token_spanned!{$tt1 _s _span} $crate::quote_token_spanned!{$tt2 _s _span} _s @@ -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::GetSpan($span).__into_span(); + let _span = $crate::__private::get_span($span).__into_span(); $crate::quote_each_token_spanned!{_s _span $($tt)*} _s }}; diff --git a/src/runtime.rs b/src/runtime.rs index 3861395..a2c72a5 100644 --- a/src/runtime.rs +++ b/src/runtime.rs @@ -1,8 +1,8 @@ +use self::get_span::{GetSpanInner, GetSpan}; use crate::{IdentFragment, ToTokens, TokenStreamExt}; use core::fmt; use core::iter; -use core::ops::{BitOr, Deref}; -use proc_macro2::extra::DelimSpan; +use core::ops::BitOr; use proc_macro2::{Group, Ident, Punct, Spacing, TokenTree}; pub use core::option::Option; @@ -166,34 +166,41 @@ impl ToTokens for RepInterp { } } -#[repr(transparent)] -pub struct GetSpan(pub T); - -#[repr(transparent)] -pub struct WrapperDelimSpan { - span: DelimSpan, +#[inline] +pub fn get_span(span: T) -> GetSpan { + GetSpan(GetSpanInner(span)) } -impl GetSpan { - #[inline] - pub fn __into_span(self) -> Span { - self.0 +mod get_span { + use core::ops::Deref; + use proc_macro2::extra::DelimSpan; + use proc_macro2::Span; + + pub struct GetSpan(pub(crate) GetSpanInner); + + pub struct GetSpanInner(pub(crate) T); + + impl GetSpan { + #[inline] + pub fn __into_span(self) -> Span { + self.0.0 + } } -} -impl WrapperDelimSpan { - #[inline] - pub fn __into_span(&self) -> Span { - self.span.join() + impl GetSpanInner { + #[inline] + pub fn __into_span(&self) -> Span { + self.0.join() + } } -} -impl Deref for GetSpan { - type Target = WrapperDelimSpan; + impl Deref for GetSpan { + type Target = GetSpanInner; - #[inline] - fn deref(&self) -> &Self::Target { - unsafe { &*(self as *const GetSpan as *const WrapperDelimSpan) } + #[inline] + fn deref(&self) -> &Self::Target { + &self.0 + } } } diff --git a/tests/ui/wrong-type-span.stderr b/tests/ui/wrong-type-span.stderr index 5061c60..bbc5abd 100644 --- a/tests/ui/wrong-type-span.stderr +++ b/tests/ui/wrong-type-span.stderr @@ -1,9 +1,9 @@ -error[E0599]: no method named `__into_span` found for struct `GetSpan<&str>` in the current scope +error[E0599]: no method named `__into_span` found for struct `__private::get_span::GetSpan<&str>` in the current scope --> tests/ui/wrong-type-span.rs:6:5 | 6 | quote_spanned!(span=> #x); | ^^^^^^^^^^^^^^^^^^^^^^^^^ method not found in `GetSpan<&str>` | = note: the method was found for - - `GetSpan` + - `__private::get_span::GetSpan` = note: this error originates in the macro `quote_spanned` (in Nightly builds, run with -Z macro-backtrace for more info)