Commit graph

1329 commits

Author SHA1 Message Date
bors
b08c11df4b Auto merge of #151162 - GuillaumeGomez:cleanup-attr-parsing, r=JonathanBrouwer
Clean up `rustc_attr_parsing`

Follow-up of rust-lang/rust#150934.

It removes the `Option<>` wrapping for `SharedContext::target` field and completely removed the `target_id` field. Considering this type contains a closure and never updates its `target_id` field, there is no need to keep it around, it can be used directly in the lint emitter.

r? @JonathanBrouwer
2026-01-16 13:03:57 +00:00
Guillaume Gomez
cd7d40d975 Update rustc_attr_parsing::SharedContext::target type from Option<Target> to Target 2026-01-15 15:06:29 +01:00
Jonathan Brouwer
daae6601be
Rollup merge of #151046 - semiopaque-eii-fix, r=jdonszelmann
compiler: Make Externally Implementable Item (eii) macros "semiopaque"

Otherwise eiis defined by std will produce large amounts of `missing stability attribute` errors. This problem is not eii specific, as can be seen in https://github.com/rust-lang/rust/issues/146993 and which is demonstrated in https://github.com/rust-lang/rust/pull/151022.

As can be seen with

```console
$ git grep rustc_macro_transparency
compiler/rustc_arena/src/lib.rs:#[rustc_macro_transparency = "semiopaque"]
[...]
```

it is very common for macros to use `"semiopaque"`.

r? @jdonszelmann

Tracking issue: https://github.com/rust-lang/rust/issues/125418

Needed for: https://github.com/rust-lang/rust/issues/150588
2026-01-14 22:29:59 +01:00
Jonathan Brouwer
46d4bbf6df
Rollup merge of #150971 - disallow-eii-in-statement-position, r=wafflelapkin
Disallow eii in statement position

With how v2 macros resolve, and the name resolution of `super` works, I realized with @WaffleLapkin that there's actually no way to consistently expand EIIs in statement position.

r? @WaffleLapkin
2026-01-14 22:29:58 +01:00
Jana Dönszelmann
df55233c6d
disallow in statement position 2026-01-13 12:08:09 +01:00
Jana Dönszelmann
467a2d2a1a
use self instead of super 2026-01-13 11:59:59 +01:00
Lukas Bergdoll
506762f3ff Explicitly export core and std macros
Currently all core and std macros are automatically added to the prelude
via #[macro_use]. However a situation arose where we want to add a new macro
`assert_matches` but don't want to pull it into the standard prelude for
compatibility reasons. By explicitly exporting the macros found in the core and
std crates we get to decide on a per macro basis and can later add them via
the rust_20xx preludes.
2026-01-13 08:47:48 +01:00
Martin Nordholts
8a8b31a4d1 compiler: Make Externally Implementable Item (eii) macros "semiopaque"
Otherwise eiis defined by std will produce large amounts of `missing
stability attribute` errors.
2026-01-13 06:27:55 +01:00
Jana Dönszelmann
6d0f23adad
rename extern item to foreign item 2026-01-12 08:07:23 +01:00
Jana Dönszelmann
322bbdfaaf
rename eii-extern-target 2026-01-12 08:07:23 +01:00
Jana Dönszelmann
d993bd1bb1
improve eii macro by using ecx methods 2026-01-11 13:03:05 +01:00
Stuart Cook
1987b15908
Rollup merge of #150913 - eii-macro-attrs, r=jdonszelmann
compiler: Forward attributes to eii-expanded macros

Since https://github.com/rust-lang/rust/pull/150592 is quite complicated to reason about I figured it would be good to split it up in smaller pieces that are easier to digest. Here is the attribute fix in isolation.

## The Problem

With this eii in **library/std/src/io/mod.rs**:
```rs
/// Foo
#[eii(on_broken_pipe)]
#[unstable(feature = "on_broken_pipe", issue = "150588")]
pub fn on_broken_pipe() -> OnBrokenPipe {
    OnBrokenPipe::BackwardsCompatible
}
```

you currently get this compilation error:

```
error: attribute macro has missing stability attribute
    --> library/std/src/io/mod.rs:2269:1
     |
2269 | #[eii(on_broken_pipe)]
     | ^^^^^^^^^^^^^^^^^^^^--
     | |
     | in this attribute macro expansion
     |
    ::: library/core/src/macros/mod.rs:1899:5
     |
1899 |     pub macro eii($item:item) {
     |     ------------- in this expansion of `#[eii]`
```

because with ` MAGIC_EXTRA_RUSTFLAGS=-Zunpretty=expanded ./x build library/std` we can see that a pub item in the expanded code is indeed missing that attribute:

```rs
const _: () =
    {
        #[on_broken_pipe]
        fn on_broken_pipe() -> OnBrokenPipe {
            OnBrokenPipe::BackwardsCompatible
        }
    };
unsafe extern "Rust" {
    /// Foo
    #[unstable(feature = "on_broken_pipe", issue = "150588")]
    #[rustc_eii_extern_item]
    pub safe fn on_broken_pipe()
    -> OnBrokenPipe;
}
#[rustc_builtin_macro(eii_shared_macro)]
#[eii_extern_target(on_broken_pipe)]
pub macro on_broken_pipe { () => {} }
```

## The Solution

With the fix, that error goes away because we get this expanded code instead:

```rs
const _: () =
    {
        #[on_broken_pipe]
        fn on_broken_pipe() -> OnBrokenPipe {
            OnBrokenPipe::BackwardsCompatible
        }
    };
unsafe extern "Rust" {
    /// Foo
    #[unstable(feature = "on_broken_pipe", issue = "150588")]
    #[rustc_eii_extern_item]
    pub safe fn on_broken_pipe()
    -> OnBrokenPipe;
}
/// Foo
#[unstable(feature = "on_broken_pipe", issue = "150588")]
#[rustc_builtin_macro(eii_shared_macro)]
#[eii_extern_target(on_broken_pipe)]
pub macro on_broken_pipe { () => {} }
```

Note that we also need to forward the docs, otherwise get get (fatal) warnings like these:

```
warning: missing documentation for an attribute macro
    --> library/std/src/io/mod.rs:2269:1
```

r? @jdonszelmann

Tracking issues:
- https://github.com/rust-lang/rust/issues/125418
- https://github.com/rust-lang/rust/issues/150588

### What about a test?

https://github.com/rust-lang/rust/pull/150591 will prevent regressions once it lands since it does not build without this fix. I think it is overkill to add a temporary eii to std before that.
2026-01-11 14:28:06 +11:00
Martin Nordholts
a0df7b2ad5 compiler: Forward attributes to eii-expanded macros
Otherwise you get errors like these if you have an Externally
Implementable Item defined in std:

    error: attribute macro has missing stability attribute
        --> library/std/src/io/mod.rs:2269:1
         |
    2269 | #[eii(on_broken_pipe)]
         | ^^^^^^^^^^^^^^^^^^^^--
         | |
         | in this attribute macro expansion
         |
        ::: library/core/src/macros/mod.rs:1899:5
         |
    1899 |     pub macro eii($item:item) {
         |     ------------- in this expansion of `#[eii]`

Or (fatal) warnings like these:

    warning: missing documentation for an attribute macro
        --> library/std/src/io/mod.rs:2269:1
2026-01-10 10:04:40 +01:00
Matthias Krüger
a8d66d4670
Rollup merge of #150765 - ua/missing-colon, r=estebank
rustc_parse_format: improve error for missing `:` before `?` in format args

Detect the `{ident?}` pattern where `?` is immediately followed by `}` and emit a clearer diagnostic explaining that `:` is required for Debug formatting. This avoids falling back to a generic “invalid format string” error and adds a targeted UI test for the case.
2026-01-10 08:33:57 +01:00
Jana Dönszelmann
52b3ac476c
trick with super imports that fixes nameres in anonymous modules 2026-01-09 09:29:02 +01:00
Jana Dönszelmann
5ddda0c37b
fix up diagnostics referring to the right items 2026-01-09 09:29:02 +01:00
Jana Dönszelmann
e3cff18370
dont resolve defaults anymore, store foreign item defid instead of macro 2026-01-09 09:29:02 +01:00
Usman Akinyemi
b0e65da2af rustc_parse_format: improve error for missing : before ? in format args
Detect the `{ident?}` pattern where `?` is immediately followed by `}` and emit
a clearer diagnostic explaining that `:` is required for Debug formatting.
This avoids falling back to a generic “invalid format string” error and adds
a targeted UI test for the case.

