Uh oh!
There was an error while loading. Please reload this page.
syntax: Make _ a reserved identifier - #48842
Conversation
rust-highfive
commented
Mar 8, 2018
r? @aturon (rust_highfive has picked a reviewer for you, use r? to override) |
petrochenkov
commented
Mar 8, 2018
cc @rust-lang/lang |
There was a problem hiding this comment.
I can move it into the keyword list below, the only difference would be whether error messages say "keyword _" or "reserved identifier _".
There was a problem hiding this comment.
I think "reserved identifier" is better than "keyword" as it doesn't really look/feel like a keyword.
|
nikomatsakis
commented
Mar 8, 2018
I agree with @rkruppe. I don't want |
Centril
commented
Mar 8, 2018
I also think this also conflicts with one of the alternatives to |
petrochenkov
commented
Mar 8, 2018
@Centril
|
_ an identifier_ a reserved identifiernikomatsakis
commented
Mar 9, 2018
👍 to that plan, this is what I was going to suggest. |
petrochenkov
commented
Mar 10, 2018
Updated with |
petrochenkov
commented
Mar 12, 2018
Ok, I guess I'll have to tweak the |
nikomatsakis
left a comment
There was a problem hiding this comment.
Well, the code seems fine modulo one nit -- but remind me, what is the motivation for this change again? =)
There was a problem hiding this comment.
can we add some sort of helper for this check, like is_macro_ident?
There was a problem hiding this comment.
we could then use the helper here too, rather than open-coding this
nikomatsakis
commented
Mar 14, 2018
Wait: one other question .. is |
nikomatsakis
commented
Mar 14, 2018
Re-read motivation from the intro. r=me with nit addressed -- presuming |
petrochenkov
commented
Mar 14, 2018
@nikomatsakis We can prohibit this for consistency (both |
petrochenkov
commented
Mar 15, 2018
One more important thing that I didn't mention - |
eddyb
commented
Mar 15, 2018
@petrochenkov@jseyfried IMO we should just switch to relying on |
petrochenkov
commented
Mar 15, 2018
This reminded me of something - (This is totally orthogonal to this PR though, it would still be more convenient for |
petrochenkov
commented
Mar 15, 2018
Question: given that #48942 is already in flight, should |
nikomatsakis
commented
Mar 15, 2018
I believe that indeed |
nikomatsakis
commented
Mar 15, 2018
Well, I'm mildly reconsidering that -- see this comment |
petrochenkov
commented
Mar 17, 2018
@bors r=nikomatsakis |
bors
commented
Mar 17, 2018
📌 Commit ed5ea5c has been approved by |
bors
commented
Mar 17, 2018
syntax: Make `_` a reserved identifier Why: - Lexically `_` is an identifier. - Internally it makes implementation of `use Trait as _;` (#48216) and some other things cleaner. - We prevent the externally observable effect of `_` being accepted by macros expecting `ident` by treating `_` specially in the `ident` matcher: ```rust macro_rules! m { ($i: ident) => { let $i = 10; } } m!(_); // Still an error ```
bors
commented
Mar 18, 2018
☀️ Test successful - status-appveyor, status-travis |
AST: Give spans to all identifiers
Change representation of `ast::Ident` from `{ name: Symbol, ctxt: SyntaxContext }` to `{ name: Symbol, span: Span }`.
Syntax contexts still can be extracted from spans (`span.ctxt()`).
Why this should not require more memory:
- `Span` is `u32` just like `SyntaxContext`.
- Despite keeping more spans in AST we don't actually *create* more spans, so the number of "outlined" spans kept in span interner shouldn't become larger.
Why this may be slightly slower:
- When we need to extract ctxt from an identifier instead of just field read we need to do bit field extraction possibly followed by and access by index into span interner's vector. Both operations should be fast (unless the span interner is under some synchronization) and we already do ctxt extraction from spans all the time during macro expansion, so the difference should be lost in noise.
cc rust-lang#48842 (comment)AST: Give spans to all identifiers
Change representation of `ast::Ident` from `{ name: Symbol, ctxt: SyntaxContext }` to `{ name: Symbol, span: Span }`.
Syntax contexts still can be extracted from spans (`span.ctxt()`).
Why this should not require more memory:
- `Span` is `u32` just like `SyntaxContext`.
- Despite keeping more spans in AST we don't actually *create* more spans, so the number of "outlined" spans kept in span interner shouldn't become larger.
Why this may be slightly slower:
- When we need to extract ctxt from an identifier instead of just field read we need to do bit field extraction possibly followed by and access by index into span interner's vector. Both operations should be fast (unless the span interner is under some synchronization) and we already do ctxt extraction from spans all the time during macro expansion, so the difference should be lost in noise.
cc #48842 (comment)8122: Make bare underscore token an Ident rather than Punct in proc-macro r=edwin0cheng a=kevinmehall In rustc and proc-macro2, a bare `_` token is parsed for procedural macro purposes as `Ident` rather than `Punct` (see rust-lang/rust#48842). This changes rust-analyzer to match rustc's behavior and implementation by handling `_` as an Ident in token trees, but explicitly preventing `$x:ident` from matching it in MBE. proc macro crate: ```rust #[proc_macro] pub fn input(input: proc_macro::TokenStream) -> proc_macro::TokenStream { dbg!(input) } ``` test crate: ```rust test_proc_macro::input!(_); ``` output (rustc): ```rust [test-proc-macro/src/lib.rs:10] input = TokenStream [ Ident { ident: "_", span: #0 bytes(173..174), }, ] ``` output (rust-analyzer before this change): ```rust [test-proc-macro/src/lib.rs:10] input = TokenStream [ Punct { ch: '_', spacing: Joint, span: 4294967295, }, ] ``` output (rust-analyzer after this change): ```rust [test-proc-macro/src/lib.rs:10] input = TokenStream [ Ident { ident: "_", span: 4294967295, }, ] ``` Co-authored-by: Kevin Mehall <km@kevinmehall.net>
Why:
_is an identifier.use Trait as _;(Tracking issue for RFC #2166: impl-only-use #48216) and some other things cleaner._being accepted by macros expectingidentby treating_specially in theidentmatcher: