Commit Graph

1696 Commits

Author SHA1 Message Date
Jacob Pratt
f5196ef8be Rollup merge of #161003 - GuillaumeGomez:doc-attr-macro-invoc, r=mejrs
Also warn if an invalid `doc` attribute is used on a macro invocation

Fixes rust-lang/rust#160880.

So about this: I think the error message is good enough, but I can specialize it in case it's not a doc comment but a doc attribute.

Also: currently the ignored attributes are not kept in the AST, so when we reach `rustc_attr_parsing`, the attribute is not present and there won't trigger an error. I don't think it's worth changing this logic but in case someone thinks otherwise, it's mentioned. :)

r? @JonathanBrouwer
2026-08-12 20:17:37 -04:00
Jacob Pratt
472478d224 Rollup merge of #160990 - GuillaumeGomez:old-cfg-dead-code, r=JonathanBrouwer
Remove old cfg parser which is now dead code

Since rustdoc was fully migrated to the new `cfg` parser, this code can be removed.

r? @JonathanBrouwer
2026-08-12 20:17:34 -04:00
Guillaume Gomez
a09d72a3bf Also warn if an invalid doc attribute is used on a macro invocation 2026-08-12 21:38:13 +02:00
Guillaume Gomez
2982507037 Remove old cfg parser which is now dead code 2026-08-12 16:30:57 +02:00
mejrs
b9e9138428 move resolve_path to Session inherent method 2026-08-12 12:59:52 +02:00
mejrs
8c85df353e rustc_expand: depend more on rustc_attr_ir 2026-08-10 13:08:08 +02:00
Chris Denton
4641990929 Remove ensure_sufficient_stack 2026-08-09 19:58:29 +00:00
Vadim Petrochenkov
f639348a1b expand: Change feature gate wording for feature(proc_macro_hygiene) 2026-08-06 14:31:49 +03:00
Vadim Petrochenkov
86b915f4dd expand: Feature gate AST-based attribute macros on expressions and non-item statements 2026-08-06 14:12:56 +03:00
Jonathan Brouwer
f056d893a6 Rollup merge of #158110 - pbkx:issue-147135-macro-attr-feature-gate-span, r=joshtriplett
fix macro attribute feature-gate span

Fixes rust-lang/rust#147135.

Issue rust-lang/rust#147135 reports a misleading feature-gate diagnostic for cross-crate `macro_rules!` macro attributes. This misleading  feature-gate diagnostic causes rustc to point at the macro definition instead of user’s attribute call site.

My PR makes sure externally defined `macro_rules!` attributes are gated at the invocation span. Regression test verifies that the primary error points at the downstream `#[foo::identity]` attribute use
2026-08-04 18:35:32 +02:00
bjorn3
1a3d919258 Deny multiple EII impls on a single item
This allows implementing EIIs that don't have a default impl without the
involvement of symbol aliases by simply changing the mangled symbol name
of the implementation, which would make them trivially compatible with
all backends and targets. This change will be left to a followup PR.
2026-08-03 11:04:23 +02:00
mejrs
f5b4f8d209 Prefix rustc_ast::token types with tk:: 2026-07-31 14:14:45 +02:00
Jonathan Brouwer
bca856424e Rollup merge of #158038 - nbdd0121:register_tool, r=mejrs
Split register_tool into register_attribute_tool and register_lint_tool

Tracking issue: rust-lang/rust#66079

This implements the attribute parsing part of RFC 3808. The resolution part is yet to be implemented.

cc @jyn514
2026-07-28 21:07:45 +02:00
Gary Guo
75999c498f Split register_tool into register_attribute_tool and register_lint_tool
This splits the functionality to two parts per RFC. The `#![register_tool]`
attribute registers the tool in both ways.

Semantics remain unchanged and will be adjusted in later commit.
2026-07-27 19:06:33 +01: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
Gary Guo
747ce1a484 Make AttributeParser::parse_limited accept filters instead of symbol
The existing API is preserved as `parse_limited_sym` as a simple helper.

