Skip to content

Commit d1cd77e

Browse files
Rollup merge of #157119 - petrochenkov:dropin, r=mejrs
ast_lowering: Simplify `resolve_pin_drop_sugar_impl_item` The `trait_item_def_id` and `effective_ident` parts are independent of each other and don't need to interact. Follow up to #156452.
2 parents 75223c5 + 650106e commit d1cd77e

3 files changed

Lines changed: 38 additions & 68 deletions

File tree

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 31 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,50 +1192,34 @@ impl<'hir> LoweringContext<'_, 'hir> {
11921192
})
11931193
}
11941194

1195-
fn resolve_pin_drop_sugar_impl_item(
1195+
fn check_pin_drop_sugar_impl_item(
11961196
&self,
11971197
i: &AssocItem,
11981198
ident: Ident,
1199-
span: Span,
1200-
) -> (Ident, Result<DefId, ErrorGuaranteed>) {
1201-
let trait_item_def_id = self
1202-
.get_partial_res(i.id)
1203-
.and_then(|r| r.expect_full_res().opt_def_id())
1204-
.ok_or_else(|| {
1205-
self.dcx().span_delayed_bug(span, "could not resolve trait item being implemented")
1206-
});
1207-
1208-
let is_pin_drop_sugar = match &i.kind {
1209-
AssocItemKind::Fn(fn_kind) => fn_kind.is_pin_drop_sugar(),
1210-
_ => false,
1211-
};
1212-
let def_id = match trait_item_def_id {
1213-
Ok(def_id) => def_id,
1214-
Err(guar) => return (ident, Err(guar)),
1215-
};
1216-
if !is_pin_drop_sugar {
1217-
return (ident, Ok(def_id));
1218-
}
1219-
1220-
let is_drop_pin_drop = self
1221-
.tcx
1222-
.lang_items()
1223-
.drop_trait()
1224-
.is_some_and(|drop_trait| self.tcx.parent(def_id) == drop_trait);
1225-
if is_drop_pin_drop {
1226-
// Associated item collection still derives the impl item's name from HIR.
1227-
return (Ident::new(sym::pin_drop, ident.span), Ok(def_id));
1199+
trait_item: Result<DefId, ErrorGuaranteed>,
1200+
) -> Ident {
1201+
if let AssocItemKind::Fn(fn_kind) = &i.kind
1202+
&& fn_kind.is_pin_drop_sugar()
1203+
{
1204+
if let Ok(trait_item) = trait_item
1205+
&& self
1206+
.tcx
1207+
.lang_items()
1208+
.drop_trait()
1209+
.is_none_or(|drop_trait| self.tcx.parent(trait_item) != drop_trait)
1210+
{
1211+
self.dcx()
1212+
.struct_span_err(
1213+
i.span,
1214+
"method `drop` with `&pin mut self` is only supported for the `Drop` trait",
1215+
)
1216+
.with_span_label(i.span, "not a `Drop::pin_drop` implementation")
1217+
.emit();
1218+
}
1219+
return Ident::new(sym::pin_drop, ident.span);
12281220
}
12291221

1230-
let guar = self
1231-
.dcx()
1232-
.struct_span_err(
1233-
i.span,
1234-
"method `drop` with `&pin mut self` is only supported for the `Drop` trait",
1235-
)
1236-
.with_span_label(i.span, "not a `Drop::pin_drop` implementation")
1237-
.emit();
1238-
(ident, Err(guar))
1222+
ident
12391223
}
12401224

12411225
fn lower_impl_item(
@@ -1356,8 +1340,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
13561340

13571341
let span = self.lower_span(i.span);
13581342
let (effective_ident, impl_kind) = if is_in_trait_impl {
1359-
let (effective_ident, trait_item_def_id) =
1360-
self.resolve_pin_drop_sugar_impl_item(i, ident, span);
1343+
let trait_item_def_id = self
1344+
.get_partial_res(i.id)
1345+
.and_then(|r| r.expect_full_res().opt_def_id())
1346+
.ok_or_else(|| {
1347+
self.dcx()
1348+
.span_delayed_bug(span, "could not resolve trait item being implemented")
1349+
});
1350+
let effective_ident = self.check_pin_drop_sugar_impl_item(i, ident, trait_item_def_id);
13611351
(effective_ident, ImplItemImplKind::Trait { defaultness, trait_item_def_id })
13621352
} else {
13631353
(ident, ImplItemImplKind::Inherent { vis_span: self.lower_span(i.vis.span) })

tests/ui/pin-ergonomics/pinned-drop-sugar-not-other-traits.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ trait NeedsPinDrop {
1212
}
1313

1414
impl NeedsPinDrop for S {
15-
//~^ ERROR not all trait items implemented, missing: `pin_drop` [E0046]
1615
fn drop(&pin mut self) {}
1716
//~^ ERROR method `drop` with `&pin mut self` is only supported for the `Drop` trait
1817
}
@@ -53,7 +52,6 @@ mod local_drop_trait {
5352
}
5453

5554
impl Drop for S {
56-
//~^ ERROR not all trait items implemented, missing: `pin_drop` [E0046]
5755
fn drop(&pin mut self) {}
5856
//~^ ERROR method `drop` with `&pin mut self` is only supported for the `Drop` trait
5957
}
Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0407]: method `drop` is not a member of trait `HasDrop`
2-
--> $DIR/pinned-drop-sugar-not-other-traits.rs:26:5
2+
--> $DIR/pinned-drop-sugar-not-other-traits.rs:25:5
33
|
44
LL | fn drop(&pin mut self) {}
55
| ^^^----^^^^^^^^^^^^^^^^^^
@@ -8,7 +8,7 @@ LL | fn drop(&pin mut self) {}
88
| not a member of trait `HasDrop`
99

1010
error[E0407]: method `drop` is not a member of trait `HasPinnedDropReceiver`
11-
--> $DIR/pinned-drop-sugar-not-other-traits.rs:36:5
11+
--> $DIR/pinned-drop-sugar-not-other-traits.rs:35:5
1212
|
1313
LL | fn drop(&pin mut self) {}
1414
| ^^^----^^^^^^^^^^^^^^^^^^
@@ -17,28 +17,19 @@ LL | fn drop(&pin mut self) {}
1717
| not a member of trait `HasPinnedDropReceiver`
1818

1919
error: method `drop` with `&pin mut self` is only supported for the `Drop` trait
20-
--> $DIR/pinned-drop-sugar-not-other-traits.rs:16:5
20+
--> $DIR/pinned-drop-sugar-not-other-traits.rs:15:5
2121
|
2222
LL | fn drop(&pin mut self) {}
2323
| ^^^^^^^^^^^^^^^^^^^^^^^^^ not a `Drop::pin_drop` implementation
2424

2525
error: method `drop` with `&pin mut self` is only supported for the `Drop` trait
26-
--> $DIR/pinned-drop-sugar-not-other-traits.rs:57:9
26+
--> $DIR/pinned-drop-sugar-not-other-traits.rs:55:9
2727
|
2828
LL | fn drop(&pin mut self) {}
2929
| ^^^^^^^^^^^^^^^^^^^^^^^^^ not a `Drop::pin_drop` implementation
3030

31-
error[E0046]: not all trait items implemented, missing: `pin_drop`
32-
--> $DIR/pinned-drop-sugar-not-other-traits.rs:14:1
33-
|
34-
LL | fn pin_drop(self: Pin<&mut Self>);
35-
| ---------------------------------- `pin_drop` from trait
36-
...
37-
LL | impl NeedsPinDrop for S {
38-
| ^^^^^^^^^^^^^^^^^^^^^^^ missing `pin_drop` in implementation
39-
4031
error[E0046]: not all trait items implemented, missing: `drop`
41-
--> $DIR/pinned-drop-sugar-not-other-traits.rs:24:1
32+
--> $DIR/pinned-drop-sugar-not-other-traits.rs:23:1
4233
|
4334
LL | fn drop(self: Pin<&mut Self>);
4435
| ------------------------------ `drop` from trait
@@ -47,24 +38,15 @@ LL | impl HasDrop for S {
4738
| ^^^^^^^^^^^^^^^^^^ missing `drop` in implementation
4839

4940
error[E0046]: not all trait items implemented, missing: `drop`
50-
--> $DIR/pinned-drop-sugar-not-other-traits.rs:34:1
41+
--> $DIR/pinned-drop-sugar-not-other-traits.rs:33:1
5142
|
5243
LL | fn drop(self: &pin mut Self);
5344
| ----------------------------- `drop` from trait
5445
...
5546
LL | impl HasPinnedDropReceiver for S {
5647
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ missing `drop` in implementation
5748

58-
error[E0046]: not all trait items implemented, missing: `pin_drop`
59-
--> $DIR/pinned-drop-sugar-not-other-traits.rs:55:5
60-
|
61-
LL | fn pin_drop(self: Pin<&mut Self>);
62-
| ---------------------------------- `pin_drop` from trait
63-
...
64-
LL | impl Drop for S {
65-
| ^^^^^^^^^^^^^^^ missing `pin_drop` in implementation
66-
67-
error: aborting due to 8 previous errors
49+
error: aborting due to 6 previous errors
6850

6951
Some errors have detailed explanations: E0046, E0407.
7052
For more information about an error, try `rustc --explain E0046`.

0 commit comments

Comments
 (0)