@@ -34,7 +34,7 @@ use std::iter;
3434/// - `impl_m_span`: span to use for reporting errors
3535/// - `trait_m`: the method in the trait
3636/// - `impl_trait_ref`: the TraitRef corresponding to the trait implementation
37- pub ( crate ) fn compare_impl_method < ' tcx > (
37+ pub ( super ) fn compare_impl_method < ' tcx > (
3838 tcx : TyCtxt < ' tcx > ,
3939 impl_m : & ty:: AssocItem ,
4040 trait_m : & ty:: AssocItem ,
@@ -71,7 +71,7 @@ pub(crate) fn compare_impl_method<'tcx>(
7171 return ;
7272 }
7373
74- if let Err ( _) = compare_predicate_entailment (
74+ if let Err ( _) = compare_method_predicate_entailment (
7575 tcx,
7676 impl_m,
7777 impl_m_span,
@@ -150,7 +150,7 @@ pub(crate) fn compare_impl_method<'tcx>(
150150/// Finally we register each of these predicates as an obligation and check that
151151/// they hold.
152152#[ instrument( level = "debug" , skip( tcx, impl_m_span, impl_trait_ref) ) ]
153- fn compare_predicate_entailment < ' tcx > (
153+ fn compare_method_predicate_entailment < ' tcx > (
154154 tcx : TyCtxt < ' tcx > ,
155155 impl_m : & ty:: AssocItem ,
156156 impl_m_span : Span ,
@@ -337,7 +337,7 @@ fn compare_predicate_entailment<'tcx>(
337337 if !errors. is_empty ( ) {
338338 match check_implied_wf {
339339 CheckImpliedWfMode :: Check => {
340- return compare_predicate_entailment (
340+ return compare_method_predicate_entailment (
341341 tcx,
342342 impl_m,
343343 impl_m_span,
@@ -374,7 +374,7 @@ fn compare_predicate_entailment<'tcx>(
374374 // becomes a hard error (i.e. ideally we'd just call `resolve_regions_and_report_errors`
375375 match check_implied_wf {
376376 CheckImpliedWfMode :: Check => {
377- return compare_predicate_entailment (
377+ return compare_method_predicate_entailment (
378378 tcx,
379379 impl_m,
380380 impl_m_span,
@@ -407,7 +407,7 @@ enum CheckImpliedWfMode {
407407 /// re-check with `Skip`, and emit a lint if it succeeds.
408408 Check ,
409409 /// Skips checking implied well-formedness of the impl method, but will emit
410- /// a lint if the `compare_predicate_entailment ` succeeded. This means that
410+ /// a lint if the `compare_method_predicate_entailment ` succeeded. This means that
411411 /// the reason that we had failed earlier during `Check` was due to the impl
412412 /// having stronger requirements than the trait.
413413 Skip ,
@@ -441,8 +441,41 @@ fn compare_asyncness<'tcx>(
441441 Ok ( ( ) )
442442}
443443
444+ /// Given a method def-id in an impl, compare the method signature of the impl
445+ /// against the trait that it's implementing. In doing so, infer the hidden types
446+ /// that this method's signature provides to satisfy each return-position `impl Trait`
447+ /// in the trait signature.
448+ ///
449+ /// The method is also responsible for making sure that the hidden types for each
450+ /// RPITIT actually satisfy the bounds of the `impl Trait`, i.e. that if we infer
451+ /// `impl Trait = Foo`, that `Foo: Trait` holds.
452+ ///
453+ /// For example, given the sample code:
454+ ///
455+ /// ```
456+ /// #![feature(return_position_impl_trait_in_trait)]
457+ ///
458+ /// use std::ops::Deref;
459+ ///
460+ /// trait Foo {
461+ /// fn bar() -> impl Deref<Target = impl Sized>;
462+ /// // ^- RPITIT #1 ^- RPITIT #2
463+ /// }
464+ ///
465+ /// impl Foo for () {
466+ /// fn bar() -> Box<String> { Box::new(String::new()) }
467+ /// }
468+ /// ```
469+ ///
470+ /// The hidden types for the RPITITs in `bar` would be inferred to:
471+ /// * `impl Deref` (RPITIT #1) = `Box<String>`
472+ /// * `impl Sized` (RPITIT #2) = `String`
473+ ///
474+ /// The relationship between these two types is straightforward in this case, but
475+ /// may be more tenuously connected via other `impl`s and normalization rules for
476+ /// cases of more complicated nested RPITITs.
444477#[ instrument( skip( tcx) , level = "debug" , ret) ]
445- pub fn collect_trait_impl_trait_tys < ' tcx > (
478+ pub ( super ) fn collect_return_position_impl_trait_in_trait_tys < ' tcx > (
446479 tcx : TyCtxt < ' tcx > ,
447480 def_id : DefId ,
448481) -> Result < & ' tcx FxHashMap < DefId , Ty < ' tcx > > , ErrorGuaranteed > {
@@ -550,13 +583,13 @@ pub fn collect_trait_impl_trait_tys<'tcx>(
550583 // Unify the whole function signature. We need to do this to fully infer
551584 // the lifetimes of the return type, but do this after unifying just the
552585 // return types, since we want to avoid duplicating errors from
553- // `compare_predicate_entailment `.
586+ // `compare_method_predicate_entailment `.
554587 match ocx. eq ( & cause, param_env, trait_fty, impl_fty) {
555588 Ok ( ( ) ) => { }
556589 Err ( terr) => {
557- // This function gets called during `compare_predicate_entailment ` when normalizing a
590+ // This function gets called during `compare_method_predicate_entailment ` when normalizing a
558591 // signature that contains RPITIT. When the method signatures don't match, we have to
559- // emit an error now because `compare_predicate_entailment ` will not report the error
592+ // emit an error now because `compare_method_predicate_entailment ` will not report the error
560593 // when normalization fails.
561594 let emitted = report_trait_method_mismatch (
562595 infcx,
@@ -589,7 +622,7 @@ pub fn collect_trait_impl_trait_tys<'tcx>(
589622 infcx. check_region_obligations_and_report_errors (
590623 impl_m. def_id . expect_local ( ) ,
591624 & outlives_environment,
592- ) ;
625+ ) ? ;
593626
594627 let mut collected_tys = FxHashMap :: default ( ) ;
595628 for ( def_id, ( ty, substs) ) in collector. types {
@@ -1516,8 +1549,8 @@ fn compare_generic_param_kinds<'tcx>(
15161549 Ok ( ( ) )
15171550}
15181551
1519- /// Use `tcx.compare_assoc_const_impl_item_with_trait_item ` instead
1520- pub ( crate ) fn raw_compare_const_impl (
1552+ /// Use `tcx.compare_impl_const ` instead
1553+ pub ( super ) fn compare_impl_const_raw (
15211554 tcx : TyCtxt < ' _ > ,
15221555 ( impl_const_item_def, trait_const_item_def) : ( LocalDefId , DefId ) ,
15231556) -> Result < ( ) , ErrorGuaranteed > {
@@ -1617,13 +1650,13 @@ pub(crate) fn raw_compare_const_impl(
16171650 return Err ( infcx. err_ctxt ( ) . report_fulfillment_errors ( & errors, None ) ) ;
16181651 }
16191652
1620- // FIXME return `ErrorReported` if region obligations error?
16211653 let outlives_environment = OutlivesEnvironment :: new ( param_env) ;
1622- infcx. check_region_obligations_and_report_errors ( impl_const_item_def, & outlives_environment) ;
1654+ infcx. check_region_obligations_and_report_errors ( impl_const_item_def, & outlives_environment) ?;
1655+
16231656 Ok ( ( ) )
16241657}
16251658
1626- pub ( crate ) fn compare_ty_impl < ' tcx > (
1659+ pub ( super ) fn compare_impl_ty < ' tcx > (
16271660 tcx : TyCtxt < ' tcx > ,
16281661 impl_ty : & ty:: AssocItem ,
16291662 impl_ty_span : Span ,
@@ -1645,7 +1678,7 @@ pub(crate) fn compare_ty_impl<'tcx>(
16451678 } ) ( ) ;
16461679}
16471680
1648- /// The equivalent of [compare_predicate_entailment ], but for associated types
1681+ /// The equivalent of [compare_method_predicate_entailment ], but for associated types
16491682/// instead of associated functions.
16501683fn compare_type_predicate_entailment < ' tcx > (
16511684 tcx : TyCtxt < ' tcx > ,
@@ -1730,7 +1763,7 @@ fn compare_type_predicate_entailment<'tcx>(
17301763 infcx. check_region_obligations_and_report_errors (
17311764 impl_ty. def_id . expect_local ( ) ,
17321765 & outlives_environment,
1733- ) ;
1766+ ) ? ;
17341767
17351768 Ok ( ( ) )
17361769}
@@ -1749,7 +1782,7 @@ fn compare_type_predicate_entailment<'tcx>(
17491782/// from the impl could be overridden). We also can't normalize generic
17501783/// associated types (yet) because they contain bound parameters.
17511784#[ instrument( level = "debug" , skip( tcx) ) ]
1752- pub fn check_type_bounds < ' tcx > (
1785+ pub ( super ) fn check_type_bounds < ' tcx > (
17531786 tcx : TyCtxt < ' tcx > ,
17541787 trait_ty : & ty:: AssocItem ,
17551788 impl_ty : & ty:: AssocItem ,
@@ -1944,7 +1977,7 @@ pub fn check_type_bounds<'tcx>(
19441977 infcx. check_region_obligations_and_report_errors (
19451978 impl_ty. def_id . expect_local ( ) ,
19461979 & outlives_environment,
1947- ) ;
1980+ ) ? ;
19481981
19491982 let constraints = infcx. inner . borrow_mut ( ) . opaque_type_storage . take_opaque_types ( ) ;
19501983 for ( key, value) in constraints {
0 commit comments