Commit Graph

690 Commits

Author SHA1 Message Date
León Orell Valerian Liehr
8de399ac7b Remove dead parse error recovery (underscores in expressions) 2026-08-26 04:48:42 +02:00
mejrs
7cd9c920cd rustc_session; remove rustc_lint_defs rex-exports 2026-08-20 23:02:49 +02:00
bors
eaa160c49d Auto merge of #161082 - nnethercote:overhaul-CoroutineMarker, r=mejrs
Overhaul `CoroutineKind`

Its structure is a bit weird. Details in individual commits.

r? @mejrs
2026-08-17 02:41:22 +00:00
Nicholas Nethercote
613e826eaa Overhaul CoroutineKind
It is an enum with three variant, and all the variants have identical
fields, which is silly. This commit does the following.

- Renames it as `CoroutineMarker`, because the `Kind` suffix is used for
  enums. (The existing doc comment already uses the word "marker".)

- Renames the existing `GenBlockKind` as `CoroutineKind`, uses it
  within `CoroutineMarker`, and adds `CoroutineKind::is_gen`.

This removes the need for the `span`, `closure_id` and `return_id`
methods; direct field access now suffices.
2026-08-17 12:29:59 +10:00
ravlyn
349eac6d92 rustc_parse: suggest removing semicolon before if block 2026-08-12 21:11:19 +07:00
Chris Denton
4641990929 Remove ensure_sufficient_stack 2026-08-09 19:58:29 +00:00
Jonathan Brouwer
c10876b8a1 Rollup merge of #160744 - mejrs:struct-diagnostics-privacy, r=JonathanBrouwer
make more diagnostic structs pub(crate)
2026-08-09 18:22:27 +02:00
mejrs
d8ecbe7c01 Move ExprParenthesesNeeded diagnostic struct out of rustc_session
It was never used in that crate, so rustc_parse is the next obvious place to go. It's also used by rustc_hir_typeck, but sharing diagnostics between crates makes it easy for such things to become dead, so duplicate it.
2026-08-08 12:13:25 +02:00
Vadim Petrochenkov
7894a438a7 rustc_parse: A few cleanups to expression parsing next to attributes 2026-07-31 22:44:48 +03:00
Esteban Küber
0ad2a3b3c2 Detect when a macro without exclamation mark uses square brackets
```
error: expected one of `.`, `?`, `]`, or an operator, found `,`
  --> $DIR/do-not-suggest-semicolon-between-macro-without-exclamation-mark-and-array.rs:2:19
   |
LL |     let _x = vec[1, 2, 3];
   |                   ^ expected one of `.`, `?`, `]`, or an operator
   |
help: you might have meant to call a macro
   |
LL |     let _x = vec![1, 2, 3];
   |                 +
```
2026-07-27 07:52:45 +00:00
Vadim Petrochenkov
75529d4b8e rustc_parse: Stop returning Option from statement parsing 2026-07-24 18:35:36 +03:00
Jacob Pratt
625d5f9fc9 Rollup merge of #159667 - estebank:verbose-parse-suggestions, r=TaKO8Ki
Make some parser structured suggestions verbose and tweak their wording

Replace most of the `.span_suggestion(` in `rustc_parse` with `.span_suggestion_verbose(`, as they are more readabale, if more verbose. Verbose suggestions also tend to highlight off-by-one `Span` errors better.

Tweak some of the touched diagnostics to bring them more in-line with our house style.

CC https://github.com/rust-lang/rust/issues/141973
2026-07-24 01:59:27 -04:00
Esteban Küber
1b7ae04e21 Tweak code and diagnostic formatting and messages 2026-07-21 16:11:09 +00:00
Esteban Küber
737220233d Make some parser structured suggestions verbose
Replace most of the `.span_suggestion(` in `rustc_parse` with `.span_suggestion_verbose(`, as they are more readabale, if more verbose. Verbose suggestions also tend to highlight off-by-one `Span` errors better.
2026-07-21 15:50:58 +00:00
Nicholas Nethercote
3f2624ea64 Make TokenTreeCursor private
And also the fields of `TokenCursor`.

It's good hygiene in general, and will allow larger changes to
`TokenCursor` down the road (e.g. hopefully avoiding the flattening of
token trees to a linear token stream).
2026-07-21 06:55:08 +10:00
bors
470556c8c1 Auto merge of #158720 - nnethercote:Expr64, r=folkertdev
Shrink `ast::Expr64`



From 72 bytes to 64 bytes, by boxing `ForLoop`, which is the biggest
variant.

