Skip to content

Commit ec6405b

Browse files
nikomatsakislqd
authored andcommitted
identify when implemented for "some specific lifetime"
1 parent ab80162 commit ec6405b

File tree

3 files changed

+134
-139
lines changed

3 files changed

+134
-139
lines changed

src/librustc/infer/error_reporting/nice_region_error/placeholder_error.rs

Lines changed: 120 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,15 @@ impl NiceRegionError<'me, 'gcx, 'tcx> {
3434
sub_placeholder @ ty::RePlaceholder(_),
3535
_,
3636
sup_placeholder @ ty::RePlaceholder(_),
37-
))
38-
if expected.def_id == found.def_id =>
39-
{
40-
Some(self.try_report_placeholders_trait(
41-
Some(self.tcx().mk_region(ty::ReVar(*vid))),
42-
cause,
43-
Some(sub_placeholder),
44-
Some(sup_placeholder),
45-
expected.def_id,
46-
expected.substs,
47-
found.substs,
48-
))
49-
}
37+
)) if expected.def_id == found.def_id => Some(self.try_report_placeholders_trait(
38+
Some(self.tcx().mk_region(ty::ReVar(*vid))),
39+
cause,
40+
Some(sub_placeholder),
41+
Some(sup_placeholder),
42+
expected.def_id,
43+
expected.substs,
44+
found.substs,
45+
)),
5046

5147
Some(RegionResolutionError::SubSupConflict(
5248
vid,
@@ -58,19 +54,15 @@ impl NiceRegionError<'me, 'gcx, 'tcx> {
5854
sub_placeholder @ ty::RePlaceholder(_),
5955
_,
6056
_,
61-
))
62-
if expected.def_id == found.def_id =>
63-
{
64-
Some(self.try_report_placeholders_trait(
65-
Some(self.tcx().mk_region(ty::ReVar(*vid))),
66-
cause,
67-
Some(sub_placeholder),
68-
None,
69-
expected.def_id,
70-
expected.substs,
71-
found.substs,
72-
))
73-
}
57+
)) if expected.def_id == found.def_id => Some(self.try_report_placeholders_trait(
58+
Some(self.tcx().mk_region(ty::ReVar(*vid))),
59+
cause,
60+
Some(sub_placeholder),
61+
None,
62+
expected.def_id,
63+
expected.substs,
64+
found.substs,
65+
)),
7466

7567
Some(RegionResolutionError::SubSupConflict(
7668
vid,
@@ -82,19 +74,15 @@ impl NiceRegionError<'me, 'gcx, 'tcx> {
8274
_,
8375
_,
8476
sup_placeholder @ ty::RePlaceholder(_),
85-
))
86-
if expected.def_id == found.def_id =>
87-
{
88-
Some(self.try_report_placeholders_trait(
89-
Some(self.tcx().mk_region(ty::ReVar(*vid))),
90-
cause,
91-
None,
92-
Some(*sup_placeholder),
93-
expected.def_id,
94-
expected.substs,
95-
found.substs,
96-
))
97-
}
77+
)) if expected.def_id == found.def_id => Some(self.try_report_placeholders_trait(
78+
Some(self.tcx().mk_region(ty::ReVar(*vid))),
79+
cause,
80+
None,
81+
Some(*sup_placeholder),
82+
expected.def_id,
83+
expected.substs,
84+
found.substs,
85+
)),
9886

