1519 Commits

Author SHA1 Message Date
Jonathan Brouwer 139bcba8f6 Rollup merge of #155822 - mejrs:default_fmt_args, r=JonathanBrouwer
Add default field values to diagnostic FormatArgs

This type has a decent amount of unused fields (and I am planning to add more).
2026-04-26 19:06:32 +02:00
Jonathan Brouwer 382ec81061 Rollup merge of #155757 - mejrs:ident_help, r=Kivooeo
macro_metavar_expr_concat: explain why idents are invalid

Recently I've been playing around with `macro_metavar_expr_concat` and in the process wasted more time than I'd have liked on debugging my dodgy idents. This should make that experience much nicer going forward.
2026-04-26 19:06:26 +02:00
mejrs 48fe89f994 add default field values to diagnostic FormatArgs 2026-04-26 14:23:53 +02:00
mejrs b6c6dd1fa0 macro_metavar_expr_concat: explain why idents are invalid 2026-04-25 19:56:05 +02:00
Jacob Pratt d3eddcff9e Rollup merge of #155643 - qaijuang:fix-macro-missing-fragment-dollar-suggestion, r=eholk
Improve suggestion for $-prefixed fragment specifiers

Fixes rust-lang/rust#155505
2026-04-25 01:21:50 -04:00
Qai Juang 2b46d9204a Improve suggestion for $-prefixed fragment specifiers 2026-04-24 14:59:01 -04:00
yukang 8d75f0cbfc add on_unmatch_args 2026-04-22 19:28:44 +08:00
Jonathan Brouwer c5f6afc3e8 Remove target arguments & features from parse_limited 2026-04-18 19:13:30 +02:00
Jonathan Brouwer 6236ddec5a Remove AttributeSafety from BUILTIN_ATTRIBUTES 2026-04-15 21:22:21 +02:00
Jonathan Brouwer 439410f68a Rollup merge of #155311 - cyrgani:expand-clean, r=Kivooeo,petrochenkov
various small `rustc_expand` cleanups

Each commit should be reviewable on its own.
2026-04-15 14:39:09 +02:00
cyrgani 2e3036dc54 merge if and while conditions 2026-04-14 20:45:04 +00:00
cyrgani 3dcd8ffe88 deduplicate internal feature check 2026-04-14 19:58:15 +00:00
cyrgani 6e458a5fca simplify make_stmts_default! 2026-04-14 19:48:17 +00:00
Jonathan Brouwer 49da27c336 Rollup merge of #154049 - petrochenkov:deleglobspan, r=jackh726
delegation: Track more precise spans for glob delegations

The last commit also fixes a macro hygiene issue with `self` in delegations found in https://github.com/rust-lang/rust/pull/154002.
2026-04-14 16:29:31 +02:00
cyrgani f4780504cd add and use PanicMessage::into_string 2026-04-14 11:16:33 +00:00
cyrgani a681f34d4b add helper for profiling calls 2026-04-14 11:16:33 +00:00
cyrgani e972232f27 reduce unnecessary allocations a bit 2026-04-14 11:16:32 +00:00
cyrgani 3fa55e721a clippy fixes 2026-04-14 11:16:32 +00:00
bors 12f35ad39e Auto merge of #155209 - JonathanBrouwer:attr_cleanup2, r=jdonszelmann
Post-attribute ports cleanup pt. 1 (again)

This is a re-implementation of most (but not all) of https://github.com/rust-lang/rust/pull/154808

The code is the same as in that PR, other than a few changes, I'll leave comments where I changed things.
I did re-implement most of the commits rather than cherry-picking, so it's probably good to check the entire diff regardless

r? @jdonszelmann
2026-04-14 08:59:40 +00:00
Vadim Petrochenkov 8114c5dc2e expand: More precise location for glob delegation
The span location of the last segment in the desugared path is inherited from the star symbol's span
2026-04-14 11:32:34 +03:00
Vadim Petrochenkov 7114404a26 ast: Preserve the star symbol span in glob delegation items 2026-04-14 11:32:33 +03:00
Jonathan Brouwer e391f7717f Remove emit_fatal_malformed_builtin_attribute 2026-04-13 20:51:21 +02:00
Jonathan Brouwer d2fa85cd4d Rollup merge of #154193 - JonathanBrouwer:external-static, r=jdonszelmann
Implement EII for statics

This PR implements EII for statics. I've tried to mirror the implementation for functions in a few places, this causes some duplicate code but I'm also not really sure whether there's a clean way to merge the implementations.

This does not implement defaults for static EIIs yet, I will do that in a followup PR
2026-04-13 20:19:56 +02:00
Jacob Pratt b2088a21df Rollup merge of #155120 - Urgau:check-cfg-allow-cfg_attr-mod, r=JonathanBrouwer
Use a linting node closer the parsing of `#[cfg_attr]`