Similarly, `parse_limited_should_emit` is preserved as
`parse_limited_sym_should_emit`. I've noted that all users targets crate,
so the `target_node_id` and `target` parameters are removed from it.
2026-07-27 17:42:20 +01:00
panstromek
02a54aa3ed update thinvec to latest version 2026-07-27 10:33:18 +02: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
2ff64919d3 Rollup merge of #159700 - joshtriplett:semicolon-in-expressions-from-non-local-macros, r=petrochenkov
Split non-local `semicolon_in_expressions_from_macros` into a separate lint

The new handling for non-local macros in
`semicolon_in_expressions_from_macros` produces new deny-by-default
lints on various existing crates, which have not previously received
warnings because prior Rust versions suppressed them. Split those out
into a new `semicolon_in_expressions_from_non_local_macros` lint, and
make that one warn-by-default (and FCW warn-in-deps), rather than
deny-by-default, to give the ecosystem time to adapt.

This reintroduces the `is_local` tracking from commit
9192337331.
2026-07-26 00:21:44 -04:00
Vadim Petrochenkov
75529d4b8e rustc_parse: Stop returning Option from statement parsing 2026-07-24 18:35:36 +03:00
Jacob Pratt
59906a3aa0 Rollup merge of #159755 - nnethercote:attribute-error-messages, r=estebank
Improve consistency of attribute error messages

This PR aims to improve how attributes are referred to in error messages. A lot of test outputs will change, so there will be follow-ups, but this PR has enough changes to cover a lot of cases and give a good feel for where things are headed.

r? @estebank
2026-07-24 01:59:23 -04:00
Jacob Pratt
7a2710b9d4 Rollup merge of #159722 - GuillaumeGomez:cfg_attr-doc_cfg, r=petrochenkov,mejrs,Urgau
[rustdoc] Retrieve `cfg_attr` information for derived impls for `doc_cfg` feature

Fixes https://github.com/rust-lang/rust/issues/103300.

