Skip to content

Commit e589358

Browse files
committed
improve generic Drop error messages
- Use the span of the predicate - Use the def's description instead of "struct/enum" (notably incorrect for unions) - Align formatting with other error messages
1 parent 0a58f58 commit e589358

File tree

6 files changed

+110
-69
lines changed

6 files changed

+110
-69
lines changed

src/librustc_typeck/check/dropck.rs

Lines changed: 18 additions & 16 deletions
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/test/ui/issues/issue-17959.rs

Lines changed: 1 addition & 1 deletion
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

Lines changed: 5 additions & 11 deletions
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

Lines changed: 2 additions & 2 deletions
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/reject-specialized-drops-8142.rs

Lines changed: 20 additions & 7 deletions
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() { }
Lines changed: 64 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,41 @@
1-
error[E0367]: The requirement `'adds_bnd : 'al` is added only by the Drop impl.
2-
--> $DIR/reject-specialized-drops-8142.rs:19:1
1+
error[E0367]: `Drop` impl requires `AddsBnd: Bound` but the union it is implemented for does not
2+
--> $DIR/reject-specialized-drops-8142.rs:67:21
3+
|
4+
LL | impl<AddsBnd:Copy + Bound> Drop for Union<AddsBnd> { fn drop(&mut self) { } } // REJECT
5+
| ^^^^^
6+
|
7+
note: the implementor must specify the same requirement
8+
--> $DIR/reject-specialized-drops-8142.rs:21:1
9+
|
10+
LL | union Union<T: Copy> { f: T }
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12+
13+
error[E0367]: `Drop` impl requires `'adds_bnd : 'al` but the struct it is implemented for does not
14+
--> $DIR/reject-specialized-drops-8142.rs:23:20
315
|
4-
LL | / impl<'al,'adds_bnd:'al> Drop for K<'al,'adds_bnd> { // REJECT
5-
LL | |
6-
LL | | fn drop(&mut self) { } }
7-
| |____________________________^
16+
LL | impl<'al,'adds_bnd:'al> Drop for K<'al,'adds_bnd> { // REJECT
17+
| ^^^
818
|
9-
note: The same requirement must be part of the struct/enum definition
19+
note: the implementor must specify the same requirement
1020
--> $DIR/reject-specialized-drops-8142.rs:5:1
1121
|
1222
LL | struct K<'l1,'l2> { x: &'l1 i8, y: &'l2 u8 }
1323
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1424

15-
error[E0367]: The requirement `'adds_bnd : 'al` is added only by the Drop impl.
16-
--> $DIR/reject-specialized-drops-8142.rs:23:1
25+
error[E0367]: `Drop` impl requires `'adds_bnd : 'al` but the struct it is implemented for does not
26+
--> $DIR/reject-specialized-drops-8142.rs:27:67
1727
|
18-
LL | / impl<'al,'adds_bnd> Drop for L<'al,'adds_bnd> where 'adds_bnd:'al { // REJECT
19-
LL | |
20-
LL | | fn drop(&mut self) { } }
21-
| |____________________________^
28+
LL | impl<'al,'adds_bnd> Drop for L<'al,'adds_bnd> where 'adds_bnd:'al { // REJECT
29+
| ^^^
2230
|
23-
note: The same requirement must be part of the struct/enum definition
31+
note: the implementor must specify the same requirement
2432
--> $DIR/reject-specialized-drops-8142.rs:6:1
2533
|
2634
LL | struct L<'l1,'l2> { x: &'l1 i8, y: &'l2 u8 }
2735
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2836

2937
error[E0308]: mismatched types
30-
--> $DIR/reject-specialized-drops-8142.rs:29:1
38+
--> $DIR/reject-specialized-drops-8142.rs:33:1
3139
|
3240
LL | impl Drop for N<'static> { fn drop(&mut self) { } } // REJECT
3341
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
@@ -41,56 +49,56 @@ LL | struct N<'n> { x: &'n i8 }
4149
| ^^
4250
= note: ...does not necessarily outlive the static lifetime
4351

44-
error[E0366]: Implementations of Drop cannot be specialized
45-
--> $DIR/reject-specialized-drops-8142.rs:36:1
52+
error[E0366]: `Drop` impls cannot be specialized
53+
--> $DIR/reject-specialized-drops-8142.rs:40:1
4654
|
4755
LL | impl Drop for P<i8> { fn drop(&mut self) { } } // REJECT
4856
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4957
|
50-
note: Use same sequence of generic type and region parameters that is on the struct/enum definition
58+
note: use the same sequence of generic type, lifetime and const parameters as the struct definition
5159
--> $DIR/reject-specialized-drops-8142.rs:10:1
5260
|
5361
LL | struct P<Tp> { x: *const Tp }
5462
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5563

56-
error[E0367]: The requirement `AddsBnd: Bound` is added only by the Drop impl.
57-
--> $DIR/reject-specialized-drops-8142.rs:39:1
64+
error[E0367]: `Drop` impl requires `AddsBnd: Bound` but the struct it is implemented for does not
65+
--> $DIR/reject-specialized-drops-8142.rs:43:14
5866
|
5967
LL | impl<AddsBnd:Bound> Drop for Q<AddsBnd> { fn drop(&mut self) { } } // REJECT
60-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
68+
| ^^^^^
6169
|
62-
note: The same requirement must be part of the struct/enum definition
70+
note: the implementor must specify the same requirement
6371
--> $DIR/reject-specialized-drops-8142.rs:11:1
6472
|
6573
LL | struct Q<Tq> { x: *const Tq }
6674
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6775

68-
error[E0367]: The requirement `AddsRBnd : 'rbnd` is added only by the Drop impl.
69-
--> $DIR/reject-specialized-drops-8142.rs:42:1
76+
error[E0367]: `Drop` impl requires `AddsRBnd : 'rbnd` but the struct it is implemented for does not
77+
--> $DIR/reject-specialized-drops-8142.rs:46:21
7078
|
7179
LL | impl<'rbnd,AddsRBnd:'rbnd> Drop for R<AddsRBnd> { fn drop(&mut self) { } } // REJECT
72-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
80+
| ^^^^^
7381
|
74-
note: The same requirement must be part of the struct/enum definition
82+
note: the implementor must specify the same requirement
7583
--> $DIR/reject-specialized-drops-8142.rs:12:1
7684
|
7785
LL | struct R<Tr> { x: *const Tr }
7886
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7987

80-
error[E0366]: Implementations of Drop cannot be specialized
81-
--> $DIR/reject-specialized-drops-8142.rs:51:1
88+
error[E0366]: `Drop` impls cannot be specialized
89+
--> $DIR/reject-specialized-drops-8142.rs:55:1
8290
|
8391
LL | impl<One> Drop for V<One,One> { fn drop(&mut self) { } } // REJECT
8492
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8593
|
86-
note: Use same sequence of generic type and region parameters that is on the struct/enum definition
94+
note: use the same sequence of generic type, lifetime and const parameters as the struct definition
8795
--> $DIR/reject-specialized-drops-8142.rs:16:1
8896
|
8997
LL | struct V<Tva, Tvb> { x: *const Tva, y: *const Tvb }
9098
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9199

92100
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'lw` due to conflicting requirements
93-
--> $DIR/reject-specialized-drops-8142.rs:54:1
101+
--> $DIR/reject-specialized-drops-8142.rs:58:1
94102
|
95103
LL | impl<'lw> Drop for W<'lw,'lw> { fn drop(&mut self) { } } // REJECT
96104
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -106,14 +114,38 @@ note: ...but the lifetime must also be valid for the lifetime `'l2` as defined o
106114
LL | struct W<'l1, 'l2> { x: &'l1 i8, y: &'l2 u8 }
107115
| ^^^
108116
note: ...so that the types are compatible
109-
--> $DIR/reject-specialized-drops-8142.rs:54:1
117+
--> $DIR/reject-specialized-drops-8142.rs:58:1
110118
|
111119
LL | impl<'lw> Drop for W<'lw,'lw> { fn drop(&mut self) { } } // REJECT
112120
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
113121
= note: expected `W<'l1, 'l2>`
114122
found `W<'_, '_>`
115123

116-
error: aborting due to 8 previous errors
124+
error[E0367]: `Drop` impl requires `AddsBnd: Bound` but the enum it is implemented for does not
125+
--> $DIR/reject-specialized-drops-8142.rs:61:14
126+
|
127+
LL | impl<AddsBnd:Bound> Drop for Enum<AddsBnd> { fn drop(&mut self) { } } // REJECT
128+
| ^^^^^
129+
|
130+
note: the implementor must specify the same requirement
131+
--> $DIR/reject-specialized-drops-8142.rs:19:1
132+
|
133+
LL | enum Enum<T> { Variant(T) }
134+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
135+
136+
error[E0367]: `Drop` impl requires `AddsBnd: Bound` but the struct it is implemented for does not
137+
--> $DIR/reject-specialized-drops-8142.rs:64:14
138+
|
139+
LL | impl<AddsBnd:Bound> Drop for TupleStruct<AddsBnd> { fn drop(&mut self) { } } // REJECT
140+
| ^^^^^
141+
|
142+
note: the implementor must specify the same requirement
143+
--> $DIR/reject-specialized-drops-8142.rs:20:1
144+
|
145+
LL | struct TupleStruct<T>(T);
146+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
147+
148+
error: aborting due to 11 previous errors
117149

118150
Some errors have detailed explanations: E0308, E0366, E0367, E0495.
119151
For more information about an error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)