2968 Commits

Author SHA1 Message Date
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
Nicholas Nethercote 8955385155 Fix typo in attr_on_non_tail_expr
The line `&& segment.ident.name == sym::cfg` duplicates a line from just
a few lines above in the same if-let chain. It's clear from context that
the `segment` is a copy/paste error and should be `next_segment`. This
fixes the erroneous suggestion given for
`multiple-tail-expr-behind-cfg.rs`.
2026-07-13 16:31:02 +10:00
bors 77cf889bc1 Auto merge of #158974 - bal-e:macro-parsing-dfs-prep, r=nnethercote
Preparatory changes for macro parsing BFS->DFS



This PR contains some miscellaneous commits I accumulated while working on the BFS->DFS change. Their goal is to simplify the code and clarifying existing behavior. It is a conceptual follow-up to rust-lang/rust#158577. High-level overview:

- Adds context about the current match arm to `Tracker` through the new `Tracker::prepare()`, so that the `WhichMatcher` parameter is available implicitly. In a later PR, this will be used to reference `MatcherLoc`s by index.

- Reformulates matching failures to work more like ambiguity errors wrt. `Tracker`; `Tracker::build_failure()` (which would build a failure consumed by `Tracker::after_arm()`) becomes `Tracker::failure()` which eagerly processes the error. This removes the need for `ParseResult::Failure` to store any data at all. This relies on the match arm context provided by `Tracker::prepare()`.

- There is a subtle edge case involving `token::Eof` and non-terminal parsing; `Parser::nonterminal_may_begin_with()` would sometimes return `true` for `token::Eof`, even though the non-terminal parse would never be attempted. `TtParser` did not check `bb_mps` when handling `token::Eof`, so non-terminal parses at EOF were being silently dropped. I changed `Parser::nonterminal_may_begin_with()` to always return `false` for `token::Eof`, making this behavior much more visible. I added a test case to make sure meta-variables return the same error uniformly when parsed against EOF.

Contains rust-lang/rust#158894, which should be merged soon (after which I'll rebase onto `main`).

Best reviewed commit-by-commit.

r? @nnethercote
2026-07-12 16:12:51 +00: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
Nicholas Nethercote 4d0b5936d4 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.
2026-07-12 10:57:34 +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
Nicholas Nethercote 351026d770 Remove AttrItem::tokens
The same basic idea from the previous commit applies: the tokens are
only sometimes needed and we can store them next to the `AttrItem`
(using `WithTokens`) instead of inside the `AttrItem`. The change is a
bit more complex because more than just `parse_nonterminal` is involved
(e.g. `parse_attr_item`, `parse_cfg_attr`, `expand_cfg_attr_item`).
2026-07-10 17:15:45 +10:00
Nicholas Nethercote 402500cffe Remove tokens field from some AST nodes
Specifically, from `Block`, `Pat`, `Path`, `Ty`, `Visibility`.

This field is usually `None`. It only gets filled in for paths that
might later be re-tokenized via `TokenStream::from_ast`, e.g. because of
macro or cfg use. All such cases go through a single place:
`parse_nonterminal`.

This commit changes `parse_nonterminal` to store the non-`None` lazy
attr token stream *next* to the node in `ParseNtResult` (using the new
`WithTokens` type) instead of *inside* the parsed node.

Along with some minor changes to the relevant `HasTokens` and `HasAttrs`
impls, this is enough for things to work. These AST nodes all shrink by
8 bytes, as do various other nodes that contain nodes of these types.

A guide to the changes:
- `compiler/rustc_ast/src/tokenstream.rs` has the new `WithTokens` type.
- `compiler/rustc_parse/src/parser/nonterminal.rs`,
  `compiler/rustc_expand/src/mbe/transcribe.rs` and
  `compiler/rustc_parse/src/parser/mod.rs` have minor changes due to the
  changes to `ParseNtResult`.
- `compiler/rustc_ast/src/ast_traits.rs` has the `HasTokens`/`HasAttrs`
  changes.
- Everything else is just mechanical changes for the removal of the
  `tokens` field.
2026-07-10 17:15:43 +10:00
Nicholas Nethercote 0a9728b2ad Remove unnecessary tokens fields from AST nodes
These ones are always `None` and never read.
2026-07-10 17:11:19 +10:00
arya dradjica e6d2b6da6f Disallow EOF in nonterminal_may_begin_with()
Until now, `:tt`, `:item`, and `:stmt` metavars could be
attempted against `token::Eof`. *HOWEVER*, when a `token::Eof` is
processed, `parse_tt_inner()` ignores `bb_mps`, not even checking
for ambiguity. It's as if those `bb_mps` never existed; as if
`nonterminal_may_begin_with()` returned `false` for `Eof`. This commit
makes that behavior more explicit.

This does not change user visible behavior.

`tests/ui/macros` pass.
2026-07-10 08:17:11 +02:00
Jacob Pratt 3dc22a32be Rollup merge of #158617 - khyperia:gca-syntax-flip, r=BoxyUwU
allow mGCA const arguments to fall back to anon consts

makes good progress on https://github.com/rust-lang/project-const-generics/issues/108

`min_generic_const_args` (mGCA) is all about "is this const lowered to a const item with a body, or, is it "directly" represented and visible in the type system?"

Here's various answers to the question of "can we represent this const arg directly?" - if we answer "no", then we fall back to an anon const (or error, sometimes, depending on context)

on **stable**, the full list of things can be directly represented is (... it's just the one thing)

- paths to generic const parameters

on `main`, under mGCA, **before** this PR:

- absolutely everything, if something cannot be represented directly, compiler error
- `const { }` gives you an escape hatch to go back to being an anon const

on macro**ful** gca, the directly represented things will be:

- paths to const parameters
- `direct_const_arg!` macro

on macro**less** gca, the directly represented things will be:

- paths to const parameters
- `direct_const_arg!` macro (not *particularly* useful under macro**less** gca)
- struct expressions, arrays, blah blah
- currently, paths to *anything*, even if that produces a compiler error later down the line (potential followup work here...)

this PR implements macro**less** gca, with the intent of a quick follow-up introducing a "macroless gca" feature and transitioning `#[feature(min_generic_const_args)]` to be macro**ful** gca

Also of note is that this PR removes the logic to skip generating "fake" DefIds for directly represented AnonConsts under mGCA, we now always generate a DefId when encountering an AnonConst. This lines up with stable, which has a fake AnonConst DefId for a directly represented plain path generic parameter (i.e. the bullet point under the "stable" entry above).

r? @BoxyUwU
2026-07-09 20:09:10 -04:00
Jacob Pratt ee0bd19fe0 Rollup merge of #158862 - chenyukang:yukang-fix-131084-self-receiver-suggestion, r=nnethercote
Fix the span for parameter suggestion

Fixes rust-lang/rust#131084

Previously, the recovered parameter span could point only at the inner identifier, so the suggestion was inserted after the leading `&`, produced a werid `&&self`.

The fix keeps the recovered parameter's full span on `Param.span`

r? @nnethercote

the second commit shows the diff of stderr.
2026-07-08 22:46:00 -04:00
khyperia 67986afdaf allow mGCA const arguments to fall back to anon consts 2026-07-08 07:46:21 +02:00
Jonathan Brouwer 73cd65d1b0 Rollup merge of #156016 - scrabsha:view-types/in-ast, r=oli-obk
view-types: store view types in the AST
2026-07-07 22:26:49 +02:00
bors c4af71034e Auto merge of #158847 - jhpratt:rollup-MkjhKjV, r=jhpratt
Rollup of 24 pull requests

Successful merges:

 - rust-lang/rust#158377 (add `-Zforce-intrinsic-fallback` flag)
 - rust-lang/rust#158642 (Clarify some interning details)
 - rust-lang/rust#158694 (Positive test for closures needing expectations)
 - rust-lang/rust#158743 (Look for cdb location in the registry first)
 - rust-lang/rust#158775 (bootstrap: only encode RUSTFLAGS when a flag contains a space)
 - rust-lang/rust#158782 (Add and use cfg(target_has_threads) to enforce no_thread impl usage)
 - rust-lang/rust#158785 (hook intrinsic-test into aarch64-gnu)
 - rust-lang/rust#158819 (Put `InhabitedPredicate::NotInModule` earlier in disjunction since it can be a lot faster)
 - rust-lang/rust#157734 (Stabilize `local_key_cell_update`)
 - rust-lang/rust#158183 (std: allocate less memory in `current_exe` for OpenBSD)
 - rust-lang/rust#158310 (Remove unexpected usage of Unambig in non-infer variants)
 - rust-lang/rust#158671 (Move tests batch 17)
 - rust-lang/rust#158730 (Update `FIXME(static_mut_refs)` comments)
 - rust-lang/rust#158752 (Reorganize `tests/ui/issues` [18/N])
 - rust-lang/rust#158755 (Use `ThinVec` more in the AST)
 - rust-lang/rust#158757 (Fix incorrect tracking issue for `read_le`/`read_be`)
 - rust-lang/rust#158765 (Fix ICE on non-ident path in `doc(auto_cfg values)`)
 - rust-lang/rust#158771 (library: expand HashSet::extract_if coverage)
 - rust-lang/rust#158772 (rustc-dev-guide subtree update)
 - rust-lang/rust#158776 (fix: emit diagnostic for AVR target without target-cpu)
 - rust-lang/rust#158786 (Add regression test for builtin attr macro values)
 - rust-lang/rust#158810 (Add supplementary information for get_unchecked(mut))
 - rust-lang/rust#158825 (Fix typo)
 - rust-lang/rust#158838 (tidy: Use `empty_alternate = true` for triagebot mention glob check)
2026-07-06 18:54:19 +00:00
Yukang d09f7ca759 correct the span for parameter suggestion 2026-07-06 22:55:05 +08:00
Nicholas Nethercote 3a0a97727d Use ThinVec in GenericBounds
Because we use `ThinVec` rather than `Vec` almost everywhere else in the
AST.
2026-07-04 09:55:14 +10: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