about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAndrew Zhogin <andrew.zhogin@gmail.com>2025-05-21 15:21:01 +0700
committerAndrew Zhogin <andrew.zhogin@gmail.com>2025-05-21 18:18:27 +0700
commitdc794f18a425a20563943ea4201b34ebea2aa017 (patch)
tree3d7efcba11a5c03999d4f940dacc383d7eccb925
parent59372f2c81ba74554d9a71b12a4ed7f29adb33a2 (diff)
downloadrust-dc794f18a425a20563943ea4201b34ebea2aa017.tar.gz
rust-dc794f18a425a20563943ea4201b34ebea2aa017.zip
When AsyncDrop impl is empty, sync drop generated in elaborator (Fixes #140974)
-rw-r--r--compiler/rustc_mir_transform/src/elaborate_drop.rs24
-rw-r--r--tests/ui/async-await/async-drop/elaborate-index-out-of-bounds.rs (renamed from tests/crashes/140974.rs)7
-rw-r--r--tests/ui/async-await/async-drop/elaborate-index-out-of-bounds.stderr11
3 files changed, 39 insertions, 3 deletions
diff --git a/compiler/rustc_mir_transform/src/elaborate_drop.rs b/compiler/rustc_mir_transform/src/elaborate_drop.rs
index 73a58160a6a..5d86b1353c0 100644
--- a/compiler/rustc_mir_transform/src/elaborate_drop.rs
+++ b/compiler/rustc_mir_transform/src/elaborate_drop.rs
@@ -251,7 +251,29 @@ where
                     span_bug!(span, "invalid `AsyncDrop` impl_source: {:?}", impl_source);
                 }
             };
-            let drop_fn_def_id = tcx.associated_item_def_ids(drop_trait)[0];
+            // impl_item_refs may be empty if drop fn is not implemented in 'impl AsyncDrop for ...'
+            // (#140974).
+            // Such code will report error, so just generate sync drop here and return
+            let Some(drop_fn_def_id) =
+                tcx.associated_item_def_ids(drop_trait).into_iter().nth(0).copied()
+            else {
+                tcx.dcx().span_delayed_bug(
+                    self.elaborator.body().span,
+                    "AsyncDrop type without correct `async fn drop(...)`.",
+                );
+                self.elaborator.patch().patch_terminator(
+                    pin_obj_bb,
+                    TerminatorKind::Drop {
+                        place,
+                        target: succ,
+                        unwind: unwind.into_action(),
+                        replace: false,
+                        drop: None,
+                        async_fut: None,
+                    },
+                );
+                return pin_obj_bb;
+            };
             let drop_fn = Ty::new_fn_def(tcx, drop_fn_def_id, trait_args);
             let sig = drop_fn.fn_sig(tcx);
             let sig = tcx.instantiate_bound_regions_with_erased(sig);
diff --git a/tests/crashes/140974.rs b/tests/ui/async-await/async-drop/elaborate-index-out-of-bounds.rs
index ac1051a64fd..bd0a95eb1e4 100644
--- a/tests/crashes/140974.rs
+++ b/tests/ui/async-await/async-drop/elaborate-index-out-of-bounds.rs
@@ -1,5 +1,7 @@
-//@ known-bug: #140974
-//@edition:2021
+//@ edition: 2024
+// Ex-ICE: #140974
+#![crate_type = "lib"]
+#![allow(incomplete_features)]
 #![feature(async_drop)]
 use core::future::AsyncDrop;
 
@@ -10,5 +12,6 @@ impl Drop for HasIncompleteAsyncDrop {
     fn drop(&mut self) {}
 }
 impl AsyncDrop for HasIncompleteAsyncDrop {
+    //~^ ERROR: not all trait items implemented, missing: `drop` [E0046]
     // not implemented yet..
 }
diff --git a/tests/ui/async-await/async-drop/elaborate-index-out-of-bounds.stderr b/tests/ui/async-await/async-drop/elaborate-index-out-of-bounds.stderr
new file mode 100644
index 00000000000..d8582398c79
--- /dev/null
+++ b/tests/ui/async-await/async-drop/elaborate-index-out-of-bounds.stderr
@@ -0,0 +1,11 @@
+error[E0046]: not all trait items implemented, missing: `drop`
+  --> $DIR/elaborate-index-out-of-bounds.rs:14:1
+   |
+LL | impl AsyncDrop for HasIncompleteAsyncDrop {
+   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ missing `drop` in implementation
+   |
+   = help: implement the missing item: `async fn drop(self: Pin<&mut Self>) { todo!() }`
+
+error: aborting due to 1 previous error
+
+For more information about this error, try `rustc --explain E0046`.