Skip to content
Closed
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
7 changes: 7 additions & 0 deletions crates/moxy-macro-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
6 changes: 6 additions & 0 deletions crates/moxy-macro-tests/tests/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
moxy_macro_tests::round_trip! {
const VALUES: [u8; 2] = [1, 2, 3];
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -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];
|
6 changes: 3 additions & 3 deletions crates/moxy-token/src/bridge/proc_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ impl From<proc_macro::Group> 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
}
}
Expand All @@ -169,8 +169,8 @@ impl From<Group> 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
}
Expand Down
6 changes: 3 additions & 3 deletions crates/moxy-token/src/bridge/proc_macro2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl From<Span> for proc_macro2::Span {

impl From<proc_macro2::extra::DelimSpan> 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())
}
}

Expand Down Expand Up @@ -151,8 +151,8 @@ impl From<proc_macro2::Group> for Group {
impl From<Group> 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
}
Expand Down
18 changes: 15 additions & 3 deletions crates/moxy-token/src/span/delim.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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]
Expand All @@ -46,7 +58,7 @@ impl DelimSpan {

#[inline]
pub fn span(&self) -> Span {
self.open.join(self.close)
self.whole
}
}

Expand Down