5290 Commits

Author SHA1 Message Date
Michael Goulet
d05bb98d6b Extract borrowck coroutine drop-liveness hack 2025-07-31 17:38:28 +00:00
Michael Goulet
d525e79157 Stall coroutines based off of ty::Coroutine, not ty::CoroutineWitness 2025-07-31 17:31:51 +00:00
Stuart Cook
017586c93a
Rollup merge of #144713 - nnethercote:rustc_middle-ty-cleanups, r=lcnr
`rustc_middle::ty` cleanups

r? `@davidtwco`
2025-07-31 18:52:11 +10:00
Nicholas Nethercote
a949c47f0d Remove ParamEnvAnd::into_parts.
The fields are public, so this doesn't need a method, normal
deconstruction and/or field access is good enough.
2025-07-31 15:17:20 +10:00
Nicholas Nethercote
790ab94798 Move ImplHeader out of rustc_middle.
It's not used in `rustc_middle`, and `rustc_trait_selection` is a better
place for it.
2025-07-31 11:50:36 +10:00
bors
32e7a4b92b Auto merge of #144405 - lcnr:hir-typeck-uniquify, r=BoxyUwU
uniquify root goals during HIR typeck

We need to rely on region identity to deal with hangs such as https://github.com/rust-lang/trait-system-refactor-initiative/issues/210 and to keep the current behavior of `fn try_merge_responses`.

This is a problem as borrowck starts by replacing each *occurrence* of a region with a unique inference variable. This frequently splits a single region during HIR typeck into multiple distinct regions. As we assume goals to always succeed during borrowck, relying on two occurances of a region being identical during HIR typeck causes ICE. See the now fixed examples in https://github.com/rust-lang/trait-system-refactor-initiative/issues/27 and rust-lang/rust#139409.

We've previously tried to avoid this issue by always *uniquifying* regions when canonicalizing goals. This prevents caching subtrees during canonicalization which resulted in hangs for very large types. People rely on such types in practice, which caused us to revert our attempt to reinstate `#[type_length_limit]` in https://github.com/rust-lang/rust/pull/127670. The complete list of changes here:
- rust-lang/rust#107981
- rust-lang/rust#110180
- rust-lang/rust#114117
- rust-lang/rust#130821

After more consideration, all occurrences of such large types need to happen outside of typeck/borrowck. We know this as we already walk over all types in the MIR body when replacing their regions with nll vars.

This PR therefore enables us to rely on region identity inside of the trait solver by exclusively **uniquifying root goals during HIR typeck**. These are the only goals we assume to hold during borrowck. This is insufficient as type inference variables may "hide" regions we later uniquify. Because of this, we now stash proven goals which depend on inference variables in HIR typeck and reprove them after writeback. This closes https://github.com/rust-lang/trait-system-refactor-initiative/issues/127.

This was originally part of rust-lang/rust#144258 but I've moved it into a separate PR. While I believe we need to rely on region identity to fix the performance issues in some way, I don't know whether rust-lang/rust#144258 is the best approach to actually do so. Regardless of how we deal with the hangs however, this change is necessary and desirable regardless.

r? `@compiler-errors` or `@BoxyUwU`
2025-07-31 00:32:55 +00:00
lcnr
df2e54376c add comment and opaque type fixme 2025-07-30 14:01:37 +02:00
lcnr
b6cbe33aeb handle region dependent goals due to infer vars 2025-07-30 14:01:37 +02:00
lcnr
64a27c2e37 resuse eagerly resolved goal from previous iteration 2025-07-29 09:47:10 +00:00
Cameron Steffen
172af038a7 Rename trait_of_item -> trait_of_assoc 2025-07-28 09:53:50 -05:00
Kivooeo
b8eb046e6e use let chains in mir, resolve, target 2025-07-28 06:10:36 +05:00
Michael Goulet
1e96d7a553 Consider param-env for fast path 2025-07-20 17:45:01 +00:00
bors
1aa5b22465 Auto merge of #143545 - compiler-errors:coroutine-obl, r=oli-obk
`-Zhigher-ranked-assumptions`: Consider WF of coroutine witness when proving outlives assumptions

### TL;DR

This PR introduces an unstable flag `-Zhigher-ranked-assumptions` which tests out a new algorithm for dealing with some of the higher-ranked outlives problems that come from auto trait bounds on coroutines. See:

* rust-lang/rust#110338

While it doesn't fix all of the issues, it certainly fixed many of them, so I'd like to get this landed so people can test the flag on their own code.

