Commit Graph

2985 Commits

Author SHA1 Message Date
Jonathan Brouwer
239ab02029 Rollup merge of #160233 - estebank:issue-92685, r=JohnTitor
Bubble bad path error while parsing field to avoid unecessary second error

Noticed while looking at rust-lang/rust#92685. Add test for the case in that ticket.
2026-07-31 16:02:25 +02:00
Jonathan Brouwer
d7f1ae627d Rollup merge of #160192 - chenyukang:yukang-fix-160171-pub-before-close-brace, r=petrochenkov
Fix ICE for parsing issue with a closing brace

Fixes rust-lang/rust#160171

r? @petrochenkov
2026-07-31 00:06:41 +02:00
Jonathan Brouwer
0b4f6fcf51 Rollup merge of #160175 - estebank:issue-88416, r=jieyouxu
Try to recover less from incorrectly parsed const arg

The error recovery can cause a cascade of irrelevant errors, so instead return the `PResult::Err` and let the parser fail further up in the logic.

Fix rust-lang/rust#88416.
2026-07-31 00:06:38 +02:00
Jonathan Brouwer
bafc9b3206 Rollup merge of #160040 - kpreid:parse-fn, r=TaKO8Ki
Split function parsing out of `item.rs` to a new module.

`item.rs` is approaching the tidy-enforced length limit, and this will make room for new additions.

I have split this change out of PR rust-lang/rust#159044 to reduce its complexity (and because this large code move quickly becomes stale).
2026-07-31 00:06:35 +02:00
Esteban Küber
791e56a931 Bubble bad path error while parsing field to avoid unecessary second error 2026-07-30 19:41:56 +00:00
Yukang
b60abe4fbf Fix ICE for parsing issue with unexpected closing brace 2026-07-30 10:28:14 +08:00
Esteban Küber
5d78eab4dd Try to recover less from incorrectly parsed const arg 2026-07-29 18:23:20 +00:00
Jonathan Brouwer
9e4d2e62c7 Rollup merge of #160008 - TaKO8Ki:fix-stale-closure-recovery, r=mati865
Avoid stale closure recovery state across statements

Fixes rust-lang/rust#159989

`current_closure` was cleared when starting a new expression through `parse_expr`, but statement expressions call `parse_expr_res` directly. This allowed closure recovery state from one statement to remain active while parsing the next statement.

This pr clears `current_closure` when entering `parse_stmt_without_recovery`, matching the behavior of `parse_expr`.

da2697d1d8/compiler/rustc_parse/src/parser/expr.rs (L55-L56)

da2697d1d8/compiler/rustc_parse/src/parser/expr.rs (L63-L64)
2026-07-28 21:07:59 +02:00
Kevin Reid
0c1fefd9de Split function parsing out of item.rs to a new module.
`item.rs` is approaching the tidy-enforced length limit, and this will
make room for new additions.
2026-07-27 21:17:47 -07:00
Jonathan Brouwer
447eef1523 Rollup merge of #159974 - panstromek:update-thin-vec, r=mejrs
Update thinvec to 0.2.19

It contains `may_dangle` Drop impl under a feature flag. This allows us to use it in a lot more places, notable in types that hold `&'tcx` references. This would previously fail in borrowck in arena code.

We have to enable  its "unstable" feature for this. That also enables `TrustedLen` impl, which is behind unstable flag, too. I left the flag disabled, because it regressed next-solver benchmarks, but that will be fixed in PR that needs this flag (see the comment: https://github.com/rust-lang/rust/pull/159974#issuecomment-5091132427)
2026-07-27 19:24:50 +02:00
Takayuki Maeda
da2697d1d8 reset closure recovery state before parsing a statement 2026-07-27 20:19:58 +09:00
panstromek
02a54aa3ed update thinvec to latest version 2026-07-27 10:33:18 +02: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
Jonathan Brouwer
d01433cf71 Rollup merge of #159849 - petrochenkov:estmt, r=JonathanBrouwer
rustc_parse: Stop returning `Option` from statement parsing

`parse_stmt_without_recovery` had one corner case in which it returned `Ok(None)` - when parsing immediately encountered a closing brace `}`, possibly after parsing outer attributes.

It is simpler to never call `parse_stmt_without_recovery` in such contexts than deal with a possibility of no statement being returned without an error.
So this PR changes the function's return type from `PResult<'a, Option<Stmt>>` to `PResult<'a, Stmt>` and adjusts one call site to check for a closing brace.
2026-07-26 15:47:19 +02:00
Jacob Pratt
0a72846a4d Rollup merge of #159695 - Dnreikronos:proc_macro/cfg_attr_file_mod_inner_attr, r=nnethercote,petrochenkov
proc_macro: Fix cfg_attr inner attrs in file modules

Fixes rust-lang/rust#159648

Inner proc-macro attrs produced by `cfg_attr` only carry the nested attr span. Splitting source around that span tries to lex two unmatched halves, so file modules fail with an unclosed delimiter.

Lex the complete module body first, then remove the expanded attr from its nested token stream. Sibling attrs stay in place, and empty `cfg_attr` wrappers use their trace span and disappear. imo this is cleaner than teaching source slicing about every nested shape.

Tests exercise sibling attrs on either side of the proc macro, then cover the single-attr case. fyi, denied lints make sibling preservation observable; ltm if another nested case should live here.
2026-07-26 00:21:54 -04:00
Joao Roberto
1f8b15fba1 Fix cfg_attr inner attributes in file modules
Return to pretty-printed token reconstruction when lexing source slices around an inner attribute produces unmatched delimiters.

Cover cfg_attr-expanded proc-macro attributes in an out-of-line module, including sibling attributes on both sides.
2026-07-25 12:08:44 -03: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
panstromek
283ec3aa54 Make FieldDef smaller 2026-07-23 14:43:52 +02:00
khyperia
e895ed8c90 unify the AST repr of type const and const RHS 2026-07-22 09:22:45 +02: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
Jonathan Brouwer
ef3632338e Rollup merge of #159636 - nnethercote:some-comments, r=petrochenkov
Improve some comments (and an error message)

Details in individual commits.

r? @petrochenkov
2026-07-21 12:24:58 +02:00
Jonathan Brouwer
82d262fcc4 Rollup merge of #159590 - oli-obk:unsafe_attrs_old, r=petrochenkov
Remove some dead code

Leftover from a previous revision of `#[unsafe(no_mangle)]`. All call sites disallowed it anyway, and the real handling is in the parser.
2026-07-21 12:24:57 +02:00
Oli Scherer
4e2ebd6235 Remove some dead code 2026-07-21 11:34:36 +02:00
Nicholas Nethercote
efbdd60649 Add a comment about why we collect tokens
Non-obvious information that took me some time to work out.
2026-07-21 16:16:09 +10:00
Jacob Pratt
d2f94b7374 Rollup merge of #159601 - nnethercote:privatize-TokenTreeCursor, r=Kobzol
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).

r? @Kobzol
2026-07-20 23:38:07 -04:00
Jacob Pratt
18b6b903da Rollup merge of #159543 - chenyukang:yukang-fix-159542-remove-extra-semicolon, r=estebank
Remove extra semicolons in parsing item lists

Fixes rust-lang/rust#159542
2026-07-20 23:38:00 -04: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
Jonathan Brouwer
c905c76604 Rollup merge of #159603 - nnethercote:clarify-push_stream, r=Kobzol
Clarify `push_stream`/`push_tree`

More `TokenStream` cleanups. Details in individual commits.

r? @Kobzol
2026-07-20 18:06:17 +02:00
Jonathan Brouwer
152c269c8e Rollup merge of #159587 - nnethercote:AttrItem-span, r=petrochenkov
Improve `AttrItem::span`

It has some shortcomings. Details in individual commits.

r? @petrochenkov
2026-07-20 18:06:05 +02:00
Jonathan Brouwer
09e88c10af Rollup merge of #158046 - Dnreikronos:proc_macro/file_mod_inner_attr_spans, r=petrochenkov
proc_macro: preserve file module spans for inner attrs

fixes rust-lang/rust#157881

out-of-line mods with custom inner attrs were sending proc macros a pretty-printed module wrapper. the awkward bit: tokens inside the module picked up the span from the parent mod foo;, so later diags pointed at the decl instead of the file where the code actually came from.

this keeps the wrapper synthetic, but puts the loaded file body back into the token stream with its own spans. it also skips the attr currently being expanded, so we don't replay it by accident. added a small ui repro too, plus the older span-debug output changed because those spans are now real.

imo this is the least annoying fix for now. fwiw, it keeps the old fake-wrapper path instead of trying to make full token capture for file mods happen in this PR.
2026-07-20 18:06:01 +02:00
Nicholas Nethercote
693756d643 Rename push_tree/push_stream
To clarify that they are for specific uses, and not intended as general
`TokenStream` builders.
2026-07-20 21:42:26 +10:00
Nicholas Nethercote
ea7bee7515 Change AttrItem::span from a method to a field
Currently the method computes the span from the path's span and the
args' span. That used to be fine but then `unsafe(..)`/`safe(..)` were
added without it being changed. So it doesn't account for those optional
keywords and parentheses. The only way to handle those reliably is with
a dedicated span for the entire `AttrItem`.

Consequences:
- `NormalAttr` grows from 72 bytes to 80. (It's the only AST node that
  contains an `AttrItem`.) Not ideal, but non-doc-comment attributes
  aren't that common (not compared to expressions, for example). Also,
  `NormalAttr` is always boxed and jemalloc will round up 72 byte
  allocations to 80 bytes anyway.
- In `lint-unsafe-code.rs` various spans now point to `unsafe(foo)`
  rather than just `foo`. (It makes sense to include the `unsafe` keyword
  in the complaint about unsafety.)
2026-07-20 15:10:16 +10:00
Yukang
4aae99010b make suggestion verbose 2026-07-19 10:57:26 +08:00
Yukang
a99f2dd07f Remove extra semicolons in parsing item lists 2026-07-19 10:43:39 +08:00
Dnreikronos
daff59158e Preserve file module spans for inner attributes 2026-07-18 09:04:06 -03:00
Nicholas Nethercote
17d526f1be Replace "early parsed" terminology
AST attributes use "early parsed"/"parsed" terminology to refer to the
`CfgTrace` and `CfgAttrTrace` attributes. I think this terminology is
meant to echo the terminology used for HIR attributes, i.e.
`hir::Attribute::{Parsed,Unparsed}`, probably because
`hir::Attribute::Parsed` is used for attributes that aren't stored in a
token-based form.

But this naming is misleading. "Early parsed" attributes aren't parsed
at all because they are inserted by the compiler and cannot be written
in source code. There are also two comments that claim that these
attributes are kept in parsed form "so they don't have to be reparsed
every time they're used, for performance", which is simply incorrect.

This commit renames these as "synthetic" attributes, which better
reflects their nature. The commit also fixes the incorrect comments.

Note that `is_parsed_attribute` is unchanged, because it refers to the
HIR attribute meaning. (And the removal of the synthetic attributes from
it in the previous commit is now more obviously correct.)
2026-07-17 08:59:25 +10:00
Nicholas Nethercote
1a8ecb0b5b Remove sym::cfg_trace and sym::cfg_attr_trace
This commit pushes further than the previous one, relying more heavily
on looking directly at `AttrKind::Parsed` to handle various cases.

Note that the removed clippy code was actually dead.
2026-07-17 08:59:25 +10:00
Nicholas Nethercote
b357c7c0ff Remove AttrItemKind
`AttrItem` currently contains an `AttrItemKind` which is either `Parsed`
or `Unparsed`. In the `Parsed` case the surrounding `AttrItem` is
basically fake, with a meaningless `unsafety` field, a synthetic
(non-ident) symbol, and an empty `tokens`.

This commit moves `Parsed` up one level to become a new variant of
`AttrKind`. It no longer needs to pretend to be an `AttrKind::Normal`,
and makes a number of things simpler:
- No `unparsed_ref` calls needed.
- Replaces the `attr_into_trace` and `Attribute::replace_args` mess with
  the much simpler `convert_normal_to_parsed`.

The commit converts a numberof non-exhaustive `AttrKind` matches (those
that don't involve matching a single attribute) to exhaustive, for
future-proofing. It uses `unreachable!` on all the `AttrKind::Parsed`
cases that aren't hit by the full test suite.
2026-07-17 08:59:24 +10:00
bors
2d0faf8fea Auto merge of #158323 - Unique-Usman:ua/invalid_source_file, r=nnethercote
rustc: improve diagnostics for file-open failures



Fix: [#156070](https://github.com/rust-lang/rust/issues/156070)
2026-07-16 02:08:28 +00:00
Usman Akinyemi
dbee3dd369 rustc: improve diagnostics for file-open failures
Emit more targeted diagnostics when an input file cannot be opened,
including dedicated messages for common error kinds and typo
suggestions for missing files. Add run-make tests covering NotFound,
PermissionDenied, IsADirectory, and non-existent directory cases.

Signed-off-by: Usman Akinyemi <usmanakinyemi202@gmail.com>
2026-07-15 23:25:52 +05:30
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
Jonathan Brouwer
989749518a Rollup merge of #159160 - nnethercote:HasAttrs-HasTokens, r=petrochenkov
Make `HasTokens` a sub-trait of `HasAttrs`.

Because that's what it is: any type with tokens must also have attrs. This simplifies some trait bounds.

r? @petrochenkov
2026-07-14 16:49:03 +02: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
Jonathan Brouwer
490097dc9a Rollup merge of #157706 - jdonszelmann:deny-todo, r=wafflelapkin
Deny `todo!()` in tidy

Implements https://github.com/rust-lang/compiler-team/issues/999

Part of https://github.com/rust-lang/rust/issues/158173
2026-07-13 20:17:57 +02:00
Jacob Pratt
7cc01ce349 Rollup merge of #159208 - nnethercote:fix-attr_on_non_tail_expr-typo, r=chenyukang
Fix `attr_on_non_tail_expr` typo

This PR fixes a minor diagnostics bug from rust-lang/rust#117988. Details in individual commits.

r? @estebank
2026-07-13 05:52:54 -04:00
Jana Dönszelmann
bdc1397933 bless tests 2026-07-13 11:02:01 +02:00
Jana Dönszelmann
25745ee1f1 fix all tidy annotations 2026-07-13 10:54:53 +02:00