After long last, this PR finally allows the derive proc-macro `cfg_attr` cfg predicates information to be kept so rustdoc can use it for its `doc_cfg` feature (cc https://github.com/rust-lang/rust/issues/43781).

It works as follows: for `impl` generated by macro expansion, we then look for the attributes on the type for which the `impl` block is implemented. Then, to know which `cfg_attr` attribute we want to look at, we use `expn_that_defined` on the impl's `DefId` which returns the `Span` where the macro was expanded. If it's part of a `cfg_attr`, then the `cfg_attr`'s `Span` will contain the derive's, so from then, we just need to add the `cfg` information.

This PR also adds the cfg predicates into the `AttributeKind::CfgAttrTrace` variant so it can be reused by rustdoc (hence the `compiler/*` changes).

Thanks a lot @petrochenkov for the pointers here!

r? @petrochenkov
2026-07-24 01:59:21 -04:00
Nicholas Nethercote
d36d39f202 Improve consistency of some cfg_attr attribute error messages
Similar to the previous two commits.
2026-07-24 12:58:31 +10:00
Jacob Pratt
2c7069ce5b Rollup merge of #159773 - panstromek:remove-p-workaround, r=fee1-dead
Remove outdated workaround for `P` type

P is long gone, so this workaround is no longer necessary.

The workaround took attrs out of node, inspected and possibly modified it and then put it back at the end of each branch of the following match.
2026-07-23 19:18:48 -04:00
panstromek
a6d0a76a5b Remove deref outdated workaround for P type
P is long gone, so this workaround is no longer necessary
2026-07-23 14:57:39 +02:00
panstromek
283ec3aa54 Make FieldDef smaller 2026-07-23 14:43:52 +02:00
Guillaume Gomez
7e294a5206 Fix tests/ui/rust-2018/removing-extern-crate-malformed-cfg.rs test 2026-07-23 12:06:42 +02:00
Guillaume Gomez
0eb2348e8b Keep cfg predicate in CfgAttrTrace so it can be used by rustdoc 2026-07-22 23:50:54 +02:00
Jonathan Brouwer
92190394b9 Rollup merge of #159659 - mejrs:limit, r=petrochenkov
Move `Limit` out of `rustc_hir`

It feels rather out of place in `rustc_hir`, so this PR moves it to `rustc_data_structures`. Part of hopefully moving attributes from `rustc_hir` to their own crate.

~~This also lets `rustc_type_ir` use it~~ actually rustc_type_ir needs to compile on stable and `rustc_data_structures` uses like 30 features 😢
2026-07-22 18:02:21 +02:00
khyperia
e895ed8c90 unify the AST repr of type const and const RHS 2026-07-22 09:22:45 +02:00
Josh Triplett
ebbdcf083e Split non-local semicolon_in_expressions_from_macros into a separate lint
The new handling for non-local macros in
`semicolon_in_expressions_from_macros` produces new deny-by-default
lints on various existing crates, which have not previously received
warnings because prior Rust versions suppressed them. Split those out
into a new `semicolon_in_expressions_from_non_local_macros` lint, and
make that one warn-by-default (and FCW warn-in-deps), rather than
deny-by-default, to give the ecosystem time to adapt.

This reintroduces the `is_local` tracking from commit
9192337331.
2026-07-21 23:36:14 -07:00
mejrs
ac91844c53 Fixup Limit imports 2026-07-21 15:26:08 +02: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
Oli Scherer
4e2ebd6235 Remove some dead code 2026-07-21 11:34:36 +02:00
Nicholas Nethercote
c266b1cc16 Fix an indefinite article in an error message
No matter how you say `#![cfg_attr]` -- "hash...", "pound...",
"cfg_attr..." -- it doesn't start with a vowel sound and therefore
should be preceded with "a" rather than "an".
2026-07-21 16:16:09 +10:00
Nicholas Nethercote
7396a594f7 Augment comment on Annotatable
With some useful information.
2026-07-21 16:16:09 +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
203ec33699 Avoid unnecessary push_tree/push_stream usage
They are meant to be specialized methods for particular cases, not
general-purpose construction methods. They can be replaced with vanilla
`Vec<TokenTree>` construction, which this commit does.
2026-07-20 21:42:12 +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
Dnreikronos
daff59158e Preserve file module spans for inner attributes 2026-07-18 09:04:06 -03:00
Nicholas Nethercote
fa56a01dad Address review comments. 2026-07-17 08:59:25 +10: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
Jonathan Brouwer
ac05d382c9 Rollup merge of #159222 - joshtriplett:main, r=petrochenkov
semicolon_in_expressions_from_macros: Lint on non-local macros too

The `semicolon_in_expressions_from_macros` lint previously suppressed
warnings about non-local macros. This masks a lint that will
subsequently become a hard error.

Stop suppressing it, dropping the `is_local` flag and check.

Fix the test for this case to now expect the error. Drop a separate test
that depended on the lack of error (and that specifically notes it
should be dropped once the lint becomes a hard error).
2026-07-15 16:17:09 +02:00
Jonathan Brouwer
8fdc29c9ea Rollup merge of #159197 - camsteffen:more-modid, r=petrochenkov
Rename ModDefId to ModId and use more

Addresses some FIXMEs.

The `ModDefId` was used pretty sparingly, and honestly I wondered if it should exist at all. After making these changes, I think the type is worth it. Most notably it is now used in `Visibility::Restricted`.

I first renamed `ModDefId` to `ModId` since I think the former is over-specific, and also this is more in line with `BodyId` or `OwnerId`.

I'll wait for initial feedback before fixing rustdoc/clippy.
2026-07-15 16:17:07 +02:00
Jacob Pratt
2d4ae662df Rollup merge of #158608 - mejrs:opaque, r=estebank
Implement `#[diagnostic::opaque]` attribute to hide backtraces of macros.

r? @estebank

There are some more places where we can use this, for example 345632878c/compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs (L623-L639) but those turned out to be a little complicated so are left for followup prs.

Tracking issue: rust-lang/rust#158813
2026-07-14 23:23:07 -04:00