Skip to content

Add proc_macro::Span::{before, after}. - #86165

Merged
bors merged 2 commits into
rust-lang:masterfrom
m-ou-se:proc-macro-span-shrink
Sep 11, 2021
Merged

Add proc_macro::Span::{before, after}.#86165
bors merged 2 commits into
rust-lang:masterfrom
m-ou-se:proc-macro-span-shrink

Conversation

@m-ou-se

@m-ou-sem-ou-se commented Jun 9, 2021

Copy link
Copy Markdown
Member

This adds proc_macro::Span::before() and proc_macro::Span::after() to get a zero width span at the start or end of the span.

These are equivalent to rustc's Span::shrink_to_lo() and Span::shrink_to_hi() but with a less cryptic name. They are useful when generating diagnostlics like "missing <thing> after <thing>".

E.g.

syn::Error::new(ident.span().after(),"missing `:` after field name").into_compile_error()

@m-ou-sem-ou-se added T-libs-api [DEPRECATED; DO NOT USE] A-proc-macros Area: Procedural macros labels Jun 9, 2021
@rust-highfive

Copy link
Copy Markdown
Contributor

r? @petrochenkov

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfiverust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 9, 2021
Comment threadlibrary/proc_macro/src/lib.rs Outdated
@petrochenkovpetrochenkov removed their assignment Jun 9, 2021
@jhpratt

Copy link
Copy Markdown
Member

Part of me would prefer this be called start and end, but those names are already (unstably) taken.

Would it makes sense to repurpose these names and provide a generalized .location() method that would return the LineColumn? Presumably it could point to the start of the span. Alternatively .start_location() and .end_location() could be provided.

Just a random thought I had from a usability perspective; I could certainly get accustomed to the before/after naming.

@JohnCSimonJohnCSimon added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 5, 2021
@joelpalmerjoelpalmer added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 20, 2021
@m-ou-se

Copy link
Copy Markdown
MemberAuthor

@jhpratt Since a span is not a point/location but a range, calling it start and end doesn't feel entirely accurate to me.

@JohnCSimonJohnCSimon added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 15, 2021
@JohnCSimonJohnCSimon added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 6, 2021
@m-ou-se

Copy link
Copy Markdown
MemberAuthor

r? @dtolnay

@dtolnaydtolnay left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This API looks good to me as implemented.

@dtolnay

Copy link
Copy Markdown
Member

@bors r+

@bors

bors commented Sep 9, 2021

Copy link
Copy Markdown
Collaborator

📌 Commit f9be6cd has been approved by dtolnay

@bors

bors commented Sep 9, 2021

Copy link
Copy Markdown
Collaborator

🌲 The tree is currently closed for pull requests below priority 100. This pull request will be tested once the tree is reopened.

@borsbors 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, 2021
bors added a commit to rust-lang-ci/rust that referenced this pull request Sep 11, 2021
…arth
Rollup of 15 pull requests
Successful merges:
- rust-lang#85200 (Ignore derived Clone and Debug implementations during dead code analysis)
- rust-lang#86165 (Add proc_macro::Span::{before, after}.)
- rust-lang#87088 (Fix stray notes when the source code is not available)
- rust-lang#87441 (Emit suggestion when passing byte literal to format macro)
- rust-lang#88546 (Emit proper errors when on missing closure braces)
- rust-lang#88578 (fix(rustc): suggest `items` be borrowed in `for i in items[x..]`)
- rust-lang#88632 (Fix issues with Markdown summary options)
- rust-lang#88639 (rustdoc: Fix ICE with `doc(hidden)` on tuple variant fields)
- rust-lang#88667 (Tweak `write_fmt` doc.)
- rust-lang#88720 (Rustdoc coverage fields count)
- rust-lang#88732 (RustWrapper: avoid deleted unclear attribute methods)
- rust-lang#88742 (Fix table in docblocks)
- rust-lang#88776 (Workaround blink/chromium grid layout limitation of 1000 rows)
- rust-lang#88807 (Fix typo in docs for iterators)
- rust-lang#88812 (Fix typo `option` -> `options`.)
Failed merges:
r? `@ghost`
`@rustbot` modify labels: rollup
@bors
bors merged commit 000dbd2 into rust-lang:masterSep 11, 2021
@rustbotrustbot added this to the 1.57.0 milestone Sep 11, 2021
@m-ou-se
m-ou-se deleted the proc-macro-span-shrink branch September 11, 2021 10:01
github-actionsBot pushed a commit to tautschnig/verify-rust-std that referenced this pull request Mar 11, 2025
…tolnay
Add proc_macro::Span::{before, after}.
This adds `proc_macro::Span::before()` and `proc_macro::Span::after()` to get a zero width span at the start or end of the span.
These are equivalent to rustc's `Span::shrink_to_lo()` and `Span::shrink_to_hi()` but with a less cryptic name. They are useful when generating diagnostlics like "missing \<thing\> after \<thing\>".
E.g.
```rust
syn::Error::new(ident.span().after(), "missing `:` after field name").into_compile_error()
```
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-proc-macrosArea: Procedural macrosS-waiting-on-borsStatus: Waiting on bors to run and complete tests. Bors will change the label on completion.T-libs-api[DEPRECATED; DO NOT USE]

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants

@m-ou-se@rust-highfive@jhpratt@dtolnay@bors@petrochenkov@JohnCSimon@joelpalmer@rustbot