The parsing of `#[cfg_attr]` unconditionally used the crate-root as a linting node to attach lints, but the `unexpected_cfgs` lint can fire when parsing the attribute and users expect to be able to allow it at places other than the crate-root.

Let's instead use the linting node id, which is unfortunately always the parent but that's better than the crate-root when you are in a sub-module.

The parsing of `#[cfg]` and other already use the expansion linting node id, so no change required there.

Related to https://github.com/rust-lang/rust/issues/155118
2026-04-11 04:50:17 -04:00
Jonathan Brouwer e756d1607d Add eii_impls argument to StaticItem 2026-04-11 10:17:07 +02:00
bors 02c7f9bec0 Auto merge of #155115 - JonathanBrouwer:rollup-RePrRPQ, r=JonathanBrouwer
Rollup of 6 pull requests

Successful merges:

 - rust-lang/rust#152901 (Introduce a `#[diagnostic::on_unknown]` attribute)
 - rust-lang/rust#155078 (Reject dangling attributes in where clauses)
 - rust-lang/rust#154449 (Invert dependency between `rustc_errors` and `rustc_abi`.)
 - rust-lang/rust#154646 (Add suggestion to `.to_owned()` used on `Cow` when borrowing)
 - rust-lang/rust#154993 (compiletest: pass -Zunstable-options for unpretty and no-codegen paths)
 - rust-lang/rust#155097 (Make `rustc_attr_parsing::SharedContext::emit_lint` take a `MultiSpan` instead of a `Span`)
2026-04-10 18:19:03 +00:00
Urgau f97143d951 Use a linting node closer the parsing of #[cfg_attr] 2026-04-10 20:04:37 +02:00
Jonathan Brouwer 45223feeba Rollup merge of #153796 - GokhanKabar:fix-ice-missing-tokens-eii-attr-expansion, r=jdonszelmann
Fix ICE when combining #[eii] with #[core::contracts::ensures]

Fixes rust-lang/rust#153745

Builtin attribute macros like #[eii] generate AST items programmatically without collected tokens. When another attribute macro was present on the same item, the compiler would panic in TokenStream::from_ast() trying to tokenize the generated items during subsequent attribute expansion.

Generate fake token streams (via pretty-print and re-parse) for Item and ForeignItem nodes that lack collected tokens, following the existing pattern used for Crate and out-of-line modules.
2026-04-10 15:33:12 +02:00
Georg Semmler 6e5fc9075c Introduce a #[diagnostic::on_unknown_item] attribute
This PR introduces a `#[diagnostic::on_unknown_item]` attribute that
allows crate authors to customize the error messages emitted by
unresolved imports. The main usecase for this is using this attribute as
part of a proc macro that expects a certain external module structure to
exist or certain dependencies to be there.

For me personally the motivating use-case are several derives in diesel,
that expect to refer to a `tabe` module. That is done either
implicitly (via the name of the type with the derive) or explicitly by
the user. This attribute would allow us to improve the error message in
both cases:

* For the implicit case we could explicity call out our
assumptions (turning the name into lower case, adding an `s` in the end)
+ point to the explicit variant as alternative
* For the explicit variant we would add additional notes to tell the
user why this is happening and what they should look for to fix the
problem (be more explicit about certain diesel specific assumptions of
the module structure)

I assume that similar use-cases exist for other proc-macros as well,
therefore I decided to put in the work implementing this new attribute.
I would also assume that this is likely not useful for std-lib internal
usage.
2026-04-10 08:55:02 +02:00
Jonathan Brouwer 65745a1b95 Revert #152369 because of multiple regressions
The regressions are documented in the PR comments.
This reverts commit 2972b5e, reversing changes made to f908263.
2026-04-09 18:53:59 +02:00
Jonathan Brouwer 30107e89e6 Revert #154808 because it is based on #152369
This reverts commit 0c94559d48, reversing
changes made to 33528612ba.
2026-04-09 18:32:49 +02:00
bors 9004856428 Auto merge of #153838 - oli-obk:use-tree-span, r=davidtwco
Use fine grained component-wise span tracking in use trees

This often produces nicer spans and even doesn't need a Span field anymore (not that I expect the unused field to affect any perf, but still neat).
2026-04-08 20:09:27 +00:00
Oli Scherer 9017621ace Use fine grained component-wise span tracking in use trees 2026-04-08 12:37:25 +02:00
Jonathan Brouwer 0c94559d48 Rollup merge of #154808 - JonathanBrouwer:attr_cleanup, r=jdonszelmann
Post-attribute ports cleanup pt. 1

r? @jdonszelmann

This cleans up some checks I could find were for non-parsed attributes, and works towards removing BUILTIN_ATTRIBUTES

