diff --git a/crates/moxy-macro-tests/src/lib.rs b/crates/moxy-macro-tests/src/lib.rs index f2a011aa..bf603dbb 100644 --- a/crates/moxy-macro-tests/src/lib.rs +++ b/crates/moxy-macro-tests/src/lib.rs @@ -24,6 +24,13 @@ pub fn nonzero_u8(input: proc_macro::TokenStream) -> proc_macro::TokenStream { } } +// Re-emits its input after a round trip through moxy's token stream +#[proc_macro] +pub fn round_trip(input: proc_macro::TokenStream) -> proc_macro::TokenStream { + let tokens: moxy::token::TokenStream = input.into(); + tokens.into() +} + #[derive(moxy::FromMeta)] struct RangeArgs { #[meta(default)] diff --git a/crates/moxy-macro-tests/tests/spans.rs b/crates/moxy-macro-tests/tests/spans.rs index 64bec621..b4342e01 100644 --- a/crates/moxy-macro-tests/tests/spans.rs +++ b/crates/moxy-macro-tests/tests/spans.rs @@ -11,3 +11,9 @@ fn derived_meta_errors_retain_their_source_span() { cases.pass("tests/trybuild/spans/pass/meta_*.rs"); cases.compile_fail("tests/trybuild/spans/fail/meta_*.rs"); } + +#[test] +fn round_tripped_groups_keep_their_whole_span() { + let cases = trybuild::TestCases::new(); + cases.compile_fail("tests/trybuild/spans/fail/round_trip_*.rs"); +} diff --git a/crates/moxy-macro-tests/tests/trybuild/spans/fail/round_trip_array.rs b/crates/moxy-macro-tests/tests/trybuild/spans/fail/round_trip_array.rs new file mode 100644 index 00000000..c331046f --- /dev/null +++ b/crates/moxy-macro-tests/tests/trybuild/spans/fail/round_trip_array.rs @@ -0,0 +1,5 @@ +moxy_macro_tests::round_trip! { + const VALUES: [u8; 2] = [1, 2, 3]; +} + +fn main() {} diff --git a/crates/moxy-macro-tests/tests/trybuild/spans/fail/round_trip_array.stderr b/crates/moxy-macro-tests/tests/trybuild/spans/fail/round_trip_array.stderr new file mode 100644 index 00000000..f7d28d55 --- /dev/null +++ b/crates/moxy-macro-tests/tests/trybuild/spans/fail/round_trip_array.stderr @@ -0,0 +1,13 @@ +error[E0308]: mismatched types + --> tests/trybuild/spans/fail/round_trip_array.rs:2:29 + | +2 | const VALUES: [u8; 2] = [1, 2, 3]; + | ------- ^^^^^^^^^ expected an array with a size of 2, found one with a size of 3 + | | + | expected because of the type of the constant + | +help: consider specifying the actual array length + | +2 - const VALUES: [u8; 2] = [1, 2, 3]; +2 + const VALUES: [u8; 3] = [1, 2, 3]; + | diff --git a/crates/moxy-token/src/bridge/proc_macro.rs b/crates/moxy-token/src/bridge/proc_macro.rs index 8d8fc998..4c60b2f2 100644 --- a/crates/moxy-token/src/bridge/proc_macro.rs +++ b/crates/moxy-token/src/bridge/proc_macro.rs @@ -159,7 +159,7 @@ impl From for Group { fn from(value: proc_macro::Group) -> Self { use crate::span::DelimSpan; let mut group = Self::new(value.delimiter().into(), value.stream().into()); - group.span = DelimSpan::new(value.span_open().into(), value.span_close().into()); + group.span = DelimSpan::with_whole(value.span_open().into(), value.span_close().into(), value.span().into()); group } } @@ -169,8 +169,8 @@ impl From for proc_macro::Group { fn from(value: Group) -> Self { let span = value.span.span().into(); let mut group = Self::new(value.delim.into(), value.tokens.into()); - // `proc_macro::Group` accepts one span, so separate opening and - // closing delimiter spans are represented by their combined span. + // `proc_macro::Group` accepts one span, the whole group's, which also + // places both delimiters. group.set_span(span); group } diff --git a/crates/moxy-token/src/bridge/proc_macro2.rs b/crates/moxy-token/src/bridge/proc_macro2.rs index a4aa91f3..d9f5d714 100644 --- a/crates/moxy-token/src/bridge/proc_macro2.rs +++ b/crates/moxy-token/src/bridge/proc_macro2.rs @@ -35,7 +35,7 @@ impl From for proc_macro2::Span { impl From for DelimSpan { fn from(value: proc_macro2::extra::DelimSpan) -> Self { - Self::new(value.open().into(), value.close().into()) + Self::with_whole(value.open().into(), value.close().into(), value.join().into()) } } @@ -151,8 +151,8 @@ impl From for Group { impl From for proc_macro2::Group { fn from(value: Group) -> Self { let mut group = Self::new(value.delim.into(), value.tokens.into()); - // `proc_macro2::Group` exposes one output span, so preserve the - // combined delimiter span rather than dropping both endpoints. + // `proc_macro2::Group` accepts one span, the whole group's, which also + // places both delimiters. group.set_span(value.span.span().into()); group } diff --git a/crates/moxy-token/src/span/delim.rs b/crates/moxy-token/src/span/delim.rs index b97f5e6c..6541a8d7 100644 --- a/crates/moxy-token/src/span/delim.rs +++ b/crates/moxy-token/src/span/delim.rs @@ -1,11 +1,13 @@ use crate::Span; -/// The opening and closing spans of a delimited token group. +/// The opening and closing spans of a delimited token group, and the span of +/// the whole group. #[derive(Debug, Copy, Clone)] #[cfg_attr(feature = "serde", derive(serde::Serialize))] pub struct DelimSpan { open: Span, close: Span, + whole: Span, } impl Default for DelimSpan { @@ -29,9 +31,19 @@ impl std::hash::Hash for DelimSpan { } impl DelimSpan { + /// The whole group's span joins `open` and `close`, which on stable keeps + /// `close` alone for compiler spans; use [`DelimSpan::with_whole`] when the + /// group's full span is known. #[inline] pub fn new(open: Span, close: Span) -> Self { - Self { open, close } + Self::with_whole(open, close, open.join(close)) + } + + /// Delimiter spans with the whole group's span, as the compiler reports it + /// for a group it lexed. + #[inline] + pub fn with_whole(open: Span, close: Span, whole: Span) -> Self { + Self { open, close, whole } } #[inline] @@ -46,7 +58,7 @@ impl DelimSpan { #[inline] pub fn span(&self) -> Span { - self.open.join(self.close) + self.whole } }