Skip to content
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

Get the span of an Expr #267

Closed
antoyo opened this issue Dec 7, 2017 · 6 comments
Closed

Get the span of an Expr #267

antoyo opened this issue Dec 7, 2017 · 6 comments

Comments

@antoyo
Copy link

antoyo commented Dec 7, 2017

Hi.
Is there an easy way to get the Span of an Expr?
That is without having to use pattern matching on node and getting the span manually.
Thank you.

@mystor
Copy link
Collaborator

mystor commented Dec 18, 2017

No, currently there is no easy way to extract the span of an individual AST node, as spans are only stored on the tokens at the leafs of the AST. It might be possible in the future to add a fn span() -> Option<Span> method which uses the Span.join() method to try to make a span for the full node, but we don't support it right now. (We'd also need dtolnay/proc-macro2#36 to merge before we could do that).

@antoyo
Copy link
Author

antoyo commented Dec 18, 2017

I started to write such a function here even though it is not completely accurate (I don't merge Spans yet).

@mystor
Copy link
Collaborator

mystor commented Dec 18, 2017

I think we would probably want to generate that function, if we decided we wanted it, behind a feature flag and using syn_codegen so we can avoid manually writing large error-prone match statements.

cc @dtolnay to see if we'd want to have something like that in syn behind a feature gate

(I'm imagining we'd add some sort of #span attributes to some fields, or something like that, which would tell the codegen which fields to pull spans out of)

@dtolnay
Copy link
Owner

dtolnay commented Dec 18, 2017

Seems reasonable! I like the idea of the implementations being generated.

@dtolnay
Copy link
Owner

dtolnay commented Jan 7, 2018

This has been added in syn::spanned::Spanned.

@dtolnay dtolnay closed this as completed Jan 7, 2018
@da-x
Copy link

da-x commented Dec 1, 2018

Hi, I have a question regarding this issue -

If I run a.b.c through syn::parse_str, a Debug print of the result yields: Field(ExprField { attrs: [], base: Field(ExprField { attrs: [], base: Path(ExprPath { attrs: [], qself: None, path: Path { leading_colon: None, segments: [PathSegment { ident: Ident(a), arguments: None }] } }), dot_token: Dot, member: Named(Ident(b)) }), dot_token: Dot, member: Named(Ident(c)) })

So far so good, however it's not clear to me how to use syn::spanned::Spanned in order to obtain Span information by pattern matching on that result. I'd expect it would be in attrs, no? But these vectors seem empty.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants