1
+ use errors:: DiagnosticBuilder ;
1
2
use hir:: def_id:: DefId ;
2
3
use infer:: error_reporting:: nice_region_error:: NiceRegionError ;
3
4
use infer:: lexical_region_resolve:: RegionResolutionError ;
@@ -259,7 +260,7 @@ impl NiceRegionError<'me, 'gcx, 'tcx> {
259
260
. tcx ( )
260
261
. any_free_region_meets ( & expected_trait_ref. self_ty ( ) , |r| Some ( r) == vid) ;
261
262
262
- let self_ty_has_vid = actual_self_ty_has_vid || expected_self_ty_has_vid;
263
+ let any_self_ty_has_vid = actual_self_ty_has_vid || expected_self_ty_has_vid;
263
264
264
265
debug ! (
265
266
"try_report_placeholders_trait: actual_has_vid={:?}" ,
@@ -280,6 +281,43 @@ impl NiceRegionError<'me, 'gcx, 'tcx> {
280
281
expected_self_ty_has_vid
281
282
) ;
282
283
284
+ self . explain_actual_impl_that_was_found (
285
+ & mut err,
286
+ sub_placeholder,
287
+ sup_placeholder,
288
+ has_sub,
289
+ has_sup,
290
+ expected_trait_ref,
291
+ actual_trait_ref,
292
+ vid,
293
+ expected_has_vid,
294
+ actual_has_vid,
295
+ any_self_ty_has_vid,
296
+ ) ;
297
+
298
+ err. emit ( ) ;
299
+ ErrorReported
300
+ }
301
+
302
+ /// Add notes with details about the expected and actual trait refs, with attention to cases
303
+ /// when placeholder regions are involved: either the trait or the self type containing
304
+ /// them needs to be mentioned the closest to the placeholders.
305
+ /// This makes the error messages read better, however at the cost of some complexity
306
+ /// due to the number of combinations we have to deal with.
307
+ fn explain_actual_impl_that_was_found (
308
+ & self ,
309
+ err : & mut DiagnosticBuilder < ' _ > ,
310
+ sub_placeholder : Option < ty:: Region < ' tcx > > ,
311
+ sup_placeholder : Option < ty:: Region < ' tcx > > ,
312
+ has_sub : Option < usize > ,
313
+ has_sup : Option < usize > ,
314
+ expected_trait_ref : ty:: TraitRef < ' _ > ,
315
+ actual_trait_ref : ty:: TraitRef < ' _ > ,
316
+ vid : Option < ty:: Region < ' tcx > > ,
317
+ expected_has_vid : Option < usize > ,
318
+ actual_has_vid : Option < usize > ,
319
+ any_self_ty_has_vid : bool ,
320
+ ) {
283
321
// The weird thing here with the `maybe_highlighting_region` calls and the
284
322
// the match inside is meant to be like this:
285
323
//
@@ -299,7 +337,7 @@ impl NiceRegionError<'me, 'gcx, 'tcx> {
299
337
RegionHighlightMode :: maybe_highlighting_region ( sup_placeholder, has_sup, || {
300
338
match ( has_sub, has_sup) {
301
339
( Some ( n1) , Some ( n2) ) => {
302
- if self_ty_has_vid {
340
+ if any_self_ty_has_vid {
303
341
err. note ( & format ! (
304
342
"`{}` would have to be implemented for the type `{}`, \
305
343
for any two lifetimes `'{}` and `'{}`",
@@ -320,7 +358,7 @@ impl NiceRegionError<'me, 'gcx, 'tcx> {
320
358
}
321
359
}
322
360
( Some ( n) , _) | ( _, Some ( n) ) => {
323
- if self_ty_has_vid {
361
+ if any_self_ty_has_vid {
324
362
err. note ( & format ! (
325
363
"`{}` would have to be implemented for the type `{}`, \
326
364
for any lifetime `'{}`",
@@ -350,7 +388,7 @@ impl NiceRegionError<'me, 'gcx, 'tcx> {
350
388
n,
351
389
) ) ;
352
390
} else {
353
- if self_ty_has_vid {
391
+ if any_self_ty_has_vid {
354
392
err. note ( & format ! (
355
393
"`{}` would have to be implemented for the type `{}`" ,
356
394
expected_trait_ref,
@@ -375,7 +413,7 @@ impl NiceRegionError<'me, 'gcx, 'tcx> {
375
413
actual_has_vid,
376
414
|| match actual_has_vid {
377
415
Some ( n) => {
378
- if self_ty_has_vid {
416
+ if any_self_ty_has_vid {
379
417
err. note ( & format ! (
380
418
"but `{}` is actually implemented for the type `{}`, \
381
419
for the specific lifetime `'{}`",
@@ -402,8 +440,5 @@ impl NiceRegionError<'me, 'gcx, 'tcx> {
402
440
}
403
441
} ,
404
442
) ;
405
-
406
- err. emit ( ) ;
407
- ErrorReported
408
443
}
409
444
}
0 commit comments