about summary refs log tree commit diff
path: root/compiler/rustc_parse/src/parser/mod.rs
AgeCommit message (Collapse)AuthorLines
2023-10-26Reserve `gen` keyword for `gen {}` blocks and `gen fn` in 2024 editionOli Scherer-0/+11
2023-10-13Format all the let chains in compilerMichael Goulet-8/+7
2023-10-03Detect missing `=>` after match guard during parsingEsteban Küber-0/+1
``` error: expected one of `,`, `:`, or `}`, found `.` --> $DIR/missing-fat-arrow.rs:25:14 | LL | Some(a) if a.value == b { | - while parsing this struct LL | a.value = 1; | -^ expected one of `,`, `:`, or `}` | | | while parsing this struct field | help: try naming a field | LL | a: a.value = 1; | ++ help: you might have meant to start a match arm after the match guard | LL | Some(a) if a.value == b => { | ++ ``` Fix #78585.
2023-09-11Move let expression checking to parsingMatthew Jasper-0/+1
There was an incomplete version of the check in parsing and a second version in AST validation. This meant that some, but not all, invalid uses were allowed inside macros/disabled cfgs. It also means that later passes have a hard time knowing when the let expression is in a valid location, sometimes causing ICEs. - Add a field to ExprKind::Let in AST/HIR to mark whether it's in a valid location. - Suppress later errors and MIR construction for invalid let expressions.
2023-08-18Rename `NtOrTt` as `ParseNtResult`.Nicholas Nethercote-1/+1
It's more descriptive, and future-proofs it if/when additional variants get added.
2023-08-17Add some useful comments to `Parser::look_ahead`.Nicholas Nethercote-8/+23
2023-08-13Remove reached_eof from ParseSessbjorn3-13/+0
It was only ever set in a function which isn't called anywhere.
2023-08-03Remove `MacDelimiter`.Nicholas Nethercote-4/+2
It's the same as `Delimiter`, minus the `Invisible` variant. I'm generally in favour of using types to make impossible states unrepresentable, but this one feels very low-value, and the conversions between the two types are annoying and confusing. Look at the change in `src/tools/rustfmt/src/expr.rs` for an example: the old code converted from `MacDelimiter` to `Delimiter` and back again, for no good reason. This suggests the author was confused about the types.
2023-08-02Avoid an unnecessary local variable.Nicholas Nethercote-2/+1
2023-08-02Move `TokenCursor::break_last_token` into `Parser`.Nicholas Nethercote-31/+20
Similar to the last commit, it's more of a `Parser`-level concern than a `TokenCursor`-level concern. And the struct size reductions are nice. After this change, `TokenCursor` is as minimal as possible (two fields and two methods) which is nice.
2023-08-02Move `TokenCursor::num_next_calls` into `Parser` and rename it.Nicholas Nethercote-8/+5
It's more of a `Parser`-level concern than a `TokenCursor`-level concern. Also, `num_bump_calls` is a more accurate name, because it's incremented in `Parser::bump`.
2023-08-02Inline and remove `parse_all_token_trees`.Nicholas Nethercote-10/+1
It has a single call site.
2023-08-02`parse_all_token_trees` cannot fail.Nicholas Nethercote-2/+2
2023-08-01Auto merge of #114273 - nnethercote:move-doc-comment-desugaring, r=petrochenkovbors-78/+13
Move doc comment desugaring out of `TokenCursor`. It's awkward that `TokenCursor` sometimes desugars doc comments on the fly, but usually doesn't. r? `@petrochenkov`
2023-07-31Remove `desugar_doc_comments` arg from `Parser::new()`.Nicholas Nethercote-6/+1
It's only true at one call site; do the desugaring there instead.
2023-07-31Move doc comment desugaring out of `TokenCursor`.Nicholas Nethercote-73/+13
`TokenCursor` currently does doc comment desugaring on the fly, if the `desugar_doc_comment` field is set. This requires also modifying the token stream on the fly with `replace_prev_and_rewind`. This commit moves the doc comment desugaring out of `TokenCursor`, by introducing a new `TokenStream::desugar_doc_comment` method. This separation of desugaring and iterating makes the code nicer.
2023-07-31Fix a typo in a comment.Nicholas Nethercote-3/+3
2023-07-31Remove an unnecessary `return` keyword.Nicholas Nethercote-1/+1
2023-07-30inline format!() args up to and including rustc_middleMatthias Krüger-3/+3
2023-07-26Add a comment to `TokenCursor::desugar_doc_comments`.Nicholas Nethercote-0/+3
Useful information that took me some time to discern.
2023-07-26Remove `desugar_doc_comments` arguments from `TokenCursor::{inlined_,}next`.Nicholas Nethercote-18/+23
Because it's now always `self.desugar_doc_comments`.
2023-07-26Tweak `Parser::look_ahead`.Nicholas Nethercote-1/+1
It doesn't really matter what the `desugar_doc_comments` argument is here, because in practice we never look ahead through doc comments. Changing it to `cursor.desugar_doc_comments` will allow some follow-up simplifications.
2023-07-26Remove `Parser::desugar_doc_comments`.Nicholas Nethercote-3/+1
It's currently stored twice: once in `Parser`, once in the `TokenStream` within `Parser`. We only need the latter.
2023-07-19Fix inline_const with interpolated blockMichael Goulet-1/+2
2023-07-12Re-format let-else per rustfmt updateMark Rousskov-1/+3
2023-05-13fix(parse): return unpected when current token is EOFbohan-1/+3
2023-05-03Restrict `From<S>` for `{D,Subd}iagnosticMessage`.Nicholas Nethercote-2/+2
Currently a `{D,Subd}iagnosticMessage` can be created from any type that impls `Into<String>`. That includes `&str`, `String`, and `Cow<'static, str>`, which are reasonable. It also includes `&String`, which is pretty weird, and results in many places making unnecessary allocations for patterns like this: ``` self.fatal(&format!(...)) ``` This creates a string with `format!`, takes a reference, passes the reference to `fatal`, which does an `into()`, which clones the reference, doing a second allocation. Two allocations for a single string, bleh. This commit changes the `From` impls so that you can only create a `{D,Subd}iagnosticMessage` from `&str`, `String`, or `Cow<'static, str>`. This requires changing all the places that currently create one from a `&String`. Most of these are of the `&format!(...)` form described above; each one removes an unnecessary static `&`, plus an allocation when executed. There are also a few places where the existing use of `&String` was more reasonable; these now just use `clone()` at the call site. As well as making the code nicer and more efficient, this is a step towards possibly using `Cow<'static, str>` in `{D,Subd}iagnosticMessage::{Str,Eager}`. That would require changing the `From<&'a str>` impls to `From<&'static str>`, which is doable, but I'm not yet sure if it's worthwhile.
2023-05-01fix testsyukang-4/+3
2023-05-01fix parser sizeyukang-1/+1
2023-05-01Rip it outNilstrieb-11/+12
My type ascription Oh rip it out Ah If you think we live too much then You can sacrifice diagnostics Don't mix your garbage Into my syntax So many weird hacks keep diagnostics alive Yet I don't even step outside So many bad diagnostics keep tyasc alive Yet tyasc doesn't even bother to survive!
2023-04-27Migrate trivially translatable `rustc_parse` diagnosticsclubby789-10/+5
2023-04-04Use a simpler atomic operation than the `compare_exchange` hammerOli Scherer-3/+1
2023-04-04Replace a lock with an atomicOli Scherer-2/+7
2023-03-20feat: implement error recovery in `expected_ident_found`Ezra Shaw-4/+9
2023-03-19refactor: refactor identifier parsing somewhatEzra Shaw-14/+13
2023-03-11Gate const closures even when they appear in macrosMichael Goulet-3/+7
2023-03-04Rollup merge of #108715 - chenyukang:yukang/cleanup-parser-delims, ↵Matthias Krüger-14/+3
r=compiler-errors Remove unclosed_delims from parser After landing https://github.com/rust-lang/rust/pull/108297 we could remove `unclosed_delims` from the parser now.
2023-03-03Remove unclosed_delims from parseryukang-14/+3
2023-03-03Match unmatched backticks in comments in compiler/est31-1/+1
2023-02-28rename unmatched_braces to unmatched_delimsyukang-5/+5
2023-02-23parser: provide better errors on closures with braces missingYutaro Ohno-1/+5
We currently provide wrong suggestions and unhelpful errors on closure bodies with braces missing. For example, given the following code: ``` fn main() { let _x = Box::new(|x|x+1;); } ``` the current output is like this: ``` error: expected expression, found `)` --> ./main.rs:2:30 | 2 | let _x = Box::new(|x|x+1;); | ^ expected expression error: closure bodies that contain statements must be surrounded by braces --> ./main.rs:2:25 | 2 | let _x = Box::new(|x|x+1;); | ^ 3 | } | ^ | ... help: try adding braces | 2 ~ let _x = Box::new(|x| {x+1;); 3 ~ }} ... error: expected `;`, found `}` --> ./main.rs:2:32 | 2 | let _x = Box::new(|x|x+1;); | ^ help: add `;` here 3 | } | - unexpected token error: aborting due to 3 previous errors ``` This commit allows outputting correct suggestions and errors. The above code would output like this: ``` error: closure bodies that contain statements must be surrounded by braces --> ./main.rs:2:25 | 2 | let _x = Box::new(|x|x+1;); | ^ ^ | note: statement found outside of a block --> ./main.rs:2:29 | 2 | let _x = Box::new(|x|x+1;); | ---^ this `;` turns the preceding closure into a statement | | | this expression is a statement because of the trailing semicolon note: the closure body may be incorrectly delimited --> ./main.rs:2:23 | 2 | let _x = Box::new(|x|x+1;); | ^^^^^^ - ...but likely you meant the closure to end here | | | this is the parsed closure... help: try adding braces | 2 | let _x = Box::new(|x| {x+1;}); | + + error: aborting due to previous error ```
2023-02-21Use `ThinVec` in various AST types.Nicholas Nethercote-8/+9
This commit changes the sequence parsers to produce `ThinVec`, which triggers numerous conversions.
2023-02-16`if $c:expr { Some($r:expr) } else { None }` =>> `$c.then(|| $r)`Maybe Waffle-15/+9
2023-02-03Rollup merge of #107551 - fee1-dead-contrib:rm_const_fnmut_helper, r=oli-obkMichael Goulet-5/+16
Replace `ConstFnMutClosure` with const closures Also fixes a parser bug. cc `@oli-obk` for compiler changes
2023-02-03Rename `Cursor`/`CursorRef` as `TokenTreeCursor`/`RefTokenTreeCursor`.Nicholas Nethercote-5/+8
This makes it clear they return token trees, and makes for a nice comparison against `TokenCursor` which returns tokens.
2023-02-03Remove `TokenCursorFrame`.Nicholas Nethercote-40/+32
The motivation here is to eliminate the `Option<(Delimiter, DelimSpan)>`, which is `None` for the outermost token stream and `Some` for all other token streams. We are already treating the innermost frame specially -- this is the `frame` vs `stack` distinction in `TokenCursor`. We can push that further so that `frame` only contains the cursor, and `stack` elements contain the delimiters for their children. When we are in the outermost token stream `stack` is empty, so there are no stored delimiters, which is what we want because the outermost token stream *has* no delimiters. This change also shrinks `TokenCursor`, which shrinks `Parser` and `LazyAttrTokenStreamImpl`, which is nice.
2023-02-03Make clear that `TokenTree::Token` shouldn't contain a delimiter.Nicholas Nethercote-1/+7
2023-02-03Improve doc comment desugaring.Nicholas Nethercote-27/+21
Sometimes the parser needs to desugar a doc comment into `#[doc = r"foo"]`. Currently it does this in a hacky way: by pushing a "fake" new frame (one without a delimiter) onto the `TokenCursor` stack. This commit changes things so that the token stream itself is modified in place. The nice thing about this is that it means `TokenCursorFrame::delim_sp` is now only `None` for the outermost frame.
2023-02-01fix parser mistaking const closures for const itemDeadbeef-5/+16
2023-01-17Remove double spaces after dots in commentsMaybe Waffle-2/+2