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

Add From<Span> for all tokens #279

Merged
merged 3 commits into from
Jan 12, 2018
Merged

Add From<Span> for all tokens #279

merged 3 commits into from
Jan 12, 2018

Conversation

kdy1
Copy link
Contributor

@kdy1 kdy1 commented Dec 22, 2017

Fixes #278.

Copy link
Owner

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

As a general rule in the standard library we have tried to always associate conversions with the most specific type involved -- whichever one provides some additional invariant or interpretation that is not present in the other type. For example str is more specific then &[u8] since it is a UTF-8 encoded sequence of bytes. Thus str provides both the as_bytes method and the from_utf8 constructor for converting to and from &[u8] values.

In syn we have token which is more specific than Span, and Ident which is more specific than Span, so I would prefer to associate constructing tokens and idents with those types rather than associating it with Span.

The keyword tokens are already easy to create from a Span: for example Else(span). For the punctuation tokens which hold an array of spans I added a new constructor: for example AddEq::new(span). And for Ident there is Ident::new("value", span). Could you take another look and let me know whether anything still needs to be improved?

@kdy1
Copy link
Contributor Author

kdy1 commented Jan 7, 2018

I'm happy with Ident::new(), but I'm not sure if Else(span) is better.
Currently span from procedural macros work in strange way.
In my experience, which span is used is important than which token is I'm currently creating.

For example, rustc fails to resolve module if span from function body is used in ::crate_name (first part) of return type.

@dtolnay dtolnay requested a review from mystor January 12, 2018 00:06
@dtolnay
Copy link
Owner

dtolnay commented Jan 12, 2018

@mystor could you take a look and recommend a path forward? Thanks!

@kdy1
Copy link
Contributor Author

kdy1 commented Jan 12, 2018

IMHO, it's similar to From<Span> and Into<token::XX>. Implementing From<Span> for tokens and using span.into() would be better than span.as_token().

While creating pr, I completely forgot why I chose span.as_token() instead of span.into().

@dtolnay
Copy link
Owner

dtolnay commented Jan 12, 2018

I would be on board with providing From<Span> for all the token types. Good idea! Could you rebase the PR and change it to From impls?

@kdy1
Copy link
Contributor Author

kdy1 commented Jan 12, 2018

I'll do it soon.

@kdy1
Copy link
Contributor Author

kdy1 commented Jan 12, 2018

I did it.
(I made a mistake because ide didn't show it...)

Copy link
Owner

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@dtolnay dtolnay changed the title Add span.as_token() and span.new_ident() Add From<Span> for all tokens Jan 12, 2018
@dtolnay dtolnay merged commit d5229da into dtolnay:master Jan 12, 2018
@dtolnay
Copy link
Owner

dtolnay commented Jan 12, 2018

I published this in syn 0.12.5.

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

Successfully merging this pull request may close these issues.

2 participants