### Background

Consider, for example:

```rust
use std::future::Future;

trait Client {
    type Connecting<'a>: Future + Send
    where
        Self: 'a;

    fn connect(&self) -> Self::Connecting<'_>;
}

fn call_connect<C>(c: C) -> impl Future + Send
where
    C: Client + Send + Sync,
{
    async move { c.connect().await }
}
```

Due to the fact that we erase the lifetimes in a coroutine, we can think of the interior type of the async block as something like: `exists<'r, 's> { C, &'r C, C::Connecting<'s> }`. The first field is the `c` we capture, the second is the auto-ref that we perform on the call to `.connect()`, and the third is the resulting future we're awaiting at the first and only await point. Note that every region is uniquified differently in the interior types.

For the async block to be `Send`, we must prove that both of the interior types are `Send`. First, we have an `exists<'r, 's>` binder, which needs to be instantiated universally since we treat the regions in this binder as *unknown*[^exist]. This gives us two types: `{ &'!r C, C::Connecting<'!s> }`. Proving `&'!r C: Send` is easy due to a [`Send`](https://doc.rust-lang.org/nightly/std/marker/trait.Send.html#impl-Send-for-%26T) impl for references.

Proving `C::Connecting<'!s>: Send` can only be done via the item bound, which then requires `C: '!s` to hold (due to the `where Self: 'a` on the associated type definition). Unfortunately, we don't know that `C: '!s` since we stripped away any relationship between the interior type and the param `C`. This leads to a bogus borrow checker error today!

### Approach

Coroutine interiors are well-formed by virtue of them being borrow-checked, as long as their callers are invoking their parent functions in a well-formed way, then substitutions should also be well-formed. Therefore, in our example above, we should be able to deduce the assumption that `C: '!s` holds from the well-formedness of the interior type `C::Connecting<'!s>`.

This PR introduces the notion of *coroutine assumptions*, which are the outlives assumptions that we can assume hold due to the well-formedness of a coroutine's interior types. These are computed alongside the coroutine types in the `CoroutineWitnessTypes` struct. When we instantiate the binder when proving an auto trait for a coroutine, we instantiate the `CoroutineWitnessTypes` and stash these newly instantiated assumptions in the region storage in the `InferCtxt`. Later on in lexical region resolution or MIR borrowck, we use these registered assumptions to discharge any placeholder outlives obligations that we would otherwise not be able to prove.

### How well does it work?

I've added a ton of tests of different reported situations that users have shared on issues like rust-lang/rust#110338, and an (anecdotally) large number of those examples end up working straight out of the box! Some limitations are described below.

### How badly does it not work?

The behavior today is quite rudimentary, since we currently discharge the placeholder assumptions pretty early in region resolution. This manifests itself as some limitations on the code that we accept.

For example, `tests/ui/async-await/higher-ranked-auto-trait-11.rs` continues to fail. In that test, we must prove that a placeholder is equal to a universal for a param-env candidate to hold when proving an auto trait, e.g. `'!1 = 'a` is required to prove `T: Trait<'!1>` in a param-env that has `T: Trait<'a>`. Unfortunately, at that point in the MIR body, we only know that the placeholder is equal to some body-local existential NLL var `'?2`, which only gets equated to the universal `'a` when being stored into the return local later on in MIR borrowck.

This could be fixed by integrating these assumptions into the type outlives machinery in a more first-class way, and delaying things to the end of MIR typeck when we know the full relationship between existential and universal NLL vars. Doing this integration today is quite difficult today.

`tests/ui/async-await/higher-ranked-auto-trait-11.rs` fails because we don't compute the full transitive outlives relations between placeholders. In that test, we have in our region assumptions that some `'!1 = '!2` and `'!2 = '!3`, but we must prove `'!1 = '!3`.

This can be fixed by computing the set of coroutine outlives assumptions in a more transitive way, or as I mentioned above, integrating these assumptions into the type outlives machinery in a more first-class way, since it's already responsible for the transitive outlives assumptions of universals.

### Moving forward

I'm still quite happy with this implementation, and I'd like to land it for testing. I may work on overhauling both the way we compute these coroutine assumptions and also how we deal with the assumptions during (lexical/nll) region checking. But for now, I'd like to give users a chance to try out this new `-Zhigher-ranked-assumptions` flag to uncover more shortcomings.

[^exist]: Instantiating this binder with infer regions would be incomplete, since we'd be asking for *some* instantiation of the interior types, not proving something for *all* instantiations of the interior types.
2025-07-18 02:23:50 +00:00
bors
e466296627 Auto merge of #141762 - compiler-errors:witnesser, r=lcnr
Unify `CoroutineWitness` sooner in typeck, and stall coroutine obligations based off of `TypingEnv`

* Stall coroutine obligations based off of `TypingMode` in the old solver.
* Eagerly assign `TyKind::CoroutineWitness` to the witness arg of coroutines during typeck, rather than deferring them to the end of typeck.

r? lcnr

This is part of https://github.com/rust-lang/rust/issues/143017.
2025-07-17 20:02:22 +00:00
Michael Goulet
216cdb7b22 Eagerly unify coroutine witness in old solver 2025-07-17 17:42:28 +00:00
Michael Goulet
72bc11d146 Unstall obligations by looking for coroutines in old solver 2025-07-17 17:38:23 +00:00
Michael Goulet
96171dc78f Check if type has coroutines before visiting 2025-07-17 17:38:23 +00:00
bors
9cd918bcbb Auto merge of #143879 - fee1-dead-contrib:push-lrlpoouyqqry, r=fmease
parse `const trait Trait`

r? oli-obk or anyone from project-const-traits

cc `@rust-lang/project-const-traits`
2025-07-17 15:54:33 +00:00
Deadbeef
69326878ee parse const trait Trait 2025-07-17 18:06:26 +08:00
Matthias Krüger
14b6ac4d6d
Rollup merge of #143431 - xizheyin:143392, r=compiler-errors
Use relative visibility when noting sealed trait to reduce false positive

Fixes rust-lang/rust#143392

I used relative visibility instead of just determining if it's public or not.

r? compiler
2025-07-17 10:41:44 +02:00
bors
f8f6997469 Auto merge of #144044 - fmease:rollup-kg413pt, r=fmease
Rollup of 15 pull requests

Successful merges:

 - rust-lang/rust#142304 (tests: Add `RUST_BACKTRACE` and `-Cpanic` revisions to `panic-main.rs` test)
 - rust-lang/rust#143388 (Various refactors to the LTO handling code)
 - rust-lang/rust#143409 (Enable xgot feature for mips64 musl targets)
 - rust-lang/rust#143592 (UWP: link ntdll functions using raw-dylib)
 - rust-lang/rust#143595 (add `const_make_global`; err for `const_allocate` ptrs if didn't call)
 - rust-lang/rust#143678 (Added error for invalid char cast)
 - rust-lang/rust#143820 (Fixed a core crate compilation failure when enabling the `optimize_for_size` feature on some targets)
 - rust-lang/rust#143829 (Trim `BorrowedCursor` API)
 - rust-lang/rust#143851 (ci cleanup: rustdoc-gui-test now installs browser-ui-test)
 - rust-lang/rust#143856 (Linting public reexport of private dependencies)
 - rust-lang/rust#143895 (Dont collect assoc ty item bounds from trait where clause for host effect predicates)
 - rust-lang/rust#143922 (Improve path segment joining)
 - rust-lang/rust#143964 (Fix handling of SCRIPT_ARG in docker images)
 - rust-lang/rust#144002 (Update poison.rs)
 - rust-lang/rust#144016 (trait_sel: `MetaSized` always holds temporarily)

r? `@ghost`
`@rustbot` modify labels: rollup
2025-07-17 05:24:30 +00:00
León Orell Valerian Liehr
d5a471c07e
Rollup merge of #144016 - davidtwco:sized-hierarchy-temp-pseudo-revert, r=lcnr
trait_sel: `MetaSized` always holds temporarily

As a temporary measure while a proper fix for `tests/ui/sized-hierarchy/incomplete-inference-issue-143992.rs` is implemented, make `MetaSized` obligations always hold. In effect, temporarily reverting the `sized_hierarchy` feature. This is a small change that can be backported.

cc rust-lang/rust#143992
r? ```@lcnr```
2025-07-17 03:58:36 +02:00
Nicholas Nethercote
6414352bff Use join_path_syms in one more place.
This one is a bit marginal, because the segments are a mix of symbols
and strings.
2025-07-17 08:37:20 +10:00
David Wood
8d64937dc2
trait_sel: MetaSized always holds temporarily
As a temporary measure while a proper fix for
`tests/ui/sized-hierarchy/incomplete-inference-issue-143992.rs`
is implemented, make `MetaSized` obligations always hold. In effect,
temporarily reverting the `sized_hierarchy` feature. This is a small
change that can be backported.
2025-07-16 12:35:44 +00:00
Michael Goulet
3634f46fdb Add alias for ArgOutlivesPredicate 2025-07-15 16:02:26 +00:00
Michael Goulet
e3f643c706 Consider outlives assumptions when proving auto traits for coroutine interiors 2025-07-15 16:02:26 +00:00
tiif
7356ff7517 Implement other logics 2025-07-15 13:48:30 +00:00
tiif
1e5c7b2877 Add the core logic in old and new solvers 2025-07-15 13:48:30 +00:00
Samuel Tardieu
8fcef5674a
Rollup merge of #143901 - compiler-errors:region-constraint-nits, r=lcnr
Region constraint nits

Couple miscellaneous region constraints that have a bit to do with rust-lang/rust#143545 but stand on their own.
2025-07-14 18:05:47 +02:00
Michael Goulet
f6f2f83043 Simplify make_query_region_constraints 2025-07-13 19:22:17 +00:00
Camille GILLOT
21fd82adbc Retire hir::*ItemRef. 2025-07-13 13:50:01 +00:00
Camille GILLOT
277b0ecf34 Remove hir::AssocItemKind. 2025-07-13 13:50:00 +00:00
bors
b1d2f2c64c Auto merge of #140717 - mejrs:diagnostic_lints, r=oli-obk
Split up the `unknown_or_malformed_diagnostic_attributes` lint

This splits up the lint into the following lint group:
- `unknown_diagnostic_attributes` - triggers if the attribute is unknown to the current compiler
- `misplaced_diagnostic_attributes` - triggers if the attribute exists but it is not placed on the item kind it's meant for
- `malformed_diagnostic_attributes` - triggers if the attribute's syntax or options are invalid
- `malformed_diagnostic_format_literals` - triggers if the format string literal is invalid, for example if it has unpaired curly braces or invalid parameters
- this pr doesn't create it, but future lints for things like deprecations can also go here.

This PR does not start emitting lints in places that previously did not.

## Motivation

I want to have finer control over what `unknown_or_malformed_diagnostic_attributes` does

I have a project with fairly low msrv that is/will have a lower msrv than future diagnostic attributes. So lints will be emitted when I or others compile it on a lower msrv.

At this time, there are two options to silence these lints:

-  `#[allow(unknown_or_malformed_diagnostic_attributes)]` - this risks diagnostic regressions if I (or others) mess up using the attribute, or if the attribute's syntax ever changes.
- write a build script to detect the compiler version and emit cfgs, and then conditionally enable the attribute:
    ```rust
    #[cfg_attr(rust_version_99, diagnostic::new_attr_in_rust_99(thing = ..))]`
    struct Foo;
    ```
    or conditionally `allow`  the lint:
    ```rust
   // lib.rs
   #![cfg_attr(not(current_rust), allow(unknown_or_malformed_diagnostic_attributes))]
   ```

I like to avoid using build scripts if I can, so the following works much better for me. That is what this PR will let me do in the future:
```rust
    #[allow(unknown_diagnostic_attribute, reason = "attribute came out in rust 1.99 but msrv is 1.70")]
    #[diagnostic::new_attr_in_rust_99(thing = ..)]`
    struct Foo;
2025-07-13 01:11:56 +00:00
bors
855e0fe46e Auto merge of #142911 - mejrs:unsized, r=compiler-errors
Remove support for dynamic allocas

Followup to rust-lang/rust#141811
2025-07-11 05:27:32 +00:00
Trevor Gross
58e5c2629d
Rollup merge of #143742 - estebank:borrow-suggestion, r=compiler-errors
Rework borrowing suggestions to use `Expr` instead of just `Span`

In the suggestion machinery for borrowing expressions and types, always use the available obligation `Span` to find the appropriate `Expr` to perform appropriateness checks no the `ExprKind` instead of on the textual snippet corresponding to the `Span`. (We were already doing this, but only for a subset of cases.) This now better handles situations where parentheses and `<>` are needed for correct syntax (`&(foo + bar)`, `(&foo).bar()`, `<&Foo>::bar()`, etc.).

Unify the logic for the case where `&` *and* `&mut` are appropriate with the logic for only one of those cases. (Instead of having two branches for emitting the suggestion, we now have a single one, using `Diag::multipart_suggestions` always.)

Handle the case when `S::foo()` should have been `<&S>::foo()` (instead of suggesting the prior `&S::foo()`. Fix rust-lang/rust#143393.

Make `Diag::multipart_suggestions` always verbose. CC rust-lang/rust#141973.
2025-07-10 20:20:40 -04:00
mejrs
a7bf5c4fa2 Split up the unknown_or_malformed_diagnostic_attributes lint 2025-07-11 01:24:24 +02:00
Matthias Krüger
b4089bf417
Rollup merge of #143640 - oli-obk:const-fn-traits, r=compiler-errors
Constify `Fn*` traits

r? `@compiler-errors` `@fee1-dead`

this should unlock a few things. A few `const_closures` tests have broken even more than before, but that feature is marked as incomplete anyway

cc rust-lang/rust#67792
2025-07-10 20:28:49 +02:00
Esteban Küber
7dfc3e9af4 Rework borrowing suggestions to use Expr instead of just Span
In the suggestion machinery for borrowing expressions and types, always use the available obligation `Span` to find the appropriate `Expr` to perform appropriateness checks no the `ExprKind` instead of on the textual snippet corresponding to the `Span`.

Unify the logic for the case where `&` *and* `&mut` are appropriate with the logic for only one of those cases.

Handle the case when `S::foo()` should have been `<&S>::foo()` (instead of suggesting the prior `&S::foo()`.
2025-07-10 17:23:29 +00:00
bors
e43d139a82 Auto merge of #143538 - compiler-errors:instantiate-auto-trait, r=lcnr
Instantiate auto trait/`Copy`/`Clone`/`Sized` before computing constituent types binder

This makes the binder logic w.r.t. coroutines a bit simpler.

r? lcnr
2025-07-09 19:38:39 +00:00
Trevor Gross
5f9a70ec9f
Rollup merge of #143532 - compiler-errors:mut-ref-sugg, r=davidtwco
More carefully consider span context when suggesting remove `&mut`

Use `find_ancestor_inside` to compute a relative span that is macro-aware, rather than falling back to using BytePos arithmetic which is wrong for `&mut`.

Fixes https://github.com/rust-lang/rust/issues/143523
2025-07-08 22:50:29 -05:00
Trevor Gross
f1517ddae8
Rollup merge of #143499 - compiler-errors:predicates-of-crate, r=davidtwco
Don't call `predicates_of` on a dummy obligation cause's body id

See the test for a brief explanation

Fixes rust-lang/rust#143481.
2025-07-08 22:50:28 -05:00
mejrs
25eb3829e5 Error on moving unsized values rather than ICE'ing 2025-07-08 22:37:12 +02:00
Matthias Krüger
38bfba6124
Rollup merge of #143620 - Muscraft:remove-newline, r=compiler-errors
fix: Remove newline from multiple crate versions note

While working on getting `annotate-snippets` to match `rustc`, `annotate-snippets` was adding an extra new line after [this line](a2d45f73c7/tests/run-make/crate-loading/multiple-dep-versions.stderr (L9)) for [`run-make/crate-loading/multiple-dep-versions.rs`](a2d45f73c7/tests/run-make/crate-loading/multiple-dep-versions.rs). I found out this was because there was an explicit `\n` in the message that `annotate-snippets` was respecting, while `rustc` was [skipping it](2f8eeb2bba/compiler/rustc_errors/src/emitter.rs (L1542)). After talking with ```@estebank,``` I was told to remove the newline from the error message.

r? ```@estebank```
2025-07-08 19:29:41 +02:00
Michael Goulet
bbb409cc68 Instantiate binder for Copy/Clone/Sized eagerly 2025-07-08 16:35:06 +00:00
Michael Goulet
8d2d4eb89a Instantiate auto trait before computing higher-ranked constituent types 2025-07-08 16:33:38 +00:00
Oli Scherer
b1d45f6b3e Remove const_eval_select hack 2025-07-08 15:49:00 +00:00
Oli Scherer
543c860ea6 Constify Fn* traits 2025-07-08 14:36:43 +00:00
Matthias Krüger
b29f039ae3
Rollup merge of #143571 - lcnr:has_nested-bb, r=compiler-errors
remove `has_nested` from builtin candidates

it's no longer necessary

r? types
2025-07-08 03:09:57 +02:00
Scott Schafer
62951c2e07
fix: Remove newline from multiple crate versions note 2025-07-07 18:54:23 -06:00
Matthias Krüger
00a4418158
Rollup merge of #132469 - estebank:issue-132041, r=Nadrieril
Do not suggest borrow that is already there in fully-qualified call

When encountering `&str::from("value")` do not suggest `&&str::from("value")`.

Fix #132041.
2025-07-07 19:55:31 +02:00