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

Conversation

dtolnay
Copy link
Member

@dtolnay dtolnay commented Sep 2, 2018

Before this addition, every delimited group like (...) [...] {...} has only a single Span that covers the full source location from opening delimiter to closing delimiter. This makes it impossible for a procedural macro to trigger an error pointing to just the opening or closing delimiter. The Rust compiler does not seem to have the same limitation:

mod m {
    type T =
}
error: expected type, found `}`
 --> src/main.rs:3:1
  |
3 | }
  | ^

On that same input, a procedural macro would be forced to trigger the error on the last token inside the block, on the entire block, or on the next token after the block, none of which is really what you want for an error like above.

This commit adds group.span_open() and group.span_close() which access the Span associated with just the opening delimiter and just the closing delimiter of the group. Relevant to Syn as we implement real error messages for when parsing fails in a procedural macro: dtolnay/syn#476.

  impl Group {
      fn span(&self) -> Span;
+     fn span_open(&self) -> Span;
+     fn span_close(&self) -> Span;
  }

Fixes #48187
r? @alexcrichton

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 2, 2018
#[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.

@alexcrichton
Copy link
Member

I'd be fine adding these, but I'd prefer to add them as unstable instead of initially insta-stable.

Before this addition, every delimited group like (...) [...] {...} has
only a single Span that covers the full source location from opening
delimiter to closing delimiter. This makes it impossible for a
procedural macro to trigger an error pointing to just the opening or
closing delimiter. The Rust compiler does not seem to have the same
limitation:

    mod m {
        type T =
    }

    error: expected type, found `}`
     --> src/main.rs:3:1
      |
    3 | }
      | ^

On that same input, a procedural macro would be forced to trigger the
error on the last token inside the block, on the entire block, or on the
next token after the block, none of which is really what you want for an
error like above.

This commit adds group.span_open() and group.span_close() which access
the Span associated with just the opening delimiter and just the closing
delimiter of the group. Relevant to Syn as we implement real error
messages for when parsing fails in a procedural macro.
@alexcrichton
Copy link
Member

r? @petrochenkov

I'm personally comfortable with this as-is, but I think you've got further comments!

@dtolnay
Copy link
Member Author

dtolnay commented Sep 3, 2018

Closing in favor of #53930, span.len() and span.subspan(lo..hi) as discussed in #53904 (comment).

@dtolnay
Copy link
Member Author

dtolnay commented Sep 9, 2018

Updated to include #53930 (comment).

@@ -154,7 +154,7 @@ struct MatcherPos<'a> {
/// The beginning position in the source that the beginning of this matcher corresponds to. In
/// other words, the token in the source at `sp_lo` is matched against the first token of the
/// matcher.
sp_lo: BytePos,
sp_lo: Span,
Copy link
Member

Choose a reason for hiding this comment

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

This should be renamed to sp_open, I think.

@petrochenkov
Copy link
Contributor

@bors r+
This is a small API addition and can be deprecated if necessary.
I still think there's nothing wrong in guaranteeing that a Span is an arbitrary sliceable (possibly UTF-8 encoded) continuous byte range and providing the API from #53930.

@bors
Copy link
Contributor

bors commented Sep 9, 2018

📌 Commit 57d6ada has been approved by petrochenkov

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 9, 2018
@bors
Copy link
Contributor

bors commented Sep 9, 2018

⌛ Testing commit 57d6ada with merge 40fc8ba...

bors added a commit that referenced this pull request Sep 9, 2018
proc_macro::Group::span_open and span_close

Before this addition, every delimited group like `(`...`)` `[`...`]` `{`...`}` has only a single Span that covers the full source location from opening delimiter to closing delimiter. This makes it impossible for a procedural macro to trigger an error pointing to just the opening or closing delimiter. The Rust compiler does not seem to have the same limitation:

```rust
mod m {
    type T =
}
```

```console
error: expected type, found `}`
 --> src/main.rs:3:1
  |
3 | }
  | ^
```

On that same input, a procedural macro would be forced to trigger the error on the last token inside the block, on the entire block, or on the next token after the block, none of which is really what you want for an error like above.

This commit adds `group.span_open()` and `group.span_close()` which access the Span associated with just the opening delimiter and just the closing delimiter of the group. Relevant to Syn as we implement real error messages for when parsing fails in a procedural macro: dtolnay/syn#476.

```diff
  impl Group {
      fn span(&self) -> Span;
+     fn span_open(&self) -> Span;
+     fn span_close(&self) -> Span;
  }
```

Fixes #48187
r? @alexcrichton
@bors
Copy link
Contributor

bors commented Sep 9, 2018

☀️ Test successful - status-appveyor, status-travis
Approved by: petrochenkov
Pushing 40fc8ba to master...

@bors bors merged commit 57d6ada into rust-lang:master Sep 9, 2018
@dtolnay dtolnay deleted the group branch September 9, 2018 16:22
@nnethercote
Copy link
Contributor

@dtolnay
Copy link
Member Author

dtolnay commented Sep 24, 2018

Thanks @nnethercote, I filed #54535 to follow up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Span that can point to closing delimiter of a delimited group
7 participants