There are now 11 `ExprKind` variants that are 31 bytes, so future
improvements here will be very difficult.
2026-07-15 03:22:38 +00:00
Jacob Pratt
57f0adccec Rollup merge of #159250 - GuillaumeGomez:rename-diag, r=JonathanBrouwer
Rename `errors.rs` file to `diagnostics.rs` (13/N)

Follow-up of https://github.com/rust-lang/rust/pull/157485.

r? @JonathanBrouwer
2026-07-14 00:15:25 -04:00
Guillaume Gomez
2a74949017 Update rustc_session::errors import 2026-07-13 22:31:51 +02:00
Jana Dönszelmann
25745ee1f1 fix all tidy annotations 2026-07-13 10:54:53 +02:00
Nicholas Nethercote
36487c7595 Shrink ast::Expr
From 72 bytes to 64 bytes, by boxing `ForLoop`, which is the biggest
variant.

There are now 11 `ExprKind` variants that are 31 bytes, so future
improvements here will be very difficult.
2026-07-12 11:32:26 +10:00
Guillaume Gomez
cba2097424 Rename rustc_parse/src/errors.rs into rustc_parse/src/diagnostics.rs 2026-07-12 00:19:21 +02:00
khyperia
67986afdaf allow mGCA const arguments to fall back to anon consts 2026-07-08 07:46:21 +02:00
Nicholas Nethercote
b14e429dfb Use ThinVec in Expr::OffsetOf
Because we use `ThinVec` rather than `Vec` almost everywhere else in the
AST.
2026-07-04 09:55:14 +10:00
Jonathan Brouwer
a419aafef0 Rollup merge of #157978 - TaKO8Ki:fix-raw-ref-trailing-comma-recovery, r=JonathanBrouwer
Avoid `&raw` recovery ICE after trailing comma

Fixes rust-lang/rust#157950

This is a follow up to rust-lang/rust#157888.

For `&raw x,)`, recovery first skips `x`, then consumes the trailing comma. The old condition then fell through to `parse_token_tree()` when the next token was `)`, which causes an ICE.
2026-06-22 21:01:54 +02:00
Takayuki Maeda
034044f3bd avoid raw ref recovery ICE after trailing comma 2026-06-17 00:34:32 +09:00
Haoran Wang
0ab14caeaf Don't suggest adding in to a for loop that already has one
When a `for` loop is missing its `in`, the parser suggested inserting one
based only on the token after the pattern. A malformed binding such as
`for i i in 0..10` was therefore "corrected" to `for i in i in 0..10`,
which does not parse. Only suggest inserting `in` when the loop header
does not already contain one before the body.
2026-06-15 20:38:53 +08:00
Takayuki Maeda
33d143607d avoid raw ref call recovery in nested delimiters 2026-06-14 22:39:09 +09:00
qaijuang
68381b1bd8 address clean code suggestion 2026-06-10 06:21:57 -04:00
qaijuang
5fde1fdac9 preserve arg count in call recovery 2026-06-04 04:03:33 -04:00
qaijuang
3d697650f2 refactor: delegate raw ref mut checks to a new helper method 2026-06-04 03:44:31 -04:00
qaijuang
b459cfcf43 don't recover &raw EXPR as a missing comma 2026-05-28 15:39:23 -04:00
Esteban Küber
31e0383ea6 rustfmt matches with comments in or-patterns
Using https://github.com/rust-lang/rustfmt/pull/6893, reformat the codebase. The result is that matches that *would have* been formatted under normal circumstances get their expected format. These match expressions were being entirely skipped because they contain or-patterns with comments in between patterns, causing rustfmt to bail out entirely. The or-patterns with comments themselves remain untouched, but now the match arm bodies and other patterns without comments do get formatted under that PR.

Because the fix in rustfmt isn't landed yet, I reworked some of the or-patterns with comments so that formatting doesn't regress. Tried doing this only in larger blocks that are more likely to regress in the meantime.
2026-05-13 02:35:29 +00:00
Takayuki Maeda
98348de63a add move(expr) syntax 2026-05-07 22:22:56 +09:00
Nicholas Nethercote
9454be304f Invert dependency between rustc_error_messages and rustc_ast*.
`rustc_error_messages` currently depends on
`rustc_ast`/`rustc_ast_pretty`. This is odd, because
`rustc_error_messages` feels like a very low-level module but
`rustc_ast`/`rustc_ast_pretty` do not.

The reason is that a few AST types impl `IntoDiagArg` via
pretty-printing. `rustc_error_messages` can define `IntoDiagArg` and
then impl it for the AST types. But if we invert the dependency we hit
a problem with the orphan rule: `rustc_ast` must impl `IntoDiagArg`
for the AST types, but that requires calling pretty-printing code which
is in `rustc_ast_pretty`, a downstream crate.

