about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMichael Goulet <michael@errs.io>2025-07-09 17:06:39 +0000
committerMichael Goulet <michael@errs.io>2025-07-09 17:07:04 +0000
commit728017ea8f14e0cc343a6ded0e955868d628cc76 (patch)
tree2bd4a06abfab73350cc88e04ccbcefb02fb6e9c2
parent040e2f8b9ff2d76fbe2146d6003e297ed4532088 (diff)
downloadrust-728017ea8f14e0cc343a6ded0e955868d628cc76.tar.gz
rust-728017ea8f14e0cc343a6ded0e955868d628cc76.zip
Make AsyncDrop check that it's being implemented on a local ADT
-rw-r--r--compiler/rustc_hir_analysis/messages.ftl2
-rw-r--r--compiler/rustc_hir_analysis/src/coherence/builtin.rs6
-rw-r--r--compiler/rustc_hir_analysis/src/errors.rs1
-rw-r--r--tests/ui/async-await/async-drop/foreign-fundamental.rs21
-rw-r--r--tests/ui/async-await/async-drop/foreign-fundamental.stderr24
-rw-r--r--tests/ui/error-codes/E0120.rs2
6 files changed, 53 insertions, 3 deletions
diff --git a/compiler/rustc_hir_analysis/messages.ftl b/compiler/rustc_hir_analysis/messages.ftl
index 529d3578985..0429b290476 100644
--- a/compiler/rustc_hir_analysis/messages.ftl
+++ b/compiler/rustc_hir_analysis/messages.ftl
@@ -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
diff --git a/compiler/rustc_hir_analysis/src/coherence/builtin.rs b/compiler/rustc_hir_analysis/src/coherence/builtin.rs
index 8356a0af63c..27948f50a4a 100644
--- a/compiler/rustc_hir_analysis/src/coherence/builtin.rs
+++ b/compiler/rustc_hir_analysis/src/coherence/builtin.rs
@@ -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)
@@ -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> {
diff --git a/compiler/rustc_hir_analysis/src/errors.rs b/compiler/rustc_hir_analysis/src/errors.rs
index c1c82839212..9e29e5f6473 100644
--- a/compiler/rustc_hir_analysis/src/errors.rs
+++ b/compiler/rustc_hir_analysis/src/errors.rs
@@ -205,6 +205,7 @@ pub(crate) struct DropImplOnWrongItem {
     #[primary_span]
     #[label]
     pub span: Span,
+    pub trait_: Symbol,
 }
 
 #[derive(Diagnostic)]
diff --git a/tests/ui/async-await/async-drop/foreign-fundamental.rs b/tests/ui/async-await/async-drop/foreign-fundamental.rs
new file mode 100644
index 00000000000..1c192fccd9f
--- /dev/null
+++ b/tests/ui/async-await/async-drop/foreign-fundamental.rs
@@ -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() {}
diff --git a/tests/ui/async-await/async-drop/foreign-fundamental.stderr b/tests/ui/async-await/async-drop/foreign-fundamental.stderr
new file mode 100644
index 00000000000..7b52329ac99
--- /dev/null
+++ b/tests/ui/async-await/async-drop/foreign-fundamental.stderr
@@ -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`.
diff --git a/tests/ui/error-codes/E0120.rs b/tests/ui/error-codes/E0120.rs
index 35f544fddfb..4e5cc7d6833 100644
--- a/tests/ui/error-codes/E0120.rs
+++ b/tests/ui/error-codes/E0120.rs
@@ -1,4 +1,4 @@
-trait MyTrait { fn foo() {} }
+trait MyTrait { fn foo(&self) {} }
 
 impl Drop for dyn MyTrait {
               //~^ ERROR E0120