Commit Graph

250022 Commits

Author SHA1 Message Date
bors
df8ac8f1d7 Auto merge of #122568 - RalfJung:mentioned-items, r=oli-obk
recursively evaluate the constants in everything that is 'mentioned'

This is another attempt at fixing https://github.com/rust-lang/rust/issues/107503. The previous attempt at https://github.com/rust-lang/rust/pull/112879 seems stuck in figuring out where the [perf regression](https://perf.rust-lang.org/compare.html?start=c55d1ee8d4e3162187214692229a63c2cc5e0f31&end=ec8de1ebe0d698b109beeaaac83e60f4ef8bb7d1&stat=instructions:u) comes from. In  https://github.com/rust-lang/rust/pull/122258 I learned some things, which informed the approach this PR is taking.

Quoting from the new collector docs, which explain the high-level idea:
```rust
//! One important role of collection is to evaluate all constants that are used by all the items
//! which are being collected. Codegen can then rely on only encountering constants that evaluate
//! successfully, and if a constant fails to evaluate, the collector has much better context to be
//! able to show where this constant comes up.
//!
//! However, the exact set of "used" items (collected as described above), and therefore the exact
//! set of used constants, can depend on optimizations. Optimizing away dead code may optimize away
//! a function call that uses a failing constant, so an unoptimized build may fail where an
//! optimized build succeeds. This is undesirable.
//!
//! To fix this, the collector has the concept of "mentioned" items. Some time during the MIR
//! pipeline, before any optimization-level-dependent optimizations, we compute a list of all items
//! that syntactically appear in the code. These are considered "mentioned", and even if they are in
//! dead code and get optimized away (which makes them no longer "used"), they are still
//! "mentioned". For every used item, the collector ensures that all mentioned items, recursively,
//! do not use a failing constant. This is reflected via the [`CollectionMode`], which determines
//! whether we are visiting a used item or merely a mentioned item.
//!
//! The collector and "mentioned items" gathering (which lives in `rustc_mir_transform::mentioned_items`)
//! need to stay in sync in the following sense:
//!
//! - For every item that the collector gather that could eventually lead to build failure (most
//!   likely due to containing a constant that fails to evaluate), a corresponding mentioned item
//!   must be added. This should use the exact same strategy as the ecollector to make sure they are
//!   in sync. However, while the collector works on monomorphized types, mentioned items are
//!   collected on generic MIR -- so any time the collector checks for a particular type (such as
//!   `ty::FnDef`), we have to just onconditionally add this as a mentioned item.
//! - In `visit_mentioned_item`, we then do with that mentioned item exactly what the collector
//!   would have done during regular MIR visiting. Basically you can think of the collector having
//!   two stages, a pre-monomorphization stage and a post-monomorphization stage (usually quite
//!   literally separated by a call to `self.monomorphize`); the pre-monomorphizationn stage is
//!   duplicated in mentioned items gathering and the post-monomorphization stage is duplicated in
//!   `visit_mentioned_item`.
//! - Finally, as a performance optimization, the collector should fill `used_mentioned_item` during
//!   its MIR traversal with exactly what mentioned item gathering would have added in the same
//!   situation. This detects mentioned items that have *not* been optimized away and hence don't
//!   need a dedicated traversal.

enum CollectionMode {
    /// Collect items that are used, i.e., actually needed for codegen.
    ///
    /// Which items are used can depend on optimization levels, as MIR optimizations can remove
    /// uses.
    UsedItems,
    /// Collect items that are mentioned. The goal of this mode is that it is independent of
    /// optimizations: the set of "mentioned" items is computed before optimizations are run.
    ///
    /// The exact contents of this set are *not* a stable guarantee. (For instance, it is currently
    /// computed after drop-elaboration. If we ever do some optimizations even in debug builds, we
    /// might decide to run them before computing mentioned items.) The key property of this set is
    /// that it is optimization-independent.
    MentionedItems,
}
```
And the `mentioned_items` MIR body field docs:
```rust
    /// Further items that were mentioned in this function and hence *may* become monomorphized,
    /// depending on optimizations. We use this to avoid optimization-dependent compile errors: the
    /// collector recursively traverses all "mentioned" items and evaluates all their
    /// `required_consts`.
    ///
    /// This is *not* soundness-critical and the contents of this list are *not* a stable guarantee.
    /// All that's relevant is that this set is optimization-level-independent, and that it includes
    /// everything that the collector would consider "used". (For example, we currently compute this
    /// set after drop elaboration, so some drop calls that can never be reached are not considered
    /// "mentioned".) See the documentation of `CollectionMode` in
    /// `compiler/rustc_monomorphize/src/collector.rs` for more context.
    pub mentioned_items: Vec<Spanned<MentionedItem<'tcx>>>,
```

Fixes #107503
2024-03-21 09:01:18 +00:00
Ralf Jung
8c01b85dba make sure we don't inline these generic fn as that could monomorphize them 2024-03-21 09:05:47 +01:00
bors
47dd709bed Auto merge of #121123 - compiler-errors:item-assumptions, r=oli-obk
Split an item bounds and an item's super predicates

This is the moral equivalent of #107614, but instead for predicates this applies to **item bounds**. This PR splits out the item bounds (i.e. *all* predicates that are assumed to hold for the alias) from the item *super predicates*, which are the subset of item bounds which share the same self type as the alias.

## Why?

Much like #107614, there are places in the compiler where we *only* care about super-predicates, and considering predicates that possibly don't have anything to do with the alias is problematic. This includes things like closure signature inference (which is at its core searching for `Self: Fn(..)` style bounds), but also lints like `#[must_use]`, error reporting for aliases, computing type outlives predicates.

Even in cases where considering all of the `item_bounds` doesn't lead to bugs, unnecessarily considering irrelevant bounds does lead to a regression (#121121) due to doing extra work in the solver.

## Example 1 - Trait Aliases

This is best explored via an example:

```
type TAIT<T> = impl TraitAlias<T>;

trait TraitAlias<T> = A + B where T: C;
```

The item bounds list for `Tait<T>` will include:
* `Tait<T>: A`
* `Tait<T>: B`
* `T: C`

While `item_super_predicates` query will include just the first two predicates.

Side-note: You may wonder why `T: C` is included in the item bounds for `TAIT`? This is because when we elaborate `TraitAlias<T>`, we will also elaborate all the predicates on the trait.

## Example 2 - Associated Type Bounds

```
type TAIT<T> = impl Iterator<Item: A>;
```

The `item_bounds` list for `TAIT<T>` will include:
* `Tait<T>: Iterator`
* `<Tait<T> as Iterator>::Item: A`

But the `item_super_predicates` will just include the first bound, since that's the only bound that is relevant to the *alias* itself.

## So what

This leads to some diagnostics duplication just like #107614, but none of it will be user-facing. We only see it in the UI test suite because we explicitly disable diagnostic deduplication.

Regarding naming, I went with `super_predicates` kind of arbitrarily; this can easily be changed, but I'd consider better names as long as we don't block this PR in perpetuity.
2024-03-21 06:12:24 +00:00
bors
6e1f7b538a Auto merge of #121587 - ShoyuVanilla:fix-issue-121267, r=TaKO8Ki
Fix bad span for explicit lifetime suggestions

Fixes #121267

Current explicit lifetime suggestions are not showing correct spans for some lifetimes - e.g. elided lifetime generic parameters;

This should be done correctly regarding elided lifetime kind like the following code

43fdd4916d/compiler/rustc_resolve/src/late/diagnostics.rs (L3015-L3044)
2024-03-21 04:11:09 +00:00
bors
6a6cd6517d Auto merge of #122803 - jhpratt:rollup-nmgs79k, r=jhpratt
Rollup of 8 pull requests

Successful merges:

 - #122545 (Ignore paths from expansion in `unused_qualifications`)
 - #122729 (Relax SeqCst ordering in standard library.)
 - #122740 (use more accurate terminology)
 - #122749 (make `type_flags(ReError) & HAS_ERROR`)
 - #122764 (coverage: Remove incorrect assertions from counter allocation)
 - #122765 (Add `usize::MAX` arg tests for Vec)
 - #122776 (Rename `hir::Let` into `hir::LetExpr`)
 - #122786 (compiletest: Introduce `remove_and_create_dir_all()` helper)

r? `@ghost`
`@rustbot` modify labels: rollup
2024-03-21 02:09:07 +00:00
Shoyu Vanilla
c270a42fea Fix bad span for explicit lifetime suggestion
Move verbose logic to a function

Minor renaming
2024-03-21 10:31:04 +09:00
Jacob Pratt
f25397adc0
Rollup merge of #122786 - Enselic:remove_and_create_dir_all, r=onur-ozkan
compiletest: Introduce `remove_and_create_dir_all()` helper

The code

    let _ = fs::remove_dir_all(&dir);
    create_dir_all(&dir).unwrap();

is duplicated in 7 places. Let's introduce a helper.
2024-03-20 20:29:47 -04:00
Jacob Pratt
afdbad80b1
Rollup merge of #122776 - GuillaumeGomez:rename-hir-let, r=oli-obk
Rename `hir::Let` into `hir::LetExpr`

As discussed on [zulip](https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/Improve.20naming.20of.20.60ExprKind.3A.3ALet.60.3F).

r? `````@Zalathar`````
2024-03-20 20:29:47 -04:00
Jacob Pratt
7a5ffccded
Rollup merge of #122765 - workingjubilee:test-for-vec-handling-usize-max, r=Nilstrieb
Add `usize::MAX` arg tests for Vec

Tests to prevent recurrence of the UB from the rust-lang/rust#122760 issue.

I skipped the `with_capacity`, `drain`, `reserve`, etc. APIs because they actually had a good assortment of tests earlier in the same file.

r? Nilstrieb
2024-03-20 20:29:46 -04:00
Jacob Pratt
c6a49220d6
Rollup merge of #122764 - Zalathar:loopy, r=oli-obk
coverage: Remove incorrect assertions from counter allocation

These assertions detect situations where a BCB node (in the coverage graph) would have both a physical counter and one or more in-edge counters/expressions.

For most BCBs that situation would indicate an implementation bug. However, it's perfectly fine in the case of a BCB having an edge that loops back to itself.

Given the complexity and risk involved in fixing the assertions, and the fact that nothing relies on them actually being true, this patch just removes them instead.

Fixes #122738.

`````@rustbot````` label +A-code-coverage
2024-03-20 20:29:46 -04:00
Jacob Pratt
4e792df4ed
Rollup merge of #122749 - aliemjay:region-err, r=compiler-errors
make `type_flags(ReError) & HAS_ERROR`

Self-explanatory. `TypeVisitableExt::references_error(ReError)` incorrectly returned `false`.
2024-03-20 20:29:45 -04:00
Jacob Pratt
d53df5a51e
Rollup merge of #122740 - tshepang:patch-1, r=clubby789
use more accurate terminology

rustc is just one tool/executable, even if at the center of the toolchain
2024-03-20 20:29:45 -04:00
Jacob Pratt
43ad753adb
Rollup merge of #122729 - m-ou-se:relax, r=Amanieu
Relax SeqCst ordering in standard library.

Every single SeqCst in the standard library is unnecessary. In all cases, Relaxed or Release+Acquire was sufficient.

As I [wrote](https://marabos.nl/atomics/memory-ordering.html#common-misconceptions) in my book on atomics:

> [..] when reading code, SeqCst basically tells the reader: "this operation depends on the total order of every single SeqCst operation in the program," which is an incredibly far-reaching claim. The same code would likely be easier to review and verify if it used weaker memory ordering instead, if possible. For example, Release effectively tells the reader: "this relates to an acquire operation on the same variable," which involves far fewer considerations when forming an understanding of the code.
>
> It is advisable to see SeqCst as a warning sign. Seeing it in the wild often means that either something complicated is going on, or simply that the author did not take the time to analyze their memory ordering related assumptions, both of which are reasons for extra scrutiny.

r? ````@Amanieu```` ````@joboet````
2024-03-20 20:29:44 -04:00
Jacob Pratt
31adfd77d2
Rollup merge of #122545 - Alexendoo:unused-qualifications, r=petrochenkov
Ignore paths from expansion in `unused_qualifications`

If any of the path segments are from an expansion the lint is skipped currently, but a path from an expansion where all of the segments are passed in would not be. Doesn't seem that likely to occur but it could happen
2024-03-20 20:29:44 -04:00
bors
6ec953c5ea Auto merge of #122772 - nikic:update-llvm-22, r=cuviper
Update to LLVM 18.1.2

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

Also contains fixes for https://github.com/Rahix/avr-hal/issues/505 and https://github.com/llvm/llvm-project/issues/83362.

r? `@cuviper`
2024-03-21 00:03:55 +00:00
bors
1388d7a069 Auto merge of #122761 - jwong101:fix/vec-insert, r=workingjubilee,Nilstrieb
fix OOB pointer formed in Vec::index

Move the length check to before using `index` with `ptr::add` to prevent an out of bounds pointer from being formed.

Fixes #122760
2024-03-20 21:37:30 +00:00
Martin Nordholts
c3cc6c1990 compiletest: Introduce remove_and_create_dir_all() helper
The code

    let _ = fs::remove_dir_all(&dir);
    create_dir_all(&dir).unwrap();

is duplicated in 7 places. Let's introduce a helper.
2024-03-20 20:28:30 +01:00
bors
e3df96cfda Auto merge of #122779 - estebank:test-svg-non-determinism, r=compiler-errors
When comparing SVG tests against their blessed version, ignore the first line

`anstyle_svg` has some weird non-determinism in the width parameter, which makes tests blessed in one environment to fail in another. This is the *only* non-determinism detected so far, so we modify the diff check to ignore the first line of the SVG. In order for a test to fail/be updated by `--bless`, a different part of the file needs to also have changed. If other sources of non-determinism are found, we should back out of the "`--color=always` means `.svg`" change.

r? `@compiler-errors`
2024-03-20 18:47:34 +00:00
Martin Nordholts
352587af44 compiletest: mir_dump_dir.as_path() -> &mir_dump_dir 2024-03-20 19:24:18 +01:00
Ali MJ Al-Nasrawy
0dc006b3a8 register opaques that reference errors 2024-03-20 17:30:19 +00:00
Ali MJ Al-Nasrawy
19e0ea4a6d make type_flags(ReError) & HAS_ERROR 2024-03-20 17:29:58 +00:00
Michael Goulet
ce5f8c93fa Bless test fallout (duplicate diagnostics) 2024-03-20 13:00:34 -04:00
Michael Goulet
aa39dbb962 Split item bounds and item super predicates 2024-03-20 13:00:34 -04:00
Alex Macleod
a8452461dc Ignore paths from expansion in unused_qualifications 2024-03-20 16:30:26 +00:00
Esteban Küber
bf63f7eefe When comparing SVG tests against their blessed version, ignore the first line
`anstyle_svg` has some weird non-determinism in the width parameter, which makes tests blessed in one environment to fail in another. This is the *only* non-determinism detected so far, so we modify the diff check to ignore the first line of the SVG. In order for a test to fail/be updated by `--bless`, a different part of the file needs to also have changed.
2024-03-20 16:25:29 +00:00
Guillaume Gomez
98e66553a6 Rename hir::Let into hir::LetExpr 2024-03-20 16:47:11 +01:00
bors
94b72d6beb Auto merge of #122359 - Zoxc:missing-static-notes, r=wesleywiser
Print the crates not available as static

This prints out the crates not available to be statically linked when static linking is preferred and we run into an error with duplicated crates.
2024-03-20 15:46:15 +00:00
Mara Bos
34621757ea SeqCst->Relaxed in condvar test.
Relaxed is enough here. Synchronization is done by the mutex.
2024-03-20 15:38:09 +01:00
Mara Bos
acddc55748 SeqCst->Relaxed in thread local test.
Relaxed memory ordering is fine because spawn()/join() already provides
all the synchronization we need.
2024-03-20 15:38:09 +01:00
Mara Bos
b45a725cbc SeqCst->Relaxed in std::net::test.
Relaxed is enough to have fetch_add(1) return each value only once
(until it wraps around).
2024-03-20 15:35:13 +01:00
Mara Bos
8b519f98e2 Use less restricted memory ordering in xous::thread_local_key.
SeqCst isn't necessary in any of these cases.
2024-03-20 15:35:11 +01:00
bors
a128516cf9 Auto merge of #122754 - Mark-Simulacrum:bootstrap-bump, r=albertlarsan68
Bump to 1.78 bootstrap compiler

https://forge.rust-lang.org/release/process.html#master-bootstrap-update-t-2-day-tuesday
2024-03-20 13:43:41 +00:00
Mark Rousskov
9a22a0fdab Fix bootstrap bump fallout 2024-03-20 08:49:13 -04:00
Mark Rousskov
283db5abfc Workaround for rustdoc bug in new beta
Filed #122758 to track a proper fix, but this seems to solve the
problem in the meantime and is probably OK in terms of impact on
(internal) doc quality.
2024-03-20 08:49:13 -04:00
Mark Rousskov
02f1930595 step cfgs 2024-03-20 08:49:13 -04:00
bors
c86f3ac24f Auto merge of #120717 - compiler-errors:cap-closure-kind, r=oli-obk
For async closures, cap closure kind, get rid of `by_mut_body`

Right now we have three `AsyncFn*` traits, and three corresponding futures that are returned by the `call_*` functions for them. This is fine, but it is a bit excessive, since the future returned by `AsyncFn` and `AsyncFnMut` are identical. Really, the only distinction we need to make with these bodies is "by ref" and "by move".

This PR removes `AsyncFn::CallFuture` and renames `AsyncFnMut::CallMutFuture` to `AsyncFnMut::CallRefFuture`. This simplifies MIR building for async closures, since we don't need to build an extra "by mut" body, but just a "by move" body which is materially different.

We need to do a bit of delicate handling of the ClosureKind for async closures, since we need to "cap" it to `AsyncFnMut` in some cases when we only care about what body we're looking for.

This also fixes a bug where `<{async closure} as Fn>::call` was returning a body that takes the async-closure receiver *by move*.

This also helps align the `AsyncFn` traits to the `LendingFn` traits' eventual designs.
2024-03-20 11:40:45 +00:00
Nikita Popov
022e42db00 Update to LLVM 18.1.2 2024-03-20 12:26:19 +01:00
Ralf Jung
0cb1065d7e collector: move functions around so that the 'root collection' section really only has root collection things under it 2024-03-20 11:57:27 +01:00
Ralf Jung
feeffaeff9 mentioned_items: avoid adding str/slice unsizing casts 2024-03-20 11:16:11 +01:00
Ralf Jung
682991d2c7 explicitly set opt-level=0 2024-03-20 11:07:12 +01:00
Ralf Jung
0d6a16ac4b mentioned_items: record all callee and coerced closure types, whether they are FnDef/Closure or not
They may become FnDef during monomorphization!
2024-03-20 11:07:12 +01:00
Ralf Jung
f1ec494c32 mentioned items: also handle closure-to-fn-ptr coercions 2024-03-20 11:07:12 +01:00
Ralf Jung
347ca50bc8 mentioned items: also handle vtables 2024-03-20 11:07:12 +01:00
Ralf Jung
ee4b758161 avoid processing mentioned items that are also still used 2024-03-20 11:07:12 +01:00
Ralf Jung
91b35a1b40 fix comments in required-consts tests 2024-03-20 11:07:12 +01:00
Ralf Jung
712fe36611 collector: recursively traverse 'mentioned' items to evaluate their constants 2024-03-20 11:07:12 +01:00
bors
0f706af330 Auto merge of #122763 - matthiaskrgr:rollup-o8a2mye, r=matthiaskrgr
Rollup of 6 pull requests

Successful merges:

 - #121543 (various clippy fixes)
 - #122540 (Do not use `?`-induced skewing of type inference in the compiler)
 - #122730 (Expose `ucred::peer_cred` on QNX targets to enable dist builds)
 - #122732 (Remove redundant coroutine captures note)
 - #122739 (Add "put" as a confusable for insert on hash map/set)
 - #122748 (Reduce `pub` usage in `rustc_session`.)

r? `@ghost`
`@rustbot` modify labels: rollup
2024-03-20 09:37:39 +00:00
Jubilee Young
92f668c20b Add usize::MAX arg tests for Vec 2024-03-20 01:21:19 -07:00
Zalathar
2f21e4f8bb coverage: Tidy imports in rustc_mir_transform::coverage::counters 2024-03-20 18:25:53 +11:00
Zalathar
85bec7a50c coverage: Remove incorrect assertions from counter allocation
These assertions detect situations where a BCB node would have both a physical
counter and one or more in-edge counters/expressions.

For most BCBs that situation would indicate an implementation bug. However,
it's perfectly fine in the case of a BCB having an edge that loops back to
itself.

Given the complexity and risk involved in fixing the assertions, and the fact
that nothing relies on them actually being true, this patch just removes them
instead.
2024-03-20 18:22:15 +11:00