From dca4be4603dd410efe1ac8f5b6ec73497fe77a97 Mon Sep 17 00:00:00 2001 From: Faysal Aberkane Date: Thu, 19 Nov 2026 08:24:23 +0000 Subject: [PATCH 1/2] fix: keep a group's whole span through the compiler bridge Converting a `proc_macro::Group` kept only its delimiter spans, and converting back joined them. On stable `Span::join` returns the second span, so a group that went through moxy covered only its closing delimiter. Diagnostics on a re-emitted group pointed at that delimiter (an array literal of the wrong length underlined `]`), and a lint that reads the source between a keyword and a group, such as clippy's `suspicious_else_formatting` on `} else {`, read the whole block and fired on correctly formatted code. `DelimSpan` now stores the whole group's span next to the delimiter spans. The `proc_macro` and `proc_macro2` bridges fill it from the compiler's group span, and `DelimSpan::new` still joins the two delimiter spans. Three derive error fixtures now underline the whole node they name instead of its last token, and a trybuild fixture covers the round trip. --- crates/moxy-macro-tests/src/lib.rs | 7 +++++++ crates/moxy-macro-tests/tests/spans.rs | 6 ++++++ .../trybuild/spans/fail/round_trip_array.rs | 5 +++++ .../spans/fail/round_trip_array.stderr | 13 +++++++++++++ crates/moxy-token/src/bridge/proc_macro.rs | 6 +++--- crates/moxy-token/src/bridge/proc_macro2.rs | 6 +++--- crates/moxy-token/src/span/delim.rs | 18 +++++++++++++++--- .../derive/fail/meta_invalid_enum.stderr | 4 ++-- .../trybuild/derive/fail/meta_union.stderr | 9 ++++++--- 9 files changed, 60 insertions(+), 14 deletions(-) create mode 100644 crates/moxy-macro-tests/tests/trybuild/spans/fail/round_trip_array.rs create mode 100644 crates/moxy-macro-tests/tests/trybuild/spans/fail/round_trip_array.stderr 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 } } diff --git a/fixtures/trybuild/derive/fail/meta_invalid_enum.stderr b/fixtures/trybuild/derive/fail/meta_invalid_enum.stderr index 6f18ffb6..56dab79c 100644 --- a/fixtures/trybuild/derive/fail/meta_invalid_enum.stderr +++ b/fixtures/trybuild/derive/fail/meta_invalid_enum.stderr @@ -1,5 +1,5 @@ error: `Meta` tuple variants must contain exactly one field - --> fixtures/trybuild/derive/fail/meta_invalid_enum.rs:3:20 + --> fixtures/trybuild/derive/fail/meta_invalid_enum.rs:3:9 | 3 | Pair(bool, bool), - | ^ + | ^^^^^^^^^^^^ diff --git a/fixtures/trybuild/derive/fail/meta_union.stderr b/fixtures/trybuild/derive/fail/meta_union.stderr index 627fd349..2231599c 100644 --- a/fixtures/trybuild/derive/fail/meta_union.stderr +++ b/fixtures/trybuild/derive/fail/meta_union.stderr @@ -1,5 +1,8 @@ error: `Meta` cannot be derived for unions - --> fixtures/trybuild/derive/fail/meta_union.rs:4:1 + --> fixtures/trybuild/derive/fail/meta_union.rs:2:19 | -4 | } - | ^ +2 | union Unsupported { + | ___________________^ +3 | | flag: bool, +4 | | } + | |_^ From 3d9d11230fd649ab8b74a3ce9bc9dd2162c1e377 Mon Sep 17 00:00:00 2001 From: Faysal Aberkane Date: Sat, 21 Nov 2026 06:59:14 +0000 Subject: [PATCH 2/2] Retain upstream enum and union diagnostic fixtures --- fixtures/trybuild/derive/fail/meta_invalid_enum.stderr | 4 ++-- fixtures/trybuild/derive/fail/meta_union.stderr | 9 +++------ 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/fixtures/trybuild/derive/fail/meta_invalid_enum.stderr b/fixtures/trybuild/derive/fail/meta_invalid_enum.stderr index 56dab79c..6f18ffb6 100644 --- a/fixtures/trybuild/derive/fail/meta_invalid_enum.stderr +++ b/fixtures/trybuild/derive/fail/meta_invalid_enum.stderr @@ -1,5 +1,5 @@ error: `Meta` tuple variants must contain exactly one field - --> fixtures/trybuild/derive/fail/meta_invalid_enum.rs:3:9 + --> fixtures/trybuild/derive/fail/meta_invalid_enum.rs:3:20 | 3 | Pair(bool, bool), - | ^^^^^^^^^^^^ + | ^ diff --git a/fixtures/trybuild/derive/fail/meta_union.stderr b/fixtures/trybuild/derive/fail/meta_union.stderr index 2231599c..627fd349 100644 --- a/fixtures/trybuild/derive/fail/meta_union.stderr +++ b/fixtures/trybuild/derive/fail/meta_union.stderr @@ -1,8 +1,5 @@ error: `Meta` cannot be derived for unions - --> fixtures/trybuild/derive/fail/meta_union.rs:2:19 + --> fixtures/trybuild/derive/fail/meta_union.rs:4:1 | -2 | union Unsupported { - | ___________________^ -3 | | flag: bool, -4 | | } - | |_^ +4 | } + | ^