@@ -14,10 +14,8 @@ use rustc_infer::traits::util;
1414use rustc_middle:: ty:: error:: { ExpectedFound , TypeError } ;
1515use rustc_middle:: ty:: util:: ExplicitSelf ;
1616use rustc_middle:: ty:: {
17- self , AssocItem , DefIdTree , TraitRef , Ty , TypeFoldable , TypeFolder , TypeSuperFoldable ,
18- TypeVisitable ,
17+ self , DefIdTree , InternalSubsts , Ty , TypeFoldable , TypeFolder , TypeSuperFoldable , TypeVisitable ,
1918} ;
20- use rustc_middle:: ty:: { FnSig , InternalSubsts } ;
2119use rustc_middle:: ty:: { GenericParamDefKind , ToPredicate , TyCtxt } ;
2220use rustc_span:: Span ;
2321use rustc_trait_selection:: traits:: error_reporting:: TypeErrCtxtExt ;
@@ -51,11 +49,11 @@ pub(crate) fn compare_impl_method<'tcx>(
5149 return ;
5250 }
5351
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 ) {
5553 return ;
5654 }
5755
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 ) {
5957 return ;
6058 }
6159
@@ -144,9 +142,9 @@ pub(crate) fn compare_impl_method<'tcx>(
144142#[ instrument( level = "debug" , skip( tcx, impl_m_span, impl_trait_ref) ) ]
145143fn compare_predicate_entailment < ' tcx > (
146144 tcx : TyCtxt < ' tcx > ,
147- impl_m : & AssocItem ,
145+ impl_m : & ty :: AssocItem ,
148146 impl_m_span : Span ,
149- trait_m : & AssocItem ,
147+ trait_m : & ty :: AssocItem ,
150148 impl_trait_ref : ty:: TraitRef < ' tcx > ,
151149) -> Result < ( ) , ErrorGuaranteed > {
152150 let trait_to_impl_substs = impl_trait_ref. substs ;
@@ -157,8 +155,7 @@ fn compare_predicate_entailment<'tcx>(
157155 // FIXME(@lcnr): remove that after removing `cause.body_id` from
158156 // obligations.
159157 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 (
162159 impl_m_span,
163160 impl_m_hir_id,
164161 ObligationCauseCode :: CompareImplItemObligation {
@@ -291,30 +288,19 @@ fn compare_predicate_entailment<'tcx>(
291288 // type would be more appropriate. In other places we have a `Vec<Span>`
292289 // corresponding to their `Vec<Predicate>`, but we don't have that here.
293290 // 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) ;
305292
306293 if let Err ( terr) = result {
307294 debug ! ( ?terr, "sub_types failed: impl ty {:?}, trait ty {:?}" , impl_fty, trait_fty) ;
308295
309296 let emitted = report_trait_method_mismatch (
310- tcx,
311- & mut cause,
312297 & infcx,
298+ cause,
313299 terr,
314300 ( trait_m, trait_fty) ,
315301 ( impl_m, impl_fty) ,
316- & trait_sig,
317- & impl_trait_ref,
302+ trait_sig,
303+ impl_trait_ref,
318304 ) ;
319305 return Err ( emitted) ;
320306 }
@@ -352,11 +338,15 @@ pub fn collect_trait_impl_trait_tys<'tcx>(
352338 let impl_trait_ref = tcx. impl_trait_ref ( impl_m. impl_container ( tcx) . unwrap ( ) ) . unwrap ( ) ;
353339 let param_env = tcx. param_env ( def_id) ;
354340
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+
355345 let trait_to_impl_substs = impl_trait_ref. substs ;
356346
357347 let impl_m_hir_id = tcx. hir ( ) . local_def_id_to_hir_id ( impl_m. def_id . expect_local ( ) ) ;
358348 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 (
360350 return_span,
361351 impl_m_hir_id,
362352 ObligationCauseCode :: CompareImplItemObligation {
@@ -376,6 +366,7 @@ pub fn collect_trait_impl_trait_tys<'tcx>(
376366 let infcx = & tcx. infer_ctxt ( ) . build ( ) ;
377367 let ocx = ObligationCtxt :: new ( infcx) ;
378368
369+ // Normalize the impl signature with fresh variables for lifetime inference.
379370 let norm_cause = ObligationCause :: misc ( return_span, impl_m_hir_id) ;
380371 let impl_sig = ocx. normalize (
381372 norm_cause. clone ( ) ,
@@ -388,6 +379,10 @@ pub fn collect_trait_impl_trait_tys<'tcx>(
388379 ) ;
389380 let impl_return_ty = impl_sig. output ( ) ;
390381
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.
391386 let mut collector = ImplTraitInTraitCollector :: new ( & ocx, return_span, param_env, impl_m_hir_id) ;
392387 let unnormalized_trait_sig = tcx
393388 . liberate_late_bound_regions (
@@ -448,14 +443,13 @@ pub fn collect_trait_impl_trait_tys<'tcx>(
448443 // emit an error now because `compare_predicate_entailment` will not report the error
449444 // when normalization fails.
450445 let emitted = report_trait_method_mismatch (
451- tcx,
452- & mut cause,
453446 infcx,
447+ cause,
454448 terr,
455449 ( trait_m, trait_fty) ,
456450 ( impl_m, impl_fty) ,
457- & trait_sig,
458- & impl_trait_ref,
451+ trait_sig,
452+ impl_trait_ref,
459453 ) ;
460454 return Err ( emitted) ;
461455 }
@@ -625,23 +619,21 @@ impl<'tcx> TypeFolder<'tcx> for ImplTraitInTraitCollector<'_, 'tcx> {
625619}
626620
627621fn report_trait_method_mismatch < ' tcx > (
628- tcx : TyCtxt < ' tcx > ,
629- cause : & mut ObligationCause < ' tcx > ,
630622 infcx : & InferCtxt < ' tcx > ,
623+ mut cause : ObligationCause < ' tcx > ,
631624 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 > ,
636629) -> ErrorGuaranteed {
630+ let tcx = infcx. tcx ;
637631 let ( impl_err_span, trait_err_span) =
638632 extract_spans_for_error_reporting ( & infcx, terr, & cause, impl_m, trait_m) ;
639633
640- cause. span = impl_err_span;
641-
642634 let mut diag = struct_span_err ! (
643635 tcx. sess,
644- cause . span ( ) ,
636+ impl_err_span ,
645637 E0053 ,
646638 "method `{}` has an incompatible type for trait" ,
647639 trait_m. name
@@ -712,6 +704,7 @@ fn report_trait_method_mismatch<'tcx>(
712704 _ => { }
713705 }
714706
707+ cause. span = impl_err_span;
715708 infcx. err_ctxt ( ) . note_type_err (
716709 & mut diag,
717710 & cause,
@@ -922,9 +915,9 @@ fn compare_self_type<'tcx>(
922915fn compare_number_of_generics < ' tcx > (
923916 tcx : TyCtxt < ' tcx > ,
924917 impl_ : & ty:: AssocItem ,
925- _impl_span : Span ,
926918 trait_ : & ty:: AssocItem ,
927919 trait_span : Option < Span > ,
920+ delay : bool ,
928921) -> Result < ( ) , ErrorGuaranteed > {
929922 let trait_own_counts = tcx. generics_of ( trait_. def_id ) . own_counts ( ) ;
930923 let impl_own_counts = tcx. generics_of ( impl_. def_id ) . own_counts ( ) ;
@@ -1054,7 +1047,7 @@ fn compare_number_of_generics<'tcx>(
10541047 err. span_label ( * span, "`impl Trait` introduces an implicit type parameter" ) ;
10551048 }
10561049
1057- let reported = err. emit ( ) ;
1050+ let reported = err. emit_unless ( delay ) ;
10581051 err_occurred = Some ( reported) ;
10591052 }
10601053 }
@@ -1306,6 +1299,7 @@ fn compare_generic_param_kinds<'tcx>(
13061299 tcx : TyCtxt < ' tcx > ,
13071300 impl_item : & ty:: AssocItem ,
13081301 trait_item : & ty:: AssocItem ,
1302+ delay : bool ,
13091303) -> Result < ( ) , ErrorGuaranteed > {
13101304 assert_eq ! ( impl_item. kind, trait_item. kind) ;
13111305
@@ -1363,7 +1357,7 @@ fn compare_generic_param_kinds<'tcx>(
13631357 err. span_label ( impl_header_span, "" ) ;
13641358 err. span_label ( param_impl_span, make_param_message ( "found" , param_impl) ) ;
13651359
1366- let reported = err. emit ( ) ;
1360+ let reported = err. emit_unless ( delay ) ;
13671361 return Err ( reported) ;
13681362 }
13691363 }
@@ -1489,9 +1483,9 @@ pub(crate) fn compare_ty_impl<'tcx>(
14891483 debug ! ( "compare_impl_type(impl_trait_ref={:?})" , impl_trait_ref) ;
14901484
14911485 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 ) ?;
14931487
1494- compare_generic_param_kinds ( tcx, impl_ty, trait_ty) ?;
1488+ compare_generic_param_kinds ( tcx, impl_ty, trait_ty, false ) ?;
14951489
14961490 let sp = tcx. def_span ( impl_ty. def_id ) ;
14971491 compare_type_predicate_entailment ( tcx, impl_ty, sp, trait_ty, impl_trait_ref) ?;
0 commit comments