about summary refs log tree commit diff
path: root/compiler/rustc_parse/src
AgeCommit message (Collapse)AuthorLines
2024-05-17use signature name for argardi-2/+2
2024-05-17s/(Ident, ItemKind)/ItemInfo/ardi-2/+2
2024-05-17Clarify that the diff_marker is talking about version control systemardi-38/+47
conflicts specifically and a few more improvements.
2024-05-15delegation: Implement list delegationVadim Petrochenkov-10/+28
```rust reuse prefix::{a, b, c} ```
2024-05-14improve maybe_consume_incorrect_semicolonardi-26/+23
2024-05-13Warn against redundant use<...>Michael Goulet-28/+30
2024-05-14Remove `NtIdent` and `NtLifetime`.Nicholas Nethercote-27/+36
The extra span is now recorded in the new `TokenKind::NtIdent` and `TokenKind::NtLifetime`. These both consist of a single token, and so there's no operator precedence problems with inserting them directly into the token stream. The other way to do this would be to wrap the ident/lifetime in invisible delimiters, but there's a lot of code that assumes an interpolated ident/lifetime fits in a single token, and changing all that code to work with invisible delimiters would have been a pain. (Maybe it could be done in a follow-up.) This change might not seem like much of a win, but it's a first step toward the much bigger and long-desired removal of `Nonterminal` and `TokenKind::Interpolated`. That change is big and complex enough that it's worth doing this piece separately. (Indeed, this commit is based on part of a late commit in #114647, a prior attempt at that big and complex change.)
2024-05-13Macros: match const { ... } with expr nonterminal in edition 2024Vincenzo Palazzo-1/+8
Co-authored-by: Eric Holk <eric@theincredibleholk.org> Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
2024-05-13Add expr_2021 nonterminal and feature flagEric Holk-2/+4
This commit adds a new nonterminal `expr_2021` in macro patterns, and `expr_fragment_specifier_2024` feature flag. For now, `expr` and `expr_2021` are treated the same, but in future PRs we will update `expr` to match to new grammar. Co-authored-by: Vincezo Palazzo <vincenzopalazzodev@gmail.com>
2024-05-12Disallow cast with trailing braced macro in let-elseDavid Tolnay-11/+17
2024-05-13Remove a `Span` from `TokenKind::Interpolated`.Nicholas Nethercote-50/+29
This span records the declaration of the metavariable in the LHS of the macro. It's used in a couple of error messages. Unfortunately, it gets in the way of the long-term goal of removing `TokenKind::Interpolated`. So this commit removes it, which degrades a couple of (obscure) error messages but makes things simpler and enables the next commit.
2024-05-11Add classify::expr_is_completeDavid Tolnay-59/+3
2024-05-11Remove MacCall special cases from Parser::parse_full_stmtDavid Tolnay-9/+3
It is impossible for expr here to be a braced macro call. Expr comes from `parse_stmt_without_recovery`, in which macro calls are parsed by `parse_stmt_mac`. See this part: let kind = if (style == MacStmtStyle::Braces && self.token != token::Dot && self.token != token::Question) || self.token == token::Semi || self.token == token::Eof { StmtKind::MacCall(P(MacCallStmt { mac, style, attrs, tokens: None })) } else { // Since none of the above applied, this is an expression statement macro. let e = self.mk_expr(lo.to(hi), ExprKind::MacCall(mac)); let e = self.maybe_recover_from_bad_qpath(e)?; let e = self.parse_expr_dot_or_call_with(e, lo, attrs)?; let e = self.parse_expr_assoc_with( 0, LhsExpr::AlreadyParsed { expr: e, starts_statement: false }, )?; StmtKind::Expr(e) }; A braced macro call at the head of a statement is always either extended into ExprKind::Field / MethodCall / Await / Try / Binary, or else returned as StmtKind::MacCall. We can never get a StmtKind::Expr containing ExprKind::MacCall containing brace delimiter.
2024-05-11Remove MacCall special case from recovery after missing 'if' after 'else'David Tolnay-6/+28
The change to the test is a little goofy because the compiler was guessing "correctly" before that `falsy! {}` is the condition as opposed to the else body. But I believe this change is fundamentally correct. Braced macro invocations in statement position are most often item-like (`thread_local! {...}`) as opposed to parenthesized macro invocations which are condition-like (`cfg!(...)`).
2024-05-11Document MacCall special case in Parser::parse_armDavid Tolnay-0/+11
2024-05-11Document MacCall special case in Parser::expr_is_completeDavid Tolnay-7/+44
2024-05-11Mark Parser::expr_is_complete call sitesDavid Tolnay-5/+7
2024-05-11Macro call with braces does not require semicolon to be statementDavid Tolnay-7/+20
This commit by itself is supposed to have no effect on behavior. All of the call sites are updated to preserve their previous behavior. The behavior changes are in the commits that follow.
2024-05-11Mark expr_requires_semi_to_be_stmt call sitesDavid Tolnay-5/+6
For each of these, we need to decide whether they need to be using `expr_requires_semi_to_be_stmt`, or `expr_requires_comma_to_be_match_arm`, which are supposed to be 2 different behaviors. Previously they were conflated into one, causing either too much or too little parenthesization.
2024-05-11Rollup merge of #124930 - compiler-errors:consume-arg, r=petrochenkov许杰友 Jieyou Xu (Joe)-1/+5
Make sure we consume a generic arg when checking mistyped turbofish When recovering un-turbofish-ed args in expr position (e.g. `let x = a<T, U>();` in `check_mistyped_turbofish_with_multiple_type_params`, we used `parse_seq_to_before_end` to parse the fake generic args; however, it used `parse_generic_arg` which *optionally* parses a generic arg. If it doesn't end up parsing an arg, it returns `Ok(None)` and consumes no tokens. If we don't find a delimiter after this (`,`), we try parsing *another* element. In this case, we just infinitely loop looking for a subsequent element. We can fix this by making sure that we either parse a generic arg or error in `parse_seq_to_before_end`'s callback. Fixes #124897
2024-05-11Rollup merge of #124318 - bvanjoi:fix-123911, r=petrochenkov许杰友 Jieyou Xu (Joe)-7/+12
ignore generics args in attribute paths Fixes #97006 Fixes #123911 Fixes #123912 This patch ensures that we no longer have to handle invalid generic arguments in attribute paths. r? `@petrochenkov`
2024-05-11ignore generics args in attribute pathsbohan-7/+12
2024-05-10Rollup merge of #124778 - fmease:fix-diag-msg-parse-meta-item, r=nnethercoteMatthias Krüger-37/+33
Fix parse error message for meta items Addresses https://github.com/rust-lang/rust/issues/122796#issuecomment-2010803906, cc [``@]Thomasdezeeuw.`` For attrs inside of a macro like `#[doc(alias = $ident)]` or `#[cfg(feature = $ident)]` where `$ident` is a macro metavariable of fragment kind `ident`, we used to say the following when expanded (with `$ident` ⟼ `ident`): ``` error: expected unsuffixed literal or identifier, found `ident` --> weird.rs:6:19 | 6 | #[cfg(feature = $ident)] | ^^^^^^ ... 11 | m!(id); | ------ in this macro invocation | = note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info) ``` This was incorrect and caused confusion, justifiably so (see #122796). In this position, we only accept/expect *unsuffixed literals* which consist of numeric & string literals as well as the boolean literals / the keywords / the reserved identifiers `false` & `true` **but not** arbitrary identifiers. Furthermore, we used to suggest garbage when encountering unexpected non-identifier tokens: ``` error: expected unsuffixed literal, found `-` --> weird.rs:16:17 | 16 | #[cfg(feature = -1)] | ^ | help: surround the identifier with quotation marks to parse it as a string | 16 | #[cfg(feature =" "-1)] | + + ``` Now we no longer do.
2024-05-10Fix parse error message for meta itemsLeón Orell Valerian Liehr-37/+33
2024-05-09Make sure we consume a generic arg when checking mistyped turbofishMichael Goulet-1/+5
2024-05-09Add `ErrorGuaranteed` to `Recovered::Yes` and use it more.Nicholas Nethercote-77/+60
The starting point for this was identical comments on two different fields, in `ast::VariantData::Struct` and `hir::VariantData::Struct`: ``` // FIXME: investigate making this a `Option<ErrorGuaranteed>` recovered: bool ``` I tried that, and then found that I needed to add an `ErrorGuaranteed` to `Recovered::Yes`. Then I ended up using `Recovered` instead of `Option<ErrorGuaranteed>` for these two places and elsewhere, which required moving `ErrorGuaranteed` from `rustc_parse` to `rustc_ast`. This makes things more consistent, because `Recovered` is used in more places, and there are fewer uses of `bool` and `Option<ErrorGuaranteed>`. And safer, because it's difficult/impossible to set `recovered` to `Recovered::Yes` without having emitted an error.
2024-05-08Rollup merge of #123344 - pietroalbini:pa-unused-imports, r=NilstriebMatthias Krüger-2/+6
Remove braces when fixing a nested use tree into a single item [Back in 2019](https://github.com/rust-lang/rust/pull/56645) I added rustfix support for the `unused_imports` lint, to automatically remove them when running `cargo fix`. For the most part this worked great, but when removing all but one childs of a nested use tree it turned `use foo::{Unused, Used}` into `use foo::{Used}`. This is slightly annoying, because it then requires you to run `rustfmt` to get `use foo::Used`. This PR automatically removes braces and the surrouding whitespace when all but one child of a nested use tree are unused. To get it done I had to add the span of the nested use tree to the AST, and refactor a bit the code I wrote back then. A thing I noticed is, there doesn't seem to be any `//@ run-rustfix` test for fixing the `unused_imports` lint. I created a test in `tests/suggestions` (is that the right directory?) that for now tests just what I added in the PR. I can followup in a separate PR to add more tests for fixing `unused_lints`. This PR is best reviewed commit-by-commit.
2024-05-08Auto merge of #124779 - workingjubilee:debug-formatting-my-beloved, ↵bors-9/+54
r=compiler-errors Improve `rustc_parse::Parser`'s debuggability The main event is the final commit where I add `Parser::debug_lookahead`. Everything else was basically cleaning up things that bugged me (debugging, as it were) until I felt comfortable enough to actually work on it. The motivation is that it's annoying as hell to try to figure out how the debug infra works in rustc without having basic queries like `debug!(?parser);` come up "empty". However, Parser has a lot of fields that are mostly irrelevant for most debugging, like the entire ParseSess. I think `Parser::debug_lookahead` with a capped lookahead might be fine as a general-purpose Debug impl, but this adapter version was suggested to allow more choice, and admittedly, it's a refined version of what I was already handrolling just to get some insight going.
2024-05-07compiler: add `Parser::debug_lookahead`Jubilee Young-0/+42
I tried debugging a parser-related issue but found it annoying to not be able to easily peek into the Parser's token stream. Add a convenience fn that offers an opinionated view into the parser, but one that is useful for answering basic questions about parser state.
2024-05-07compiler: derive Debug in parserJubilee Young-8/+11
It's annoying to debug the parser if you have to stop every five seconds to add a Debug impl.
2024-05-07narrow down visibilities in `rustc_parse::lexer`Lin Yihai-6/+6
2024-05-05compiler: Privatize `Parser::current_closure`Jubilee Young-1/+1
This was added as pub in 2021 and remains only privately used in 2024!
2024-05-06Move some tests from `rustc_expand` to `rustc_parse`.Nicholas Nethercote-0/+1615
There are some test cases involving `parse` and `tokenstream` and `mut_visit` that are located in `rustc_expand`. Because it used to be the case that constructing a `ParseSess` required the involvement of `rustc_expand`. However, since #64197 merged (a long time ago) `rust_expand` no longer needs to be involved. This commit moves the tests into `rustc_parse`. This is the optimal place for the `parse` tests. It's not ideal for the `tokenstream` and `mut_visit` tests -- they would be better in `rustc_ast` -- but they still rely on parsing, which is not available in `rustc_ast`. But `rustc_parse` is lower down in the crate graph and closer to `rustc_ast` than `rust_expand`, so it's still an improvement for them. The exact renaming is as follows: - rustc_expand/src/mut_visit/tests.rs -> rustc_parse/src/parser/mut_visit/tests.rs - rustc_expand/src/tokenstream/tests.rs -> rustc_parse/src/parser/tokenstream/tests.rs - rustc_expand/src/tests.rs + rustc_expand/src/parse/tests.rs -> compiler/rustc_parse/src/parser/tests.rs The latter two test files are combined because there's no need for them to be separate, and having a `rustc_parse::parser::parse` module would be weird. This also means some `pub(crate)`s can be removed.
2024-04-29Add StaticForeignItem and use it on ForeignItemKindSantiago Pastorino-1/+5
2024-04-24Auto merge of #104087 - nbdd0121:const, r=scottmcmbors-2/+0
Stabilise inline_const # Stabilisation Report ## Summary This PR will stabilise `inline_const` feature in expression position. `inline_const_pat` is still unstable and will *not* be stabilised. The feature will allow code like this: ```rust foo(const { 1 + 1 }) ``` which is roughly desugared into ```rust struct Foo; impl Foo { const FOO: i32 = 1 + 1; } foo(Foo::FOO) ``` This feature is from https://github.com/rust-lang/rfcs/pull/2920 and is tracked in #76001 (the tracking issue should *not* be closed as it needs to track inline const in pattern position). The initial implementation is done in #77124. ## Difference from RFC There are two major differences (enhancements) as implemented from the RFC. First thing is that the RFC says that the type of an inline const block inferred from the content *within* it, but we currently can infer the type using the information from outside the const block as well. This is a frequently requested feature to the initial implementation (e.g. #89964). The inference is implemented in #89561 and is done by treating inline const similar to a closure and therefore share inference context with its parent body. This allows code like: ```rust let v: Vec<i32> = const { Vec::new() }; ``` Another enhancement that differs from the RFC is that we currently allow inline consts to reference generic parameters. This is implemented in #96557. This allows code like: ```rust fn create_none_array<T, const N: usize>() -> [Option<T>; N] { [const { None::<T> }; N] } ``` This enhancement also makes inline const usable as static asserts: ```rust fn require_zst<T>() { const { assert!(std::mem::size_of::<T>() == 0) } } ``` ## Documentation Reference: rust-lang/reference#1295 ## Unresolved issues We still have a few issues that are not resolved, but I don't think it necessarily has to block stabilisation: * expr fragment specifier issue: #86730 * ~~`const {}` behaves similar to `async {}` but not to `{}` and `unsafe {}` (they are treated as `ExpressionWithoutBlock` rather than `ExpressionWithBlock`): https://rust-lang.zulipchat.com/#narrow/stream/213817-t-lang/topic/const.20blocks.20differ.20from.20normal.20and.20from.20unsafe.20blocks/near/290229453~~ ## Tests There are a few tests in https://github.com/rust-lang/rust/tree/master/src/test/ui/inline-const
2024-04-24Stabilise `inline_const`Gary Guo-2/+0
2024-04-24Auto merge of #122500 - petrochenkov:deleg, r=fmeasebors-5/+5
delegation: Support renaming, and async, const, extern "ABI" and C-variadic functions Also allow delegating to functions with opaque types (`impl Trait`). The delegation item will refer to the original opaque type from the callee, fresh opaque type won't be created, which seems like a reasonable behavior. (Such delegation items will cause query cycles when used in trait impls, but it can be fixed later.) Part of https://github.com/rust-lang/rust/issues/118212.
2024-04-23delegation: Support renamingVadim Petrochenkov-5/+5
2024-04-23Auto merge of #124302 - matthiaskrgr:rollup-2aya8n8, r=matthiaskrgrbors-0/+1
Rollup of 3 pull requests Successful merges: - #124003 (Dellvmize some intrinsics (use `u32` instead of `Self` in some integer intrinsics)) - #124169 (Don't fatal when calling `expect_one_of` when recovering arg in `parse_seq`) - #124286 (Subtree sync for rustc_codegen_cranelift) r? `@ghost` `@rustbot` modify labels: rollup
2024-04-23Rollup merge of #124169 - compiler-errors:parser-fatal, r=oli-obkMatthias Krüger-0/+1
Don't fatal when calling `expect_one_of` when recovering arg in `parse_seq` In `parse_seq`, when parsing a sequence of token-separated items, if we don't see a separator, we try to parse another item eagerly in order to give a good diagnostic and recover from a missing separator: https://github.com/rust-lang/rust/blob/d1a0fa5ed3ffe52d72f761d3c95cbeb0a9cdfe66/compiler/rustc_parse/src/parser/mod.rs#L900-L901 If parsing the item itself calls `expect_one_of`, then we will fatal because of #58903: https://github.com/rust-lang/rust/blob/d1a0fa5ed3ffe52d72f761d3c95cbeb0a9cdfe66/compiler/rustc_parse/src/parser/mod.rs#L513-L516 For `precise_capturing` feature I implemented, we do end up calling `expected_one_of`: https://github.com/rust-lang/rust/blob/d1a0fa5ed3ffe52d72f761d3c95cbeb0a9cdfe66/compiler/rustc_parse/src/parser/ty.rs#L712-L714 This leads the compiler to fatal *before* having emitted the first error, leading to absolutely no useful information for the user about what happened in the parser. This PR makes it so that we stop doing that. Fixes #124195
2024-04-23Rollup merge of #124218 - Xiretza:subsubdiagnostics, r=davidtwcoLeón Orell Valerian Liehr-1/+1
Allow nesting subdiagnostics in #[derive(Subdiagnostic)]
2024-04-23Rollup merge of #124284 - klensy:no-reads, r=fmeaseMatthias Krüger-4/+0
parser: remove unused(no reads) max_angle_bracket_count field Isn't there (clippy) lint for variables with only writes? They should be marked as dead too, probably. Found only https://rust-lang.github.io/rust-clippy/master/index.html#/collection_is_never_read
2024-04-23Rollup merge of #124099 - voidc:disallow-ambiguous-expr-attrs, r=davidtwcoMatthias Krüger-13/+28
Disallow ambiguous attributes on expressions This implements the suggestion in [#15701](https://github.com/rust-lang/rust/issues/15701#issuecomment-2033124217) to disallow ambiguous outer attributes on expressions. This should resolve one of the concerns blocking the stabilization of `stmt_expr_attributes`.
2024-04-23parser: remove ununsed(no reads) max_angle_bracket_count fieldklensy-4/+0
2024-04-22Improve handling of expr->field errorsSasha Pourcelot-0/+15
The current message for "`->` used for field access" is the following: ```rust error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `->` --> src/main.rs:2:6 | 2 | a->b; | ^^ expected one of 8 possible tokens ``` (playground link[1]) This PR tries to address this by adding a dedicated error message and recovery. The proposed error message is: ``` error: `->` used for field access or method call --> ./tiny_test.rs:2:6 | 2 | a->b; | ^^ help: try using `.` instead | = help: the `.` operator will dereference the value if needed ``` (feel free to bikeshed it as much as necessary) [1]: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=7f8b6f4433aa7866124123575456f54e Signed-off-by: Sasha Pourcelot <sasha.pourcelot@protonmail.com>
2024-04-21Pass translation closure to add_to_diag_with() as referenceXiretza-1/+1
2024-04-19Don't fatal when calling expect_one_of when recovering arg in parse_seqMichael Goulet-0/+1
2024-04-18Rollup merge of #123752 - estebank:emoji-prefix, r=wesleywiserJubilee-1/+4
Properly handle emojis as literal prefix in macros Do not accept the following ```rust macro_rules! lexes {($($_:tt)*) => {}} lexes!(🐛"foo"); ``` Before, invalid emoji identifiers were gated during parsing instead of lexing in all cases, but this didn't account for macro pre-expansion of literal prefixes. Fix #123696.
2024-04-18Disallow ambiguous attributes on expressionsDominik Stolz-13/+28
2024-04-18Auto merge of #124008 - nnethercote:simpler-static_assert_size, r=Nilstriebbors-3/+3
Simplify `static_assert_size`s. We want to run them on all 64-bit platforms. r? `@ghost`