Signed-off-by: Usman Akinyemi <usmanakinyemi202@gmail.com>
2026-01-08 06:39:55 +05:30
Jonathan Brouwer
03fb7eeced
Create a rustc_ast representation for parsed attributes 2026-01-06 09:03:35 +01:00
Jakub Beránek
b198dffd98
Rollup merge of #149790 - JonathanBrouwer:attr-path-perf, r=jdonszelmann
Remove `Span` from segments of `AttrPath`

r? jdonszelmann
2026-01-05 15:54:12 +01:00
bors
5c53093374 Auto merge of #150133 - ZuseZ4:enzyme-frontend-nightly, r=jieyouxu
remove llvm_enzyme and enzyme fallbacks from most places

Using dlopen to get symbols has the nice benefit that rustc itself doesn't depend on libenzyme symbols anymore. We can therefore delete most fallback implementations in the backend (independently of whether we enable enzyme or not). When trying to use autodiff on nightly, we will now fail with a nice error if and only if we fail to load libEnzyme-21.so in our backend.

Verified:
Build as nightly, without Enzyme
Build as nightly, with Enzyme
Build as stable (without Enzyme)

With this PR we will now run `tests/ui/autodiff` on nightly, the tests are passing.

r? `@kobzol`
2025-12-23 02:49:04 +00:00
Jonathan Brouwer
7afba2977d
Make attr path symbols rather than idents 2025-12-22 16:26:14 +01:00
Matthias Krüger
5e365fd9de
Rollup merge of #150160 - jdonszelmann:fix-149980, r=Kivooeo
Fix ICE (#149980) for invalid EII in statement position

Based on https://github.com/rust-lang/rust/pull/150159

Fixes rust-lang/rust#149980
2025-12-21 18:50:44 +01:00
Matthias Krüger
d1cf7f8e61
Rollup merge of #146377 - fmease:no-incl-shebang-expr, r=Urgau
Don't strip shebang in expr-ctxt `include!(…)`

No longer strip shebang interpreter directives in files that were `include`d in expression (statement) contexts.
2025-12-21 18:50:40 +01:00
Manuel Drehwald
c34ea6e56d remove llvm_enzyme and enzyme fallbacks from most places, enable the autodiff frontend on nightly 2025-12-19 11:02:57 -08:00
Jana Dönszelmann
c316c05dc5
refactor how eii expansion is wrapped fixing 149980 2025-12-19 18:43:04 +01:00
Jana Dönszelmann
6608f6ace7
split up expansion code of eii macro into functions 2025-12-19 18:43:04 +01:00
Folkert de Vries
82ee8b21cb
cfg_select!: emit parse errors in unused branches 2025-12-17 22:42:42 +01:00
bors
3f4dc1e02d Auto merge of #146348 - jdonszelmann:eiiv3, r=lcnr,oli-obk
Externally implementable items

Supersedes https://github.com/rust-lang/rust/pull/140010
Tracking issue: https://github.com/rust-lang/rust/issues/125418

Getting started:

```rust
#![feature(eii)]

#[eii(eii1)]
pub fn decl1(x: u64)
// body optional (it's the default)
{
    println!("default {x}");
}

// in another crate, maybe
#[eii1]
pub fn decl2(x: u64) {
    println!("explicit {x}");
}

fn main() {
    decl1(4);
}
```

- tiny perf regression, underlying issue makes multiple things in the compiler slow, not just EII, planning to solve those separately.
- No codegen_gcc support, they don't have bindings for weak symbols yet but could
- No windows support yet for weak definitions

This PR merges the implementation of EII for just llvm + not windows, doesn't yet contain like a new panic handler implementation or alloc handler. With this implementation, it would support implementing the panic handler in terms of EII already since it requires no default implementation so no weak symbols

The PR has been open in various forms for about a year now, but I feel that having some implementation merged to build upon
2025-12-14 04:20:26 +00:00
bors
8188f6c808 Auto merge of #149709 - Urgau:overhaul-filenames, r=davidtwco
Overhaul filename handling for cross-compiler consistency

This PR overhauls the way we handle filenames in the compiler and `rmeta` in order to achieve achieve cross-compiler consistency (ie. having the same path no matter if the filename was created in the current compiler session or is coming from `rmeta`).

This is required as some parts of the compiler rely on consistent paths for the soundness of generated code (see rust-lang/rust#148328).

In order to achieved consistency multiple steps are being taken by this PR:
 - by making `RealFileName` immutable
 - by only having `SourceMap::to_real_filename` create `RealFileName`
   - currently `RealFileName` can be created from any `Path` and are remapped afterwards, which creates consistency issue
 - by also making `RealFileName` holds it's working directory, embeddable name and the remapped scopes
   - this removes the need for a `Session`, to know the current(!) scopes and cwd, which is invalid as they may not be equal to the scopes used when creating the filename

In order for `SourceMap::to_real_filename` to know which scopes to apply `FilePathMapping` now takes the current remapping scopes to apply, which makes `FileNameDisplayPreference` and company useless and are removed.

This PR is split-up in multiple commits (unfortunately not atomic), but should help review the changes.

Unblocks https://github.com/rust-lang/rust/pull/147611
Fixes https://github.com/rust-lang/rust/issues/148328
2025-12-13 14:32:09 +00:00
Boxy Uwu
acc3a0e2da Syntactically distinguish anon const const args 2025-12-12 15:45:37 +00:00
Jana Dönszelmann
5768b234de
use our own alternative to STD_INTERNAL_SYMBOL and make sure we mangle EIIs properly 2025-12-12 12:14:54 +01:00
Jana Dönszelmann
92c03a26fd
EII (builtin) macros in std 2025-12-12 11:17:33 +01:00
Jana Dönszelmann
2de02ac86e
EII ast changes 2025-12-12 11:17:33 +01:00
Urgau
8cbfb26383 Overhaul filename handling for cross-compiler consistency
This commit refactors `SourceMap` and most importantly `RealFileName` to
make it self-contained in order to achieve cross-compiler consistency.

This is achieved:
 - by making `RealFileName` immutable
 - by only having `SourceMap::to_real_filename` create `RealFileName`
 - by also making `RealFileName` holds it's working directory,
   it's embeddable name and the remapped scopes
 - by making most `FileName` and `RealFileName` methods take a scope as
   an argument

In order for `SourceMap::to_real_filename` to know which scopes to apply
`FilePathMapping` now takes the current remapping scopes to apply, which
makes `FileNameDisplayPreference` and company useless and are removed.

The scopes type `RemapPathScopeComponents` was moved from
`rustc_session::config` to `rustc_span`.

The previous system for scoping the local/remapped filenames
`RemapFileNameExt::for_scope` is no longer useful as it's replaced by
methods on `FileName` and `RealFileName`.
2025-12-12 07:33:09 +01:00
Matthias Krüger
02a58a9bee
Rollup merge of #149489 - scottmcm:try-bikeshed, r=nnethercote
Experimentally add *heterogeneous* `try` blocks

rust-lang/rust#148725 moved the default to being homogeneous; this adds heterogeneous ones back under an obvious-bikeshed syntax so people can experiment with that as well.

Essentially resolves rust-lang/rust#149025 by letting them move to this syntax instead.

New tracking issue: rust-lang/rust#149488
Related RFC: https://github.com/rust-lang/rfcs/pull/3721#issuecomment-3208342727 (specifically about experimenting)
2025-12-10 17:16:47 +01:00
Scott McMurray
4033d19b79 Experimentally add *heterogeneous* try blocks
148725 moved the default to being homogeneous; this adds heterogeneous ones back under an obvious-bikeshed syntax so people can experiment with that as well.

Essentially resolves 149025 by letting them move to this syntax instead.
2025-12-09 20:18:43 -08:00
Jonathan Brouwer
71b093fd2c
Emit check-cfg lints during attribute parsing rather than evaluation#149215 2025-12-06 10:22:13 +01:00
Matthias Krüger
0c95abb979
Rollup merge of #149524 - JonathanBrouwer:move_attr_safety, r=jdonszelmann
Move attribute safety checking to attribute parsing

This PR moves attribute safety checking to be done during attribute parsing. The `cfg` and `cfg_attr` attribute no longer need special-cased safety checking, yay!

This PR is a part 1 of 2, in the second part I'd like to define attribute safety in the attribute parsers rather than getting the information from BUILTIN_ATTRIBUTE_MAP, but to keep PRs reviewable lets do that separately.

Fixes https://github.com/rust-lang/rust/issues/148453 by reordering the diagnostics. The "cannot find attribute" diagnostic now appears first, but both diagnostics still appear.

r? `@jdonszelmann`
2025-12-04 16:07:53 +01:00
Jonathan Brouwer
884fb5aef2
Move attribute safety checking to rustc_attr_parsing 2025-12-03 17:00:06 +01:00
Jana Dönszelmann
97d4d2154f
fixup name in diagnostics 2025-12-03 16:38:24 +01:00
Jana Dönszelmann
9dd3caeebe
only discard items with #[test] on it when target is valid 2025-12-03 16:38:24 +01:00
Jonathan Brouwer
e59a6f89d9
Run eval_config_entry on all branches so we always emit lints 2025-11-27 13:36:03 +01:00
León Orell Valerian Liehr
e477732253
Don't strip shebang in expr-ctxt include!(…) 2025-11-26 04:57:58 +01:00
Matthias Krüger
04e4f95e7e
Rollup merge of #147736 - folkertdev:stabilize-asm-cfg, r=jdonszelmann
Stabilize `asm_cfg`

tracking issue: https://github.com/rust-lang/rust/issues/140364
closes https://github.com/rust-lang/rust/issues/140364

Reference PR:

- https://github.com/rust-lang/reference/pull/2063

# Request for Stabilization

## Summary

The `cfg_asm` feature allows `#[cfg(...)]` and `#[cfg_attr(...)]` on  the arguments of the assembly macros, for instance:

```rust
asm!( // or global_asm! or naked_asm!
    "nop",
    #[cfg(target_feature = "sse2")]
    "nop",
    // ...
    #[cfg(target_feature = "sse2")]
    a = const 123, // only used on sse2
);
```

## Semantics

Templates, operands, `options` and `clobber_abi` in the assembly macros (`asm!`, `naked_asm!` and `global_asm!`) can be annotated with `#[cfg(...)]` and `#[cfg_attr(...)]`. When the condition evaluates to true, the annotated argument has no effect, and is completely ignored when expanding the assembly macro.
## Documentation

reference PR: https://github.com/rust-lang/reference/pull/2063

## Tests

- [tests/ui/asm/cfg.rs](https://github.com/rust-lang/rust/blob/master/tests/ui/asm/cfg.rs) checks that `cfg`'d arguments where the condition evaluates to false have no effect
- [tests/ui/asm/cfg-parse-error.rs](https://github.com/rust-lang/rust/blob/master/tests/ui/asm/cfg.rs) checks the parsing rules (parsing effectively assumes that the cfg conditions are all true)

## History

- https://github.com/rust-lang/rust/issues/140279
- https://github.com/rust-lang/rust/pull/140367

# Resolved questions

**how are other attributes handled**

Other attributes are parsed,  but explicitly rejected.

# unresolved questions

**operand before template**

The current implementation expects at least one template string before any operands. In the example below, if the `cfg` condition evaluates to true, the assembly block is ill-formed. But even when it evaluates to `false` this block is rejected, because the parser still expects just a template (a template is parsed as an expression and then validated to ensure that it is or expands to a string literal).

Changing how this works is difficult.
```rust
// This is rejected because `a = out(reg) x` does not parse as an expresion.
asm!(
	#[cfg(false)]
	a = out(reg) x, //~ ERROR expected token: `,`
	"",
);
```

**lint on positional arguments?**

Adding a lint to warn on the definition or use of positional arguments being `cfg`'d out was discussed in https://github.com/rust-lang/rust/issues/140279#issuecomment-2832237372 and subsequent comments. Such a lint is not currently implemented, but that may not be a blocker based on the comments there.

r? `@traviscross` (I'm assuming you'll reassign as needed)
2025-11-25 17:51:13 +01:00
Oli Scherer
2a36d33930 Give all impls a constness 2025-11-18 09:20:21 +00:00
Stuart Cook
e26b42333d
Rollup merge of #148712 - JonathanBrouwer:cfg_select, r=jdonszelmann
Port `cfg_select!` to the new attribute parsing system

Best reviewed commit by commit, since it involves some moving around of code

r? `````@jdonszelmann`````
2025-11-11 21:11:48 +11:00
bors
055d0d6aaf Auto merge of #135634 - joboet:trivial-clone, r=Mark-Simulacrum
stop specializing on `Copy`

fixes https://github.com/rust-lang/rust/issues/132442

`std` specializes on `Copy` to optimize certain library functions such as `clone_from_slice`. This is unsound, however, as the `Copy` implementation may not be always applicable because of lifetime bounds, which specialization does not take into account; the result being that values are copied even though they are not `Copy`. For instance, this code:
```rust
struct SometimesCopy<'a>(&'a Cell<bool>);

impl<'a> Clone for SometimesCopy<'a> {
    fn clone(&self) -> Self {
        self.0.set(true);
        Self(self.0)
    }
}

impl Copy for SometimesCopy<'static> {}

let clone_called = Cell::new(false);
// As SometimesCopy<'clone_called> is not 'static, this must run `clone`,
// setting the value to `true`.
let _ = [SometimesCopy(&clone_called)].clone();
assert!(clone_called.get());
```
should not panic, but does ([playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=6be7a48cad849d8bd064491616fdb43c)).

To solve this, this PR introduces a new `unsafe` trait: `TrivialClone`. This trait may be implemented whenever the `Clone` implementation is equivalent to copying the value (so e.g. `fn clone(&self) -> Self { *self }`). Because of lifetime erasure, there is no way for the `Clone` implementation to observe lifetime bounds, meaning that even if the `TrivialClone` has stricter bounds than the `Clone` implementation, its invariant still holds. Therefore, it is sound to specialize on `TrivialClone`.

I've changed all `Copy` specializations in the standard library to specialize on `TrivialClone` instead. Unfortunately, the unsound `#[rustc_unsafe_specialization_marker]` attribute on `Copy` cannot be removed in this PR as `hashbrown` still depends on it. I'll make a PR updating `hashbrown` once this lands.

With `Copy` no longer being considered for specialization, this change alone would result in the standard library optimizations not being applied for user types unaware of `TrivialClone`. To avoid this and restore the optimizations in most cases, I have changed the expansion of `#[derive(Clone)]`: Currently, whenever both `Clone` and `Copy` are derived, the `clone` method performs a copy of the value. With this PR, the derive macro also adds a `TrivialClone` implementation to make this case observable using specialization. I anticipate that most users will use `#[derive(Clone, Copy)]` whenever both are applicable, so most users will still profit from the library optimizations.

Unfortunately, Hyrum's law applies to this PR: there are some popular crates which rely on the precise specialization behaviour of `core` to implement "specialization at home", e.g. [`libAFL`](89cff63702/libafl_bolts/src/tuples.rs (L27-L49)). I have no remorse for breaking such horrible code, but perhaps we should open other, better ways to satisfy their needs – for example by dropping the `'static` bound on `TypeId::of`...
2025-11-10 15:41:43 +00:00
joboet
16d2b5534e
prevent TrivialClone implementations from appearing in rustdoc output 2025-11-09 22:26:15 +01:00
joboet
e4e765b1f6
add a TrivialClone implementation when deriving both Clone and Copy 2025-11-09 17:31:17 +01:00