Skip to content

Rollup of 5 pull requests #145240

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open

Conversation

Zalathar
Copy link
Contributor

@Zalathar Zalathar commented Aug 11, 2025

Successful merges:

r? @ghost
@rustbot modify labels: rollup

Create a similar rollup

fmease and others added 11 commits July 20, 2025 13:30
…lon in expr

The test cases for issue 41731 are about infinite macro recursion that
incorporates `print!` and `println!`. However, they also included
trailing semicolons despite expanding to expressions; that isn't what
these particular test cases are designed to test.

Eliminate the trailing semicolons, to simplify future work on removing
this special case. Every *other* macro that expands to a semicolon in an
expression is a test case for that specifically.
Reject relaxed bounds inside associated type bounds (ATB)

**Reject** relaxed bounds — most notably `?Sized` — inside associated type bounds `TraitRef<AssocTy: …>`.

This was previously accepted without warning despite being incorrect: ATBs are *not* a place where we perform *sized elaboration*, meaning `TraitRef<AssocTy: …>` does *not* elaborate to `TraitRef<AssocTy: Sized + …>` if `…` doesn't contain `?Sized`. Therefore `?Sized` is meaningless. In no other (stable) place do we (intentionally) allow relaxed bounds where we don't also perform sized elab, this is highly inconsistent and confusing! Another point of comparison: For the desugared `$SelfTy: TraitRef, $SelfTy::AssocTy: …` we don't do sized elab either (and thus also don't allow relaxed bounds).

