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

proc_macro::Group::span_open and span_close #53902

Merged
merged 3 commits into from
Sep 9, 2018
Merged
Changes from 1 commit
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
43 changes: 42 additions & 1 deletion src/libproc_macro/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ use syntax::errors::DiagnosticBuilder;
use syntax::parse::{self, token};
use syntax::symbol::Symbol;
use syntax::tokenstream;
use syntax_pos::{Pos, FileName};
use syntax_pos::{BytePos, Pos, FileName};

/// The main type provided by this crate, representing an abstract stream of
/// tokens, or, more specifically, a sequence of token trees.
Expand Down Expand Up @@ -671,11 +671,52 @@ impl Group {

/// Returns the span for the delimiters of this token stream, spanning the
/// entire `Group`.
///
/// ```text
/// pub fn span(&self) -> Span {
/// ^^^^^^^
/// ```
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
pub fn span(&self) -> Span {
self.span
}

/// Returns the span pointing to the opening delimiter of this group, or the
/// span of the entire group if this is a None-delimited group.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, this sounds too specific.
Perhaps more general methods on Span like fn shrink_to_lo/fn shrink_to_hi in the compiler would be more useful?

Copy link
Contributor

@petrochenkov petrochenkov Sep 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Need to figure out specifics though, zero-character spans are still showed as single-character in errors, but positions may be +/-1 wrong depending on use case.)

Copy link
Member Author

@dtolnay dtolnay Sep 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either way works for me, but in my use case I don't really see this being called other than on the span of a Group. I opened #53904 with methods on Span.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some quick review of uses of shrink_to_lo/shrink_to_hi in the compiler:

  • Pointing to the private visibility / place to insert visibility (not a group).
  • Pointing at elided () return type in functions (group).
  • Pointing to the place to insert missing imports (not a group).
  • Pointing at the end of fn call to insert .as_ref() (group).
  • Pointing at the last missing argument in fn call (group).
  • Pointing at elided lifetime in &Type (not a group).
  • Pointing at missing lifetime argument at the start of generic argument list (not a group).
  • ... (ok, that's enough)

So, there are different possible uses for both groups and non-groups.

///
/// ```text
/// pub fn span_open(&self) -> Span {
/// ^
/// ```
#[unstable(feature = "proc_macro_span", issue = "38356")]
pub fn span_open(&self) -> Span {
if self.delimiter == Delimiter::None {
self.span
} else {
let lo = self.span.0.lo();
let new_hi = BytePos::from_usize(lo.to_usize() + 1);
Span(self.span.0.with_hi(new_hi))
}
}

/// Returns the span pointing to the closing delimiter of this group, or the
/// span of the entire group if this is a None-delimited group.
///
/// ```text
/// pub fn span_close(&self) -> Span {
/// ^
/// ```
#[unstable(feature = "proc_macro_span", issue = "38356")]
pub fn span_close(&self) -> Span {
let hi = self.span.0.hi();
if self.delimiter == Delimiter::None || hi.to_usize() == 0 {
self.span
} else {
let new_lo = BytePos::from_usize(hi.to_usize() - 1);
Span(self.span.0.with_lo(new_lo))
}
}

/// Configures the span for this `Group`'s delimiters, but not its internal
/// tokens.
///
Expand Down