-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Add 'Span::len()' and 'Span::trimmed()' proc-macro methods. #54373
Conversation
r? @aturon (rust_highfive has picked a reviewer for you, use r? to override) |
Previous attempt: #53930 |
Thanks for the PR! My original comment on #53930 applies here as well though I think, do you have thoughts on that? cc @dtolnay, you're likely interested in this as well |
Copying over your comment from #53930:
If I understood this correctly, I think you're saying that An alternative is to expose
We should definitely handle ensuring that the |
The "a={a} b={b} c={c}"
^^^ then I would rather express this as |
Another aspect I'm worried about as well is that it's not clear that we could actually support this sort of span selection via indices that well. For example with strings if you wanted to have character at the letter 'a' how do you handle strings like |
(I do agree with @dtolnay as well that the API to behave slice-like makes more sense to me too) |
I do too, actually. Happy to change that.
I was wondering this, actually; what is I see ~three paths forward here:
I'm inclined to suggest 3) as the path forward, though I do want to note that 1) allows the user to implement 3) themselves, and is much easier to implement. I'm interested in learning if you have any other ideas @dtolnay, @alexcrichton. It's really important for Rocket's diagnostics that we're able to create subspans. In Rocket's case, we only need subspans of cooked strings, so I'd be happy with a targeted solution there, though I'm sure others will need a more general solution that works for other literals or idents. |
I like 3 a lot if the implementation works out. What is the right behavior if a macro scrambles spans across different tokens? Suppose we have: #[a]
#[b]
const S: &str = "hel\u{0x6c}o"; If
If possible, I would prefer that subspan allow slicing anywhere that is in bounds. Afterward whatever code needs a span aligned to character boundaries can round outward to the nearest character boundary. This way the naive cases like |
I think we'd need to either 1) keep track of the original span and always return the
Why do you prefer this behavior? I'm not necessarily opposed to it, except that it's not consistent with how how slicing on strings works today. Is there a benefit to this relaxed approach? |
I also really like option 3 if we can get it to work, but I wonder if we could perhaps strike a more minimal compromise for now? What if we only added I think it'd also provide a clear location to document the internal parsing that rustc would be doing as we could clearly say "these indices are indexes into a string literal's actual literal value" and... do "something reasonable" if the literal isn't a string (maybe return |
Absolutely. That's what I was proposing. :) Sounds like we all agree that option 3 is the way to go. I'll give its implementation some thought and see what I come up with. |
Er sorry, then yeah that sounds great to me! |
Ping from triage @SergioBenitez: What is the status of this PR? |
@TimNN I have not had a chance to implement the revised strategy. |
Ping from triage @SergioBenitez: It looks like this PR hasn't been updated in a while, so I'm closing it for now, per our guidelines. Thanks for your contributions and please feel free to re-open in the future. |
Span::len()
is fairly self explanatory. The latter,trimmed()
, is necessary to get sub-spans of an existingSpan
. Rocket uses this, for instance, to point into slices of a string.