-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Macro expansion often produces invalid Span
values
#23480
Comments
We should have a library of common span operations that ensures the syntax contexts match. These operations are necessarily partial; there's no sensible way to union spans from two different contexts. But we could introduce a "span set" or "compound span" to handle that. |
My proposal would be to use the context that the defining operator/token for the given AST node is in. For example, for a binary operation AST node, use the context the operator token originally occurs. For the above example, that would mean:
|
…erflow-bug, r=alexcrichton This should solve issues rust-lang#23115, rust-lang#23469, and rust-lang#23407. As the title says, this is just a workaround. The underlying problem is that macro expansion can produce invalid spans. I've opened issue rust-lang#23480 so we don't forget about that.
Triage: there's been a lot of work on spans with the new error format, but I'm not close enough to it to know if it fixes this issue. |
cc #39450, which is on the declarative macros 2.0 roadmap. @kmcallister @michaelwoerister |
macros: improve `Span`'s expansion information This PR improves `Span`'s expansion information. More specifically: - It refactors AST node span construction to preserve expansion information. - Today, we only use the underlying tokens' `BytePos`s, throwing away the `ExpnId`s. - This improves the accuracy of AST nodes' expansion information, fixing #30506. - It refactors `span.expn_id: ExpnId` to `span.ctxt: SyntaxContext` and removes `ExpnId`. - This gives all tokens as much hygiene information as `Ident`s. - This is groundwork for procedural macros 2.0 `TokenStream` API. - This is also groundwork for declarative macros 2.0, which will need this hygiene information for some non-`Ident` tokens. - It simplifies processing of spans' expansion information throughout the compiler. - It fixes #40649. - It fixes #39450 and fixes part of #23480. r? @nrc
…s Minimize weird spans involving macro context Sometimes the parser attempts to synthesize spans from within a macro context with the span for the captured argument, leading to non-sensical spans with very bad output. Given that an incorrect span is worse than a partially incomplete span, when detecting this situation return only one of the spans without merging them. Fix rust-lang#32072, rust-lang#47778. CC rust-lang#23480.
I believe this is minimized (if not out right eliminated) after #47942. |
I'm going to close this in favor of opening more targeted bugs/PRs as we come across them, in particular in light of @estebank's previous comment about this being most likely fixed today. |
The span of many AST node types is pieced together from the spans of the node's children. Here is an example from
Parser::parse_more_binops
:Unfortunately, in the macro expansion phase, this can lead to undesirable results. Consider the following example:
Here
lhs
andrhs
come from completely different contexts.lhs_span
comes from the macro-definition-site (lo: 50, hi: 51
) whilerhs_span
comes from the expansion-site (lo: 6, hi:7
). Takinglo
fromlhs
andhi
fromrhs
, as the above code does, we end up with a span oflo: 50, hi: 7
. This value can't be interpreted in any meaningful way.A similar error can occur for any kind of AST node where a sub-node is located directly at the border of a parent:
This invalid
Span
values may lead to strange error messages and caused at least one ICE (#23115).The text was updated successfully, but these errors were encountered: