summary refs log tree commit diff
path: root/compiler/rustc_parse/src/parser/mod.rs
AgeCommit message (Collapse)AuthorLines
2021-02-27Combine HasAttrs and HasTokens into AstLikeAaron Hill-2/+2
When token-based attribute handling is implemeneted in #80689, we will need to access tokens from `HasAttrs` (to perform cfg-stripping), and we will to access attributes from `HasTokens` (to construct a `PreexpTokenStream`). This PR merges the `HasAttrs` and `HasTokens` traits into a new `AstLike` trait. The previous `HasAttrs` impls from `Vec<Attribute>` and `AttrVec` are removed - they aren't attribute targets, so the impls never really made sense.
2021-02-23Rollup merge of #81235 - reese:rw-tuple-diagnostics, r=estebankDylan DPC-1/+1
Improve suggestion for tuple struct pattern matching errors. Closes #80174 This change allows numbers to be parsed as field names when pattern matching on structs, which allows us to provide better error messages when tuple structs are matched using a struct pattern. r? ``@estebank``
2021-02-21parser: remove unneccessary wrapping of return value in parse_extern()Matthias Krüger-6/+2
2021-02-15Simplify pattern grammar by allowing nested leading vertmark-0/+1
Along the way, we also implement a handful of diagnostics improvements and fixes, particularly with respect to the special handling of `||` in place of `|` and when there are leading verts in function params, which don't allow top-level or-patterns anyway.
2021-02-13Address review commentsAaron Hill-159/+6
2021-02-13Require passing an `AttrWrapper` to `collect_tokens_trailing_token`Aaron Hill-20/+41
This is a pure refactoring split out from #80689. It represents the most invasive part of that PR, requiring changes in every caller of `parse_outer_attributes` In order to eagerly expand `#[cfg]` attributes while preserving the original `TokenStream`, we need to know the range of tokens that corresponds to every attribute target. This is accomplished by making `parse_outer_attributes` return an opaque `AttrWrapper` struct. An `AttrWrapper` must be converted to a plain `AttrVec` by passing it to `collect_tokens_trailing_token`. This makes it difficult to accidentally construct an AST node with attributes without calling `collect_tokens_trailing_token`, since AST nodes store an `AttrVec`, not an `AttrWrapper`. As a result, we now call `collect_tokens_trailing_token` for attribute targets which only support inert attributes, such as generic arguments and struct fields. Currently, the constructed `LazyTokenStream` is simply discarded. Future PRs will record the token range corresponding to the attribute target, allowing those tokens to be removed from an enclosing `collect_tokens_trailing_token` call if necessary.
2021-01-28Clone entire `TokenCursor` when collecting tokensAaron Hill-9/+1
Reverts PR #80830 Fixes taiki-e/pin-project#312 We can have an arbitrary number of `None`-delimited group frames pushed on the stack due to proc-macro invocations, which can legally be exited. Attempting to account for this would add a lot of complexity for a tiny performance gain, so let's just use the original strategy.
2021-01-24parser: Collect tokens for values in key-value attributesVadim Petrochenkov-6/+2
2021-01-23Auto merge of #80065 - b-naber:parse-angle-arg-diagnostics, r=petrochenkovbors-1/+1
Improve diagnostics when parsing angle args https://github.com/rust-lang/rust/pull/79266 introduced parsing of generic arguments in associated type constraints, this however resulted in possibly very confusing error messages in cases in which closing angle brackets were missing such as in `Vec<(u32, _, _) = vec![]`, which outputs an incorrectly parsed equality constraint error, as noted by `@cynecx.` This PR tries to provide better error messages in such cases. r? `@petrochenkov`
2021-01-22improve diagnostics for angle argsb-naber-1/+1
2021-01-22Refactor token collection to capture trailing token immediatelyAaron Hill-27/+28
2021-01-20Improve suggestion for tuple struct pattern matching errors.Reese Williams-1/+1
Currently, when a user uses a struct pattern to pattern match on a tuple struct, the errors we emit generally suggest adding fields using their field names, which are numbers. However, numbers are not valid identifiers, so the suggestions, which use the shorthand notation, are not valid syntax. This commit changes those errors to suggest using the actual tuple struct pattern syntax instead, which is a more actionable suggestion.
2021-01-20Force token collection to run when parsing nonterminalsAaron Hill-0/+20
Fixes #81007 Previously, we would fail to collect tokens in the proper place when only builtin attributes were present. As a result, we would end up with attribute tokens in the collected `TokenStream`, leading to duplication when we attempted to prepend the attributes from the AST node. We now explicitly track when token collection must be performed due to nomterminal parsing.
2021-01-13Set tokens on AST node in `collect_tokens`Aaron Hill-6/+7
A new `HasTokens` trait is introduced, which is used to move logic from the callers of `collect_tokens` into the body of `collect_tokens`. In addition to reducing duplication, this paves the way for PR #80689, which needs to perform additional logic during token collection.
2021-01-09Auto merge of #80441 - petrochenkov:kwtok, r=Aaron1011bors-2/+2
ast: Remove some indirection layers from values in key-value attributes Trying to address some perf regressions from https://github.com/rust-lang/rust/pull/78837#issuecomment-745380762.
2021-01-09ast: Remove some indirection layers from values in key-value attributesVadim Petrochenkov-2/+2
2021-01-08Use an empty `TokenCursorFrame` stack when capturing tokensAaron Hill-1/+9
We will never need to pop past our starting frame during token capturing. Using an empty stack allows us to avoid pointless heap allocations/deallocations.
2020-12-31Auto merge of #80459 - mark-i-m:or-pat-reg, r=petrochenkovbors-1/+0
Implement edition-based macro :pat feature This PR does two things: 1. Fixes the perf regression from https://github.com/rust-lang/rust/pull/80100#issuecomment-750893149 2. Implements `:pat2018` and `:pat2021` matchers, as described by `@joshtriplett` in https://github.com/rust-lang/rust/issues/54883#issuecomment-745509090 behind the feature gate `edition_macro_pat`. r? `@petrochenkov` cc `@Mark-Simulacrum`
2020-12-30Implement edition-based macro pat featuremark-1/+0
2020-12-30Fix ICE when pointing at multi bytes characterYuki Okushi-5/+1
2020-12-19implement edition-specific :pat behavior for 2015/18mark-0/+1
2020-12-12Properly capture trailing 'unglued' tokenAaron Hill-9/+58
If we try to capture the `Vec<u8>` in `Option<Vec<u8>>`, we'll need to capture a `>` token which was 'unglued' from a `>>` token. The processing of unglueing a token for parsing purposes bypasses the usual capturing infrastructure, so we currently lose the trailing `>`. As a result, we fall back to the reparsed `TokenStream`, causing us to lose spans. This commit makes token capturing keep track of a trailing 'unglued' token. Note that we don't need to care about unglueing except at the end of the captured tokens - if we capture both the first and second unglued tokens, then we'll end up capturing the full 'glued' token, which already works correctly.
2020-12-09Accept arbitrary expressions in key-value attributes at parse timeVadim Petrochenkov-9/+18
2020-12-04A slightly clearer diagnostic when misusingRyan Levick-1/+1
2020-11-28Rollup merge of #78853 - calebcartwright:fix-const-block-expr-span, r=spastorinoJonas Schievink-1/+2
rustc_parse: fix ConstBlock expr span The span for a ConstBlock expression should presumably run through the end of the block it contains and not stop at the keyword, just like is done with similar block-containing expression kinds, such as a TryBlock
2020-11-26Properly handle attributes on statementsAaron Hill-2/+17
We now collect tokens for the underlying node wrapped by `StmtKind` instead of storing tokens directly in `Stmt`. `LazyTokenStream` now supports capturing a trailing semicolon after it is initially constructed. This allows us to avoid refactoring statement parsing to wrap the parsing of the semicolon in `parse_tokens`. Attributes on item statements (e.g. `fn foo() { #[bar] struct MyStruct; }`) are now treated as item attributes, not statement attributes, which is consistent with how we handle attributes on other kinds of statements. The feature-gating code is adjusted so that proc-macro attributes are still allowed on item statements on stable. Two built-in macros (`#[global_allocator]` and `#[test]`) needed to be adjusted to support being passed `Annotatable::Stmt`.
2020-11-12rustc_parse: Remove optimization for 0-length streams in `collect_tokens`Vadim Petrochenkov-9/+5
The optimization conflates empty token streams with unknown token stream, which is at least suspicious, and doesn't affect performance because 0-length token streams are very rare.
2020-11-07fix(rustc_parse): ConstBlock expr spanCaleb Cartwright-1/+2
2020-10-31parser: Cleanup `LazyTokenStream` and avoid some clonesVadim Petrochenkov-26/+37
by using a named struct instead of a closure.
2020-10-27Auto merge of #77502 - varkor:const-generics-suggest-enclosing-braces, ↵bors-0/+1
r=petrochenkov Suggest that expressions that look like const generic arguments should be enclosed in brackets I pulled out the changes for const expressions from https://github.com/rust-lang/rust/pull/71592 (without the trait object diagnostic changes) and made some small changes; the implementation is `@estebank's.` We're also going to want to make some changes separately to account for trait objects (they result in poor diagnostics, as is evident from one of the test cases here), such as an adaption of https://github.com/rust-lang/rust/pull/72273. Fixes https://github.com/rust-lang/rust/issues/70753. r? `@petrochenkov`
2020-10-26Suggest expressions that look like const generic arguments should be ↵varkor-0/+1
enclosed in brackets Co-Authored-By: Esteban Kuber <github@kuber.com.ar>
2020-10-24Auto merge of #77255 - Aaron1011:feature/collect-attr-tokens, r=petrochenkovbors-4/+10
Unconditionally capture tokens for attributes. This allows us to avoid synthesizing tokens in `prepend_attr`, since we have the original tokens available. We still need to synthesize tokens when expanding `cfg_attr`, but this is an unavoidable consequence of the syntax of `cfg_attr` - the user does not supply the `#` and `[]` tokens that a `cfg_attr` expands to. This is based on PR https://github.com/rust-lang/rust/pull/77250 - this PR exposes a bug in the current `collect_tokens` implementation, which is fixed by the rewrite.
2020-10-22Make inline const work for half open rangesSantiago Pastorino-3/+3
2020-10-22Rename parse_const_expr to parse_const_blockSantiago Pastorino-1/+1
2020-10-22Don't create an empty `LazyTokenStream`Aaron Hill-4/+10
2020-10-21Auto merge of #77250 - Aaron1011:feature/flat-token-collection, r=petrochenkovbors-134/+131
Rewrite `collect_tokens` implementations to use a flattened buffer Instead of trying to collect tokens at each depth, we 'flatten' the stream as we go allong, pushing open/close delimiters to our buffer just like regular tokens. One capturing is complete, we reconstruct a nested `TokenTree::Delimited` structure, producing a normal `TokenStream`. The reconstructed `TokenStream` is not created immediately - instead, it is produced on-demand by a closure (wrapped in a new `LazyTokenStream` type). This closure stores a clone of the original `TokenCursor`, plus a record of the number of calls to `next()/next_desugared()`. This is sufficient to reconstruct the tokenstream seen by the callback without storing any additional state. If the tokenstream is never used (e.g. when a captured `macro_rules!` argument is never passed to a proc macro), we never actually create a `TokenStream`. This implementation has a number of advantages over the previous one: * It is significantly simpler, with no edge cases around capturing the start/end of a delimited group. * It can be easily extended to allow replacing tokens an an arbitrary 'depth' by just using `Vec::splice` at the proper position. This is important for PR #76130, which requires us to track information about attributes along with tokens. * The lazy approach to `TokenStream` construction allows us to easily parse an AST struct, and then decide after the fact whether we need a `TokenStream`. This will be useful when we start collecting tokens for `Attribute` - we can discard the `LazyTokenStream` if the parsed attribute doesn't need tokens (e.g. is a builtin attribute). The performance impact seems to be neglibile (see https://github.com/rust-lang/rust/pull/77250#issuecomment-703960604). There is a small slowdown on a few benchmarks, but it only rises above 1% for incremental builds, where it represents a larger fraction of the much smaller instruction count. There a ~1% speedup on a few other incremental benchmarks - my guess is that the speedups and slowdowns will usually cancel out in practice.
2020-10-21Rollup merge of #78118 - spastorino:inline-const-followups, r=petrochenkovYuki Okushi-1/+5
Inline const followups r? @petrochenkov Follow ups of #77124
2020-10-19Allow NtBlock to parse on check inline const next tokenSantiago Pastorino-1/+5
2020-10-19Rewrite `collect_tokens` implementations to use a flattened bufferAaron Hill-134/+131
Instead of trying to collect tokens at each depth, we 'flatten' the stream as we go allong, pushing open/close delimiters to our buffer just like regular tokens. One capturing is complete, we reconstruct a nested `TokenTree::Delimited` structure, producing a normal `TokenStream`. The reconstructed `TokenStream` is not created immediately - instead, it is produced on-demand by a closure (wrapped in a new `LazyTokenStream` type). This closure stores a clone of the original `TokenCursor`, plus a record of the number of calls to `next()/next_desugared()`. This is sufficient to reconstruct the tokenstream seen by the callback without storing any additional state. If the tokenstream is never used (e.g. when a captured `macro_rules!` argument is never passed to a proc macro), we never actually create a `TokenStream`. This implementation has a number of advantages over the previous one: * It is significantly simpler, with no edge cases around capturing the start/end of a delimited group. * It can be easily extended to allow replacing tokens an an arbitrary 'depth' by just using `Vec::splice` at the proper position. This is important for PR #76130, which requires us to track information about attributes along with tokens. * The lazy approach to `TokenStream` construction allows us to easily parse an AST struct, and then decide after the fact whether we need a `TokenStream`. This will be useful when we start collecting tokens for `Attribute` - we can discard the `LazyTokenStream` if the parsed attribute doesn't need tokens (e.g. is a builtin attribute). The performance impact seems to be neglibile (see https://github.com/rust-lang/rust/pull/77250#issuecomment-703960604). There is a small slowdown on a few benchmarks, but it only rises above 1% for incremental builds, where it represents a larger fraction of the much smaller instruction count. There a ~1% speedup on a few other incremental benchmarks - my guess is that the speedups and slowdowns will usually cancel out in practice.
2020-10-19Avoid cloning the contents of a `TokenStream` in a few placesAaron Hill-5/+5
2020-10-16Parse inline const expressionsSantiago Pastorino-3/+24
2020-10-07Detect blocks that could be struct expr bodiesEsteban Küber-0/+1
This approach lives exclusively in the parser, so struct expr bodies that are syntactically correct on their own but are otherwise incorrect will still emit confusing errors, like in the following case: ```rust fn foo() -> Foo { bar: Vec::new() } ``` ``` error[E0425]: cannot find value `bar` in this scope --> src/file.rs:5:5 | 5 | bar: Vec::new() | ^^^ expecting a type here because of type ascription error[E0214]: parenthesized type parameters may only be used with a `Fn` trait --> src/file.rs:5:15 | 5 | bar: Vec::new() | ^^^^^ only `Fn` traits may use parentheses error[E0107]: wrong number of type arguments: expected 1, found 0 --> src/file.rs:5:10 | 5 | bar: Vec::new() | ^^^^^^^^^^ expected 1 type argument ``` If that field had a trailing comma, that would be a parse error and it would trigger the new, more targetted, error: ``` error: struct literal body without path --> file.rs:4:17 | 4 | fn foo() -> Foo { | _________________^ 5 | | bar: Vec::new(), 6 | | } | |_^ | help: you might have forgotten to add the struct literal inside the block | 4 | fn foo() -> Foo { Path { 5 | bar: Vec::new(), 6 | } } | ``` Partially address last part of #34255.
2020-10-06rustc_parse: Make `Parser::unexpected` public and use it in built-in macrosVadim Petrochenkov-1/+1
2020-09-11repairing broken error message and rustfix application for the new testAurélien Deharbe-1/+5
case
2020-09-10Attach `TokenStream` to `ast::Visibility`Aaron Hill-7/+27
A `Visibility` does not have outer attributes, so we only capture tokens when parsing a `macro_rules!` matcher
2020-09-03Rename IsJoint -> SpacingAleksey Kladov-5/+5
To match better naming from proc-macro
2020-09-02Auto merge of #76160 - scileo:format-recovery, r=petrochenkovbors-0/+4
Improve recovery on malformed format call The token following a format expression should be a comma. However, when it is replaced with a similar token (such as a dot), then the corresponding error is emitted, but the token is treated as a comma, and the parsing step continues. r? @petrochenkov
2020-09-02Improve recovery on malformed format callSasha-0/+4
If a comma in a format call is replaced with a similar token, then we emit an error and continue parsing, instead of stopping at this point.
2020-08-30parser: restore some fn visibility for rustfmtCaleb Cartwright-1/+2
2020-08-30mv compiler to compiler/mark-0/+1270