All commits do one thing and every commit passes tests, so best reviewed commit by commit
2026-04-07 17:26:32 +02:00
GokhanKabar b544edd56c Preserve EII link through AttrProcMacro token roundtrip and add run-pass test
When a function has `eii_impls` set (via `eii_shared_macro`), the `#[hello]`
attribute is consumed from `node.attrs()`. A subsequent `AttrProcMacro` expander
like `contracts::requires` calls `item.to_tokens()` which uses the current
`node.attrs()` — so `#[hello]` is missing from the token stream. After the
roundtrip and `parse_ast_fragment`, the new AST item has empty `eii_impls`
and the EII link is broken.

Fix this by using `fake_token_stream_for_item` when the item is a function
with non-empty `eii_impls`. The pretty-printer re-emits `eii_impls` as
`#[hello]` in `print_fn_full`, which survives the roundtrip and gets
re-expanded by `eii_shared_macro` on the resulting item.

Add a run-pass test to verify EII + contract annotation works correctly
at runtime.
2026-04-07 15:25:55 +02:00
Jonathan Brouwer 6b6bf8def8 Remove unused attribute check for unparsed builtin attributes 2026-04-07 08:57:33 +02:00
Jason Newcomb 5394268ca3 Mark method receivers in builtin derives as being from the derive. 2026-04-05 14:26:40 -04:00
Jonathan Brouwer cb87c36bd9 Remove template from BUILTIN_ATTRIBUTES 2026-04-05 11:57:05 +02:00
Jonathan Brouwer abb15f5a63 Remove emit_fatal_malformed_builtin_attribute 2026-04-05 11:57:05 +02:00
Edvin Bryntesson bcebd2c909 don't parse attributes in pre-expansion lints
also buffer any lints coming from pre-expansion, so that they are
emitted with proper lint level
2026-04-03 11:08:11 +02:00
Edvin Bryntesson 4ab05bc659 make tools on AttributeParser hold reference to RegisteredTools 2026-04-03 11:08:10 +02:00
Sasha Pourcelot eb20cada00 make sure the right target is passed to #[cfg()] when it is parsed 2026-04-02 14:15:48 +00:00
bors 1174f78409 Auto merge of #154014 - Unique-Usman:ua/decmacrounrepeatable2, r=estebank
rustc_expand: improve diagnostics for non-repeatable metavars



There was an initally opened pr which solve this issue here https://github.com/rust-lang/rust/pull/152679. It got merged but, there was a perf regression. And this new pr is opened to address the problem. The first did the computation of binding and matched_rule and then passed them as owned value down to `diagnostics::emit_frag_parse_err(` but, now this pr address the issue by passing `lhs` and `rules` as borrowed value to from_tts and the move the logic to `diagnostics::emit_frag_parse_err(`.

Fix https://github.com/rust-lang/rust/issues/47452.
2026-03-25 23:14:18 +00: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`](https://github.com/rust-lang/reference/blob/50a1075e879be75aeec436252c84eef0fad489f4/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
Usman Akinyemi ed4a1f8b80 rustc_expand: improve diagnostics for non-repeatable metavars
Co-authored-by: Esteban Küber <esteban@kuber.com.ar>
Signed-off-by: Usman Akinyemi <usmanakinyemi202@gmail.com>
2026-03-24 08:12:13 +05:30
Jonathan Brouwer a9b484936c Rollup merge of #154245 - ZuseZ4:autodiff-trait-support, r=JonathanBrouwer,jdonszelmann
Allow applying autodiff macros to trait functions.

It will use enzyme to generate a default derivative implementation, which can be overwritten by the user.

closes: https://github.com/rust-lang/rust/issues/153329

r? @JonathanBrouwer
2026-03-23 20:18:36 +01:00
Manuel Drehwald 26c9f7255a Allow applying autodiff macros to trait functions.
It will use enzyme to generate a default derivative implementation,
which can be overwritten by the user.
2026-03-23 11:27:15 +01:00
Jonathan Brouwer b9f8e25f6e Rollup merge of #146961 - Jules-Bertholet:expr-cfg, r=JonathanBrouwer
Allow passing `expr` metavariable as `cfg` predicate

This PR allows expanding `expr` metavariables inside the configuration predicates of `cfg` and `cfg_attr` invocations.
For example, the following code will now compile:

```rust
macro_rules! mac {
    ($e:expr) => {
        #[cfg_attr($e, inline)]
        #[cfg($e)]
        fn func() {}

        #[cfg(not($e))]
        fn func() {
            panic!()
        }
    }
}

mac!(any(unix, feature = "foo"));
```

There is currently no `macro_rules` fragment specifier that can represent all valid `cfg` predicates. `meta` comes closest, but excludes `true` and `false`. By fixing that, this change makes it easier to write declarative macros that parse `cfg` or `cfg_attr` invocations, for example https://github.com/rust-lang/rust/pull/146281/.

@rustbot label T-lang needs-fcp A-attributes A-cfg A-macros
2026-03-21 00:42:47 +01:00
Jules Bertholet ab36d506d2 Address review comments 2026-03-20 18:48:38 -04:00
Jules Bertholet 7f83c784bd Allow passing expr metavariable as a cfg predicate 2026-03-15 13:44:14 -04:00