1360 Commits

Author SHA1 Message Date
Stuart Cook
57e8a5317a
Rollup merge of #146090 - Kobzol:invisible-origin-eq, r=petrochenkov
Derive `PartialEq` for `InvisibleOrigin`

For https://github.com/rust-lang/rust/pull/145354, we need `PartialEq` for `TokenStream` to "just work". However, due to the special comparison implementation that was used for `InvisibleOrigin`, this wasn't the case.

So I derived `PartialEq` for `InvisibleOrigin`, and used the previous special comparison logic only on the single place where it was actually required.

r? `````````@petrochenkov`````````
2025-09-04 10:01:58 +10:00
Guillaume Gomez
7b2bfa361e
Rollup merge of #146064 - jullanggit:patch-1, r=fmease
Add compiler error when trying to use concat metavar expr in repetitions

## Disclaimer
This is my first PR to rust, so if I missed/could improve something about this PR, please excuse and tell me!
## The improvement
The [metavar_expr_concat feature](https://github.com/rust-lang/rust/issues/124225) currently does not seem to support nested repetitions, and throws an ICE without much explanation if the relevant code path is hit.
This PR adds a draft compiler error that attempts to explain the issue. I am not 100% sure what all the ways of triggering this error are, so the message is currently pretty generic, please do correct me if there's something wrong with it or it could be improved.

Thank you for you time!

Fixes rust-lang/rust#140479.
2025-09-02 17:08:54 +02:00
Jakub Beránek
a8537ab84b
Remove special implementation of PartialEq for InvisibleOrigin outside macro matching 2025-09-02 16:38:31 +02:00
Nicholas Nethercote
301655eafe Revert introduction of [workspace.dependencies].
This was done in #145740 and #145947. It is causing problems for people
using r-a on anything that uses the rustc-dev rustup package, e.g. Miri,
clippy.

This repository has lots of submodules and subtrees and various
different projects are carved out of pieces of it. It seems like
`[workspace.dependencies]` will just be more trouble than it's worth.
2025-09-02 19:12:54 +10:00
jullang
f6e7c81061 Add compiler error when trying to use concat metavar expr in repetitions
Replace unimplemented()! with a more helpful compiler error.
2025-09-01 08:27:30 +02:00
Nicholas Nethercote
c50d2cc807 Add tracing to [workspace.dependencies]. 2025-08-27 14:21:19 +10:00
Nicholas Nethercote
777e2d6a2a Add thin-vec to newly added [workspace.dependencies]. 2025-08-27 13:59:32 +10:00
Jonathan Brouwer
3851e6c7b6
Warn on macro calls for attributes that had this behaviour previously 2025-08-24 14:29:03 +02:00
Jacob Pratt
d3c9908a8a
Rollup merge of #145747 - joshtriplett:builtin-diag-dyn, r=jdonszelmann
Refactor lint buffering to avoid requiring a giant enum

Lint buffering currently relies on a giant enum `BuiltinLintDiag` containing all the lints that might potentially get buffered. In addition to being an unwieldy enum in a central crate, this also makes `rustc_lint_defs` a build bottleneck: it depends on various types from various crates (with a steady pressure to add more), and many crates depend on it.

Having all of these variants in a separate crate also prevents detecting when a variant becomes unused, which we can do with a dedicated type defined and used in the same crate.

Refactor this to use a dyn trait, to allow using `LintDiagnostic` types directly.

Because the existing `BuiltinLintDiag` requires some additional types in order to decorate some variants, which are only available later in `rustc_lint`, use an enum `DecorateDiagCompat` to handle both the `dyn LintDiagnostic` case and the `BuiltinLintDiag` case.

---

With the infrastructure in place, use it to migrate three of the enum variants to use `LintDiagnostic` directly, as a proof of concept and to demonstrate that the net result is a reduction in code size and a removal of a boilerplate-heavy layer of indirection.

Also remove an unused `BuiltinLintDiag` variant.
2025-08-22 22:00:59 -04:00
Jacob Pratt
2dbd411d22
Rollup merge of #144897 - fee1-dead-contrib:raw_lifetimes_printing, r=fmease
print raw lifetime idents with r#

This replaces rust-lang/rust#143185 and fixes rust-lang/rust#143150

cc ``@fmease``
2025-08-22 22:00:47 -04:00
bors
f5703d5dd3 Auto merge of #144689 - JonathanBrouwer:share_parse_path, r=jdonszelmann
Rewrite the new attribute argument parser

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

This rewrites the parser, should improve performance and maintainability.
This can be reviewed commit by commit
2025-08-22 10:27:12 +00:00
Josh Triplett
c99320156d Refactor lint buffering to avoid requiring a giant enum
Lint buffering currently relies on a giant enum `BuiltinLintDiag`
containing all the lints that might potentially get buffered. In
addition to being an unwieldy enum in a central crate, this also makes
`rustc_lint_defs` a build bottleneck: it depends on various types from
various crates (with a steady pressure to add more), and many crates
depend on it.

Having all of these variants in a separate crate also prevents detecting
when a variant becomes unused, which we can do with a dedicated type
defined and used in the same crate.

Refactor this to use a dyn trait, to allow using `LintDiagnostic` types
directly.

This requires boxing, but all of this is already on the slow path
(emitting an error).

Because the existing `BuiltinLintDiag` requires some additional types in
order to decorate some variants, which are only available later in
`rustc_lint`, use an enum `DecorateDiagCompat` to handle both the `dyn
LintDiagnostic` case and the `BuiltinLintDiag` case.
2025-08-22 01:59:56 -07:00
Jonathan Brouwer
17e34f6b24
Use the new attribute parser throughout the codebase 2025-08-22 08:58:45 +02:00
Jonathan Brouwer
21d3189779
Move validate_attr to rustc_attr_parsing 2025-08-22 08:37:19 +02:00
Deadbeef
1e5b5ba1e7 print raw lifetime idents with r# 2025-08-22 12:58:37 +08:00
bors
d127901d94 Auto merge of #145410 - cuviper:expand-stack, r=lqd
rustc_expand: ensure stack in `InvocationCollector::visit_expr`

In Fedora, when we built rustc with PGO on ppc64le, we started failing
the test `issue-74564-if-expr-stack-overflow.rs`. This could also be
reproduced on other arches by setting a smaller `RUST_MIN_STACK`, so
it's probably just unlucky that ppc64le PGO created a large stack frame
somewhere in this recursion path. Adding an `ensure_sufficient_stack`
solves the stack overflow.

Historically, that test and its fix were added in rust-lang/rust#74708,
which was also an `ensure_sufficient_stack` in this area of code at the
time. However, the refactor in rust-lang/rust#92573 basically left that
to the general `MutVisitor`, and then rust-lang/rust#142240 removed even
that ensure call. It may be luck that our tier-1 tested targets did not
regress the original issue across those refactors.
2025-08-22 04:05:26 +00:00
Jacob Pratt
25b81bf5ad
Rollup merge of #145590 - nnethercote:ModKind-Inline, r=petrochenkov
Prevent impossible combinations in `ast::ModKind`.

`ModKind::Loaded` has an `inline` field and a `had_parse_error` field. If the `inline` field is `Inline::Yes` then `had_parse_error` must be `Ok(())`.

This commit moves the `had_parse_error` field into the `Inline::No` variant. This makes it impossible to create the nonsensical combination of `inline == Inline::Yes` and `had_parse_error = Err(_)`.

r? ```@Urgau```
2025-08-21 01:12:19 -04:00
Nicholas Nethercote
bfd5d59f97 Prevent impossible combinations in ast::ModKind.
`ModKind::Loaded` has an `inline` field and a `had_parse_error` field.
If the `inline` field is `Inline::Yes` then `had_parse_error` must be
`Ok(())`.

This commit moves the `had_parse_error` field into the `Inline::No`
variant. This makes it impossible to create the nonsensical combination
of `inline == Inline::Yes` and `had_parse_error = Err(_)`.
2025-08-19 21:57:31 +10:00
许杰友 Jieyou Xu (Joe)
c1a1222ece
Rollup merge of #139345 - smoelius:into-iter-stability, r=lcnr
Extend `QueryStability` to handle `IntoIterator` implementations

This PR extends the `rustc::potential_query_instability` lint to check values passed as `IntoIterator` implementations.

Full disclosure: I want the lint to warn about this line (please see #138871 for why): aa8f0fd716/src/librustdoc/json/mod.rs (L261)

However, the lint warns about several other lines as well.

Final note: the functions `get_callee_generic_args_and_args` and `get_input_traits_and_projections` were copied directly from [Clippy's source code](4fd8c04da0/src/tools/clippy/clippy_lints/src/methods/unnecessary_to_owned.rs (L445-L496)).
2025-08-19 19:42:00 +08:00
Samuel Moelius
c3c2c23e0d Extend QueryStability to handle IntoIterator implementations
Fix adjacent code

Fix duplicate warning; merge test into `tests/ui-fulldeps/internal-lints`

Use `rustc_middle::ty::FnSig::inputs`

Address two review comments

- https://github.com/rust-lang/rust/pull/139345#discussion_r2109006991
- https://github.com/rust-lang/rust/pull/139345#discussion_r2109058588

Use `Instance::try_resolve`

Import `rustc_middle::ty::Ty` as `Ty` rather than `MiddleTy`

Simplify predicate handling

Add more `#[allow(rustc::potential_query_instability)]` following rebase

Remove two `#[allow(rustc::potential_query_instability)]` following rebase

Address review comment

Update compiler/rustc_lint/src/internal.rs

Co-authored-by: lcnr <rust@lcnr.de>
2025-08-15 12:10:54 -04:00
Josh Stone
f68bcb376d rustc_expand: ensure stack in InvocationCollector::visit_expr
In Fedora, when we built rustc with PGO on ppc64le, we started failing
the test `issue-74564-if-expr-stack-overflow.rs`. This could also be
reproduced on other arches by setting a smaller `RUST_MIN_STACK`, so
it's probably just unlucky that ppc64le PGO created a large stack frame
somewhere in this recursion path. Adding an `ensure_sufficient_stack`
solves the stack overflow.

Historically, that test and its fix were added in rust-lang/rust#74708,
which was also an `ensure_sufficient_stack` in this area of code at the
time. However, the refactor in rust-lang/rust#92573 basically left that
to the general `MutVisitor`, and then rust-lang/rust#142240 removed even
that ensure call. It may be luck that our tier-1 tested targets did not
regress the original issue across those refactors.
2025-08-14 16:02:31 -07:00
Josh Triplett
354fcf2b52 mbe: Handle applying macro_rules derives
Add infrastructure to apply a derive macro to arguments, consuming and
returning a `TokenTree` only.

Handle `SyntaxExtensionKind::MacroRules` when expanding a derive, if the
macro's kinds support derive.

Add tests covering various cases of `macro_rules` derives.

Note that due to a pre-existing FIXME in `expand.rs`, derives are
re-queued and some errors get emitted twice. Duplicate diagnostic
suppression makes them not visible, but the FIXME should still get
fixed.
2025-08-14 14:23:05 -07:00
Josh Triplett
8fb98ef368 mbe: Parse macro derive rules
This handles various kinds of errors, but does not allow applying the
derive yet.

This adds the feature gate `macro_derive`.
2025-08-14 13:53:57 -07:00
Guillaume Gomez
bd6fb63596
Rollup merge of #145153 - joshtriplett:macro-kinds-plural, r=petrochenkov
Handle macros with multiple kinds, and improve errors

(I recommend reviewing this commit-by-commit.)

Switch to a bitflags `MacroKinds` to support macros with more than one kind

Review everything that uses `MacroKind`, and switch anything that could refer to more than one kind to use `MacroKinds`.

Add a new `SyntaxExtensionKind::MacroRules` for `macro_rules!` macros, using the concrete `MacroRulesMacroExpander` type, and have it track which kinds it can handle. Eliminate the separate optional `attr_ext`, now that a `SyntaxExtension` can handle multiple macro kinds.

This also avoids the need to downcast when calling methods on `MacroRulesMacroExpander`, such as `get_unused_rule`.

Integrate macro kind checking into name resolution's `sub_namespace_match`, so that we only find a macro if it's the right type, and eliminate the special-case hack for attributes.

This allows detecting and report macro kind mismatches early, and more precisely, improving various error messages. In particular, this eliminates the case in `failed_to_match_macro` to check for a function-like invocation of a macro with no function-like rules.

Instead, macro kind mismatches now result in an unresolved macro, and we detect this case in `unresolved_macro_suggestions`, which now carefully distinguishes between a kind mismatch and other errors.

This also handles cases of forward-referenced attributes and cyclic attributes.

----

In this PR, I've minimally fixed up `rustdoc` so that it compiles and passes tests. This is just the minimal necessary fixes to handle the switch to `MacroKinds`, and it only works for macros that don't actually have multiple kinds. This will panic (with a `todo!`) if it encounters a macro with multiple kinds.

rustdoc needs further fixes to handle macros with multiple kinds, and to handle attributes and derive macros that aren't proc macros. I'd appreciate some help from a rustdoc expert on that.

----

r? ````````@petrochenkov````````
2025-08-13 18:43:01 +02:00
Jakub Beránek
c2bc9265f0
Rollup merge of #145274 - compiler-errors:unused-must-use, r=fmease
Remove unused `#[must_use]`

Self-explanatory

Fixes https://github.com/rust-lang/rust/issues/145257
2025-08-13 07:03:49 +02:00
Michael Goulet
2c0409c7e8 Remove unused must_use 2025-08-12 19:54:57 +00:00
Josh Triplett
b74e1cb769 mbe: Rename macro parsing state names to use Bang instead of Not
The use of `Not` to describe the `!` in `macro_rules!` reads
confusingly, and also results in search collisions with the diagnostic
structure `MacroRulesNot` elsewhere in the compiler. Rename it to use
the more conventional `Bang` for `!`.
2025-08-12 09:24:45 -07:00
Josh Triplett
ba231db3f3 Detect and report macro kind mismatches early, and more precisely
This eliminates the case in `failed_to_match_macro` to check for a
function-like invocation of a macro with no function-like rules.

Instead, macro kind mismatches now result in an unresolved macro, and we
detect this case in `unresolved_macro_suggestions`, which now carefully
distinguishes between a kind mismatch and other errors.

This also handles cases of forward-referenced attributes and cyclic
attributes.

Expand test coverage to include all of these cases.
2025-08-12 09:24:45 -07:00
Josh Triplett
d7b30bef7e Expand documentation of GlobDelegation
I discovered this via research through the git log, and I want to leave
additional guidance for future macro spelunkers.
2025-08-12 09:24:45 -07:00
Josh Triplett
0b855bcdc9 Switch to a bitflags MacroKinds to support macros with more than one kind
Review everything that uses `MacroKind`, and switch anything that could
refer to more than one kind to use `MacroKinds`.

Add a new `SyntaxExtensionKind::MacroRules` for `macro_rules!` macros,
using the concrete `MacroRulesMacroExpander` type, and have it track
which kinds it can handle. Eliminate the separate optional `attr_ext`,
now that a `SyntaxExtension` can handle multiple macro kinds.

This also avoids the need to downcast when calling methods on
`MacroRulesMacroExpander`, such as `get_unused_rule`.

Integrate macro kind checking into name resolution's
`sub_namespace_match`, so that we only find a macro if it's the right
type, and eliminate the special-case hack for attributes.
2025-08-12 09:24:45 -07:00
Sasha Pourcelot
6603fe1caa Port #[allow_internal_unsafe] to the new attribute system (attempt 2) 2025-08-11 15:01:52 +02:00
Jacob Pratt
a164527df7
Rollup merge of #145200 - joshtriplett:mbe-typo-fix, r=lqd
mbe: Fix typo in attribute tracing
2025-08-10 15:43:55 -04:00
Josh Triplett
caa6d3615b mbe: Fix typo in attribute tracing 2025-08-09 23:01:08 -07:00
Deadbeef
ad1113f87e remove P 2025-08-09 15:47:01 +08:00
bors
4c7749e8c8 Auto merge of #145086 - jdonszelmann:revert-allow-internal-unsafe, r=Kobzol
Revert "Port `#[allow_internal_unsafe]` to the new attribute system"

This reverts commit 4f7a6ace9e2f2192af7b5d32f4b1664189e0e143 (PR: https://github.com/rust-lang/rust/pull/144857)

r? `@Kobzol`
cc: `@scrabsha`

clean revert it seems :3
2025-08-09 00:04:35 +00:00
Trevor Gross
804d1a194e
Rollup merge of #144579 - joshtriplett:mbe-attr, r=petrochenkov
Implement declarative (`macro_rules!`) attribute macros (RFC 3697)

This implements [RFC 3697](https://github.com/rust-lang/rust/issues/143547), "Declarative (`macro_rules!`) attribute macros".

I would suggest reading this commit-by-commit. This first introduces the
feature gate, then adds parsing for attribute rules (doing nothing with them),
then adds the ability to look up and apply `macro_rules!` attributes by path,
then adds support for local attributes, then adds a test, and finally makes
various improvements to errors.
2025-08-08 14:22:45 -05:00
Josh Triplett
34be8abb70 mbe: Handle applying attribute rules with paths
Add infrastructure to apply an attribute macro given argument tokens and
body tokens.

Teach the resolver to consider `macro_rules` macros when looking for an
attribute via a path.

This does not yet handle local `macro_rules` attributes.
2025-08-08 11:01:12 -07:00
Josh Triplett
0cc0b11cce mbe: Emit an error if a macro call has no function-like rules
Add a FIXME for moving this error earlier.
2025-08-08 11:01:12 -07:00
Josh Triplett
bad0d45b2d mbe: Parse macro attribute rules
This handles various kinds of errors, but does not allow applying the
attributes yet.

This adds the feature gate `macro_attr`.
2025-08-08 11:00:54 -07:00
Jana Dönszelmann
866bc26475
Revert "Port #[allow_internal_unsafe] to the new attribute system"
This reverts commit 4f7a6ace9e2f2192af7b5d32f4b1664189e0e143.
2025-08-08 11:54:20 +02:00
Sasha Pourcelot
4f7a6ace9e Port #[allow_internal_unsafe] to the new attribute system 2025-08-07 15:47:21 +02:00
bors
889701db1f Auto merge of #129183 - estebank:cfg-visitor, r=davidtwco
Detect more `cfg`d out items in resolution errors

Use a visitor to collect *all* items (including those nested) that were stripped behind a `cfg` condition.

```
error[E0425]: cannot find function `f` in this scope
  --> $DIR/nested-cfg-attrs.rs:4:13
   |
LL | fn main() { f() }
   |             ^ not found in this scope
   |
note: found an item that was configured out
  --> $DIR/nested-cfg-attrs.rs:2:4
   |
LL | fn f() {}
   |    ^
note: the item is gated here
  --> $DIR/nested-cfg-attrs.rs:1:35
   |
LL | #[cfg_attr(all(), cfg_attr(all(), cfg(FALSE)))]
   |                                   ^^^^^^^^^^
```
2025-08-02 05:09:31 +00:00
Esteban Küber
77f75f91c5 tiny cleanup 2025-08-01 22:11:45 +00:00
Esteban Küber
4ba4559a9d remove recursive search for items 2025-08-01 22:02:52 +00:00
Esteban Küber
5210c501bc Limit how deep we visit items to find cfg'd out names 2025-08-01 21:51:05 +00:00
Esteban Küber
adcda6ca9a Detect more cfgd out items in resolution errors
Use a visitor to collect *all* items (including those nested) that were stripped behind a `cfg` condition.

```
error[E0425]: cannot find function `f` in this scope
  --> $DIR/nested-cfg-attrs.rs:4:13
   |
LL | fn main() { f() }
   |             ^ not found in this scope
   |
note: found an item that was configured out
  --> $DIR/nested-cfg-attrs.rs:2:4
   |
LL | fn f() {}
   |    ^
note: the item is gated here
  --> $DIR/nested-cfg-attrs.rs:1:35
   |
LL | #[cfg_attr(all(), cfg_attr(all(), cfg(FALSE)))]
   |                                   ^^^^^^^^^^
```
2025-08-01 21:50:36 +00:00
Jana Dönszelmann
e1d3ad89c7
remove rustc_attr_data_structures 2025-07-31 14:19:27 +02:00
Matthias Krüger
6bd327374a
Rollup merge of #143607 - JonathanBrouwer:proc_macro_attrs, r=jdonszelmann,traviscross
Port the proc macro attributes to the new attribute parsing infrastructure

Ports `#[proc_macro]`, `#[proc_macro_attribute]`, `#[proc_macro_derive]` and `#[rustc_builtin_macro]` to the new attribute parsing infrastructure for https://github.com/rust-lang/rust/issues/131229#issuecomment-2971351163

I've split this PR into commits for reviewability, and left some comments to clarify things
I did 4 related attributes in one PR because they share a lot of their code and logic, and doing them separately is kind of annoying as I need to leave both the old and new parsing in place then.

r? ``@oli-obk``
cc ``@jdonszelmann``
2025-07-28 01:16:38 +02:00
LorrensP-2158466
451a93e2b8 split up define into define_extern and define_local 2025-07-27 12:17:23 +02:00
Jonathan Brouwer
e64f75b3d2
Remove now un-used code
Signed-off-by: Jonathan Brouwer <jonathantbrouwer@gmail.com>
2025-07-26 20:26:16 +02:00