-
-
Notifications
You must be signed in to change notification settings - Fork 117
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
Expose span locations on stable #166
Conversation
This comment has been minimized.
This comment has been minimized.
Can confirm this works! |
Tagging wip because this needs tests. Does not work when I tried it: use proc_macro2::TokenTree;
fn main() {
let sp = syn::parse_str::<TokenTree>("testing").unwrap().span();
println!("start line={} column={}", sp.start().line, sp.start().column);
println!("end line={} column={}", sp.end().line, sp.end().column);
} start line=1 column=0
end line=1 column=0 I am not going to be able to get to this today. @mitsuhiko could you investigate? |
Yes, will investigate. Information seems absent indeed. |
If we're going to offer this in a published stable version then I think we can probably go ahead and do this without a feature. The I think that may also simplify the cfg story here (which I am always confused by every time I look at this!). That way outside of a proc-macro context you always have accurate information, but in a proc-macro context you'll only have accurate information if a nightly feature toggle is enabled. |
I kept a feature because last time I checked there was a big performance hit for Span going from 0 bytes to 8 bytes. There are a lot of spans in the syntax tree... |
Ah ok that makes sense to me, and in that case seems fine to document as such as to why it's a feature |
Hmm, I wonder if a span interning system could compress span sizes down even more, and if that would be helpful here. |
I added a note in build.rs about the performance hit. |
I think this works now. I filed #167 to follow up on mitigating the performance impact. |
Is this blocked on #167 or is it mergeable as such? |
I don't think this needs to block on the performance work, because the increase in size of Span only happens if you opt in to the new feature. Thanks for the reminder! |
Published in 0.4.27. |
@dtolnay noob question here - is this performance hit increased memory, noticeably slower speed for working with a TokenStream.. or both.. or something else? #167 suggests that it's increased memory but I'm not sure if that's also having a big impact on other things? Trying to better understand whether or not to start taking advantage of this. Thanks!! |
@chinedufn it was noticeably longer compile time in debug mode for Serde's test suite the last time I tried it. You could enable the feature and check the compile time for some crate that uses your macro heavily. |
Awesome advice, perfect, thanks!! |
Hmm so I'm seeing some weird behavior for span locations Take the following proc macro #[test]
fn text_space_before_end_tag() {
assert_eq!(
&html! { <div>Before End Tag </div> }.to_string(),
"<div>Before End Tag </div>"
)
} For the token However, the I would expect it to be 38 here since there is a space between the end of the word Does that sound familiar at all? Held off on pushing up something easily reproducible in case there is an obvious reason here that you might already know but if not I can totally do that if that would help! |
@chinedufn that's likely actually a bug in rustc itself, want to see if you can reduce it to a standalone proc-macro and report it upstream? |
Will do, thanks! |
For posterity! -> rust-lang/rust#58958 |
In the way suggested by #165 (comment).