Rollup merge of #67823 - euclio:drop-improvements, r=petrochenkov

improve some `Drop`-related error messages
This commit is contained in:
Guillaume Gomez 2020-01-04 13:17:29 +01:00 committed by GitHub
commit 1140ceebcc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 151 additions and 111 deletions

View file

@ -47,7 +47,6 @@ pub fn check_drop_impl(tcx: TyCtxt<'_>, drop_impl_did: DefId) -> Result<(), Erro
ensure_drop_predicates_are_implied_by_item_defn(
tcx,
drop_impl_did,
dtor_predicates,
adt_def.did,
self_to_impl_substs,
@ -95,16 +94,23 @@ fn ensure_drop_params_and_item_params_correspond<'tcx>(
}
Err(_) => {
let item_span = tcx.def_span(self_type_did);
let self_descr = tcx
.def_kind(self_type_did)
.map(|kind| kind.descr(self_type_did))
.unwrap_or("type");
struct_span_err!(
tcx.sess,
drop_impl_span,
E0366,
"Implementations of Drop cannot be specialized"
"`Drop` impls cannot be specialized"
)
.span_note(
item_span,
"Use same sequence of generic type and region \
parameters that is on the struct/enum definition",
&format!(
"use the same sequence of generic type, lifetime and const parameters \
as the {} definition",
self_descr,
),
)
.emit();
return Err(ErrorReported);
@ -143,7 +149,6 @@ fn ensure_drop_params_and_item_params_correspond<'tcx>(
/// implied by assuming the predicates attached to self_type_did.
fn ensure_drop_predicates_are_implied_by_item_defn<'tcx>(
tcx: TyCtxt<'tcx>,
drop_impl_did: DefId,
dtor_predicates: ty::GenericPredicates<'tcx>,
self_type_did: DefId,
self_to_impl_substs: SubstsRef<'tcx>,
@ -187,8 +192,6 @@ fn ensure_drop_predicates_are_implied_by_item_defn<'tcx>(
let self_type_hir_id = tcx.hir().as_local_hir_id(self_type_did).unwrap();
let drop_impl_span = tcx.def_span(drop_impl_did);
// We can assume the predicates attached to struct/enum definition
// hold.
let generic_assumptions = tcx.predicates_of(self_type_did);
@ -205,7 +208,7 @@ fn ensure_drop_predicates_are_implied_by_item_defn<'tcx>(
// just to look for all the predicates directly.
assert_eq!(dtor_predicates.parent, None);
for (predicate, _) in dtor_predicates.predicates {
for (predicate, predicate_sp) in dtor_predicates.predicates {
// (We do not need to worry about deep analysis of type
// expressions etc because the Drop impls are already forced
// to take on a structure that is roughly an alpha-renaming of
@ -241,18 +244,17 @@ fn ensure_drop_predicates_are_implied_by_item_defn<'tcx>(
if !assumptions_in_impl_context.iter().any(predicate_matches_closure) {
let item_span = tcx.hir().span(self_type_hir_id);
let self_descr =
tcx.def_kind(self_type_did).map(|kind| kind.descr(self_type_did)).unwrap_or("type");
struct_span_err!(
tcx.sess,
drop_impl_span,
*predicate_sp,
E0367,
"The requirement `{}` is added only by the Drop impl.",
predicate
)
.span_note(
item_span,
"The same requirement must be part of \
the struct/enum definition",
"`Drop` impl requires `{}` but the {} it is implemented for does not",
predicate,
self_descr,
)
.span_note(item_span, "the implementor must specify the same requirement")
.emit();
result = Err(ErrorReported);
}

View file

@ -13,7 +13,6 @@ use rustc::ty::util::CopyImplementationError;
use rustc::ty::TypeFoldable;
use rustc::ty::{self, Ty, TyCtxt};
use hir::Node;
use rustc::hir::def_id::DefId;
use rustc::hir::{self, ItemKind};
@ -51,35 +50,25 @@ impl<'tcx> Checker<'tcx> {
}
fn visit_implementation_of_drop(tcx: TyCtxt<'_>, impl_did: DefId) {
if let ty::Adt(..) = tcx.type_of(impl_did).kind {
/* do nothing */
} else {
// 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"
);
}
// Destructors only work on nominal types.
if let ty::Adt(..) | ty::Error = tcx.type_of(impl_did).kind {
return;
}
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) {