-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Change proc_macro::Span::byte_range -> byte_offset. #139901
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
base: master
Are you sure you want to change the base?
Conversation
This is more consistent with Span::line and Span::column.
This comment was marked as off-topic.
This comment was marked as off-topic.
I slightly prefer option 1, as it fits well in our earlier design choice to get rid of let location = range.end();
let _ = location.line();
let _ = location.column();
let _ = location.byte_offset(); If one is using an (empty) |
@rust-lang/libs-api If you have an opinion on this, please speak up! |
I'm fine with option (1) personally. I don't think option (1) precludes option (2) though, and I could see it being a nice convenience routine to have. But I don't feel strongly about it. Following the API pattern created with line and column numbers SGTM. |
I prefer to keep The use case for byte offset is significantly different than line/column so the consistency objective is weak. For line/column, the overwhelming majority use cases is rendering a line/column in an error message or log message or similar, where you would practically never also want the upper end. Exposing directly on Span the line/column you almost definitely want, and putting the end line/column behind A Range-based API for line/column would not fit. For a span that goes from line 2 column 18 to line 4 column 5 (e.g. the braces of a Byte range is different than this. In my experience the majority use case for byte offset is slicing. Code like https://github.com/dtolnay/cxx/blob/f9d547b60324bc02d9983622159973a75d06ea10/gen/src/error.rs#L115-L142. The PR summary writes off Range for being an iterator: "This gives you both ends of the range at once, but uses the I don't find that pushing this to be consistent with line/column improves the API. Unlike line/column, a single byte offset ( |
I've reworded it a bit, instead focussing on how it can be used to index a slice. Thanks for pointing that out. |
Your argument sounds convincing to me. If anyone still prefers option 1 over 2, please speak up. |
I also prefer option 2. However I have a different concern about stabilization: how will this interact with the new range types tracked in #123741? |
This is about #54725's open question on byte offsets:
Next to
span.line()
andspan.column()
, we also want a way to get the byte offset into the file.We have basically two competing options to expose this:
Option 1 (merge this PR)
This is consistent with the
line
andcolumn
methods onSpan
.To get the offset of the end of the span, you use
span.end().byte_offset()
, just like how you usespan.end().column()
for the end column.Option 2 (close this PR)
This gives you both ends of the range at once, and uses the
Range
type (which is usable to index a slice), but is arguably less consistent with theline
andcolumn
methods.Curious to hear what y'all think.
👍 for option 1 (merge this PR)
👎 for option 2 (close this PR)
(The decision is still up to the libs-api team. Votes are just useful as input, not as the final decision.)