Skip to content
Merged
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
15 changes: 14 additions & 1 deletion compiler/rustc_hir_analysis/src/coherence/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@ fn visit_implementation_of_copy(checker: &Checker<'_>) -> Result<(), ErrorGuaran
Err(CopyImplementationError::HasDestructor(did)) => {
let span = tcx.hir_expect_item(impl_did).expect_impl().self_ty.span;
let impl_ = tcx.def_span(did);
Err(tcx.dcx().emit_err(diagnostics::CopyImplOnTypeWithDtor { span, impl_ }))
Err(tcx.dcx().emit_err(diagnostics::TraitImplOnTypeWithDtor {
span,
impl_,
trait_name: sym::Copy,
}))
}
Err(CopyImplementationError::HasUnsafeFields) => {
let span = tcx.hir_expect_item(impl_did).expect_impl().self_ty.span;
Expand Down Expand Up @@ -537,6 +541,15 @@ pub(crate) fn reborrow_info<'tcx>(
assert_field_type_is_copy(tcx, &infcx, impl_did, param_env, field.ty, field.span)?;
}

if let Some(did) = def.destructor(tcx).map(|dtor| dtor.did) {
let impl_ = tcx.def_span(did);
return Err(tcx.dcx().emit_err(diagnostics::TraitImplOnTypeWithDtor {
span,
impl_,
trait_name: sym::Reborrow,
}));
}

Ok(())
}

Expand Down
7 changes: 4 additions & 3 deletions compiler/rustc_hir_analysis/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,13 +292,14 @@ pub(crate) struct FieldAlreadyDeclaredNestedHelp {
}

#[derive(Diagnostic)]
#[diag("the trait `Copy` cannot be implemented for this type; the type has a destructor", code = E0184)]
pub(crate) struct CopyImplOnTypeWithDtor {
#[diag("the trait `{$trait_name}` cannot be implemented for this type; the type has a destructor", code = E0184)]
pub(crate) struct TraitImplOnTypeWithDtor {
#[primary_span]
#[label("`Copy` not allowed on types with destructors")]
#[label("`{$trait_name}` not allowed on types with destructors")]
pub span: Span,
#[note("destructor declared here")]
pub impl_: Span,
pub trait_name: Symbol,
}

#[derive(Diagnostic)]
Expand Down
14 changes: 14 additions & 0 deletions tests/ui/reborrow/reborrow-drop.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//@ compile-flags: --crate-type=lib

#![feature(reborrow)]

use std::marker::Reborrow;

struct MyMut<'a>(&'a mut ());

impl Reborrow for MyMut<'_> {}
//~^ ERROR the trait `Reborrow` cannot be implemented for this type; the type has a destructor [E0184]

impl Drop for MyMut<'_> {
fn drop(&mut self) {}
}
15 changes: 15 additions & 0 deletions tests/ui/reborrow/reborrow-drop.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
error[E0184]: the trait `Reborrow` cannot be implemented for this type; the type has a destructor
--> $DIR/reborrow-drop.rs:9:1
|
LL | impl Reborrow for MyMut<'_> {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Reborrow` not allowed on types with destructors
|
note: destructor declared here
--> $DIR/reborrow-drop.rs:13:5
|
LL | fn drop(&mut self) {}
| ^^^^^^^^^^^^^^^^^^

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0184`.
Loading