Skip to content

Stabilize proc_macro::Span::{start,end,line,column}. #139865

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

Merged
merged 1 commit into from
Apr 26, 2025

Conversation

m-ou-se
Copy link
Member

@m-ou-se m-ou-se commented Apr 15, 2025

This stabilizes part of #54725

Specifically, the part related to getting the location of a span:

impl Span {
    pub fn start(&self) -> Span; // Empty span at the start of this span
    pub fn end(&self) -> Span; // Empty span at the end of this span

    pub fn line(&self) -> usize; // Line where the span starts
    pub fn column(&self) -> usize; // Column where the span starts
}

History of this part of the API:

Originally, start and end returned a LineColumn struct (containing the line and column).

This has been simplified/changed:

  • No more LineColumn: Span now directly has .line() and .column() methods. This means we can easily add .byte_offset() or .byte_range() in the future if we want to.
  • Span::start() and Span::end() are now the equivalent of rustc's internal shrink_to_lo() and shrink_to_hi(). This means you can do e.g. span.end().column(), removing the need for a span.end_column() or similar.

@m-ou-se m-ou-se added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. A-proc-macros Area: Procedural macros I-libs-api-nominated Nominated for discussion during a libs-api team meeting. labels Apr 15, 2025
@m-ou-se m-ou-se self-assigned this Apr 15, 2025
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 15, 2025
@m-ou-se m-ou-se added S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). needs-fcp This change is insta-stable, so needs a completed FCP to proceed. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 15, 2025
@m-ou-se m-ou-se force-pushed the stabilize-proc-macro-span-location branch from bf6b54b to c5c9362 Compare April 15, 2025 15:29
@m-ou-se m-ou-se changed the title Stabilize proc_macro::Span::{start,end,line,column,file,local_file}. Stabilize proc_macro::Span::{start,end,line,column}. Apr 15, 2025
@m-ou-se
Copy link
Member Author

m-ou-se commented Apr 15, 2025

@rfcbot merge

@rfcbot
Copy link
Collaborator

rfcbot commented Apr 15, 2025

Team member @m-ou-se has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Apr 15, 2025
@sam0x17
Copy link

sam0x17 commented Apr 15, 2025

This is the most exciting thing I've seen for the state of proc macros in years hahaha

@rfcbot rfcbot added final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. and removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. labels Apr 15, 2025
@rfcbot
Copy link
Collaborator

rfcbot commented Apr 15, 2025

🔔 This is now entering its final comment period, as per the review above. 🔔

@m-ou-se m-ou-se added S-waiting-on-fcp Status: PR is in FCP and is awaiting for FCP to complete. and removed I-libs-api-nominated Nominated for discussion during a libs-api team meeting. S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). labels Apr 15, 2025
@Aloso
Copy link
Contributor

Aloso commented Apr 23, 2025

Does Span::column() count code points, grapheme clusters, or something else? Does it include zero-width characters? It would be nice to have this documented.

@mamekoro
Copy link

Has there been a discussion about whether to change the return values of line() and column() from usize to NonZero<usize>?

I believe that using NonZero would have the advantage of eliminating confusion about whether it is 0-based or 1-based.

@m-ou-se
Copy link
Member Author

m-ou-se commented Apr 24, 2025

Does Span::column() count code points, grapheme clusters, or something else? Does it include zero-width characters? It would be nice to have this documented.

Same as everywhere else in Rust, such as in format!("{x:40}") and .chars().count() etc.: codepoints (including zero width chars).

I'll document it.

@m-ou-se
Copy link
Member Author

m-ou-se commented Apr 24, 2025

Has there been a discussion about whether to change the return values of line() and column() from usize to NonZero<usize>?

I believe that using NonZero would have the advantage of eliminating confusion about whether it is 0-based or 1-based.

The NonZero types can sometimes be a bit annoying to work with. In places where it is important for optimization (e.g. Option<NonZeroUsize> in perf-sensitive code) or correctness (e.g. avoid dividing by zero) it makes sense to pay the (small) ergonomics cost of using NonZero. But in this case, I don't think we're improving anyones lives by using NonZero for these methods.

I don't think there will be a lot of confusion: the line+column numbers are exactly what rustc would show for a diagnostic, and the line number matches you see in your editor. In text editors, code listings, compiler diagnostics, etc., line numbers always start at one.

Just documenting it seems more than enough.

@rfcbot rfcbot added finished-final-comment-period The final comment period is finished for this PR / Issue. to-announce Announce this issue on triage meeting and removed final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. labels Apr 25, 2025
@rfcbot
Copy link
Collaborator

rfcbot commented Apr 25, 2025

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

This will be merged soon.

@tgross35
Copy link
Contributor

FCP completed above

r? tgross35
@bors r+ rollup

@bors
Copy link
Collaborator

bors commented Apr 25, 2025

📌 Commit c5c9362 has been approved by tgross35

It is now in the queue for this repository.

@bors bors added the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label Apr 25, 2025
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Apr 25, 2025
…location, r=tgross35

Stabilize proc_macro::Span::{start,end,line,column}.

This stabilizes part of rust-lang#54725

Specifically, the part related to getting the location of a span:

