about summary refs log tree commit diff
path: root/compiler/rustc_parse/src
AgeCommit message (Collapse)AuthorLines
2024-12-09Rollup merge of #134084 - estebank:typo, r=compiler-errorsLeón Orell Valerian Liehr-1/+1
Fix typo in RFC mention 3598 -> 3593 https://github.com/rust-lang/rfcs/blob/master/text/3593-unprefixed-guarded-strings.md
2024-12-09Rollup merge of #134043 - ehuss:unicode-version, r=jieyouxuLeón Orell Valerian Liehr-0/+1
Add test to check unicode identifier version This adds a test to verify which version of Unicode is used for identifiers. This is part of the language, documented at https://doc.rust-lang.org/nightly/reference/identifiers.html#r-ident.unicode. The version here often changes implicitly due to dependency updates pulling in new versions, and thus we often don't notice it has changed leaving the documentation out of date. The intent here is to have a canary to give us a notification when it changes so that we can update the documentation.
2024-12-09Detect `struct S(ty = val);`Esteban Küber-1/+17
Emit a specific error for unsupported default field value syntax in tuple structs.
2024-12-09Introduce `default_field_values` featureEsteban Küber-12/+9
Initial implementation of `#[feature(default_field_values]`, proposed in https://github.com/rust-lang/rfcs/pull/3681. Support default fields in enum struct variant Allow default values in an enum struct variant definition: ```rust pub enum Bar { Foo { bar: S = S, baz: i32 = 42 + 3, } } ``` Allow using `..` without a base on an enum struct variant ```rust Bar::Foo { .. } ``` `#[derive(Default)]` doesn't account for these as it is still gating `#[default]` only being allowed on unit variants. Support `#[derive(Default)]` on enum struct variants with all defaulted fields ```rust pub enum Bar { #[default] Foo { bar: S = S, baz: i32 = 42 + 3, } } ``` Check for missing fields in typeck instead of mir_build. Expand test with `const` param case (needs `generic_const_exprs` enabled). Properly instantiate MIR const The following works: ```rust struct S<A> { a: Vec<A> = Vec::new(), } S::<i32> { .. } ``` Add lint for default fields that will always fail const-eval We *allow* this to happen for API writers that might want to rely on users' getting a compile error when using the default field, different to the error that they would get when the field isn't default. We could change this to *always* error instead of being a lint, if we wanted. This will *not* catch errors for partially evaluated consts, like when the expression relies on a const parameter. Suggestions when encountering `Foo { .. }` without `#[feature(default_field_values)]`: - Suggest adding a base expression if there are missing fields. - Suggest enabling the feature if all the missing fields have optional values. - Suggest removing `..` if there are no missing fields.
2024-12-09Fix typo in RFC mention 3598 -> 3593Esteban Küber-1/+1
https://github.com/rust-lang/rfcs/blob/master/text/3593-unprefixed-guarded-strings.md
2024-12-09Add test to check unicode identifier versionEric Huss-0/+1
2024-12-08Rollup merge of #133424 - Nadrieril:guard-patterns-parsing, r=fee1-deadMatthias Krüger-48/+68
Parse guard patterns This implements the parsing of [RFC3637 Guard Patterns](https://rust-lang.github.io/rfcs/3637-guard-patterns.html) (see also [tracking issue](https://github.com/rust-lang/rust/issues/129967)). This PR is extracted from https://github.com/rust-lang/rust/pull/129996 with minor modifications. cc `@max-niederman`
2024-12-04Fix suggestion when shorthand self has erroneous typeMichael Goulet-1/+48
2024-12-03Rollup merge of #133545 - clubby789:symbol-intern-lit, r=jieyouxuMatthias Krüger-2/+2
Lint against Symbol::intern on a string literal Disabled in tests where this doesn't make much sense
2024-12-03Rollup merge of #132612 - compiler-errors:async-trait-bounds, r=lcnrMatthias Krüger-2/+2
Gate async fn trait bound modifier on `async_trait_bounds` This PR moves `async Fn()` trait bounds into a new feature gate: `feature(async_trait_bounds)`. The general vibe is that we will most likely stabilize the `feature(async_closure)` *without* the `async Fn()` trait bound modifier, so we need to gate that separately. We're trying to work on the general vision of `async` trait bound modifier general in: https://github.com/rust-lang/rfcs/pull/3710, however that RFC still needs more time for consensus to converge, and we've decided that the value that users get from calling the bound `async Fn()` is *not really* worth blocking landing async closures in general.
2024-12-02Rollup merge of #133746 - oli-obk:push-xwyrylxmrtvq, r=jieyouxuGuillaume Gomez-4/+6
Change `AttrArgs::Eq` to a struct variant Cleanups for simplifying https://github.com/rust-lang/rust/pull/131808 Basically changes `AttrArgs::Eq` to a struct variant and then avoids several matches on `AttrArgsEq` in favor of methods on it. This will make future refactorings simpler, as they can either keep methods or switch to field accesses without having to restructure code
2024-12-02Gate async fn trait bound modifier on async_trait_boundsMichael Goulet-2/+2
2024-12-02Rollup merge of #133603 - dtolnay:precedence, r=lcnrGuillaume Gomez-31/+35
Eliminate magic numbers from expression precedence Context: see https://github.com/rust-lang/rust/pull/133140. This PR continues on backporting Syn's expression precedence design into rustc. Rustc's design used mysterious integer quantities represented variously as `i8` or `usize` (e.g. `PREC_CLOSURE = -40i8`), a special significance around `0` that is never named, and an extra `PREC_FORCE_PAREN` precedence level that does not correspond to any expression. Syn's design uses a C-like enum with variants that clearly correspond to specific sets of expression kinds. This PR is a refactoring that has no intended behavior change on its own, but it unblocks other precedence work that rustc's precedence design was poorly suited to accommodate. - Asymmetrical precedence, so that a pretty-printer can tell `(return 1) + 1` needs parens but `1 + return 1` does not. - Squashing the `Closure` and `Jump` cases into a single precedence level. - Numerous remaining false positives and false negatives in rustc pretty-printer's parenthesization of macro metavariables, for example in `$e < rhs` where $e is `lhs as Thing<T>`. FYI `@fmease` &mdash; you don't need to review if rustbot picks someone else, but you mentioned being interested in the followup PRs.
2024-12-02Change `AttrArgs::Eq` into a struct variantOli Scherer-4/+6
2024-12-01Only error raw lifetime followed by \' in edition 2021+Michael Goulet-2/+21
2024-11-30Eliminate magic numbers from expression precedenceDavid Tolnay-16/+11
2024-11-30Eliminate precedence arithmetic from rustc_parseDavid Tolnay-17/+26
2024-11-30Rollup merge of #133623 - nnethercote:parse_expr_bottom-spans, r=compiler-errors许杰友 Jieyou Xu (Joe)-21/+14
Improve span handling in `parse_expr_bottom`. `parse_expr_bottom` stores `this.token.span` in `lo`, but then fails to use it in many places where it could. This commit fixes that, and likewise (to a smaller extent) in `parse_ty_common`. r? ``@spastorino``
2024-11-28Replace `Symbol::intern` calls with preinterned symbolsclubby789-2/+2
2024-11-28Rollup merge of #133560 - clubby789:mut-mut-space, r=jieyouxuGuillaume Gomez-2/+5
Trim extra space in 'repeated `mut`' diagnostic Trim an extra space when removing repeated `mut`. Also an extra test for even more repeated `mut`s
2024-11-28Rollup merge of #133487 - pitaj:reserve-guarded-strings, r=fee1-deadGuillaume Gomez-6/+19
fix confusing diagnostic for reserved `##` Closes #131615
2024-11-28Improve span handling in `parse_expr_bottom`.Nicholas Nethercote-21/+14
`parse_expr_bottom` stores `this.token.span` in `lo`, but then fails to use it in many places where it could. This commit fixes that, and likewise (to a smaller extent) in `parse_ty_common`.
2024-11-28Trim extra space in 'repeated `mut`' diagnosticclubby789-2/+5
2024-11-26Rollup merge of #133140 - dtolnay:precedence, r=fmeaseMichael Goulet-7/+10
Inline ExprPrecedence::order into Expr::precedence The representation of expression precedence in rustc_ast has been an obstacle to further improvements in the pretty-printer (continuing from #119105 and #119427). Previously the operation of *"does this expression have lower precedence than that one"* (relevant for parenthesis insertion in macro-generated syntax trees) consisted of 3 steps: 1. Convert `Expr` to `ExprPrecedence` using `.precedence()` 2. Convert `ExprPrecedence` to `i8` using `.order()` 3. Compare using `<` As far as I can guess, the reason for the separation between `precedence()` and `order()` was so that both `rustc_ast::Expr` and `rustc_hir::Expr` could convert as straightforwardly as possible to the same `ExprPrecedence` enum, and then the more finicky logic performed by `order` could be present just once. The mapping between `Expr` and `ExprPrecedence` was intended to be as straightforward as possible: ```rust match self.kind { ExprKind::Closure(..) => ExprPrecedence::Closure, ... } ``` although there were exceptions of both many-to-one, and one-to-many: ```rust ExprKind::Underscore => ExprPrecedence::Path, ExprKind::Path(..) => ExprPrecedence::Path, ... ExprKind::Match(_, _, MatchKind::Prefix) => ExprPrecedence::Match, ExprKind::Match(_, _, MatchKind::Postfix) => ExprPrecedence::PostfixMatch, ``` Where the nature of `ExprPrecedence` becomes problematic is when a single expression kind might be associated with multiple different precedence levels depending on context (outside the expression) and contents (inside the expression). For example consider what is the precedence of an ExprKind::Closure `$closure`. Well, on the left-hand side of a binary operator it would need parentheses in order to avoid the trailing binary operator being absorbed into the closure body: `($closure) + Rhs`, so the precedence is something lower than that of `+`. But on the right-hand side of a binary operator, a closure is just a straightforward prefix expression like a unary op, which is a relatively high precedence level, higher than binops but lower than method calls: `Lhs + $closure` is fine without parens but `($closure).method()` needs them. But as a third case, if the closure contains an explicit return type, then the precedence is an even higher level than that, never needing parenthesization even in a binop left-hand side or method call: `|| -> bool { false } + Rhs` or `|| -> bool { false }.method()`. You can see that trying to capture all of this resolution about expressions into `ExprPrecedence` violates the intention of `ExprPrecedence` being a straightforward one-to-one correspondence from each AST and HIR `ExprKind` variant. It would be possible to attempt that by doing stuff like `ExprPrecedence::Closure(Side::Leading, ReturnType::No)`, but I don't foresee the original envisioned benefit of the `precedence()`/`order()` distinction being retained in this approach. Instead I want to move toward a model that Syn has been using successfully. In Syn, there is a Precedence enum but it differs from rustc in the following ways: - There are [relatively few variants](https://github.com/dtolnay/syn/blob/2.0.87/src/precedence.rs#L11-L47) compared to rustc's `ExprPrecedence`. For example there is no distinction at the precedence level between returns and closures, or between loops and method calls. - We distinguish between [leading](https://github.com/dtolnay/syn/blob/2.0.87/src/fixup.rs#L293) and [trailing](https://github.com/dtolnay/syn/blob/2.0.87/src/fixup.rs#L309) precedence, taking into account an expression's context such as what token follows it (for various syntactic bail-outs in Rust's grammar, like ambiguities around break-with-value) and how it relates to operators from the surrounding syntax tree. - There are no hardcoded mysterious integer quantities like rustc's `PREC_CLOSURE = -40`. All precedence comparisons are performed via PartialOrd on a C-like enum. This PR is just a first step in these changes. As you can tell from Syn, I definitely think there is value in having a dedicated type to represent precedence, instead of what `order()` is doing with `i8`. But that is a whole separate adventure because rustc_ast doesn't even agree consistently on `i8` being the type for precedence order; `AssocOp::precedence` instead uses `usize` and there are casts in both directions. It is likely that a type called `ExprPrecedence` will re-appear, but it will look substantially different from the one that existed before this PR.
2024-11-26Rollup merge of #133070 - nnethercote:lexer-tweaks, r=chenyukangMichael Goulet-80/+86
Lexer tweaks Some cleanups and small performance improvements. r? ```@chenyukang```
2024-11-25fix confusing diagnostic for reserved `##`Peter Jaszkowiak-6/+19
2024-11-25Refactor `where` predicates, and reserve for attributes supportFrank King-32/+30
2024-11-25Streamline `lex_token_trees` error handling.Nicholas Nethercote-20/+14
- Use iterators instead of `for` loops. - Use `if`/`else` instead of `match`.
2024-11-25Fix some formatting.Nicholas Nethercote-5/+15
Must be one of those cases where the function is too long and rustfmt bails out.
2024-11-25Split `Lexer::bump`.Nicholas Nethercote-7/+27
It has two different ways of being called.
2024-11-25Merge `TokenTreesReader` into `StringReader`.Nicholas Nethercote-49/+31
There is a not-very-useful layering in the lexer, where `TokenTreesReader` contains a `StringReader`. This commit combines them and names the result `Lexer`, which is a more obvious name for it. The methods of `Lexer` are now split across `mod.rs` and `tokentrees.rs` which isn't ideal, but it doesn't seem worth moving a bunch of code to avoid it.
2024-11-24parse guard patternsNadrieril-37/+56
Co-authored-by: Max Niederman <max@maxniederman.com>
2024-11-24refactor pat parser method names/doc-comments to agree with RFC 3637Max Niederman-21/+22
2024-11-21Implement the unsafe-fields RFC.Luca Versari-2/+20
Co-Authored-By: Jacob Pratt <jacob@jhpratt.dev>
2024-11-21Remove `ErrorGuaranteed` retval from `error_unexpected_after_dot`.Nicholas Nethercote-7/+7
It was added in #130349, but it's not used meaningfully, and causes difficulties for Nonterminal removal in #124141.
2024-11-21Prepare for invisible delimiters.Nicholas Nethercote-10/+68
Current places where `Interpolated` is used are going to change to instead use invisible delimiters. This prepares for that. - It adds invisible delimiter cases to the `can_begin_*`/`may_be_*` methods and the `failed_to_match_macro` that are equivalent to the existing `Interpolated` cases. - It adds panics/asserts in some places where invisible delimiters should never occur. - In `Parser::parse_struct_fields` it excludes an ident + invisible delimiter from special consideration in an error message, because that's quite different to an ident + paren/brace/bracket.
2024-11-21Add metavariables to `TokenDescription`.Nicholas Nethercote-17/+52
Pasted metavariables are wrapped in invisible delimiters, which pretty-print as empty strings, and changing that can break some proc macros. But error messages saying "expected identifer, found ``" are bad. So this commit adds support for metavariables in `TokenDescription` so they print as "metavariable" in error messages, instead of "``". It's not used meaningfully yet, but will be needed to get rid of interpolated tokens.
2024-11-21Introduce `InvisibleOrigin` on invisible delimiters.Nicholas Nethercote-8/+8
It's not used meaningfully yet, but will be needed to get rid of interpolated tokens.
2024-11-19Auto merge of #133219 - matthiaskrgr:rollup-hnuq0zf, r=matthiaskrgrbors-3/+2
Rollup of 8 pull requests Successful merges: - #123947 (Add vec_deque::Iter::as_slices and friends) - #125405 (Add std::thread::add_spawn_hook.) - #133175 (ci: use free runner in dist-i686-msvc) - #133183 (Mention std::fs::remove_dir_all in std::fs::remove_dir) - #133188 (Add `visit` methods to ast nodes that already have `walk`s on ast visitors) - #133201 (Remove `TokenKind::InvalidPrefix`) - #133207 (Default-enable `llvm_tools_enabled` when no `config.toml` is present) - #133213 (Correct the tier listing of `wasm32-wasip2`) r? `@ghost` `@rustbot` modify labels: rollup
2024-11-19Auto merge of #132761 - nnethercote:resolve-tweaks, r=petrochenkovbors-1/+2
Resolve tweaks A couple of small perf improvements, and some minor refactorings, all in `rustc_resolve`. r? `@petrochenkov`
2024-11-19Remove `TokenKind::InvalidPrefix`.Nicholas Nethercote-3/+2
It was added in #123752 to handle some cases involving emoji, but it isn't necessary because it's always treated the same as `TokenKind::InvalidIdent`. This commit removes it, which makes things a little simpler.
2024-11-17Diagnostics for let mut in item contextKornel-8/+25
2024-11-17Inline ExprPrecedence::order into Expr::precedenceDavid Tolnay-7/+10
2024-11-17Rollup merge of #133060 - tyrone-wu:removelet-span-suggestion, r=jieyouxu许杰友 Jieyou Xu (Joe)-2/+3
Trim whitespace in RemoveLet primary span Separate `RemoveLet` span into primary span for `let` and removal suggestion span for `let `, so that primary span does not include whitespace. Fixes: #133031
2024-11-16review comment: move logic to new methodEsteban Küber-88/+103
2024-11-16Reword suggestion messageEsteban Küber-2/+2
2024-11-16Better account for `else if` macro conditions mising an `if`Esteban Küber-1/+10
If a macro statement has been parsed after `else`, suggest a missing `if`: ``` error: expected `{`, found `falsy` --> $DIR/else-no-if.rs:47:12 | LL | } else falsy! {} { | ---- ^^^^^ | | | expected an `if` or a block after this `else` | help: add an `if` if this is the condition of a chained `else if` statement | LL | } else if falsy! {} { | ++ ```
2024-11-16Increase accuracy of `if` condition misparse suggestionEsteban Küber-9/+89
Look at the expression that was parsed when trying to recover from a bad `if` condition to determine what was likely intended by the user beyond "maybe this was meant to be an `else` body". ``` error: expected `{`, found `map` --> $DIR/missing-dot-on-if-condition-expression-fixable.rs:4:30 | LL | for _ in [1, 2, 3].iter()map(|x| x) {} | ^^^ expected `{` | help: you might have meant to write a method call | LL | for _ in [1, 2, 3].iter().map(|x| x) {} | + ```
2024-11-15Trim whitespace in RemoveLet primary spanTyrone Wu-2/+3
Separate `RemoveLet` span into primary span for `let` and removal suggestion span for `let `, so that primary span does not include whitespace. Fixes: #133031 Signed-off-by: Tyrone Wu <wudevelops@gmail.com>
2024-11-13Trim extra space when suggesting removing bad `let`clubby789-1/+3