Skip to content

AsyncDrop trait without sync Drop generates an error #142606

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 1 commit into
base: master
Choose a base branch
from

Conversation

azhogin
Copy link
Contributor

@azhogin azhogin commented Jun 17, 2025

When type implements AsyncDrop trait, it must also implement sync Drop trait to be used in sync context and unwinds.
This PR adds error generation in such a case.

Fixes: #140696

@rustbot
Copy link
Collaborator

rustbot commented Jun 17, 2025

r? @oli-obk

rustbot has assigned @oli-obk.
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

@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. labels Jun 17, 2025
@azhogin azhogin force-pushed the azhogin/async-drop-without-sync-drop-error branch from 73c7006 to f1df989 Compare June 17, 2025 03:30
if let Some(async_dtor) = adt_async_destructor(tcx, def_id) {
// When type has AsyncDrop impl, but doesn't have Drop impl, generate error
let span = tcx.def_span(def_id.to_def_id());
let impl_span = tcx.def_span(async_dtor.impl_did);
Copy link
Contributor

Choose a reason for hiding this comment

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

Make this the main span. Unsure if pointing to the type is useful, we could just leave it off entirely

@@ -46,6 +46,10 @@ hir_analysis_associated_type_trait_uninferred_generic_params = cannot use the {$
hir_analysis_associated_type_trait_uninferred_generic_params_multipart_suggestion = use a fully qualified path with explicit lifetimes
hir_analysis_async_drop_without_sync_drop = `AsyncDrop` impl without `Drop` impl
.help = type implementing `AsyncDrop` trait must also implement `Drop` trait to be used in sync context and unwinds
Copy link
Contributor

Choose a reason for hiding this comment

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

Which "sync contexts" are currently remaining besides unwinds?
Ideally async drop should just always be called, but while it's not it would be good to list the unimplemented cases somewhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sync context - any sync function, where AsyncDrop type is dropped inside.
fn foo() { _ = Dropee::new(); }, where Dropee has only AsyncDrop trait without Drop trait.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, I thought cases like this are already reported as errors (they should be).

Copy link
Contributor

Choose a reason for hiding this comment

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

Yea that's an alternative to this PR (or that allows reverting this PR): error at use sites of sync drop of types that have no Drop impl but an AsyncDrop impl. This has some fun implications though, as we'd need to make sure that Copy and AsyncDrop also need to be mutually exclusive, just like Copy and Drop are. Or more generally: any logic checking for Drop needs to also check for AsyncDrop and handle that.

So it may be better long term to merge various things like adt_destructor and adt_async_destructor to only return a single Option with an enum inside for either an async or a sync variant, making it easier not to forget to check both together. There are probably many other functions that need similar treatment.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, error at use sites of sync drop may help only after implementing async-drop support for unwinds. Otherwise, we still need both traits.

@azhogin azhogin force-pushed the azhogin/async-drop-without-sync-drop-error branch from f1df989 to eee2d7b Compare June 18, 2025 02:05
@oli-obk
Copy link
Contributor

oli-obk commented Jun 18, 2025

@bors r+ rollup

@bors
Copy link
Collaborator

bors commented Jun 18, 2025

📌 Commit eee2d7b has been approved by oli-obk

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 Jun 18, 2025
Kobzol added a commit to Kobzol/rust that referenced this pull request Jun 18, 2025
…-sync-drop-error, r=oli-obk

AsyncDrop trait without sync Drop generates an error

When type implements `AsyncDrop` trait, it must also implement sync `Drop` trait to be used in sync context and unwinds.
This PR adds error generation in such a case.

Fixes: rust-lang#140696
bors added a commit that referenced this pull request Jun 18, 2025
Rollup of 12 pull requests

Successful merges:

 - #135656 (Add `-Z hint-mostly-unused` to tell rustc that most of a crate will go unused)
 - #140774 (Affirm `-Cforce-frame-pointers=off` does not override)
 - #141610 (Stabilize `feature(generic_arg_infer)`)
 - #142123 (Implement initial support for timing sections (`--json=timings`))
 - #142383 (CodeGen: rework Aggregate implemention for rvalue_creates_operand cases)
 - #142502 (rustdoc_json: improve handling of generic args)
 - #142591 (Add spawn APIs for BootstrapCommand to support deferred command execution)
 - #142606 (AsyncDrop trait without sync Drop generates an error)
 - #142619 (apply clippy::or_fun_call)
 - #142624 (Actually take `--build` into account in bootstrap)
 - #142627 (Add `StepMetadata` to describe steps)
 - #142660 (remove joboet from review rotation)

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
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.

async drop not work without sync drop
5 participants