Skip to content

Avoid leaking opaque hidden types via auto trait candidates - #159589

Open
bit-aloo wants to merge 6 commits into
rust-lang:mainfrom
bit-aloo:2026-07-29-opaque-type
Open

Avoid leaking opaque hidden types via auto trait candidates#159589
bit-aloo wants to merge 6 commits into
rust-lang:mainfrom
bit-aloo:2026-07-29-opaque-type

Conversation

@bit-aloo

@bit-aloo bit-aloo commented Jul 20, 2026

Copy link
Copy Markdown
Member

@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. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver) labels Jul 20, 2026
@rustbot

rustbot commented Jul 20, 2026

Copy link
Copy Markdown
Collaborator

r? @nnethercote

rustbot has assigned @nnethercote.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 74 candidates
  • Random selection from 16 candidates

@bit-aloo

Copy link
Copy Markdown
Member Author

r? @lcnr

@rustbot rustbot assigned lcnr and unassigned nnethercote Jul 20, 2026
Comment thread compiler/rustc_next_trait_solver/src/solve/trait_goals.rs Outdated
Comment thread compiler/rustc_next_trait_solver/src/solve/trait_goals.rs Outdated
Comment thread compiler/rustc_next_trait_solver/src/solve/trait_goals.rs
@bit-aloo
bit-aloo requested a review from lcnr July 21, 2026 07:10
@rust-bors

This comment has been minimized.

@rust-cloud-vms
rust-cloud-vms Bot force-pushed the 2026-07-29-opaque-type branch from 2e546e0 to 7c319d5 Compare July 23, 2026 15:28
Comment thread compiler/rustc_next_trait_solver/src/solve/assembly/structural_traits.rs Outdated
@rust-cloud-vms
rust-cloud-vms Bot force-pushed the 2026-07-29-opaque-type branch from 7c319d5 to c54dd33 Compare July 25, 2026 17:42
@rustbot

rustbot commented Jul 25, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred to the core trait solver

cc @rust-lang/initiative-trait-system-refactor

@bit-aloo
bit-aloo requested a review from lcnr July 26, 2026 00:12
}
}

pub(in crate::solve) fn consider_auto_trait_candidate_for_opaque_ty<D, I>(

@lcnr lcnr Jul 27, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please move this function into trait_goals.rs 🤔

it is only used from there and it doesn't feel "purely structural" enough to belong in this file

View changes since the review

return false;
}

let ty::Alias(_, ty::AliasTy { kind: ty::Opaque { def_id, .. }, .. }) =

@lcnr lcnr Jul 27, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let ty::Alias(_, ty::AliasTy { kind: ty::Opaque { def_id, .. }, .. }) =
let ty::Alias(ty::Rigid::Yes, ty::AliasTy { kind: ty::Opaque { def_id, .. }, .. }) =

View changes since the review

return false;
};

!matches!(tcx.opaque_ty_origin(*def_id), hir::OpaqueTyOrigin::AsyncFn { .. })

@lcnr lcnr Jul 27, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bit-aloo bit-aloo Jul 30, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we remove this we have two failing test:

[ui] tests/ui/traits/error-reporting/leaking-vars-in-cause-code-1.rs
[ui] tests/ui/traits/next-solver/auto-with-drop_tracking_mir.rs#fail

For auto-with-drop_tracking_mir.rs, we lose the cause and .await location:

@@ -1,29 +1,12 @@
-error[E0277]: `dyn AsyncFn<Fut = Pin<Box<dyn Future<Output = ()> + Send>>>` cannot be shared between threads safely
+error[E0277]: `impl Future<Output = ()>` cannot be sent between threads safely
   --> $DIR/leaking-vars-in-cause-code-1.rs:32:17
    |
 LL |     assert_send(cursed_fut());
-   |     ----------- ^^^^^^^^^^^^ `dyn AsyncFn<Fut = Pin<Box<dyn Future<Output = ()> + Send>>>` cannot be shared between threads safely
+   |     ----------- ^^^^^^^^^^^^ `impl Future<Output = ()>` cannot be sent between threads safely
    |     |
    |     required by a bound introduced by this call
    |
