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

Add DelimSpan to hold the 3 different spans of a delimiter #366

Merged
merged 1 commit into from
Mar 12, 2023

Conversation

dtolnay
Copy link
Owner

@dtolnay dtolnay commented Mar 12, 2023

For getting the span of an opening punctuation or closing punctuation of a group, proc_macro only lets you use span_open() and span_close() if you still have the original Group that came in through the macro input. If you just have the group's Span, there is no possible way to split that into span_open and span_close after the fact (even though rustc is capable of doing this operation). You can't even fool it by doing let mut tmp = Group::new(Delimiter::Parenthesis, TokenStream::new()); tmp.set_span(span); tmp.span_close() -- this will just give you back span instead of the correct span_close.

Libraries sometimes want to keep track of delimiter spans separately from contents. An example of this is syn::MacroDelimiter -- https://docs.rs/syn/1/syn/enum.MacroDelimiter.html. Delimiters of a macro invocation cannot be Delimiter::None so representing them using the original Group would be too general. Today such libraries are forced to eagerly call span_open() and span_close() before dropping the original parsed Group, and then they need to store 3×proc_macro2::Span which is an excessive 36 bytes.

This PR adds a more compact 3×Span data structure which is only 12 bytes, the same size as a single proc_macro2::Span.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant