Skip to content

Commit 1140cee

Browse files
Rollup merge of #67823 - euclio:drop-improvements, r=petrochenkov
improve some `Drop`-related error messages
2 parents a469b17 + e9990bc commit 1140cee

13 files changed

+151
-111
lines changed

src/librustc_typeck/check/dropck.rs

+18-16
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ pub fn check_drop_impl(tcx: TyCtxt<'_>, drop_impl_did: DefId) -> Result<(), Erro
4747

4848
ensure_drop_predicates_are_implied_by_item_defn(
4949
tcx,
50-
drop_impl_did,
5150
dtor_predicates,
5251
adt_def.did,
5352
self_to_impl_substs,
@@ -95,16 +94,23 @@ fn ensure_drop_params_and_item_params_correspond<'tcx>(
9594
}
9695
Err(_) => {
9796
let item_span = tcx.def_span(self_type_did);
97+
let self_descr = tcx
98+
.def_kind(self_type_did)
99+
.map(|kind| kind.descr(self_type_did))
100+
.unwrap_or("type");
98101
struct_span_err!(
99102
tcx.sess,
100103
drop_impl_span,
101104
E0366,
102-
"Implementations of Drop cannot be specialized"
105+
"`Drop` impls cannot be specialized"
103106
)
104107
.span_note(
105108
item_span,
106-
"Use same sequence of generic type and region \
107-
parameters that is on the struct/enum definition",
109+
&format!(
110+
"use the same sequence of generic type, lifetime and const parameters \
111+
as the {} definition",
112+
self_descr,
113+
),
108114
)
109115
.emit();
110116
return Err(ErrorReported);
@@ -143,7 +149,6 @@ fn ensure_drop_params_and_item_params_correspond<'tcx>(
143149
/// implied by assuming the predicates attached to self_type_did.
144150
fn ensure_drop_predicates_are_implied_by_item_defn<'tcx>(
145151
tcx: TyCtxt<'tcx>,
146-
drop_impl_did: DefId,
147152
dtor_predicates: ty::GenericPredicates<'tcx>,
148153
self_type_did: DefId,
149154
self_to_impl_substs: SubstsRef<'tcx>,
@@ -187,8 +192,6 @@ fn ensure_drop_predicates_are_implied_by_item_defn<'tcx>(
187192

188193
let self_type_hir_id = tcx.hir().as_local_hir_id(self_type_did).unwrap();
189194

190-
let drop_impl_span = tcx.def_span(drop_impl_did);
191-
192195
// We can assume the predicates attached to struct/enum definition
193196
// hold.
194197
let generic_assumptions = tcx.predicates_of(self_type_did);
@@ -205,7 +208,7 @@ fn ensure_drop_predicates_are_implied_by_item_defn<'tcx>(
205208
// just to look for all the predicates directly.
206209

207210
assert_eq!(dtor_predicates.parent, None);
208-
for (predicate, _) in dtor_predicates.predicates {
211+
for (predicate, predicate_sp) in dtor_predicates.predicates {
209212
// (We do not need to worry about deep analysis of type
210213
// expressions etc because the Drop impls are already forced
211214
// 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>(
241244

242245
if !assumptions_in_impl_context.iter().any(predicate_matches_closure) {
243246
let item_span = tcx.hir().span(self_type_hir_id);
247+
let self_descr =
248+
tcx.def_kind(self_type_did).map(|kind| kind.descr(self_type_did)).unwrap_or("type");
244249
struct_span_err!(
245250
tcx.sess,
246-
drop_impl_span,
251+
*predicate_sp,
247252
E0367,
248-
"The requirement `{}` is added only by the Drop impl.",
249-
predicate
250-
)
251-
.span_note(
252-
item_span,
253-
"The same requirement must be part of \
254-
the struct/enum definition",
253+
"`Drop` impl requires `{}` but the {} it is implemented for does not",
254+
predicate,
255+
self_descr,
255256
)
257+
.span_note(item_span, "the implementor must specify the same requirement")
256258
.emit();
257259
result = Err(ErrorReported);
258260
}

src/librustc_typeck/coherence/builtin.rs

+18-29
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use rustc::ty::util::CopyImplementationError;
1313
use rustc::ty::TypeFoldable;
1414
use rustc::ty::{self, Ty, TyCtxt};
1515

16-
use hir::Node;
1716
use rustc::hir::def_id::DefId;
1817
use rustc::hir::{self, ItemKind};
1918

@@ -51,35 +50,25 @@ impl<'tcx> Checker<'tcx> {
5150
}
5251

5352
fn visit_implementation_of_drop(tcx: TyCtxt<'_>, impl_did: DefId) {
54-
if let ty::Adt(..) = tcx.type_of(impl_did).kind {
55-
/* do nothing */
56-
} else {
57-
// Destructors only work on nominal types.
58-
if let Some(impl_hir_id) = tcx.hir().as_local_hir_id(impl_did) {
59-
if let Some(Node::Item(item)) = tcx.hir().find(impl_hir_id) {
60-
let span = match item.kind {
61-
ItemKind::Impl(.., ref ty, _) => ty.span,
62-
_ => item.span,
63-
};
64-
struct_span_err!(
65-
tcx.sess,
66-
span,
67-
E0120,
68-
"the Drop trait may only be implemented on \
69-
structures"
70-
)
71-
.span_label(span, "implementing Drop requires a struct")
72-
.emit();
73-
} else {
74-
bug!("didn't find impl in ast map");
75-
}
76-
} else {
77-
bug!(
78-
"found external impl of Drop trait on \
79-
something other than a struct"
80-
);
81-
}
53+
// Destructors only work on nominal types.
54+
if let ty::Adt(..) | ty::Error = tcx.type_of(impl_did).kind {
55+
return;
8256
}
57+
58+
let impl_hir_id = tcx.hir().as_local_hir_id(impl_did).expect("foreign Drop impl on non-ADT");
59+
let sp = match tcx.hir().expect_item(impl_hir_id).kind {
60+
ItemKind::Impl(.., ty, _) => ty.span,
61+
_ => bug!("expected Drop impl item"),
62+
};
63+
64+
struct_span_err!(
65+
tcx.sess,
66+
sp,
67+
E0120,
68+
"the `Drop` trait may only be implemented for structs, enums, and unions",
69+
)
70+
.span_label(sp, "must be a struct, enum, or union")
71+
.emit();
8372
}
8473

8574
fn visit_implementation_of_copy(tcx: TyCtxt<'_>, impl_did: DefId) {
+6-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
impl<'a> Drop for &'a mut isize {
2-
//~^ ERROR the Drop trait may only be implemented on structures
2+
//~^ ERROR the `Drop` trait may only be implemented for structs, enums, and unions
33
//~^^ ERROR E0117
44
fn drop(&mut self) {
55
println!("kaboom");
66
}
77
}
88

9+
impl Drop for Nonexistent {
10+
//~^ ERROR cannot find type `Nonexistent`
11+
fn drop(&mut self) { }
12+
}
13+
914
fn main() {
1015
}

src/test/ui/dropck/drop-on-non-struct.stderr

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
error[E0120]: the Drop trait may only be implemented on structures
1+
error[E0412]: cannot find type `Nonexistent` in this scope
2+
--> $DIR/drop-on-non-struct.rs:9:15
3+
|
4+
LL | impl Drop for Nonexistent {
5+
| ^^^^^^^^^^^ not found in this scope
6+
7+
error[E0120]: the `Drop` trait may only be implemented for structs, enums, and unions
28
--> $DIR/drop-on-non-struct.rs:1:19
39
|
410
LL | impl<'a> Drop for &'a mut isize {
5-
| ^^^^^^^^^^^^^ implementing Drop requires a struct
11+
| ^^^^^^^^^^^^^ must be a struct, enum, or union
612

713
error[E0117]: only traits defined in the current crate can be implemented for arbitrary types
814
--> $DIR/drop-on-non-struct.rs:1:1
@@ -15,7 +21,7 @@ LL | impl<'a> Drop for &'a mut isize {
1521
|
1622
= note: define and implement a trait or new type instead
1723

18-
error: aborting due to 2 previous errors
24+
error: aborting due to 3 previous errors
1925

20-
Some errors have detailed explanations: E0117, E0120.
26+
Some errors have detailed explanations: E0117, E0120, E0412.
2127
For more information about an error, try `rustc --explain E0117`.

src/test/ui/error-codes/E0117.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
impl Drop for u32 {} //~ ERROR E0117
2-
//~| ERROR the Drop trait may only be implemented on structures
3-
//~| implementing Drop requires a struct
2+
//~| ERROR the `Drop` trait may only be implemented for structs, enums, and unions
43

54
fn main() {
65
}

src/test/ui/error-codes/E0117.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0120]: the Drop trait may only be implemented on structures
1+
error[E0120]: the `Drop` trait may only be implemented for structs, enums, and unions
22
--> $DIR/E0117.rs:1:15
33
|
44
LL | impl Drop for u32 {}
5-
| ^^^ implementing Drop requires a struct
5+
| ^^^ must be a struct, enum, or union
66

77
error[E0117]: only traits defined in the current crate can be implemented for arbitrary types
88
--> $DIR/E0117.rs:1:1

src/test/ui/error-codes/E0120.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0120]: the Drop trait may only be implemented on structures
1+
error[E0120]: the `Drop` trait may only be implemented for structs, enums, and unions
22
--> $DIR/E0120.rs:3:15
33
|
44
LL | impl Drop for dyn MyTrait {
5-
| ^^^^^^^^^^^ implementing Drop requires a struct
5+
| ^^^^^^^^^^^ must be a struct, enum, or union
66

77
error: aborting due to previous error
88

src/test/ui/issues/issue-17959.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ struct G<T: ?Sized> {
99
}
1010

1111
impl<T> Drop for G<T> {
12-
//~^ ERROR: The requirement `T: std::marker::Sized` is added only by the Drop impl. [E0367]
12+
//~^ ERROR `Drop` impl requires `T: std::marker::Sized`
1313
fn drop(&mut self) {
1414
if !self._ptr.is_null() {
1515
}

src/test/ui/issues/issue-17959.stderr

+5-11
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
1-
error[E0367]: The requirement `T: std::marker::Sized` is added only by the Drop impl.
2-
--> $DIR/issue-17959.rs:11:1
1+
error[E0367]: `Drop` impl requires `T: std::marker::Sized` but the struct it is implemented for does not
2+
--> $DIR/issue-17959.rs:11:6
33
|
4-
LL | / impl<T> Drop for G<T> {
5-
LL | |
6-
LL | | fn drop(&mut self) {
7-
LL | | if !self._ptr.is_null() {
8-
LL | | }
9-
LL | | }
10-
LL | | }
11-
| |_^
4+
LL | impl<T> Drop for G<T> {
5+
| ^
126
|
13-
note: The same requirement must be part of the struct/enum definition
7+
note: the implementor must specify the same requirement
148
--> $DIR/issue-17959.rs:7:1
159
|
1610
LL | / struct G<T: ?Sized> {

src/test/ui/issues/issue-38868.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0366]: Implementations of Drop cannot be specialized
1+
error[E0366]: `Drop` impls cannot be specialized
22
--> $DIR/issue-38868.rs:5:1
33
|
44
LL | / impl Drop for List<i32> {
@@ -8,7 +8,7 @@ LL | | }
88
LL | | }
99
| |_^
1010
|
11-
note: Use same sequence of generic type and region parameters that is on the struct/enum definition
11+
note: use the same sequence of generic type, lifetime and const parameters as the struct definition
1212
--> $DIR/issue-38868.rs:1:1
1313
|
1414
LL | / pub struct List<T> {

src/test/ui/issues/issue-41974.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ LL | impl<T> Drop for T where T: A {
99
where T: ?Sized;
1010
= note: downstream crates may implement trait `A` for type `std::boxed::Box<_>`
1111

12-
error[E0120]: the Drop trait may only be implemented on structures
12+
error[E0120]: the `Drop` trait may only be implemented for structs, enums, and unions
1313
--> $DIR/issue-41974.rs:7:18
1414
|
1515
LL | impl<T> Drop for T where T: A {
16-
| ^ implementing Drop requires a struct
16+
| ^ must be a struct, enum, or union
1717

1818
error[E0210]: type parameter `T` must be used as the type parameter for some local type (e.g., `MyStruct<T>`)
1919
--> $DIR/issue-41974.rs:7:6

src/test/ui/reject-specialized-drops-8142.rs

+20-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Issue 8142: Test that Drop impls cannot be specialized beyond the
2-
// predicates attached to the struct/enum definition itself.
2+
// predicates attached to the type definition itself.
33

44
trait Bound { fn foo(&self) { } }
55
struct K<'l1,'l2> { x: &'l1 i8, y: &'l2 u8 }
@@ -16,12 +16,16 @@ struct U;
1616
struct V<Tva, Tvb> { x: *const Tva, y: *const Tvb }
1717
struct W<'l1, 'l2> { x: &'l1 i8, y: &'l2 u8 }
1818

19+
enum Enum<T> { Variant(T) }
20+
struct TupleStruct<T>(T);
21+
union Union<T: Copy> { f: T }
22+
1923
impl<'al,'adds_bnd:'al> Drop for K<'al,'adds_bnd> { // REJECT
20-
//~^ ERROR The requirement `'adds_bnd : 'al` is added only by the Drop impl.
24+
//~^ ERROR `Drop` impl requires `'adds_bnd : 'al`
2125
fn drop(&mut self) { } }
2226

2327
impl<'al,'adds_bnd> Drop for L<'al,'adds_bnd> where 'adds_bnd:'al { // REJECT
24-
//~^ ERROR The requirement `'adds_bnd : 'al` is added only by the Drop impl.
28+
//~^ ERROR `Drop` impl requires `'adds_bnd : 'al`
2529
fn drop(&mut self) { } }
2630

2731
impl<'ml> Drop for M<'ml> { fn drop(&mut self) { } } // ACCEPT
@@ -34,13 +38,13 @@ impl Drop for N<'static> { fn drop(&mut self) { } } // RE
3438
impl<COkNoBound> Drop for O<COkNoBound> { fn drop(&mut self) { } } // ACCEPT
3539

3640
impl Drop for P<i8> { fn drop(&mut self) { } } // REJECT
37-
//~^ ERROR Implementations of Drop cannot be specialized
41+
//~^ ERROR `Drop` impls cannot be specialized
3842

3943
impl<AddsBnd:Bound> Drop for Q<AddsBnd> { fn drop(&mut self) { } } // REJECT
40-
//~^ ERROR The requirement `AddsBnd: Bound` is added only by the Drop impl.
44+
//~^ ERROR `Drop` impl requires `AddsBnd: Bound`
4145

4246
impl<'rbnd,AddsRBnd:'rbnd> Drop for R<AddsRBnd> { fn drop(&mut self) { } } // REJECT
43-
//~^ ERROR The requirement `AddsRBnd : 'rbnd` is added only by the Drop impl.
47+
//~^ ERROR `Drop` impl requires `AddsRBnd : 'rbnd`
4448

4549
impl<Bs:Bound> Drop for S<Bs> { fn drop(&mut self) { } } // ACCEPT
4650

@@ -49,9 +53,18 @@ impl<'t,Bt:'t> Drop for T<'t,Bt> { fn drop(&mut self) { } } // ACCEPT
4953
impl Drop for U { fn drop(&mut self) { } } // ACCEPT
5054

5155
impl<One> Drop for V<One,One> { fn drop(&mut self) { } } // REJECT
52-
//~^ ERROR Implementations of Drop cannot be specialized
56+
//~^ ERROR `Drop` impls cannot be specialized
5357

5458
impl<'lw> Drop for W<'lw,'lw> { fn drop(&mut self) { } } // REJECT
5559
//~^ ERROR cannot infer an appropriate lifetime for lifetime parameter `'lw`
5660

61+
impl<AddsBnd:Bound> Drop for Enum<AddsBnd> { fn drop(&mut self) { } } // REJECT
62+
//~^ ERROR `Drop` impl requires `AddsBnd: Bound`
63+
64+
impl<AddsBnd:Bound> Drop for TupleStruct<AddsBnd> { fn drop(&mut self) { } } // REJECT
65+
//~^ ERROR `Drop` impl requires `AddsBnd: Bound`
66+
67+
impl<AddsBnd:Copy + Bound> Drop for Union<AddsBnd> { fn drop(&mut self) { } } // REJECT
68+
//~^ ERROR `Drop` impl requires `AddsBnd: Bound`
69+
5770
pub fn main() { }

0 commit comments

Comments
 (0)