9987
Some(RegionResolutionError::SubSupConflict(
10088
vid,
@@ -106,19 +94,15 @@ impl NiceRegionError<'me, 'gcx, 'tcx> {
10694
values: ValuePairs::TraitRefs(ExpectedFound { expected, found }),
10795
}),
10896
sup_placeholder @ ty::RePlaceholder(_),
109-
))
110-
if expected.def_id == found.def_id =>
111-
{
112-
Some(self.try_report_placeholders_trait(
113-
Some(self.tcx().mk_region(ty::ReVar(*vid))),
114-
cause,
115-
None,
116-
Some(*sup_placeholder),
117-
expected.def_id,
118-
expected.substs,
119-
found.substs,
120-
))
121-
}
97+
)) if expected.def_id == found.def_id => Some(self.try_report_placeholders_trait(
98+
Some(self.tcx().mk_region(ty::ReVar(*vid))),
99+
cause,
100+
None,
101+
Some(*sup_placeholder),
102+
expected.def_id,
103+
expected.substs,
104+
found.substs,
105+
)),
122106

123107
Some(RegionResolutionError::ConcreteFailure(
124108
SubregionOrigin::Subtype(TypeTrace {
@@ -127,19 +111,15 @@ impl NiceRegionError<'me, 'gcx, 'tcx> {
127111
}),
128112
sub_region @ ty::RePlaceholder(_),
129113
sup_region @ ty::RePlaceholder(_),
130-
))
131-
if expected.def_id == found.def_id =>
132-
{
133-
Some(self.try_report_placeholders_trait(
134-
None,
135-
cause,
136-
Some(*sub_region),
137-
Some(*sup_region),
138-
expected.def_id,
139-
expected.substs,
140-
found.substs,
141-
))
142-
}
114+
)) if expected.def_id == found.def_id => Some(self.try_report_placeholders_trait(
115+
None,
116+
cause,
117+
Some(*sub_region),
118+
Some(*sup_region),
119+
expected.def_id,
120+
expected.substs,
121+
found.substs,
122+
)),
143123

144124
Some(RegionResolutionError::ConcreteFailure(
145125
SubregionOrigin::Subtype(TypeTrace {
@@ -148,19 +128,15 @@ impl NiceRegionError<'me, 'gcx, 'tcx> {
148128
}),
149129
sub_region @ ty::RePlaceholder(_),
150130
sup_region,
151-
))
152-
if expected.def_id == found.def_id =>
153-
{
154-
Some(self.try_report_placeholders_trait(
155-
Some(sup_region),
156-
cause,
157-
Some(*sub_region),
158-
None,
159-
expected.def_id,
160-
expected.substs,
161-
found.substs,
162-
))
163-
}
131+
)) if expected.def_id == found.def_id => Some(self.try_report_placeholders_trait(
132+
Some(sup_region),
133+
cause,
134+
Some(*sub_region),
135+
None,
136+
expected.def_id,
137+
expected.substs,
138+
found.substs,
139+
)),
164140

165141
Some(RegionResolutionError::ConcreteFailure(
166142
SubregionOrigin::Subtype(TypeTrace {
@@ -169,19 +145,15 @@ impl NiceRegionError<'me, 'gcx, 'tcx> {
169145
}),
170146
sub_region,
171147
sup_region @ ty::RePlaceholder(_),
172-
))
173-
if expected.def_id == found.def_id =>
174-
{
175-
Some(self.try_report_placeholders_trait(
176-
Some(sub_region),
177-
cause,
178-
None,
179-
Some(*sup_region),
180-
expected.def_id,
181-
expected.substs,
182-
found.substs,
183-
))
184-
}
148+
)) if expected.def_id == found.def_id => Some(self.try_report_placeholders_trait(
149+
Some(sub_region),
150+
cause,
151+
None,
152+
Some(*sup_region),
153+
expected.def_id,
154+
expected.substs,
155+
found.substs,
156+
)),
185157

186158
_ => None,
187159
}
@@ -206,14 +178,16 @@ impl NiceRegionError<'me, 'gcx, 'tcx> {
206178
expected_substs: &'tcx Substs<'tcx>,
207179
actual_substs: &'tcx Substs<'tcx>,
208180
) -> ErrorReported {
209-
debug!("try_report_placeholders_trait(\
210-
vid={:?}, \
211-
sub_placeholder={:?}, \
212-
sup_placeholder={:?}, \
213-
trait_def_id={:?}, \
214-
expected_substs={:?}, \
215-
actual_substs={:?})",
216-
vid, sub_placeholder, sup_placeholder, trait_def_id, expected_substs, actual_substs);
181+
debug!(
182+
"try_report_placeholders_trait(\
183+
vid={:?}, \
184+
sub_placeholder={:?}, \
185+
sup_placeholder={:?}, \
186+
trait_def_id={:?}, \
187+
expected_substs={:?}, \
188+
actual_substs={:?})",
189+
vid, sub_placeholder, sup_placeholder, trait_def_id, expected_substs, actual_substs
190+
);
217191

218192
let mut err = self.tcx().sess.struct_span_err(
219193
cause.span(&self.tcx()),
@@ -233,18 +207,14 @@ impl NiceRegionError<'me, 'gcx, 'tcx> {
233207
_ => (),
234208
}
235209

236-
let expected_trait_ref = self.infcx.resolve_type_vars_if_possible(
237-
&ty::TraitRef {
238-
def_id: trait_def_id,
239-
substs: expected_substs,
240-
}
241-
);
242-
let actual_trait_ref = self.infcx.resolve_type_vars_if_possible(
243-
&ty::TraitRef {
244-
def_id: trait_def_id,
245-
substs: actual_substs,
246-
}
247-
);
210+
let expected_trait_ref = self.infcx.resolve_type_vars_if_possible(&ty::TraitRef {
211+
def_id: trait_def_id,
212+
substs: expected_substs,
213+
});
214+
let actual_trait_ref = self.infcx.resolve_type_vars_if_possible(&ty::TraitRef {
215+
def_id: trait_def_id,
216+
substs: actual_substs,
217+
});
248218

249219
// Search the expected and actual trait references to see (a)
250220
// whether the sub/sup placeholders appear in them (sometimes
@@ -285,19 +255,28 @@ impl NiceRegionError<'me, 'gcx, 'tcx> {
285255
.tcx()
286256
.any_free_region_meets(&actual_trait_ref.self_ty(), |r| Some(r) == vid);
287257

288-
debug!("try_report_placeholders_trait: actual_has_vid={:?}", actual_has_vid);
289-
debug!("try_report_placeholders_trait: expected_has_vid={:?}", expected_has_vid);
258+
debug!(
259+
"try_report_placeholders_trait: actual_has_vid={:?}",
260+
actual_has_vid
261+
);
262+
debug!(
263+
"try_report_placeholders_trait: expected_has_vid={:?}",
264+
expected_has_vid
265+
);
290266
debug!("try_report_placeholders_trait: has_sub={:?}", has_sub);
291267
debug!("try_report_placeholders_trait: has_sup={:?}", has_sup);
292-
debug!("try_report_placeholders_trait: self_ty_has_vid={:?}", self_ty_has_vid);
268+
debug!(
269+
"try_report_placeholders_trait: self_ty_has_vid={:?}",
270+
self_ty_has_vid
271+
);
293272

294273
RegionHighlightMode::maybe_highlighting_region(sub_placeholder, has_sub, || {
295274
RegionHighlightMode::maybe_highlighting_region(sup_placeholder, has_sup, || {
296275
match (has_sub, has_sup) {
297276
(Some(n1), Some(n2)) => {
298277
err.note(&format!(
299278
"`{}` would have to be implemented for the type `{}`, \
300-
for any two lifetimes `'{}` and `'{}`",
279+
for any two lifetimes `'{}` and `'{}`",
301280
expected_trait_ref,
302281
expected_trait_ref.self_ty(),
303282
std::cmp::min(n1, n2),
@@ -307,40 +286,54 @@ impl NiceRegionError<'me, 'gcx, 'tcx> {
307286
(Some(n), _) | (_, Some(n)) => {
308287
err.note(&format!(
309288
"`{}` would have to be implemented for the type `{}`, \
310-
for any lifetime `'{}`",
289+
for any lifetime `'{}`",
311290
expected_trait_ref,
312291
expected_trait_ref.self_ty(),
313292
n,
314293
));
315294
}
316-
(None, None) => {
317-
err.note(&format!(
318-
"`{}` would have to be implemented for the type `{}`",
319-
expected_trait_ref,
320-
expected_trait_ref.self_ty(),
321-
));
322-
}
295+
(None, None) => RegionHighlightMode::maybe_highlighting_region(
296+
vid,
297+
expected_has_vid,
298+
|| {
299+
if let Some(n) = expected_has_vid {
300+
err.note(&format!(
301+
"`{}` would have to be implemented for the type `{}`, \
302+
for some specific lifetime `'{}`",
303+
expected_trait_ref,
304+
expected_trait_ref.self_ty(),
305+
n,
306+
));
307+
} else {
308+
err.note(&format!(
309+
"`{}` would have to be implemented for the type `{}`",
310+
expected_trait_ref,
311+
expected_trait_ref.self_ty(),
312+
));
313+
}
314+
},
315+
),
323316
}
324317
})
325318
});
326319

327320
RegionHighlightMode::maybe_highlighting_region(
328321
vid,
329-
actual_has_vid.or(expected_has_vid),
322+
actual_has_vid,
330323
|| match actual_has_vid {
331324
Some(n) => {
332325
if self_ty_has_vid {
333326
err.note(&format!(
334327
"but `{}` is actually implemented for the type `{}`, \
335-
for the specific lifetime `'{}`",
328+
for the specific lifetime `'{}`",
336329
actual_trait_ref,
337330
actual_trait_ref.self_ty(),
338331
n
339332
));
340333
} else {
341334
err.note(&format!(
342335
"but `{}` is actually implemented for the type `{}`, \
343-
for some lifetime `'{}`",
336+
for some lifetime `'{}`",
344337
actual_trait_ref,
345338
actual_trait_ref.self_ty(),
346339
n
@@ -355,7 +348,7 @@ impl NiceRegionError<'me, 'gcx, 'tcx> {
355348
actual_trait_ref.self_ty(),
356349
));
357350
}
358-
}
351+
},
359352
);
360353

361354
err.emit();
Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,11 @@
11
error: implementation of `Trait` is not general enough
2-
--> $DIR/issue-57362.rs:20:7
2+
--> $DIR/issue-57362-1.rs:20:7
33
|
44
LL | a.f(); //~ ERROR not general enough
55
| ^
66
|
7-
= note: `Trait` would have to be implemented for the type `fn(&u8)`
7+
= note: `Trait` would have to be implemented for the type `fn(&'0 u8)`, for some specific lifetime `'0`
88
= note: but `Trait` is actually implemented for the type `for<'r> fn(&'r u8)`
99

10-
error: implementation of `X` is not general enough
11-
--> $DIR/issue-57362.rs:38:13
12-
|
13-
LL | let x = <fn (&())>::make_g(); //~ ERROR not general enough
14-
| ^^^^^^^^^^^^^^^^^^
15-
|
16-
= note: `X` would have to be implemented for the type `for<'r> fn(&'r ())`
17-
= note: but `X` is actually implemented for the type `fn(&'0 ())`, for the specific lifetime `'0`
18-
19-
error: aborting due to 2 previous errors
10+
error: aborting due to previous error
2011

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: implementation of `X` is not general enough
2+
--> $DIR/issue-57362-2.rs:22:13
3+
|
4+
LL | let x = <fn (&())>::make_g(); //~ ERROR not general enough
5+
| ^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `X` would have to be implemented for the type `for<'r> fn(&'r ())`
8+
= note: but `X` is actually implemented for the type `fn(&'0 ())`, for the specific lifetime `'0`
9+
10+
error: aborting due to previous error
11+

0 commit comments

Comments
 (0)