Commit Graph

285903 Commits

Author SHA1 Message Date
Santiago Pastorino
20f93c9e8e
Add codegen test to be sure we get rid of uneeded clones after monomorphization 2025-04-07 16:53:11 -03:00
Santiago Pastorino
a0856eaff6
Add mir opt tests to be sure we generate copy, clones and moves when corresponds 2025-04-07 16:53:11 -03:00
Santiago Pastorino
9f69c66a47
Optimize codegen of use values that are copy post monomorphization 2025-04-07 16:53:11 -03:00
Santiago Pastorino
b9159e09a7
Use a local var for tcx 2025-04-07 16:53:11 -03:00
Santiago Pastorino
b078564fe6
Make use generated TerminatorKind::Call have call_source Use 2025-04-07 16:53:11 -03:00
bors
e643f59f6d Auto merge of #139482 - Zalathar:rollup-h2ht1y6, r=Zalathar
Rollup of 9 pull requests

Successful merges:

 - #139035 (Add new `PatKind::Missing` variants)
 - #139108 (Simplify `thir::PatKind::ExpandedConstant`)
 - #139112 (Implement `super let`)
 - #139365 (Default auto traits: fix perf)
 - #139397 (coverage: Build the CGU's global file table as late as possible)
 - #139455 ( Remove support for `extern "rust-intrinsic"` blocks)
 - #139461 (Stop calling `source_span` query in significant drop order code)
 - #139465 (add sret handling for scalar autodiff)
 - #139466 (Trivial tweaks to stop tracking source span directly)

r? `@ghost`
`@rustbot` modify labels: rollup
2025-04-07 12:58:15 +00:00
Stuart Cook
6e0b67419c
Rollup merge of #139466 - compiler-errors:trivial-incr-tainting, r=oli-obk
Trivial tweaks to stop tracking source span directly

Firstly, remove some unnecessary work from `sccs_info`. This included debug printing which ends up giving all mir borrowck queries a dependency edge from the query to `source_span` (I think?).

Secondly, turn some calls from `hir_span` (which does some span adjustment, and thus incurs span tracking, which causes a dependency edge from the query to `source_span`) to `def_span`, which should cache better.

r? oli-obk
2025-04-07 22:29:22 +10:00
Stuart Cook
5863b426b9
Rollup merge of #139465 - EnzymeAD:autodiff-sret, r=oli-obk
add sret handling for scalar autodiff

r? `@oli-obk`

Fixing one of the todo's which I left in my previous batching PR.
This one handles sret for scalar autodiff.  `sret` mostly shows up when we try to return a lot of scalar floats.
People often start testing autodiff which toy functions which just use a few scalars as inputs and outputs, and those were the most likely to be affected by this issue. So this fix should make learning/teaching hopefully a bit easier.

Tracking:

- https://github.com/rust-lang/rust/issues/124509
2025-04-07 22:29:21 +10:00
Stuart Cook
0178254f46
Rollup merge of #139461 - compiler-errors:significant-drop-span, r=oli-obk
Stop calling `source_span` query in significant drop order code

`source_span` is only meant for incremental tracking. I don't really think we need to highlight the whole drop impl span anyways; it can be quite large.

r? oli-obk
2025-04-07 22:29:21 +10:00
Stuart Cook
9209c5eb60
Rollup merge of #139455 - Skgland:remove_rust-intrinsic_ABI, r=oli-obk
Remove support for `extern "rust-intrinsic"` blocks

Part of rust-lang/rust#132735

Looked manageable and there didn't appear to have been progress in the last two weeks,
so decided to give it a try.
2025-04-07 22:29:20 +10:00
Stuart Cook
ddf099ff4e
Rollup merge of #139397 - Zalathar:virtual, r=jieyouxu
coverage: Build the CGU's global file table as late as possible

Embedding coverage metadata in the output binary is a delicate dance, because per-function records need to embed references to the per-CGU filename table, but we only want to include files in that table if they are successfully used by at least one function.

The way that we build the file tables has changed a few times over the last few years. This particular change is motivated by experimental work on properly supporting macro-expansion regions, which adds some additional constraints that our previous implementation wasn't equipped to deal with.

LLVM is very strict about not allowing unused entries in local file tables. Currently that's not much of an issue, because we assume one source file per function, but to support expansion regions we need the flexibility to avoid committing to the use of a file until we're completely sure that we are able and willing to produce at least one coverage mapping region for it. In particular, when preparing a function's covfun record, we need the flexibility to decide at a late stage that a particular file isn't needed/usable after all.

(It's OK for the *global* file table to contain unused entries, but we would still prefer to avoid that if possible, and this implementation also achieves that.)
2025-04-07 22:29:20 +10:00
Stuart Cook
f4c429fde5
Rollup merge of #139365 - Bryanskiy:leak-perf, r=lcnr
Default auto traits: fix perf

Skip computing `requires_default_supertraits` if `experimental-default-bounds` option is not enabled. Possible perf fix for https://github.com/rust-lang/rust/pull/120706

r? lcnr
2025-04-07 22:29:19 +10:00
Stuart Cook
27c6e40755
Rollup merge of #139112 - m-ou-se:super-let, r=lcnr
Implement `super let`

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

This implements `super let` as proposed in #139080, based on the following two equivalence rules.

1. For all expressions `$expr` in any context, these are equivalent:
  - `& $expr`
  - `{ super let a = & $expr; a }`

2. And, additionally, these are equivalent in any context when `$expr` is a temporary (aka rvalue):
  - `& $expr`
  - `{ super let a = $expr; & a }`

So far, this experiment has a few interesting results:

## Interesting result 1

In this snippet:

```rust
super let a = f(&temp());
```

I originally expected temporary `temp()` would be dropped at the end of the statement (`;`), just like in a regular `let`, because `temp()` is not subject to temporary lifetime extension.

However, it turns out that that would break the fundamental equivalence rules.

For example, in

```rust
g(&f(&temp()));
```

the temporary `temp()` will be dropped at the `;`.

The first equivalence rule tells us this must be equivalent:

```rust
g({ super let a = &f(&temp()); a });
```

But that means that `temp()` must live until the last `;` (after `g()`), not just the first `;` (after `f()`).

While this was somewhat surprising to me at first, it does match the exact behavior we need for `pin!()`: The following _should work_. (See also https://github.com/rust-lang/rust/issues/138718)

```rust
g(pin!(f(&mut temp())));
```

Here, `temp()` lives until the end of the statement. This makes sense from the perspective of the user, as no other `;` or `{}` are visible. Whether `pin!()` uses a `{}` block internally or not should be irrelevant.

This means that _nothing_ in a `super let` statement will be dropped at the end of that super let statement. It does not even need its own scope.

This raises questions that are useful for later on:

- Will this make temporaries live _too long_ in cases where `super let` is used not in a hidden block in a macro, but as a visible statement in code like the following?

    ```rust
    let writer = {
        super let file = File::create(&format!("/home/{user}/test"));
        Writer::new(&file)
    };
    ```

- Is a `let` statement in a block still the right syntax for this? Considering it has _no_ scope of its own, maybe neither a block nor a statement should be involved

This leads me to think that instead of `{ super let $pat = $init; $expr }`, we might want to consider something like `let $pat = $init in $expr` or `$expr where $pat = $init`. Although there are also issues with these, as it isn't obvious anymore if `$init` should be subject to temporary lifetime extension. (Do we want both `let _ = _ in ..` and `super let _ = _ in ..`?)

## Interesting result 2

What about `super let x;` without initializer?

```rust
let a = {
    super let x;
    x = temp();
    &x
};
```

This works fine with the implementation in this PR: `x` is extended to live as long as `a`.

While it matches my expectations, a somewhat interesting thing to realize is that these are _not_ equivalent:

- `super let x = $expr;`
- `super let x; x = $expr;`

In the first case, all temporaries in $expr will live at least as long as (the result of) the surrounding block.
In the second case, temporaries will be dropped at the end of the assignment statement. (Because the assignment statement itself "is not `super`".)

This difference in behavior might be confusing, but it _might_ be useful.
One might want to extend the lifetime of a variable without extending all the temporaries in the initializer expression.

On the other hand, that can also be expressed as:

- `let x = $expr; super let x = x;` (w/o temporary lifetime extension), or
- `super let x = { $expr };` (w/ temporary lifetime extension)

So, this raises these questions:

- Do we want to accept `super let x;` without initializer at all?

- Does it make sense for statements other than let statements to be "super"? An expression statement also drops temporaries at its `;`, so now that we discovered that `super let` basically disables that `;` (see interesting result 1), is there a use to having other statements without their own scope? (I don't think that's ever useful?)

## Interesting result 3

This works now:

```rust
super let Some(x) = a.get(i) else { return };
```

I didn't put in any special cases for `super let else`. This is just the behavior that 'naturally' falls out when implementing `super let` without thinking of the `let else` case.

- Should `super let else` work?

## Interesting result 4

This 'works':

```rust
fn main() {
    super let a = 123;
}
```

I didn't put in any special cases for `super let` at function scope. I had expected the code to cause an ICE or other weird failure when used at function body scope, because there's no way to let the variable live as long as the result of the function.

This raises the question:

- Does this mean that this behavior is the natural/expected behavior when `super let` is used at function scope? Or is this just a quirk and should we explicitly disallow `super let` in a function body? (Probably the latter.)

---

The questions above do not need an answer to land this PR. These questions should be considered when redesigning/rfc'ing/stabilizing the feature.
2025-04-07 22:29:18 +10:00
Stuart Cook
9955b7634d
Rollup merge of #139108 - Nadrieril:simplify-expandedconstant, r=oli-obk
Simplify `thir::PatKind::ExpandedConstant`

I made it a bit less ad-hoc. In particular, I removed `is_inline: bool` that was just caching the output of `tcx.def_kind(def_id)`. This makes inline consts a tiny bit less special in patterns.

r? `@oli-obk`

cc `@Zalathar`
2025-04-07 22:29:18 +10:00
Stuart Cook
82df6229b6
Rollup merge of #139035 - nnethercote:PatKind-Missing, r=oli-obk
Add new `PatKind::Missing` variants

To avoid some ugly uses of `kw::Empty` when handling "missing" patterns, e.g. in bare fn tys. Helps with #137978. Details in the individual commits.

r? ``@oli-obk``
2025-04-07 22:29:17 +10:00
Manuel Drehwald
ca5bea3ebb move old tests, add sret test 2025-04-07 07:11:52 -04:00
Manuel Drehwald
d6467d34ae handle sret for scalar autodiff 2025-04-07 07:07:16 -04:00
Zalathar
4322b6e97d coverage: Build the CGU's global file table as late as possible 2025-04-07 17:11:49 +10:00
bors
8fb32ab8e5 Auto merge of #139473 - Kobzol:rollup-ycksn9b, r=Kobzol
Rollup of 5 pull requests

Successful merges:

 - #138314 (fix usage of `autodiff` macro with inner functions)
 - #139426 (Make the UnifyKey and UnifyValue imports non-nightly)
 - #139431 (Remove LLVM 18 inline ASM span fallback)
 - #139456 (style guide: add let-chain rules)
 - #139467 (More trivial tweaks)

r? `@ghost`
`@rustbot` modify labels: rollup
2025-04-07 06:27:35 +00:00
Jakub Beránek
58ad76313f
Rollup merge of #139467 - compiler-errors:more-trivial-tweaks, r=oli-obk
More trivial tweaks

Found some really tiny nits when trying to find places that span tracking caused queries to be recomputed in incremental; these probably don't need a perf run b/c they're so simple.

r? oli-obk
2025-04-07 08:23:36 +02:00
Jakub Beránek
1b8dbda9c7
Rollup merge of #139456 - calebcartwright:style-let-chains-final, r=joshtriplett
style guide: add let-chain rules

Reopens #110568

refs #53667 and I suppose #132833 as well

This reflects the style rules that the style team had already agreed upon back in 2023, with the addition of literals in the lhs being permissible for single line formatting, and the removal of unnecessary language/example snippets around non-`&&` operators that was a small hiccup in the original PR.

It also reflects current formatting behavior implemented in rustfmt (though note that the adjustment to include literals has been implemented & merged, but is still pending a sync to nightly)
2025-04-07 08:23:35 +02:00
Jakub Beránek
501235451c
Rollup merge of #139431 - beetrees:no-llvm-18, r=jieyouxu
Remove LLVM 18 inline ASM span fallback

The minimum supported LLVM version is now 19, so this fallback is no longer required.
2025-04-07 08:23:35 +02:00
Jakub Beránek
f4177f4bde
Rollup merge of #139426 - jackh726:rust-analyzer-next-solver-new, r=lcnr
Make the UnifyKey and UnifyValue imports non-nightly

Explicitly depend on ena in rustc_type_ir and import types from there.

This is required for rust-analyzer to use the new solver.

r? types
2025-04-07 08:23:34 +02:00
Jakub Beránek
5a43d92382
Rollup merge of #138314 - haenoe:autodiff-inner-function, r=ZuseZ4
fix usage of `autodiff` macro with inner functions

This PR adds additional handling into the expansion step of the `std::autodiff` macro (in `compiler/rustc_builtin_macros/src/autodiff.rs`), which allows the macro to be applied to inner functions.

```rust
#![feature(autodiff)]
use std::autodiff::autodiff;

fn main() {
    #[autodiff(d_inner, Forward, Dual, DualOnly)]
    fn inner(x: f32) -> f32 {
        x * x
    }
}
```

Previously, the compiler didn't allow this due to only handling `Annotatable::Item` and `Annotatable::AssocItem` and missing the handling of `Annotatable::Stmt`. This resulted in the rather generic error

```
error: autodiff must be applied to function
 --> src/main.rs:6:5
  |
6 | /     fn inner(x: f32) -> f32 {
7 | |         x * x
8 | |     }
  | |_____^

error: could not compile `enzyme-test` (bin "enzyme-test") due to 1 previous error
```

This issue was originally reported [here](https://github.com/EnzymeAD/rust/issues/184).

Quick question: would it make sense to add a ui test to ensure there is no regression on this?
This is my first contribution, so I'm extra grateful for any piece of feedback!! :D

r? `@oli-obk`

Tracking issue for autodiff: #124509
2025-04-07 08:23:34 +02:00
Michael Goulet
c51c448fae More trivial tweaks 2025-04-07 05:07:11 +00:00
Michael Goulet
d111aa87f1 Trivial tweaks to stop tracking source span directly 2025-04-07 05:03:35 +00:00
bors
b9856b6e40 Auto merge of #138766 - Zalathar:unused-fn, r=SparrowLii
coverage: Deal with unused functions and their names in one place

When coverage codegen creates dummy instances and covfun records for unused functions, we already know that they are unused, so we might as well set up the special array of unused function names at the same time.

---

The first commit only moves code around; all significant changes are in the second commit.

There should be no change in compiler output.
2025-04-07 03:19:35 +00:00
Caleb Cartwright
ee07e3f83f doc(style): add let-chain rules 2025-04-06 19:07:56 -05:00
bors
25a615bf82 Auto merge of #138951 - jwnrt:alloc-raw-vec-strict-prov, r=Noratrieb
Replace last `usize` -> `ptr` transmute in `alloc` with strict provenance API

This replaces the `usize -> ptr` transmute in `RawVecInner::new_in` with a strict provenance API (`NonNull::without_provenance`).

The API is changed to take an `Alignment` which encodes the non-null constraint needed for `Unique` and allows us to do the construction safely.

Two internal-only APIs were added to let us avoid UB-checking in this hot code: `Layout::alignment` to get the `Alignment` type directly rather than as a `usize`, and `Unique::from_non_null` to create `Unique` in const context without a transmute.
2025-04-06 23:07:48 +00:00
Michael Goulet
c8649a31a8 Stop calling source_span query in significant drop order code 2025-04-06 21:55:06 +00:00
HaeNoe
bf69443a9f
refactor: simplify function-info gathering 2025-04-06 22:08:40 +02:00
bors
2fa8b11f09 Auto merge of #136077 - rust-lang:cargo_update, r=clubby789
Weekly `cargo update`

Automation to keep dependencies in `Cargo.lock` current.

The following is the output from `cargo update`:

```txt

compiler & tools dependencies:
     Locking 11 packages to latest compatible versions
    Updating blake3 v1.8.0 -> v1.8.1
    Updating ctrlc v3.4.5 -> v3.4.6
    Updating env_logger v0.11.7 -> v0.11.8
    Updating errno v0.3.10 -> v0.3.11
    Updating flate2 v1.1.0 -> v1.1.1
    Updating indexmap v2.8.0 -> v2.9.0
    Updating miniz_oxide v0.8.5 -> v0.8.7
    Updating openssl-sys v0.9.106 -> v0.9.107
    Updating redox_syscall v0.5.10 -> v0.5.11
    Updating smallvec v1.14.0 -> v1.15.0
    Updating tokio v1.44.1 -> v1.44.2
note: pass `--verbose` to see 40 unchanged dependencies behind latest

library dependencies:
     Locking 1 package to latest compatible version
    Updating miniz_oxide v0.8.5 -> v0.8.7
note: pass `--verbose` to see 4 unchanged dependencies behind latest

rustbook dependencies:
     Locking 30 packages to latest compatible versions
    Updating cc v1.2.17 -> v1.2.18
    Updating clap v4.5.32 -> v4.5.35
    Updating clap_builder v4.5.32 -> v4.5.35
    Updating darling v0.20.10 -> v0.20.11
    Updating darling_core v0.20.10 -> v0.20.11
    Updating darling_macro v0.20.10 -> v0.20.11
    Updating env_logger v0.11.7 -> v0.11.8
    Updating errno v0.3.10 -> v0.3.11
    Updating flate2 v1.1.0 -> v1.1.1
    Updating iana-time-zone v0.1.61 -> v0.1.63
    Updating icu_locid_transform_data v1.5.0 -> v1.5.1
    Updating icu_normalizer_data v1.5.0 -> v1.5.1
    Updating icu_properties_data v1.5.0 -> v1.5.1
    Updating indexmap v2.8.0 -> v2.9.0
    Updating log v0.4.26 -> v0.4.27
    Updating miniz_oxide v0.8.5 -> v0.8.7
    Updating once_cell v1.21.1 -> v1.21.3
    Updating pest v2.7.15 -> v2.8.0
    Updating pest_derive v2.7.15 -> v2.8.0
    Updating pest_generator v2.7.15 -> v2.8.0
    Updating pest_meta v2.7.15 -> v2.8.0
    Updating redox_syscall v0.5.10 -> v0.5.11
    Updating rustix v1.0.3 -> v1.0.5
    Updating smallvec v1.14.0 -> v1.15.0
    Updating string_cache v0.8.8 -> v0.8.9
    Updating windows-core v0.52.0 -> v0.61.0
      Adding windows-implement v0.60.0
      Adding windows-interface v0.59.1
      Adding windows-result v0.3.2
      Adding windows-strings v0.4.0
```
2025-04-06 19:57:47 +00:00
Bennet Bleßmann
7dd57f085c
update/bless tests 2025-04-06 21:41:47 +02:00
Bennet Bleßmann
6dfb29624c
update docs
- src\doc\nomicon\src\ffi.md should also have its ABI list updated
2025-04-06 21:41:47 +02:00
Skgland
51b51b51d7
remove rust-analyser support for extern "rust-intrinsic" blocks 2025-04-06 21:41:46 +02:00
HaeNoe
63e825e52a
feat: apply autodiff macro twice to inner function
Verify that the expanded `inline` and `rustc_autodiff` macros are not
duplicated.
2025-04-06 21:36:20 +02:00
Skgland
5eb535c568
remove compiler support for extern "rust-intrinsic" blocks 2025-04-06 21:32:58 +02:00
HaeNoe
72091edcc4
feat: add test to validate autodiff macro expansion 2025-04-06 21:27:39 +02:00
HaeNoe
13bf79cd87
fix usage of autodiff macro with inner functions
- fix errors caused by the move of `ast::Item::ident` (see #138740)
- move the logic of getting `sig`, `vis`, and `ident` from two seperate
  `match` statements into one (less repetition especially with the
  nested `match`)
2025-04-06 21:14:59 +02:00
jackh726
cd4453fdba Explicitly depend on ena in rustc_type_ir and make the UnifyKey and UnifyValue imports non-nightly 2025-04-06 17:22:18 +00:00
bors
fd4dc18e68 Auto merge of #139452 - GuillaumeGomez:rollup-u9edkjo, r=GuillaumeGomez
Rollup of 6 pull requests

Successful merges:

 - #138562 (Optimize slice {Chunks,Windows}::nth)
 - #138876 (Trusty: Implement `write_vectored` for stdio )
 - #139072 (Add `slice::align_to_uninit_mut`)
 - #139367 (Add `*_value` methods to proc_macro lib)
 - #139391 (Check if merged attributes list is empty in expr)
 - #139414 (Fix typo in `RawList`'s documentation)

r? `@ghost`
`@rustbot` modify labels: rollup
2025-04-06 16:46:30 +00:00
Guillaume Gomez
b3e051aced
Rollup merge of #139414 - Adamkob12:fix_typo_raw_list, r=Nadrieril
Fix typo in `RawList`'s documentation
2025-04-06 18:08:11 +02:00
Guillaume Gomez
b1d67b2492
Rollup merge of #139391 - TaKO8Ki:check-if-merged-attrs-list-is-empty, r=jdonszelmann
Check if merged attributes list is empty in expr

Fixes #139373

In the example code, an [`UnrecognizedReprHint`](6b5ccfc87f/compiler/rustc_attr_parsing/src/attributes/repr.rs (L155)) error is output, and the list of merged attributes becomes empty. This causes a [panic](6b5ccfc87f/compiler/rustc_ast_lowering/src/lib.rs (L618)) to occur. So, it's necessary to check if merged attributes list is empty as other functions do.

ref: 6b5ccfc87f/compiler/rustc_ast_lowering/src/lib.rs (L896)
2025-04-06 18:08:11 +02:00
Guillaume Gomez
ed81e347f1
Rollup merge of #139367 - GuillaumeGomez:proc-macro-values, r=Urgau
Add `*_value` methods to proc_macro lib

This is the (re-)implementation of https://github.com/rust-lang/libs-team/issues/459.

It allows to get the actual value (unescaped) of the different string literals.

It was originally done in https://github.com/rust-lang/rust/pull/136355 but it broke the artifacts build so we decided to move the crate to crates.io to go around this limitation.

Part of https://github.com/rust-lang/rust/issues/136652.

Considering this is a copy-paste of the originally approved PR, no need to go through the whole process again. \o/

r? `@Urgau`
2025-04-06 18:08:10 +02:00
Guillaume Gomez
d1da78b201
Rollup merge of #139072 - nickkuk:align_to_uninit_mut, r=Mark-Simulacrum
Add `slice::align_to_uninit_mut`

Add new `slice::align_to_uninit_mut` method.

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

ACP: https://github.com/rust-lang/libs-team/issues/564
2025-04-06 18:08:10 +02:00
Guillaume Gomez
962fa98eeb
Rollup merge of #138876 - thaliaarchi:trusty-stdio, r=Noratrieb
Trusty: Implement `write_vectored` for stdio

Currently, `write` for stdout and stderr on Trusty is implemented with the semantics of `write_all`. Instead, call the underlying syscall only once in `write` and use the default implementation of `write_all` like other platforms. Also, implement `write_vectored` by adding support for `IoSlice`.

Refactor stdin to reuse the unsupported type like https://github.com/rust-lang/rust/pull/136769.

It requires #138875 to fix the build for Trusty, though they do not conflict and can merge in either order.

cc `@randomPoison`
2025-04-06 18:08:09 +02:00
Guillaume Gomez
7bd89b90e8
Rollup merge of #138562 - kornelski:nth-panic, r=Noratrieb
Optimize slice {Chunks,Windows}::nth

I've noticed that the `nth` functions on slice iters had non-optimized-out bounds checks.

The new implementation even generates branchless code.
2025-04-06 18:08:08 +02:00
Nadrieril
d912c03525 Reuse parent_args 2025-04-06 17:30:05 +02:00
Nadrieril
961c7463d8 Add the inline const type annotation in pattern lowering 2025-04-06 17:30:05 +02:00
Nadrieril
50e10b37c0 Tweak lower_pat_expr 2025-04-06 17:30:04 +02:00