clarify that Drop can be implemented for enums and unions too

This commit is contained in:
Andy Russell 2020-01-02 22:24:21 -05:00
parent e589358210
commit e9990bc65f
No known key found for this signature in database
GPG Key ID: BE2221033EDBC374
7 changed files with 41 additions and 42 deletions

View File

@ -13,7 +13,6 @@ use rustc::ty::util::CopyImplementationError;
use rustc::ty::TypeFoldable; use rustc::ty::TypeFoldable;
use rustc::ty::{self, Ty, TyCtxt}; use rustc::ty::{self, Ty, TyCtxt};
use hir::Node;
use rustc::hir::def_id::DefId; use rustc::hir::def_id::DefId;
use rustc::hir::{self, ItemKind}; use rustc::hir::{self, ItemKind};
@ -51,35 +50,25 @@ impl<'tcx> Checker<'tcx> {
} }
fn visit_implementation_of_drop(tcx: TyCtxt<'_>, impl_did: DefId) { fn visit_implementation_of_drop(tcx: TyCtxt<'_>, impl_did: DefId) {
if let ty::Adt(..) = tcx.type_of(impl_did).kind { // Destructors only work on nominal types.
/* do nothing */ if let ty::Adt(..) | ty::Error = tcx.type_of(impl_did).kind {
} else { return;
// Destructors only work on nominal types.
if let Some(impl_hir_id) = tcx.hir().as_local_hir_id(impl_did) {
if let Some(Node::Item(item)) = tcx.hir().find(impl_hir_id) {
let span = match item.kind {
ItemKind::Impl(.., ref ty, _) => ty.span,
_ => item.span,
};
struct_span_err!(
tcx.sess,
span,
E0120,
"the Drop trait may only be implemented on \
structures"
)
.span_label(span, "implementing Drop requires a struct")
.emit();
} else {
bug!("didn't find impl in ast map");
}
} else {
bug!(
"found external impl of Drop trait on \
something other than a struct"
);
}
} }
let impl_hir_id = tcx.hir().as_local_hir_id(impl_did).expect("foreign Drop impl on non-ADT");
let sp = match tcx.hir().expect_item(impl_hir_id).kind {
ItemKind::Impl(.., ty, _) => ty.span,
_ => bug!("expected Drop impl item"),
};
struct_span_err!(
tcx.sess,
sp,
E0120,
"the `Drop` trait may only be implemented for structs, enums, and unions",
)
.span_label(sp, "must be a struct, enum, or union")
.emit();
} }
fn visit_implementation_of_copy(tcx: TyCtxt<'_>, impl_did: DefId) { fn visit_implementation_of_copy(tcx: TyCtxt<'_>, impl_did: DefId) {

View File

@ -1,10 +1,15 @@
impl<'a> Drop for &'a mut isize { impl<'a> Drop for &'a mut isize {
//~^ ERROR the Drop trait may only be implemented on structures //~^ ERROR the `Drop` trait may only be implemented for structs, enums, and unions
//~^^ ERROR E0117 //~^^ ERROR E0117
fn drop(&mut self) { fn drop(&mut self) {
println!("kaboom"); println!("kaboom");
} }
} }
impl Drop for Nonexistent {
//~^ ERROR cannot find type `Nonexistent`
fn drop(&mut self) { }
}
fn main() { fn main() {
} }

View File

@ -1,8 +1,14 @@
error[E0120]: the Drop trait may only be implemented on structures error[E0412]: cannot find type `Nonexistent` in this scope
--> $DIR/drop-on-non-struct.rs:9:15
|
LL | impl Drop for Nonexistent {
| ^^^^^^^^^^^ not found in this scope
error[E0120]: the `Drop` trait may only be implemented for structs, enums, and unions
--> $DIR/drop-on-non-struct.rs:1:19 --> $DIR/drop-on-non-struct.rs:1:19
| |
LL | impl<'a> Drop for &'a mut isize { LL | impl<'a> Drop for &'a mut isize {
| ^^^^^^^^^^^^^ implementing Drop requires a struct | ^^^^^^^^^^^^^ must be a struct, enum, or union
error[E0117]: only traits defined in the current crate can be implemented for arbitrary types error[E0117]: only traits defined in the current crate can be implemented for arbitrary types
--> $DIR/drop-on-non-struct.rs:1:1 --> $DIR/drop-on-non-struct.rs:1:1
@ -15,7 +21,7 @@ LL | impl<'a> Drop for &'a mut isize {
| |
= note: define and implement a trait or new type instead = note: define and implement a trait or new type instead
error: aborting due to 2 previous errors error: aborting due to 3 previous errors
Some errors have detailed explanations: E0117, E0120. Some errors have detailed explanations: E0117, E0120, E0412.
For more information about an error, try `rustc --explain E0117`. For more information about an error, try `rustc --explain E0117`.

View File

@ -1,6 +1,5 @@
impl Drop for u32 {} //~ ERROR E0117 impl Drop for u32 {} //~ ERROR E0117
//~| ERROR the Drop trait may only be implemented on structures //~| ERROR the `Drop` trait may only be implemented for structs, enums, and unions
//~| implementing Drop requires a struct
fn main() { fn main() {
} }

View File

@ -1,8 +1,8 @@
error[E0120]: the Drop trait may only be implemented on structures error[E0120]: the `Drop` trait may only be implemented for structs, enums, and unions
--> $DIR/E0117.rs:1:15 --> $DIR/E0117.rs:1:15
| |
LL | impl Drop for u32 {} LL | impl Drop for u32 {}
| ^^^ implementing Drop requires a struct | ^^^ must be a struct, enum, or union
error[E0117]: only traits defined in the current crate can be implemented for arbitrary types error[E0117]: only traits defined in the current crate can be implemented for arbitrary types
--> $DIR/E0117.rs:1:1 --> $DIR/E0117.rs:1:1

View File

@ -1,8 +1,8 @@
error[E0120]: the Drop trait may only be implemented on structures error[E0120]: the `Drop` trait may only be implemented for structs, enums, and unions
--> $DIR/E0120.rs:3:15 --> $DIR/E0120.rs:3:15
| |
LL | impl Drop for dyn MyTrait { LL | impl Drop for dyn MyTrait {
| ^^^^^^^^^^^ implementing Drop requires a struct | ^^^^^^^^^^^ must be a struct, enum, or union
error: aborting due to previous error error: aborting due to previous error

View File

@ -9,11 +9,11 @@ LL | impl<T> Drop for T where T: A {
where T: ?Sized; where T: ?Sized;
= note: downstream crates may implement trait `A` for type `std::boxed::Box<_>` = note: downstream crates may implement trait `A` for type `std::boxed::Box<_>`
error[E0120]: the Drop trait may only be implemented on structures error[E0120]: the `Drop` trait may only be implemented for structs, enums, and unions
--> $DIR/issue-41974.rs:7:18 --> $DIR/issue-41974.rs:7:18
| |
LL | impl<T> Drop for T where T: A { LL | impl<T> Drop for T where T: A {
| ^ implementing Drop requires a struct | ^ must be a struct, enum, or union
error[E0210]: type parameter `T` must be used as the type parameter for some local type (e.g., `MyStruct<T>`) error[E0210]: type parameter `T` must be used as the type parameter for some local type (e.g., `MyStruct<T>`)
--> $DIR/issue-41974.rs:7:6 --> $DIR/issue-41974.rs:7:6