@@ -14,10 +14,8 @@ use rustc_infer::traits::util;
14
14
use rustc_middle:: ty:: error:: { ExpectedFound , TypeError } ;
15
15
use rustc_middle:: ty:: util:: ExplicitSelf ;
16
16
use rustc_middle:: ty:: {
17
- self , AssocItem , DefIdTree , TraitRef , Ty , TypeFoldable , TypeFolder , TypeSuperFoldable ,
18
- TypeVisitable ,
17
+ self , DefIdTree , InternalSubsts , Ty , TypeFoldable , TypeFolder , TypeSuperFoldable , TypeVisitable ,
19
18
} ;
20
- use rustc_middle:: ty:: { FnSig , InternalSubsts } ;
21
19
use rustc_middle:: ty:: { GenericParamDefKind , ToPredicate , TyCtxt } ;
22
20
use rustc_span:: Span ;
23
21
use rustc_trait_selection:: traits:: error_reporting:: TypeErrCtxtExt ;
@@ -51,11 +49,11 @@ pub(crate) fn compare_impl_method<'tcx>(
51
49
return ;
52
50
}
53
51
54
- if let Err ( _) = compare_number_of_generics ( tcx, impl_m, impl_m_span , trait_m, trait_item_span) {
52
+ if let Err ( _) = compare_number_of_generics ( tcx, impl_m, trait_m, trait_item_span, false ) {
55
53
return ;
56
54
}
57
55
58
- if let Err ( _) = compare_generic_param_kinds ( tcx, impl_m, trait_m) {
56
+ if let Err ( _) = compare_generic_param_kinds ( tcx, impl_m, trait_m, false ) {
59
57
return ;
60
58
}
61
59
@@ -144,9 +142,9 @@ pub(crate) fn compare_impl_method<'tcx>(
144
142
#[ instrument( level = "debug" , skip( tcx, impl_m_span, impl_trait_ref) ) ]
145
143
fn compare_predicate_entailment < ' tcx > (
146
144
tcx : TyCtxt < ' tcx > ,
147
- impl_m : & AssocItem ,
145
+ impl_m : & ty :: AssocItem ,
148
146
impl_m_span : Span ,
149
- trait_m : & AssocItem ,
147
+ trait_m : & ty :: AssocItem ,
150
148
impl_trait_ref : ty:: TraitRef < ' tcx > ,
151
149
) -> Result < ( ) , ErrorGuaranteed > {
152
150
let trait_to_impl_substs = impl_trait_ref. substs ;
@@ -157,8 +155,7 @@ fn compare_predicate_entailment<'tcx>(
157
155
// FIXME(@lcnr): remove that after removing `cause.body_id` from
158
156
// obligations.
159
157
let impl_m_hir_id = tcx. hir ( ) . local_def_id_to_hir_id ( impl_m. def_id . expect_local ( ) ) ;
160
- // We sometimes modify the span further down.
161
- let mut cause = ObligationCause :: new (
158
+ let cause = ObligationCause :: new (
162
159
impl_m_span,
163
160
impl_m_hir_id,
164
161
ObligationCauseCode :: CompareImplItemObligation {
@@ -307,14 +304,13 @@ fn compare_predicate_entailment<'tcx>(
307
304
debug ! ( ?terr, "sub_types failed: impl ty {:?}, trait ty {:?}" , impl_fty, trait_fty) ;
308
305
309
306
let emitted = report_trait_method_mismatch (
310
- tcx,
311
- & mut cause,
312
307
& infcx,
308
+ cause,
313
309
terr,
314
310
( trait_m, trait_fty) ,
315
311
( impl_m, impl_fty) ,
316
- & trait_sig,
317
- & impl_trait_ref,
312
+ trait_sig,
313
+ impl_trait_ref,
318
314
) ;
319
315
return Err ( emitted) ;
320
316
}
@@ -352,11 +348,15 @@ pub fn collect_trait_impl_trait_tys<'tcx>(
352
348
let impl_trait_ref = tcx. impl_trait_ref ( impl_m. impl_container ( tcx) . unwrap ( ) ) . unwrap ( ) ;
353
349
let param_env = tcx. param_env ( def_id) ;
354
350
351
+ // First, check a few of the same thing as `compare_impl_method`, just so we don't ICE during substitutions later.
352
+ compare_number_of_generics ( tcx, impl_m, trait_m, tcx. hir ( ) . span_if_local ( impl_m. def_id ) , true ) ?;
353
+ compare_generic_param_kinds ( tcx, impl_m, trait_m, true ) ?;
354
+
355
355
let trait_to_impl_substs = impl_trait_ref. substs ;
356
356
357
357
let impl_m_hir_id = tcx. hir ( ) . local_def_id_to_hir_id ( impl_m. def_id . expect_local ( ) ) ;
358
358
let return_span = tcx. hir ( ) . fn_decl_by_hir_id ( impl_m_hir_id) . unwrap ( ) . output . span ( ) ;
359
- let mut cause = ObligationCause :: new (
359
+ let cause = ObligationCause :: new (
360
360
return_span,
361
361
impl_m_hir_id,
362
362
ObligationCauseCode :: CompareImplItemObligation {
@@ -376,6 +376,7 @@ pub fn collect_trait_impl_trait_tys<'tcx>(
376
376
let infcx = & tcx. infer_ctxt ( ) . build ( ) ;
377
377
let ocx = ObligationCtxt :: new ( infcx) ;
378
378
379
+ // Normalize the impl signature with fresh variables for lifetime inference.
379
380
let norm_cause = ObligationCause :: misc ( return_span, impl_m_hir_id) ;
380
381
let impl_sig = ocx. normalize (
381
382
norm_cause. clone ( ) ,
@@ -388,6 +389,10 @@ pub fn collect_trait_impl_trait_tys<'tcx>(
388
389
) ;
389
390
let impl_return_ty = impl_sig. output ( ) ;
390
391
392
+ // Normalize the trait signature with liberated bound vars, passing it through
393
+ // the ImplTraitInTraitCollector, which gathers all of the RPITITs and replaces
394
+ // them with inference variables.
395
+ // We will use these inference variables to collect the hidden types of RPITITs.
391
396
let mut collector = ImplTraitInTraitCollector :: new ( & ocx, return_span, param_env, impl_m_hir_id) ;
392
397
let unnormalized_trait_sig = tcx
393
398
. liberate_late_bound_regions (
@@ -448,14 +453,13 @@ pub fn collect_trait_impl_trait_tys<'tcx>(
448
453
// emit an error now because `compare_predicate_entailment` will not report the error
449
454
// when normalization fails.
450
455
let emitted = report_trait_method_mismatch (
451
- tcx,
452
- & mut cause,
453
456
infcx,
457
+ cause,
454
458
terr,
455
459
( trait_m, trait_fty) ,
456
460
( impl_m, impl_fty) ,
457
- & trait_sig,
458
- & impl_trait_ref,
461
+ trait_sig,
462
+ impl_trait_ref,
459
463
) ;
460
464
return Err ( emitted) ;
461
465
}
@@ -625,23 +629,21 @@ impl<'tcx> TypeFolder<'tcx> for ImplTraitInTraitCollector<'_, 'tcx> {
625
629
}
626
630
627
631
fn report_trait_method_mismatch < ' tcx > (
628
- tcx : TyCtxt < ' tcx > ,
629
- cause : & mut ObligationCause < ' tcx > ,
630
632
infcx : & InferCtxt < ' tcx > ,
633
+ mut cause : ObligationCause < ' tcx > ,
631
634
terr : TypeError < ' tcx > ,
632
- ( trait_m, trait_fty) : ( & AssocItem , Ty < ' tcx > ) ,
633
- ( impl_m, impl_fty) : ( & AssocItem , Ty < ' tcx > ) ,
634
- trait_sig : & FnSig < ' tcx > ,
635
- impl_trait_ref : & TraitRef < ' tcx > ,
635
+ ( trait_m, trait_fty) : ( & ty :: AssocItem , Ty < ' tcx > ) ,
636
+ ( impl_m, impl_fty) : ( & ty :: AssocItem , Ty < ' tcx > ) ,
637
+ trait_sig : ty :: FnSig < ' tcx > ,
638
+ impl_trait_ref : ty :: TraitRef < ' tcx > ,
636
639
) -> ErrorGuaranteed {
640
+ let tcx = infcx. tcx ;
637
641
let ( impl_err_span, trait_err_span) =
638
642
extract_spans_for_error_reporting ( & infcx, terr, & cause, impl_m, trait_m) ;
639
643
640
- cause. span = impl_err_span;
641
-
642
644
let mut diag = struct_span_err ! (
643
645
tcx. sess,
644
- cause . span ( ) ,
646
+ impl_err_span ,
645
647
E0053 ,
646
648
"method `{}` has an incompatible type for trait" ,
647
649
trait_m. name
@@ -712,6 +714,7 @@ fn report_trait_method_mismatch<'tcx>(
712
714
_ => { }
713
715
}
714
716
717
+ cause. span = impl_err_span;
715
718
infcx. err_ctxt ( ) . note_type_err (
716
719
& mut diag,
717
720
& cause,
@@ -922,9 +925,9 @@ fn compare_self_type<'tcx>(
922
925
fn compare_number_of_generics < ' tcx > (
923
926
tcx : TyCtxt < ' tcx > ,
924
927
impl_ : & ty:: AssocItem ,
925
- _impl_span : Span ,
926
928
trait_ : & ty:: AssocItem ,
927
929
trait_span : Option < Span > ,
930
+ delay : bool ,
928
931
) -> Result < ( ) , ErrorGuaranteed > {
929
932
let trait_own_counts = tcx. generics_of ( trait_. def_id ) . own_counts ( ) ;
930
933
let impl_own_counts = tcx. generics_of ( impl_. def_id ) . own_counts ( ) ;
@@ -1054,7 +1057,7 @@ fn compare_number_of_generics<'tcx>(
1054
1057
err. span_label ( * span, "`impl Trait` introduces an implicit type parameter" ) ;
1055
1058
}
1056
1059
1057
- let reported = err. emit ( ) ;
1060
+ let reported = err. emit_unless ( delay ) ;
1058
1061
err_occurred = Some ( reported) ;
1059
1062
}
1060
1063
}
@@ -1306,6 +1309,7 @@ fn compare_generic_param_kinds<'tcx>(
1306
1309
tcx : TyCtxt < ' tcx > ,
1307
1310
impl_item : & ty:: AssocItem ,
1308
1311
trait_item : & ty:: AssocItem ,
1312
+ delay : bool ,
1309
1313
) -> Result < ( ) , ErrorGuaranteed > {
1310
1314
assert_eq ! ( impl_item. kind, trait_item. kind) ;
1311
1315
@@ -1363,7 +1367,7 @@ fn compare_generic_param_kinds<'tcx>(
1363
1367
err. span_label ( impl_header_span, "" ) ;
1364
1368
err. span_label ( param_impl_span, make_param_message ( "found" , param_impl) ) ;
1365
1369
1366
- let reported = err. emit ( ) ;
1370
+ let reported = err. emit_unless ( delay ) ;
1367
1371
return Err ( reported) ;
1368
1372
}
1369
1373
}
@@ -1489,9 +1493,9 @@ pub(crate) fn compare_ty_impl<'tcx>(
1489
1493
debug ! ( "compare_impl_type(impl_trait_ref={:?})" , impl_trait_ref) ;
1490
1494
1491
1495
let _: Result < ( ) , ErrorGuaranteed > = ( || {
1492
- compare_number_of_generics ( tcx, impl_ty, impl_ty_span , trait_ty, trait_item_span) ?;
1496
+ compare_number_of_generics ( tcx, impl_ty, trait_ty, trait_item_span, false ) ?;
1493
1497
1494
- compare_generic_param_kinds ( tcx, impl_ty, trait_ty) ?;
1498
+ compare_generic_param_kinds ( tcx, impl_ty, trait_ty, false ) ?;
1495
1499
1496
1500
let sp = tcx. def_span ( impl_ty. def_id ) ;
1497
1501
compare_type_predicate_entailment ( tcx, impl_ty, sp, trait_ty, impl_trait_ref) ?;
0 commit comments