Skip to content
Draft
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
378 changes: 226 additions & 152 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions compiler/rustc_hir_analysis/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ hir_analysis_coercion_between_struct_same_note = expected coercion between the s

hir_analysis_coercion_between_struct_single_note = expected a single field to be coerced, none found

hir_analysis_conflict_impl_drop_and_pin_drop = conflict implementation of `Drop::drop` and `Drop::pin_drop`
.drop_label = `drop(&mut self)` implemented here
.pin_drop_label = `pin_drop(&pin mut self)` implemented here
.suggestion = remove this implementation

hir_analysis_const_bound_for_non_const_trait = `{$modifier}` can only be applied to `const` traits
.label = can't be applied to `{$trait_name}`
.note = `{$trait_name}` can't be used with `{$modifier}` because it isn't `const`
Expand Down
63 changes: 62 additions & 1 deletion compiler/rustc_hir_analysis/src/check/always_applicable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
use rustc_data_structures::fx::FxHashSet;
use rustc_errors::codes::*;
use rustc_errors::{ErrorGuaranteed, struct_span_code_err};
use rustc_hir as hir;
use rustc_infer::infer::{RegionResolutionError, TyCtxtInferExt};
use rustc_infer::traits::{ObligationCause, ObligationCauseCode};
use rustc_middle::span_bug;
use rustc_middle::ty::util::CheckRegions;
use rustc_middle::ty::{self, GenericArgsRef, Ty, TyCtxt, TypingMode};
use rustc_span::sym;
use rustc_trait_selection::regions::InferCtxtRegionExt;
use rustc_trait_selection::traits::{self, ObligationCtxt};

Expand Down Expand Up @@ -70,7 +72,11 @@ pub(crate) fn check_drop_impl(
drop_impl_did,
adt_def.did(),
adt_to_impl_args,
)
)?;

check_drop_xor_pin_drop(tcx, drop_impl_did)?;

Ok(())
}
_ => {
span_bug!(tcx.def_span(drop_impl_did), "incoherent impl of Drop");
Expand Down Expand Up @@ -294,3 +300,58 @@ fn ensure_impl_predicates_are_implied_by_item_defn<'tcx>(

Ok(())
}