```rust
impl Span {
    pub fn start(&self) -> Span; // Empty span at the start of this span
    pub fn end(&self) -> Span; // Empty span at the end of this span

    pub fn line(&self) -> usize; // Line where the span starts
    pub fn column(&self) -> usize; // Column where the span starts
}
```

History of this part of the API:

Originally, `start` and `end` returned a `LineColumn` struct (containing the line and column).

This has been simplified/changed:

- No more `LineColumn`: `Span` now directly has `.line()` and `.column()` methods. This means we can easily add `.byte_offset()` or `.byte_range()` in the future if we want to.
- `Span::start()` and `Span::end()` are now the equivalent of rustc's internal `shrink_to_lo()` and `shrink_to_hi()`. This means you can do e.g. `span.end().column()`, removing the need for a `span.end_column()` or similar.
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 26, 2025
…iaskrgr

Rollup of 9 pull requests

Successful merges:

 - rust-lang#139865 (Stabilize proc_macro::Span::{start,end,line,column}.)
 - rust-lang#140086 (If creating a temporary directory fails with permission denied then retry with backoff)
 - rust-lang#140216 (Document that "extern blocks must be unsafe" in Rust 2024)
 - rust-lang#140220 (Fix detection of main function if there are expressions around it)
 - rust-lang#140253 (Add XtensaAsmPrinter)
 - rust-lang#140272 (Improve error message for `||` (or) in let chains)
 - rust-lang#140305 (Track per-obligation recursion depth only if there is inference in the new solver)
 - rust-lang#140306 (handle specialization in the new trait solver)
 - rust-lang#140308 (stall generator witness obligations: add regression test)

r? `@ghost`
`@rustbot` modify labels: rollup
jhpratt added a commit to jhpratt/rust that referenced this pull request Apr 26, 2025
…location, r=tgross35

Stabilize proc_macro::Span::{start,end,line,column}.

This stabilizes part of rust-lang#54725

Specifically, the part related to getting the location of a span:

```rust
impl Span {
    pub fn start(&self) -> Span; // Empty span at the start of this span
    pub fn end(&self) -> Span; // Empty span at the end of this span

    pub fn line(&self) -> usize; // Line where the span starts
    pub fn column(&self) -> usize; // Column where the span starts
}
```

History of this part of the API:

Originally, `start` and `end` returned a `LineColumn` struct (containing the line and column).

This has been simplified/changed:

- No more `LineColumn`: `Span` now directly has `.line()` and `.column()` methods. This means we can easily add `.byte_offset()` or `.byte_range()` in the future if we want to.
- `Span::start()` and `Span::end()` are now the equivalent of rustc's internal `shrink_to_lo()` and `shrink_to_hi()`. This means you can do e.g. `span.end().column()`, removing the need for a `span.end_column()` or similar.
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 26, 2025
…iaskrgr

Rollup of 8 pull requests

Successful merges:

 - rust-lang#139865 (Stabilize proc_macro::Span::{start,end,line,column}.)
 - rust-lang#140086 (If creating a temporary directory fails with permission denied then retry with backoff)
 - rust-lang#140216 (Document that "extern blocks must be unsafe" in Rust 2024)
 - rust-lang#140253 (Add XtensaAsmPrinter)
 - rust-lang#140272 (Improve error message for `||` (or) in let chains)
 - rust-lang#140305 (Track per-obligation recursion depth only if there is inference in the new solver)
 - rust-lang#140306 (handle specialization in the new trait solver)
 - rust-lang#140308 (stall generator witness obligations: add regression test)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 096c495 into rust-lang:master Apr 26, 2025
6 checks passed
@rustbot rustbot added this to the 1.88.0 milestone Apr 26, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Apr 26, 2025
Rollup merge of rust-lang#139865 - m-ou-se:stabilize-proc-macro-span-location, r=tgross35

Stabilize proc_macro::Span::{start,end,line,column}.

This stabilizes part of rust-lang#54725

Specifically, the part related to getting the location of a span:

```rust
impl Span {
    pub fn start(&self) -> Span; // Empty span at the start of this span
    pub fn end(&self) -> Span; // Empty span at the end of this span

    pub fn line(&self) -> usize; // Line where the span starts
    pub fn column(&self) -> usize; // Column where the span starts
}
```

History of this part of the API:

Originally, `start` and `end` returned a `LineColumn` struct (containing the line and column).

This has been simplified/changed:

- No more `LineColumn`: `Span` now directly has `.line()` and `.column()` methods. This means we can easily add `.byte_offset()` or `.byte_range()` in the future if we want to.
- `Span::start()` and `Span::end()` are now the equivalent of rustc's internal `shrink_to_lo()` and `shrink_to_hi()`. This means you can do e.g. `span.end().column()`, removing the need for a `span.end_column()` or similar.
@udoprog udoprog mentioned this pull request Apr 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-proc-macros Area: Procedural macros disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. needs-fcp This change is insta-stable, so needs a completed FCP to proceed. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. S-waiting-on-fcp Status: PR is in FCP and is awaiting for FCP to complete. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. to-announce Announce this issue on triage meeting
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants