-
Notifications
You must be signed in to change notification settings - Fork 123
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
Avoid stringifying and reparsing a TokenStream
#144
Conversation
This PR changes removes uses of the pattern `syn::parse_str(tokens.to_string())`, which loses `Span` information. In addition to making error messages less precise, it can break code that depends on the hygiene information carried by `Span`. Instead, `syn::parse_quote` is used to parse a `TokenStream` into the desired AST struct. I've added a test which demonstrates one of the problems with the previous code - using `$crate` would previously cause a panic, but now compiles successfully.
d8ceeb8
to
6c6ec74
Compare
Note that this PR might break some users of this crate. |
Thanks for the PR! Do you have an example of code that doesn't work now? |
@JelteF: The code I added to |
I'm not sure why the macOS jobs failed - it looks like they didn't even start? |
@JelteF: Assuming that this PR gets merged, could you make a new point release afterwards? I'm planning to merge a Rust PR (rust-lang/rust#77153) soon, which will expose additional issues caused by throwing away hygiene information. |
What I don't understand is why you say this change can break existing code.
It looks like it can only fix problems. I'm mainly asking, because I would
like to know if this change is fine for a patch release or if it requires a
bigger version bump. So could you explain your comment about this breaking
existing users a bit better?
…On Tue, 29 Sep 2020, 22:35 Aaron Hill, ***@***.***> wrote:
@JelteF <https://github.com/JelteF>: Assuming that this PR gets merged,
could you make a new point release afterwards? I'm planning to merge a Rust
PR (rust-lang/rust#77153 <rust-lang/rust#77153>)
soon, which will expose additional issues caused by throwing away hygiene
information.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#144 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAI3YJT2YR6EEXU4BKFIKDDSIJAJDANCNFSM4R5BQASQ>
.
|
@JelteF: In general, preserving hygiene can break crates that were incorrectly relying on certain path being accessible. For example, there have been several bugs in rustc that incorrectly allowed code like this to compile: macro_rules! use_it {
($code:block) => {
let a = "25";
$code
}
}
fn main() {
use_it!({
let b = a;
});
} When the compiler started properly preserving hygiene, the function This PR isn't addressing a compiler bug, but the underlying principle is the same. In this particular case, the only things being stringified are types (it looks like they get used in generic bounds). Currently, the only stable types of hygiene are call-site and mixed-site hygiene (see https://doc.rust-lang.org/beta/proc_macro/struct.Span.html#method.call_site and https://doc.rust-lang.org/beta/proc_macro/struct.Span.html#method.mixed_site), which means that there can't be multiple types at the same path, but with different hygiene. In the context of TL;DR - I was being overly cautious. The only thing that this could possibly break is code using the unstable |
Released as 0.99.11, good luck with your rustc PR. |
This PR changes removes uses of the pattern
syn::parse_str(tokens.to_string())
,which loses
Span
information. In addition to making error messagesless precise, it can break code that depends on the hygiene information
carried by
Span
. Instead,syn::parse_quote
is used to parse aTokenStream
into the desired AST struct.I've added a test which demonstrates one of the problems with
the previous code - using
$crate
would previously causea panic, but now compiles successfully.