/// This function checks at least and at most one of `Drop::drop` and `Drop::pin_drop` is implemented.
fn check_drop_xor_pin_drop<'tcx>(
tcx: TyCtxt<'tcx>,
drop_impl_did: LocalDefId,
) -> Result<(), ErrorGuaranteed> {
let item_impl = tcx.hir_expect_item(drop_impl_did).expect_impl();
let mut drop_span = None;
let mut pin_drop_span = None;
for &impl_item_id in item_impl.items {
let impl_item = tcx.hir_impl_item(impl_item_id);
if let hir::ImplItemKind::Fn(fn_sig, _) = impl_item.kind {
match impl_item.ident.name {
sym::drop => drop_span = Some(fn_sig.span),
sym::pin_drop => pin_drop_span = Some(fn_sig.span),
_ => {}
}
}
}

match (drop_span, pin_drop_span) {
(None, None) => {
if tcx.features().pin_ergonomics() {
return Err(tcx.dcx().emit_err(crate::errors::MissingOneOfTraitItem {
span: tcx.def_span(drop_impl_did),
note: None,
missing_items_msg: "drop`, `pin_drop".to_string(),
}));
} else {
return Err(tcx
.dcx()
.span_delayed_bug(tcx.def_span(drop_impl_did), "missing `Drop::drop`"));
}
}
// FIXME(pin_ergonomics): reject `Drop::drop` for types that support pin-projection.
(Some(_span), None) => {}
(None, Some(span)) => {
if !tcx.features().pin_ergonomics() {
return Err(tcx.dcx().span_delayed_bug(
span,
"`Drop::pin_drop` should be guarded by the library feature gate",
));
}
// FIXME(pin_ergonomics): reject `Drop::pin_drop` for types that don't support pin-projection.
}
(Some(drop_span), Some(pin_drop_span)) => {
return Err(tcx.dcx().emit_err(crate::errors::ConflictImplDropAndPinDrop {
span: tcx.def_span(drop_impl_did),
drop_span,
pin_drop_span,
}));
}
}
Ok(())
}
1 change: 1 addition & 0 deletions compiler/rustc_hir_analysis/src/check/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use rustc_middle::ty::{
TypeVisitable, TypeVisitableExt, fold_regions,
};
use rustc_session::lint::builtin::UNINHABITED_STATIC;
use rustc_span::sym;
use rustc_target::spec::{AbiMap, AbiMapping};
use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
use rustc_trait_selection::error_reporting::traits::on_unimplemented::OnUnimplementedDirective;
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ use rustc_middle::ty::{
use rustc_middle::{bug, span_bug};
use rustc_session::parse::feature_err;
use rustc_span::def_id::CRATE_DEF_ID;
use rustc_span::{BytePos, DUMMY_SP, Ident, Span, Symbol, kw, sym};
use rustc_span::{BytePos, DUMMY_SP, Ident, Span, Symbol, kw};
use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
use rustc_trait_selection::error_reporting::infer::ObligationCauseExt as _;
use rustc_trait_selection::error_reporting::traits::suggestions::ReturnsVisitor;
Expand Down
11 changes: 11 additions & 0 deletions compiler/rustc_hir_analysis/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1707,3 +1707,14 @@ pub(crate) struct AsyncDropWithoutSyncDrop {
#[primary_span]
pub span: Span,
}

#[derive(Diagnostic)]
#[diag(hir_analysis_conflict_impl_drop_and_pin_drop)]
pub(crate) struct ConflictImplDropAndPinDrop {
#[primary_span]
pub span: Span,
#[label(hir_analysis_drop_label)]
pub drop_span: Span,
#[label(hir_analysis_pin_drop_label)]
pub pin_drop_span: Span,
}
8 changes: 6 additions & 2 deletions compiler/rustc_hir_typeck/src/callee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@ pub(crate) fn check_legal_trait_for_method_call(
receiver: Option<Span>,
expr_span: Span,
trait_id: DefId,
_body_id: DefId,
body_id: DefId,
) -> Result<(), ErrorGuaranteed> {
if tcx.is_lang_item(trait_id, LangItem::Drop) {
if tcx.is_lang_item(trait_id, LangItem::Drop)
// Allow calling `Drop::pin_drop` in `Drop::drop`
&& let Some(parent) = tcx.opt_parent(body_id)
&& !tcx.is_lang_item(parent, LangItem::Drop)
{
let sugg = if let Some(receiver) = receiver.filter(|s| !s.is_empty()) {
errors::ExplicitDestructorCallSugg::Snippet {
lo: expr_span.shrink_to_lo(),
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1650,6 +1650,7 @@ symbols! {
pic,
pie,
pin,
pin_drop,
pin_ergonomics,
pin_macro,
platform_intrinsics,
Expand Down
26 changes: 25 additions & 1 deletion library/core/src/ops/drop.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::pin::Pin;

/// Custom code within the destructor.
///
/// When a value is no longer needed, Rust will run a "destructor" on that value.
Expand Down Expand Up @@ -237,5 +239,27 @@ pub const trait Drop {
/// [`mem::drop`]: drop
/// [`ptr::drop_in_place`]: crate::ptr::drop_in_place
#[stable(feature = "rust1", since = "1.0.0")]
fn drop(&mut self);
#[rustc_default_body_unstable(feature = "pin_ergonomics", issue = "130494")]
fn drop(&mut self) {
// SAFETY: `self` is pinned till after dropped.
Drop::pin_drop(unsafe { Pin::new_unchecked(self) })
}

/// Execute the destructor for this type, but different to [`Drop::drop`], it requires `self`
/// to be pinned.
///
/// By implementing this method instead of [`Drop::drop`], the receiver type [`Pin<&mut Self>`]
/// makes sure that the value is pinned until it is deallocated (See [`std::pin` module docs] for
/// more details), which enables us to support field projections of `Self` type safely.
///
/// At least and at most one of [`Drop::drop`] and [`Drop::pin_drop`] should be implemented.
///
// FIXME(pin_ergonomics): add requirements: `pin_drop` must be and must only be used
// for types that support pin-projection.
/// [`Drop::drop`]: crate::ops::Drop::drop
/// [`Drop::pin_drop`]: crate::ops::Drop::pin_drop
/// [`Pin<&mut Self>`]: crate::pin::Pin
/// [`std::pin` module docs]: crate::pin
#[unstable(feature = "pin_ergonomics", issue = "130494")]
fn pin_drop(self: Pin<&mut Self>) {}
}
4 changes: 4 additions & 0 deletions tests/codegen-units/item-collection/drop-glue-eager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ struct StructWithDrop {

impl Drop for StructWithDrop {
//~ MONO_ITEM fn <StructWithDrop as std::ops::Drop>::drop
//~ MONO_ITEM fn <StructWithDrop as std::ops::Drop>::pin_drop
fn drop(&mut self) {}
}

Expand All @@ -24,6 +25,7 @@ enum EnumWithDrop {

impl Drop for EnumWithDrop {
//~ MONO_ITEM fn <EnumWithDrop as std::ops::Drop>::drop
//~ MONO_ITEM fn <EnumWithDrop as std::ops::Drop>::pin_drop
fn drop(&mut self) {}
}

Expand All @@ -34,6 +36,7 @@ enum EnumNoDrop {
// We should be able to monomorphize drops for struct with lifetimes.
impl<'a> Drop for StructWithDropAndLt<'a> {
//~ MONO_ITEM fn <StructWithDropAndLt<'_> as std::ops::Drop>::drop
//~ MONO_ITEM fn <StructWithDropAndLt<'_> as std::ops::Drop>::pin_drop
fn drop(&mut self) {}
}

Expand All @@ -52,5 +55,6 @@ struct StructWithLtAndPredicate<'a: 'a> {
// We should be able to monomorphize drops for struct with lifetimes.
impl<'a> Drop for StructWithLtAndPredicate<'a> {
//~ MONO_ITEM fn <StructWithLtAndPredicate<'_> as std::ops::Drop>::drop
//~ MONO_ITEM fn <StructWithLtAndPredicate<'_> as std::ops::Drop>::pin_drop
fn drop(&mut self) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ struct StructWithDtor(u32);

impl Drop for StructWithDtor {
//~ MONO_ITEM fn <StructWithDtor as std::ops::Drop>::drop
//~ MONO_ITEM fn <StructWithDtor as std::ops::Drop>::pin_drop
fn drop(&mut self) {}
}

Expand Down
1 change: 1 addition & 0 deletions tests/codegen-units/item-collection/generic-drop-glue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ struct NonGenericWithDrop(#[allow(dead_code)] i32);

impl Drop for NonGenericWithDrop {
//~ MONO_ITEM fn <NonGenericWithDrop as std::ops::Drop>::drop
//~ MONO_ITEM fn <NonGenericWithDrop as std::ops::Drop>::pin_drop
fn drop(&mut self) {}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,6 @@ struct PresentDrop;

impl Drop for PresentDrop {
//~ MONO_ITEM fn <PresentDrop as std::ops::Drop>::drop @@ non_generic_closures-cgu.0[External]
//~ MONO_ITEM fn <PresentDrop as std::ops::Drop>::pin_drop @@ non_generic_closures-cgu.0[External]
fn drop(&mut self) {}
}
2 changes: 2 additions & 0 deletions tests/codegen-units/item-collection/non-generic-drop-glue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ struct StructWithDrop {

impl Drop for StructWithDrop {
//~ MONO_ITEM fn <StructWithDrop as std::ops::Drop>::drop
//~ MONO_ITEM fn <StructWithDrop as std::ops::Drop>::pin_drop
fn drop(&mut self) {}
}

Expand All @@ -25,6 +26,7 @@ enum EnumWithDrop {

impl Drop for EnumWithDrop {
//~ MONO_ITEM fn <EnumWithDrop as std::ops::Drop>::drop
//~ MONO_ITEM fn <EnumWithDrop as std::ops::Drop>::pin_drop
fn drop(&mut self) {}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ struct Leaf;

impl Drop for Leaf {
//~ MONO_ITEM fn <Leaf as std::ops::Drop>::drop
//~ MONO_ITEM fn <Leaf as std::ops::Drop>::pin_drop
fn drop(&mut self) {}
}

Expand Down
1 change: 1 addition & 0 deletions tests/codegen-units/item-collection/tuple-drop-glue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ struct Dropped;

impl Drop for Dropped {
//~ MONO_ITEM fn <Dropped as std::ops::Drop>::drop
//~ MONO_ITEM fn <Dropped as std::ops::Drop>::pin_drop
fn drop(&mut self) {}
}

Expand Down
22 changes: 0 additions & 22 deletions tests/crashes/122630.rs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ trait Get {
}

trait Other {
fn uhoh<U: Get>(&self, foo: U, bar: <Self as Get>::Value) where Self: Sized, Self: Get {}
fn uhoh<U: Get>(&self, foo: U, bar: <Self as Get>::Value) where Self: Sized, Self: Get, Self: Get {}
//~^ ERROR the trait bound `Self: Get` is not satisfied
//~| ERROR the trait bound `Self: Get` is not satisfied
}
Expand Down
6 changes: 4 additions & 2 deletions tests/ui/async-await/async-drop/unexpected-sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
#![feature(async_drop)]
use std::future::AsyncDrop;
struct a;
impl Drop for a { //~ ERROR: not all trait items implemented, missing: `drop`
impl Drop for a {
//~^ ERROR: not all trait items implemented, missing: `drop`
fn b() {} //~ ERROR: method `b` is not a member of trait `Drop`
}
impl AsyncDrop for a { //~ ERROR: not all trait items implemented, missing: `drop`
impl AsyncDrop for a {
//~^ ERROR: not all trait items implemented, missing: `drop`
type c = ();
//~^ ERROR: type `c` is not a member of trait `AsyncDrop`
}
Expand Down
19 changes: 13 additions & 6 deletions tests/ui/async-await/async-drop/unexpected-sort.stderr
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
error[E0407]: method `b` is not a member of trait `Drop`
--> $DIR/unexpected-sort.rs:10:5
--> $DIR/unexpected-sort.rs:11:5
|
LL | fn b() {}
| ^^^^^^^^^ not a member of trait `Drop`

error[E0437]: type `c` is not a member of trait `AsyncDrop`
--> $DIR/unexpected-sort.rs:13:5
--> $DIR/unexpected-sort.rs:15:5
|
LL | type c = ();
| ^^^^^^^^^^^^ not a member of trait `AsyncDrop`

error[E0046]: not all trait items implemented, missing: `drop`
--> $DIR/unexpected-sort.rs:9:1
|
LL | impl Drop for a {
| ^^^^^^^^^^^^^^^ missing `drop` in implementation
LL | / impl Drop for a {
LL | |
LL | | fn b() {}
LL | | }
| |_^
|
= help: implement the missing item: `fn drop(&mut self) { todo!() }`
= note: default implementation of `drop` is unstable
= note: use of unstable library feature `pin_ergonomics`
= note: see issue #130494 <https://github.com/rust-lang/rust/issues/130494> for more information
= help: add `#![feature(pin_ergonomics)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error[E0046]: not all trait items implemented, missing: `drop`
--> $DIR/unexpected-sort.rs:12:1
--> $DIR/unexpected-sort.rs:13:1
|
LL | impl AsyncDrop for a {
| ^^^^^^^^^^^^^^^^^^^^ missing `drop` in implementation
Expand Down
Loading
Loading