Skip to content

Make AsyncDrop check that it's being implemented on a local ADT #143699

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

Merged
merged 1 commit into from
Jul 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ hir_analysis_dispatch_from_dyn_zst = the trait `DispatchFromDyn` may only be imp
hir_analysis_drop_impl_negative = negative `Drop` impls are not supported

hir_analysis_drop_impl_on_wrong_item =
the `Drop` trait may only be implemented for local structs, enums, and unions
the `{$trait_}` trait may only be implemented for local structs, enums, and unions
.label = must be a struct, enum, or union in the current crate

hir_analysis_drop_impl_reservation = reservation `Drop` impls are not supported
Expand Down
6 changes: 5 additions & 1 deletion compiler/rustc_hir_analysis/src/coherence/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub(super) fn check_trait<'tcx>(
let lang_items = tcx.lang_items();
let checker = Checker { tcx, trait_def_id, impl_def_id, impl_header };
checker.check(lang_items.drop_trait(), visit_implementation_of_drop)?;
checker.check(lang_items.async_drop_trait(), visit_implementation_of_drop)?;
checker.check(lang_items.copy_trait(), visit_implementation_of_copy)?;
checker.check(lang_items.const_param_ty_trait(), |checker| {
visit_implementation_of_const_param_ty(checker, LangItem::ConstParamTy)
Expand Down Expand Up @@ -83,7 +84,10 @@ fn visit_implementation_of_drop(checker: &Checker<'_>) -> Result<(), ErrorGuaran

let impl_ = tcx.hir_expect_item(impl_did).expect_impl();

Err(tcx.dcx().emit_err(errors::DropImplOnWrongItem { span: impl_.self_ty.span }))
Err(tcx.dcx().emit_err(errors::DropImplOnWrongItem {
span: impl_.self_ty.span,
trait_: tcx.item_name(checker.impl_header.trait_ref.skip_binder().def_id),
}))
}

fn visit_implementation_of_copy(checker: &Checker<'_>) -> Result<(), ErrorGuaranteed> {
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_hir_analysis/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ pub(crate) struct DropImplOnWrongItem {
#[primary_span]
#[label]
pub span: Span,
pub trait_: Symbol,
}

#[derive(Diagnostic)]
Expand Down
21 changes: 21 additions & 0 deletions tests/ui/async-await/async-drop/foreign-fundamental.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//@ edition: 2018

#![feature(async_drop)]
//~^ WARN the feature `async_drop` is incomplete

use std::future::AsyncDrop;
use std::pin::Pin;

struct Foo;

impl AsyncDrop for &Foo {
//~^ ERROR the `AsyncDrop` trait may only be implemented for
async fn drop(self: Pin<&mut Self>) {}
}

impl AsyncDrop for Pin<Foo> {
//~^ ERROR the `AsyncDrop` trait may only be implemented for
async fn drop(self: Pin<&mut Self>) {}
}

fn main() {}
24 changes: 24 additions & 0 deletions tests/ui/async-await/async-drop/foreign-fundamental.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
warning: the feature `async_drop` is incomplete and may not be safe to use and/or cause compiler crashes
--> $DIR/foreign-fundamental.rs:3:12
|
LL | #![feature(async_drop)]
| ^^^^^^^^^^
|
= note: see issue #126482 <https://github.com/rust-lang/rust/issues/126482> for more information
= note: `#[warn(incomplete_features)]` on by default

error[E0120]: the `AsyncDrop` trait may only be implemented for local structs, enums, and unions
--> $DIR/foreign-fundamental.rs:11:20
|
LL | impl AsyncDrop for &Foo {
| ^^^^ must be a struct, enum, or union in the current crate

error[E0120]: the `AsyncDrop` trait may only be implemented for local structs, enums, and unions
--> $DIR/foreign-fundamental.rs:16:20
|
LL | impl AsyncDrop for Pin<Foo> {
| ^^^^^^^^ must be a struct, enum, or union in the current crate

error: aborting due to 2 previous errors; 1 warning emitted

For more information about this error, try `rustc --explain E0120`.
2 changes: 1 addition & 1 deletion tests/ui/error-codes/E0120.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
trait MyTrait { fn foo() {} }
trait MyTrait { fn foo(&self) {} }

impl Drop for dyn MyTrait {
//~^ ERROR E0120
Expand Down
Loading