Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upIntroduce proc_macro::Span::source_text #55780
Conversation
rust-highfive
assigned
nikomatsakis
Nov 8, 2018
This comment has been minimized.
This comment has been minimized.
|
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @nikomatsakis (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
rust-highfive
added
the
S-waiting-on-review
label
Nov 8, 2018
This comment has been minimized.
This comment has been minimized.
|
So this code seems fine, but I'm not sure from a procedural and stability point of view what is the best way to handle this. |
This comment has been minimized.
This comment has been minimized.
|
cc @dtolnay @petrochenkov @alexcrichton -- thoughts? |
This comment has been minimized.
This comment has been minimized.
|
One doubt i had was if we should return None , instead of the macro call inside for span belonging to the call site. ( |
This comment has been minimized.
This comment has been minimized.
|
This seems like a reasonable API edition to me and one that we'll want in the long haul. If any procedural macro has whitespace-sensitive parsing associated with it then accessing the source text via means like this is intended to be the main way to actually do the parsing. I don't think we're on track to stabilize this in the near term, but in terms of a long-term addition I think we'll want this which to me means it's fine to land unstable for now in |
This comment has been minimized.
This comment has been minimized.
|
We might want to strip comments. What do others think? I can get on board with whitespace-sensitive macro DSLs such as languages that differentiate between |
This comment has been minimized.
This comment has been minimized.
|
I could go either way on comments personally, but one aspect about omitting comments that may be a bit odd is if the difference of byte positions of a span is very different from the length of the source text due to comment removal |
This comment has been minimized.
This comment has been minimized.
|
Good call. We could sub out the comment with spaces. |
This comment has been minimized.
This comment has been minimized.
|
Seems plausible to me! |
This comment has been minimized.
This comment has been minimized.
|
I ... I don't know. If we're going to give the source text, I'm inclined to just give the source text, and let macros do weird things with comments. Let the market decide. =) e.g., sometimes people add "pre and post conditions" in the form of specially formatted comments. That seems not terrible to me. |
This comment has been minimized.
This comment has been minimized.
|
I think we should keep preserve the comment. As an usecase, the main reason I'm doing this change is for the cpp crate which extract C++ code. And people use comments in C++ to annotate things for static analyzers. (For example, gcc's Another usecase would be to print snippets of the code while compiling for better diagnostics. We wants the comments in this case. |
This comment has been minimized.
This comment has been minimized.
|
@ogoffart interesting. Makes sense to me. |
This comment has been minimized.
This comment has been minimized.
|
What should I do now? |
This comment has been minimized.
This comment has been minimized.
|
@nikomatsakis ping? |
src/libproc_macro/lib.rs Outdated
src/libproc_macro/lib.rs Outdated
Centril
dismissed
their
stale review
Nov 22, 2018
Requested changes done.
This comment has been minimized.
This comment has been minimized.
|
I'm worried about giving guarantees to users about whitespace and comments because that forces alternative Rust compiler implementations into preserving such things rather than just throwing such things away permanently during lexing. In other words, should we give a guarantee, this effectively forces all Rust compilers to use a certain compilation model and makes that part of the specification. If this was not a guarantee but rather "at the compilers option, you may get whitespace and comments..." then I'd be less worried. |
This comment has been minimized.
This comment has been minimized.
|
That's why it returns an Optional. If the compiler do not have access to the actual source code, it can return None. |
This comment has been minimized.
This comment has been minimized.
|
@ogoffart Ah; I thought
referred only to getting Can we clarify this in the documentation somehow that compilers are not required to give you the actual source code even in cases where it's not produced by macros? |
This comment has been minimized.
This comment has been minimized.
|
It would be good to somehow document this as unstable, "best effort" and restricted to "for diagnostics only". |
This comment has been minimized.
This comment has been minimized.
|
@petrochenkov Yeah; "best effort" / "for diagnostics only" sounds like appropriate wording; thank you <3. |
This comment has been minimized.
This comment has been minimized.
|
My specific use-case is a power_assert macro. I want an assertion macro that has the following output:
In order to do this, I get the span of the full expression ( For this to work, Span::start() and the string I print out need to match.
If we don't get whitespace and comments, then we run the risk of having Span::start() become out of sync with the raw text, breaking the above functionality if a comment was put inside the assert macro. |
This comment has been minimized.
This comment has been minimized.
|
@roblabla: do you take in to account the fact that the column is in utf-8 bytes.
In order to do that, you indeed need to know what exactly is in the comments (how many byte, corresponds to how many code points) (I guess this should be computed with |
This comment has been minimized.
This comment has been minimized.
Can you not have some fallback such that the |
This comment has been minimized.
This comment has been minimized.
|
I added a note that this should not be relied upon, and is only there for diagnostics. |
This comment has been minimized.
This comment has been minimized.
|
|
ogoffart
force-pushed the
ogoffart:span_source_text
branch
from
31fc090
to
e88b0d9
Dec 1, 2018
This comment has been minimized.
This comment has been minimized.
|
Rebased. |
This comment has been minimized.
This comment has been minimized.
|
Ping. (Also cc @eddyb as the rebase touches the proc_macro protocol) |
eddyb
referenced this pull request
Dec 6, 2018
Open
proc_macro types Display impls don’t respect the input layout #56474
This comment has been minimized.
This comment has been minimized.
|
cc #56474 Also, I'll take a look, but adding methods to the |
This comment has been minimized.
This comment has been minimized.
Just wondering if there was no versioning to do, as it seems calling a non-existing function would make rustc panic in an unreachable!. |
This comment has been minimized.
This comment has been minimized.
|
@ogoffart I have considered having some sort of version-like sanity check, but it can be fully automatic, because the problem can only appear at stage0, if somehow the locally built libproc_macro is used with the downloaded compiler (which the build system should prevent anyway). The expectation is that libproc_macro's built from the same source are ABI-compatible, despite being built by different Rust compilers, so anyone modifying libproc_macro's source shouldn't have to worry about compatibility at all. |
ogoffart commentedNov 8, 2018
A function to extract the actual source behind a Span.
Background: I would like to use
synin abuild.rsscript to parse the rust code, and extract part of the source code. However,synonly gives access to proc_macro2::Span, and i would like to get the source code behind that.I opened an issue on proc_macro2 bug tracker for this feature alexcrichton/proc-macro2#110 and @alexcrichton said the feature should first go upstream in proc_macro. So there it is!
Since most of the Span API is unstable anyway, this is guarded by the same
proc_macro_spanfeature as everything else.