This commit avoids this problem by just removing the `IntoDiagArg` impls
for these AST types. There aren't that many of them, and we can just use
`String` in the relevant error structs and use the pretty printer in the
downstream crates that construct the error structs. There are plenty of
existing examples where `String` is used in error structs.

There is now no dependency between `rustc_ast*` and
`rustc_error_messages`.
2026-05-01 10:58:14 +10:00
León Orell Valerian Liehr
07d015e566 Syntactically reject tuple index shorthands in struct patterns to fix a correctness regression 2026-04-23 22:28:00 +02:00
cijiugechu
f6294883f8 Make reparsed guard metavars collect tokens 2026-04-20 13:36:27 +08:00
León Orell Valerian Liehr
fc2c72cb67 Make the expansion of guard metavars begin guard non-terminals 2026-04-12 03:18:56 +02:00
Jonathan Brouwer
6e3c17424d Rollup merge of #153702 - SpriteOvO:guard-matcher, r=davidtwco
Add macro matcher for `guard` fragment specifier

Tracking issue #153104

This PR implements a new `guard` macro matcher to match `if-let` guards (specifically [`MatchArmGuard`](50a1075e87/src/expressions/match-expr.md (match-guards))). In the upcoming PR, we can use this new matcher in the `matches!` and `assert_matches!` macros to support their use with `if-let` guards. (see #152313)

The original `Expr` used to represent a guard has been wrapped in a new `Guard` type, allowing us to carry the span information of the leading `if` keyword. However, it might be even better to include the `if` keyword in the `Guard` type as well? I've left a FIXME comment in the code.
2026-03-25 19:52:50 +01:00
Guillaume Gomez
0e3e647e2e Remove BuiltinDiag usage in rustc_parse 2026-03-20 17:43:18 +01:00
Guillaume Gomez
bb689c41aa Create new ParseSess::dyn_buffer_lint method 2026-03-20 17:25:50 +01:00
Guillaume Gomez
9c591c6508 Start migrating DecorateDiagCompat::Builtin items to DecorateDiagCompat::Dynamic 2026-03-20 17:25:12 +01:00
Asuna
9729bb2558 Add macro matcher for guard fragment specifier 2026-03-12 02:18:34 +00:00
Nicholas Nethercote
c12ab08c14 Move Spanned.
It's defined in `rustc_span::source_map` which doesn't make any sense
because it has nothing to do with source maps. This commit moves it to
the crate root, a more sensible spot for something this basic.
2026-03-11 06:25:23 +11:00
Jonathan Brouwer
4e0b64408c Fix newly detected subdiagnostics using variables from parent 2026-03-06 19:00:25 +01:00
Jonathan Brouwer
7174c100f4 Rollup merge of #153227 - kpreid:struct-missing-field, r=estebank
Don’t report missing fields in struct exprs with syntax errors.

@Noratrieb [told me](https://internals.rust-lang.org/t/custom-cargo-command-to-show-only-errors-avoid-setting-rustflags-every-time/24032/7?u=kpreid) that “it is a bug if this recovery causes follow-up errors that would not be there if the user fixed the first error.” So, here’s a contribution to hide a follow-up error that annoyed me recently.

Specifically, if the user writes a struct literal with a syntax error, such as

```rust
StructName { foo: 1 bar: 2 }
```

the compiler will no longer report that the field `bar` is missing in addition to the syntax error.

This is my first time attempting any change to the parser or AST; please let me know if there is a better way to do what I’ve done here. ~~The part I’m least happy with is the blast radius of adding another field to `hir::ExprKind::Struct`, but this seems to be in line with the style of the rest of the code. (If this were my own code, I would consider changing `hir::ExprKind::Struct` to a nested struct, the same way it is in `ast::ExprKind`.)~~ The additional information is now stored as an additional variant of `ast::StructRest` / `hir::StructTailExpr`.

**Note to reviewers:** I recommend reviewing each commit separately, and in the case of the first one with indentation changes ignored.
2026-03-03 07:14:12 +01:00
Kevin Reid
d6eefce00d Remember whether a struct literal had syntax errors.
This adds a variant `NoneWithError` to AST and HIR representations of
the “rest” or “tail”, which is currently always treated identically to
the `None` variant.
2026-02-28 18:12:34 -08:00
Jonathan Brouwer
f3035e7c18 Rollup merge of #153190 - JonathanBrouwer:subdiag_variables, r=jdonszelmann
Don't allow subdiagnostic to use variables from their parent

Tangentially related to https://github.com/rust-lang/rust/issues/151366

This is PR 1/2 for structured diagnostics, will do the unstructured ones next. After the second PR I will be able to remove some code that should compensate for this PR being positive.

Regardless of this PR having a positive diff, I feel that subdiagnostics being able to use variables from their parent is very confusing, so this is for the better,.

r? @jdonszelmann
2026-02-28 12:52:57 +01:00
Jonathan Brouwer
1acf1c5367 Rollup merge of #152730 - BennoLossin:field-projections-lang-item, r=oli-obk
add field representing types

*[View all comments](https://triagebot.infra.rust-lang.org/gh-comments/rust-lang/rust/pull/152730)*

> [!NOTE]
> This is a rewrite of #146307 by using a lang item instead of a custom `TyKind`. We still need a `hir::TyKind::FieldOf` variant, because resolving the field name cannot be done before HIR construction. The advantage of doing it this way is that we don't need to make any changes to types after HIR (including symbol mangling). At the very beginning of this feature implementation, I tried to do it using a lang item, but then quickly abandoned the approach, because at that time I was still intending to support nested fields.

Here is a [range-diff](605f49b274..f5f42d1e03) between the two PRs

---

# Add Field Representing Types (FRTs)

This PR implements the first step of the field projection lang experiment (Tracking Issue: rust-lang/rust#145383). Field representing types (FRTs) are a new kind of type. They can be named through the use of the `field_of!` macro with the first argument being the type and the second the name of the field (or variant and field in the case of an enum). No nested fields are supported.

FRTs natively implement the `Field` trait that's also added in this PR. It exposes information about the field such as the type of the field, the type of the base (i.e. the type that contains the field) and the offset within that base type. Only fields of non-packed structs are supported, fields of enums an unions have unique types for each field, but those do not implement the `Field` trait.

This PR was created in collaboration with @dingxiangfei2009, it wouldn't have been possible without him, so huge thanks for mentoring me!

I updated my library solution for field projections to use the FRTs from `core` instead of creating my own using the hash of the name of the field. See the [Rust-for-Linux/field-projection `lang-experiment` branch](https://github.com/Rust-for-Linux/field-projection/tree/lang-experiment).

## API added to `core::field`

```rust
pub unsafe trait Field {
    type Base;

    type Type;

    const OFFSET: usize;
}

pub macro field_of($Container:ty, $($fields:expr)+ $(,)?);
```

Along with a perma-unstable type that the compiler uses in the expansion of the macro:

```rust
#[unstable(feature = "field_representing_type_raw", issue = "none")]
pub struct FieldRepresentingType<T: ?Sized, const VARIANT: u32, const FIELD: u32> {
    _phantom: PhantomData<T>,
}
```

## Explanation of Field Representing Types (FRTs)

FRTs are used for compile-time & trait-level reflection for fields of structs & tuples. Each struct & tuple has a unique compiler-generated type nameable through the `field_of!` macro. This type natively contains information about the field such as the outermost container, type of the field and its offset. Users may implement additional traits on these types in order to record custom information (for example a crate may define a [`PinnableField` trait](https://github.com/Rust-for-Linux/field-projection/blob/lang-experiment/src/marker.rs#L9-L23) that records whether the field is structurally pinned).

They are the foundation of field projections, a general operation that's generic over the fields of a struct. This genericism needs to be expressible in the trait system. FRTs make this possible, since an operation generic over fields can just be a function with a generic parameter `F: Field`.

> [!NOTE]
> The approach of field projections has changed considerably since this PR was opened. In the end we might not need FRTs, so this API is highly experimental.

FRTs should act as though they were defined as `struct MyStruct_my_field<StructGenerics>;` next to the struct. So it should be local to the crate defining the struct so that one can implement any trait for the FRT from that crate. The `Field` traits should be implemented by the compiler & populated with correct information (`unsafe` code needs to be able to rely on them being correct).

## TODOs

There are some `FIXME(FRTs)` scattered around the code:
- Diagnostics for `field_of!` can be improved
  - `tests/ui/field_representing_types/nonexistent.rs`
  - `tests/ui/field_representing_types/non-struct.rs`
  - `tests/ui/field_representing_types/offset.rs`
  - `tests/ui/field_representing_types/not-field-if-packed.rs`
  - `tests/ui/field_representing_types/invalid.rs`
- Simple type alias already seem to work, but might need some extra work in `compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs`

r? @oli-obk
2026-02-28 12:52:52 +01:00
Jonathan Brouwer
350a964d7c Fix subdiagnostics that use non-local variables 2026-02-27 15:22:47 +00:00
Benno Lossin
7b428597ff add field representing types 2026-02-27 15:54:20 +01:00