Skip to content

Commit b41deff

Browse files
authored
Rollup merge of rust-lang#143699 - compiler-errors:async-drop-fund, r=oli-obk
Make `AsyncDrop` check that it's being implemented on a local ADT Fixes rust-lang#143691
2 parents c601fa9 + 728017e commit b41deff

File tree

6 files changed

+53
-3
lines changed

6 files changed

+53
-3
lines changed

compiler/rustc_hir_analysis/messages.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ hir_analysis_dispatch_from_dyn_zst = the trait `DispatchFromDyn` may only be imp
158158
hir_analysis_drop_impl_negative = negative `Drop` impls are not supported
159159
160160
hir_analysis_drop_impl_on_wrong_item =
161-
the `Drop` trait may only be implemented for local structs, enums, and unions
161+
the `{$trait_}` trait may only be implemented for local structs, enums, and unions
162162
.label = must be a struct, enum, or union in the current crate
163163
164164
hir_analysis_drop_impl_reservation = reservation `Drop` impls are not supported

compiler/rustc_hir_analysis/src/coherence/builtin.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ pub(super) fn check_trait<'tcx>(
3737
let lang_items = tcx.lang_items();
3838
let checker = Checker { tcx, trait_def_id, impl_def_id, impl_header };
3939
checker.check(lang_items.drop_trait(), visit_implementation_of_drop)?;
40+
checker.check(lang_items.async_drop_trait(), visit_implementation_of_drop)?;
4041
checker.check(lang_items.copy_trait(), visit_implementation_of_copy)?;
4142
checker.check(lang_items.const_param_ty_trait(), |checker| {
4243
visit_implementation_of_const_param_ty(checker, LangItem::ConstParamTy)
@@ -83,7 +84,10 @@ fn visit_implementation_of_drop(checker: &Checker<'_>) -> Result<(), ErrorGuaran
8384

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

86-
Err(tcx.dcx().emit_err(errors::DropImplOnWrongItem { span: impl_.self_ty.span }))
87+
Err(tcx.dcx().emit_err(errors::DropImplOnWrongItem {
88+
span: impl_.self_ty.span,
89+
trait_: tcx.item_name(checker.impl_header.trait_ref.skip_binder().def_id),
90+
}))
8791
}
8892

8993
fn visit_implementation_of_copy(checker: &Checker<'_>) -> Result<(), ErrorGuaranteed> {

compiler/rustc_hir_analysis/src/errors.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ pub(crate) struct DropImplOnWrongItem {
205205
#[primary_span]
206206
#[label]
207207
pub span: Span,
208+
pub trait_: Symbol,
208209
}
209210

210211
#[derive(Diagnostic)]
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//@ edition: 2018
2+
3+
#![feature(async_drop)]
4+
//~^ WARN the feature `async_drop` is incomplete
5+
6+
use std::future::AsyncDrop;
7+
use std::pin::Pin;
8+
9+
struct Foo;
10+
11+
impl AsyncDrop for &Foo {
12+
//~^ ERROR the `AsyncDrop` trait may only be implemented for
13+
async fn drop(self: Pin<&mut Self>) {}
14+
}
15+
16+
impl AsyncDrop for Pin<Foo> {
17+
//~^ ERROR the `AsyncDrop` trait may only be implemented for
18+
async fn drop(self: Pin<&mut Self>) {}
19+
}
20+
21+
fn main() {}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
warning: the feature `async_drop` is incomplete and may not be safe to use and/or cause compiler crashes
2+
--> $DIR/foreign-fundamental.rs:3:12
3+
|
4+
LL | #![feature(async_drop)]
5+
| ^^^^^^^^^^
6+
|
7+
= note: see issue #126482 <https://github.com/rust-lang/rust/issues/126482> for more information
8+
= note: `#[warn(incomplete_features)]` on by default
9+
10+
error[E0120]: the `AsyncDrop` trait may only be implemented for local structs, enums, and unions
11+
--> $DIR/foreign-fundamental.rs:11:20
12+
|
13+
LL | impl AsyncDrop for &Foo {
14+
| ^^^^ must be a struct, enum, or union in the current crate
15+
16+
error[E0120]: the `AsyncDrop` trait may only be implemented for local structs, enums, and unions
17+
--> $DIR/foreign-fundamental.rs:16:20
18+
|
19+
LL | impl AsyncDrop for Pin<Foo> {
20+
| ^^^^^^^^ must be a struct, enum, or union in the current crate
21+
22+
error: aborting due to 2 previous errors; 1 warning emitted
23+
24+
For more information about this error, try `rustc --explain E0120`.

tests/ui/error-codes/E0120.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
trait MyTrait { fn foo() {} }
1+
trait MyTrait { fn foo(&self) {} }
22

33
impl Drop for dyn MyTrait {
44
//~^ ERROR E0120

0 commit comments

Comments
 (0)