-   = help: the trait `Sync` is not implemented for `dyn AsyncFn<Fut = Pin<Box<dyn Future<Output = ()> + Send>>>`
-   = note: required for `&dyn AsyncFn<Fut = Pin<Box<dyn Future<Output = ()> + Send>>>` to implement `Send`
-note: required because it's used within this `async` fn body
-  --> $DIR/leaking-vars-in-cause-code-1.rs:19:53
-   |
-LL |   async fn wrap_call<P: AsyncFn + ?Sized>(filter: &P) {
-   |  _____________________________________________________^
-LL | |     filter.call().await;
-LL | | }
-   | |_^
-note: required because it's used within this `async` fn body
-  --> $DIR/leaking-vars-in-cause-code-1.rs:27:23
-   |
-LL |   async fn cursed_fut() {
-   |  _______________________^
-LL | |     wrap_call(get_boxed_fn().as_ref()).await;
-LL | | }
-   | |_^
+   = help: the trait `Send` is not implemented for `impl Future<Output = ()>`
 note: required by a bound in `assert_send`
   --> $DIR/leaking-vars-in-cause-code-1.rs:36:19
    |

For leaking-vars-in-cause-code-1.rs, we also lose the nested async-function context:

@@ -1,21 +1,12 @@
-error: future cannot be sent between threads safely
+error[E0277]: `impl Future<Output = ()>` cannot be sent between threads safely
   --> $DIR/auto-with-drop_tracking_mir.rs:25:13
    |
 LL |     is_send(foo());
-   |             ^^^^^ future returned by `foo` is not `Send`
+   |     ------- ^^^^^ `impl Future<Output = ()>` cannot be sent between threads safely
+   |     |
+   |     required by a bound introduced by this call
    |
-help: the trait `Sync` is not implemented for `NotSync`
-  --> $DIR/auto-with-drop_tracking_mir.rs:8:1
-   |
-LL | struct NotSync;
-   | ^^^^^^^^^^^^^^
-note: future is not `Send` as this value is used across an await
-  --> $DIR/auto-with-drop_tracking_mir.rs:16:11
-   |
-LL |     let x = &NotSync;
-   |         - has type `&NotSync` which is not `Send`
-LL |     bar().await;
-   |           ^^^^^ await occurs here, with `x` maybe used later
+   = help: the trait `Send` is not implemented for `impl Future<Output = ()>`
 note: required by a bound in `is_send`
   --> $DIR/auto-with-drop_tracking_mir.rs:24:24
    |
@@ -24,3 +15,4 @@
 
 error: aborting due to 1 previous error
 
+For more information about this error, try `rustc --explain E0277`.

It seems like we should continue diagnostic traversal for AsyncFn; otherwise, the resulting diagnostic loses most of its useful context. So I’m not sure we should remove this exception. 🤔 Wanna vibe check on what you think?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we should actually just leak foreign types here for diagnostics? Given that the reason we stopped the proof tree visitor here was to avoid leaking non-local closures etc, leaking them for async functions means we should leak em everywhere and just support them in the diagnostics code?

@lcnr lcnr added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 28, 2026
@rust-cloud-vms
rust-cloud-vms Bot force-pushed the 2026-07-29-opaque-type branch from c54dd33 to 1557821 Compare July 30, 2026 10:09
@bit-aloo
bit-aloo requested a review from lcnr July 30, 2026 10:34
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 30, 2026
@rust-bors

This comment has been minimized.

@rust-cloud-vms
rust-cloud-vms Bot force-pushed the 2026-07-29-opaque-type branch from 1557821 to d974280 Compare August 13, 2026 12:22
@bit-aloo

Copy link
Copy Markdown
Member Author

@lcnr this one can get a look, almost to finish..

@lcnr

lcnr commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

@rustbot authot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

auto trait leakage can be used to leak arbitrary types

4 participants