Moreover — as I've alluded to back in rust-lang#135841 (review) — some later validation steps only happen during sized elaboration during HIR ty lowering[^1]. Namely, rejecting duplicates (e.g., `?Trait + ?Trait`) and ensuring that `Trait` in `?Trait` is equal to `Sized`[^2]. As you can probably guess, on stable/master we don't run these checks for ATBs (so we allow even more nonsensical bounds like `Iterator<Item: ?Copy>` despite T-types's ruling established in the FCP'ed rust-lang#135841).

This PR rectifies all of this. I cratered this back in 2025-01-10 with (allegedly) no regressions found ([report](rust-lang#135331 (comment)), [its analysis](rust-lang#135331 (comment))). [However a contributor manually found two occurrences](rust-lang#135229 (comment)) of `TraitRef<AssocTy: ?Sized>` in small hobby projects (presumably via GH code search). I immediately sent downstream PRs: Gui-Yom/turbo-metrics#14, ireina7/summon#1 (however, the owners have showed no reaction so far).

I'm leaning towards banning these forms **without a FCW** because a FCW isn't worth the maintenance cost[^3]. Note that associated type bounds were stabilized in 1.79.0 (released 2024-06-13 which is 13 months ago), so the proliferation of ATBs shouldn't be that high yet. If you think we should do another crater run since the last one was 6 months ago, I'm fine with that.

Fixes rust-lang#135229.

[^1]: I consider this a flaw in the implementation and [I've already added a huge FIXME](https://github.com/rust-lang/rust/blob/82a02aefe07092c737c852daccebf49ca25507e3/compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs#L195-L207).
[^2]: To be more precise, if the internal flag `-Zexperimental-default-bounds` is provided other "default traits" (needs internal feature `lang_items`) are permitted as well (cc closely related internal feature: `more_maybe_bounds`).
[^3]: Having to track this and adding an entire lint whose remnants would remain in the code base forever (we never *fully* remove lints).
Check coroutine upvars in dtorck constraint

Fix rust-lang#144155.

This PR fixes an unsoundness where we were not considering coroutine upvars as drop-live if the coroutine interior types (witness types) had nothing which required drop.

In the case that the coroutine does not have any interior types that need to be dropped, then we don't need to treat all of the upvars as use-live; instead, this PR uses the same logic as closures, and descends into the upvar types to collect anything that must be drop-live. The rest of this PR is reworking the comment to explain the behavior here.

r? `@lcnr` or reassign 😸

---

Just some thoughts --- a proper fix for this whole situation would be to consider `TypingMode` in the `needs_drop` function, and just calling `coroutine_ty.needs_drop(tcx, typing_env)` in the dtorck constraint check.

During MIR building, we should probably use a typing mode that stalls the local coroutines and considers them to be unconditionally drop, or perhaps just stall *all* coroutines in analysis mode. Then in borrowck mode, we can re-check `needs_drop` but descend into witness types properly. rust-lang#144158 implements this experimentally.

This is a pretty involved fix, and conflicts with some in-flight changes (rust-lang#144157) that I have around removing coroutine witnesses altogether. I'm happy to add a FIXME to rework this whole approach, but I don't want to block this quick fix since it's obviously more correct than the status-quo.
…enkov

`NllRegionVariableOrigin` remove `from_forall`

See added comment in the only place it was used.

cc rust-lang#144988 `@amandasystems,` going to merge that PR first.
…=lcnr

Ignore coroutine witness type region args in auto trait confirmation

## The problem

Consider code like:

```
async fn process<'a>() {
    Box::pin(process()).await;
}

fn require_send(_: impl Send) {}

fn main() {
    require_send(process());
}
```

When proving that the coroutine `{coroutine@process}::<'?0>: Send`, we end up instantiating a nested goal `{witness@process}::<'?0>: Send` by synthesizing a witness type from the coroutine's args:

Proving a coroutine witness type implements an auto trait requires looking up the coroutine's witness types. The witness types are a binder that look like `for<'r> { Pin<Box<{coroutine@process}::<'r>>> }`. We instantiate this binder with placeholders and prove `Send` on the witness types. This ends up eventually needing to prove something like `{coroutine@process}::<'!1>: Send`. Repeat this process, and we end up in an overflow during fulfillment, since fulfillment does not use freshening.

This can be visualized with a trait stack that ends up looking like:
* `{coroutine@process}::<'?0>: Send`
  * `{witness@process}::<'?0>: Send`
    * `Pin<Box<{coroutine@process}::<'!1>>>: Send`
      * `{coroutine@process}::<'!1>: Send`
        * ...
          * `{coroutine@process}::<'!2>: Send`
            * `{witness@process}::<'!2>: Send`
              * ...
                * overflow!

The problem here specifically comes from the first step: synthesizing a witness type from the coroutine's args.

## Why wasn't this an issue before?

Specifically, before 63f6845, this wasn't an issue because we were instead extracting the witness from the coroutine type itself. It turns out that given some `{coroutine@process}::<'?0>`, the witness type was actually something like `{witness@process}::<'erased>`!

So why do we end up with a witness type with `'erased` in its args? This is due to the fact that opaque type inference erases all regions from the witness. This is actually explicitly part of opaque type inference -- changing this to actually visit the witness types actually replicates this overflow even with 63f6845 reverted:

https://github.com/rust-lang/rust/blob/ca77504943887037504c7fc0b9bf06dab3910373/compiler/rustc_borrowck/src/type_check/opaque_types.rs#L303-L313

To better understand this difference and how it avoids a cycle, if you look at the trait stack before 63f6845, we end up with something like:

* `{coroutine@process}::<'?0>: Send`
  * `{witness@process}::<'erased>: Send` **<-- THIS CHANGED**
    * `Pin<Box<{coroutine@process}::<'!1>>>: Send`
      * `{coroutine@process}::<'!1>: Send`
        * ...
          * `{coroutine@process}::<'erased>: Send` **<-- THIS CHANGED**
            * `{witness@process}::<'erased>: Send` **<-- THIS CHANGED**
              * coinductive cycle! 🎉

## So what's the fix?

This hack replicates the behavior in opaque type inference to erase regions from the witness type, but instead erasing the regions during auto trait confirmation. This is kinda a hack, but is sound. It does not need to be replicated in the new trait solver, of course.

---

I hope this explanation makes sense.

We could beta backport this instead of the revert rust-lang#145193, but then I'd like to un-revert that on master in this PR along with landing this this hack. Thoughts?

r? lcnr
…i, r=lqd

Fix macro infinite recursion test to not trigger warning about semicolon in expr

The test cases for rust-lang#41731 are about infinite macro recursion that
incorporates `print!` and `println!`. However, they also included
trailing semicolons despite expanding to expressions; that isn't what
these particular test cases are designed to test.

Eliminate the trailing semicolons, to simplify future work on removing
this special case. Every *other* macro that expands to a semicolon in an
expression is a test case for that specifically.
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. rollup A PR which is a rollup labels Aug 11, 2025
@Zalathar
Copy link
Contributor Author

@bors r+ rollup=never p=5

@bors
Copy link
Collaborator

bors commented Aug 11, 2025

📌 Commit 3ce0ee2 has been approved by Zalathar

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 11, 2025
@bors
Copy link
Collaborator

bors commented Aug 11, 2025

⌛ Testing commit 3ce0ee2 with merge 5771665...

bors added a commit that referenced this pull request Aug 11, 2025
Rollup of 5 pull requests

Successful merges:

 - #135331 (Reject relaxed bounds inside associated type bounds (ATB))
 - #144156 (Check coroutine upvars in dtorck constraint)
 - #145091 (`NllRegionVariableOrigin` remove `from_forall`)
 - #145194 (Ignore coroutine witness type region args in auto trait confirmation)
 - #145225 (Fix macro infinite recursion test to not trigger warning about semicolon in expr)

r? `@ghost`
`@rustbot` modify labels: rollup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rollup A PR which is a rollup S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants