@@ -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 {
@@ -291,30 +288,19 @@ fn compare_predicate_entailment<'tcx>(
291
288
// type would be more appropriate. In other places we have a `Vec<Span>`
292
289
// corresponding to their `Vec<Predicate>`, but we don't have that here.
293
290
// Fixing this would improve the output of test `issue-83765.rs`.
294
- let mut result = ocx. sup ( & cause, param_env, trait_fty, impl_fty) ;
295
-
296
- // HACK(RPITIT): #101614. When we are trying to infer the hidden types for
297
- // RPITITs, we need to equate the output tys instead of just subtyping. If
298
- // we just use `sup` above, we'll end up `&'static str <: _#1t`, which causes
299
- // us to infer `_#1t = #'_#2r str`, where `'_#2r` is unconstrained, which gets
300
- // fixed up to `ReEmpty`, and which is certainly not what we want.
301
- if trait_fty. has_infer_types ( ) {
302
- result =
303
- result. and_then ( |( ) | ocx. eq ( & cause, param_env, trait_sig. output ( ) , impl_sig. output ( ) ) ) ;
304
- }
291
+ let result = ocx. sup ( & cause, param_env, trait_fty, impl_fty) ;
305
292
306
293
if let Err ( terr) = result {
307
294
debug ! ( ?terr, "sub_types failed: impl ty {:?}, trait ty {:?}" , impl_fty, trait_fty) ;
308
295
309
296
let emitted = report_trait_method_mismatch (
310
- tcx,
311
- & mut cause,
312
297
& infcx,
298
+ cause,
313
299
terr,
314
300
( trait_m, trait_fty) ,
315
301
( impl_m, impl_fty) ,
316
- & trait_sig,
317
- & impl_trait_ref,
302
+ trait_sig,
303
+ impl_trait_ref,
318
304
) ;
319
305
return Err ( emitted) ;
320
306
}
@@ -352,11 +338,15 @@ pub fn collect_trait_impl_trait_tys<'tcx>(
352
338
let impl_trait_ref = tcx. impl_trait_ref ( impl_m. impl_container ( tcx) . unwrap ( ) ) . unwrap ( ) ;
353
339
let param_env = tcx. param_env ( def_id) ;
354
340
341
+ // First, check a few of the same thing as `compare_impl_method`, just so we don't ICE during substitutions later.
342
+ compare_number_of_generics ( tcx, impl_m, trait_m, tcx. hir ( ) . span_if_local ( impl_m. def_id ) , true ) ?;
343
+ compare_generic_param_kinds ( tcx, impl_m, trait_m, true ) ?;
344
+
355
345
let trait_to_impl_substs = impl_trait_ref. substs ;
356
346
357
347
let impl_m_hir_id = tcx. hir ( ) . local_def_id_to_hir_id ( impl_m. def_id . expect_local ( ) ) ;
358
348
let return_span = tcx. hir ( ) . fn_decl_by_hir_id ( impl_m_hir_id) . unwrap ( ) . output . span ( ) ;
359
- let mut cause = ObligationCause :: new (
349
+ let cause = ObligationCause :: new (
360
350
return_span,
361
351
impl_m_hir_id,
362
352
ObligationCauseCode :: CompareImplItemObligation {
@@ -376,6 +366,7 @@ pub fn collect_trait_impl_trait_tys<'tcx>(
376
366
let infcx = & tcx. infer_ctxt ( ) . build ( ) ;
377
367
let ocx = ObligationCtxt :: new ( infcx) ;
378
368
369
+ // Normalize the impl signature with fresh variables for lifetime inference.
379
370
let norm_cause = ObligationCause :: misc ( return_span, impl_m_hir_id) ;
380
371
let impl_sig = ocx. normalize (
381
372
norm_cause. clone ( ) ,
@@ -388,6 +379,10 @@ pub fn collect_trait_impl_trait_tys<'tcx>(
388
379
) ;
389
380
let impl_return_ty = impl_sig. output ( ) ;
390
381
382
+ // Normalize the trait signature with liberated bound vars, passing it through
383
+ // the ImplTraitInTraitCollector, which gathers all of the RPITITs and replaces
384
+ // them with inference variables.
385
+ // We will use these inference variables to collect the hidden types of RPITITs.
391
386
let mut collector = ImplTraitInTraitCollector :: new ( & ocx, return_span, param_env, impl_m_hir_id) ;
392
387
let unnormalized_trait_sig = tcx
393
388
. liberate_late_bound_regions (
@@ -448,14 +443,13 @@ pub fn collect_trait_impl_trait_tys<'tcx>(
448
443
// emit an error now because `compare_predicate_entailment` will not report the error
449
444
// when normalization fails.
450
445
let emitted = report_trait_method_mismatch (
451
- tcx,
452
- & mut cause,
453
446
infcx,
447
+ cause,
454
448
terr,
455
449
( trait_m, trait_fty) ,
456
450
( impl_m, impl_fty) ,
457
- & trait_sig,
458
- & impl_trait_ref,
451
+ trait_sig,
452
+ impl_trait_ref,
459
453
) ;
460
454
return Err ( emitted) ;
461
455
}
@@ -625,23 +619,21 @@ impl<'tcx> TypeFolder<'tcx> for ImplTraitInTraitCollector<'_, 'tcx> {
625
619
}
626
620
627
621
fn report_trait_method_mismatch < ' tcx > (
628
- tcx : TyCtxt < ' tcx > ,
629
- cause : & mut ObligationCause < ' tcx > ,
630
622
infcx : & InferCtxt < ' tcx > ,
623
+ mut cause : ObligationCause < ' tcx > ,
631
624
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 > ,
625
+ ( trait_m, trait_fty) : ( & ty :: AssocItem , Ty < ' tcx > ) ,
626
+ ( impl_m, impl_fty) : ( & ty :: AssocItem , Ty < ' tcx > ) ,
627
+ trait_sig : ty :: FnSig < ' tcx > ,
628
+ impl_trait_ref : ty :: TraitRef < ' tcx > ,
636
629
) -> ErrorGuaranteed {
630
+ let tcx = infcx. tcx ;
637
631
let ( impl_err_span, trait_err_span) =
638
632
extract_spans_for_error_reporting ( & infcx, terr, & cause, impl_m, trait_m) ;
639
633
640
- cause. span = impl_err_span;
641
-
642
634
let mut diag = struct_span_err ! (
643
635
tcx. sess,
644
- cause . span ( ) ,
636
+ impl_err_span ,
645
637
E0053 ,
646
638
"method `{}` has an incompatible type for trait" ,
647
639
trait_m. name
@@ -712,6 +704,7 @@ fn report_trait_method_mismatch<'tcx>(
712
704
_ => { }
713
705
}
714
706
707
+ cause. span = impl_err_span;
715
708
infcx. err_ctxt ( ) . note_type_err (
716
709
& mut diag,
717
710
& cause,
@@ -922,9 +915,9 @@ fn compare_self_type<'tcx>(
922
915
fn compare_number_of_generics < ' tcx > (
923
916
tcx : TyCtxt < ' tcx > ,
924
917
impl_ : & ty:: AssocItem ,
925
- _impl_span : Span ,
926
918
trait_ : & ty:: AssocItem ,
927
919
trait_span : Option < Span > ,
920
+ delay : bool ,
928
921
) -> Result < ( ) , ErrorGuaranteed > {
929
922
let trait_own_counts = tcx. generics_of ( trait_. def_id ) . own_counts ( ) ;
930
923
let impl_own_counts = tcx. generics_of ( impl_. def_id ) . own_counts ( ) ;
@@ -1054,7 +1047,7 @@ fn compare_number_of_generics<'tcx>(
1054
1047
err. span_label ( * span, "`impl Trait` introduces an implicit type parameter" ) ;
1055
1048
}
1056
1049
1057
- let reported = err. emit ( ) ;
1050
+ let reported = err. emit_unless ( delay ) ;
1058
1051
err_occurred = Some ( reported) ;
1059
1052
}
1060
1053
}
@@ -1306,6 +1299,7 @@ fn compare_generic_param_kinds<'tcx>(
1306
1299
tcx : TyCtxt < ' tcx > ,
1307
1300
impl_item : & ty:: AssocItem ,
1308
1301
trait_item : & ty:: AssocItem ,
1302
+ delay : bool ,
1309
1303
) -> Result < ( ) , ErrorGuaranteed > {
1310
1304
assert_eq ! ( impl_item. kind, trait_item. kind) ;
1311
1305
@@ -1363,7 +1357,7 @@ fn compare_generic_param_kinds<'tcx>(
1363
1357
err. span_label ( impl_header_span, "" ) ;
1364
1358
err. span_label ( param_impl_span, make_param_message ( "found" , param_impl) ) ;
1365
1359
1366
- let reported = err. emit ( ) ;
1360
+ let reported = err. emit_unless ( delay ) ;
1367
1361
return Err ( reported) ;
1368
1362
}
1369
1363
}
@@ -1489,9 +1483,9 @@ pub(crate) fn compare_ty_impl<'tcx>(
1489
1483
debug ! ( "compare_impl_type(impl_trait_ref={:?})" , impl_trait_ref) ;
1490
1484
1491
1485
let _: Result < ( ) , ErrorGuaranteed > = ( || {
1492
- compare_number_of_generics ( tcx, impl_ty, impl_ty_span , trait_ty, trait_item_span) ?;
1486
+ compare_number_of_generics ( tcx, impl_ty, trait_ty, trait_item_span, false ) ?;
1493
1487
1494
- compare_generic_param_kinds ( tcx, impl_ty, trait_ty) ?;
1488
+ compare_generic_param_kinds ( tcx, impl_ty, trait_ty, false ) ?;
1495
1489
1496
1490
let sp = tcx. def_span ( impl_ty. def_id ) ;
1497
1491
compare_type_predicate_entailment ( tcx, impl_ty, sp, trait_ty, impl_trait_ref) ?;
0 commit comments