@@ -34,7 +34,7 @@ use std::iter;
34
34
/// - `impl_m_span`: span to use for reporting errors
35
35
/// - `trait_m`: the method in the trait
36
36
/// - `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 > (
38
38
tcx : TyCtxt < ' tcx > ,
39
39
impl_m : & ty:: AssocItem ,
40
40
trait_m : & ty:: AssocItem ,
@@ -71,7 +71,7 @@ pub(crate) fn compare_impl_method<'tcx>(
71
71
return ;
72
72
}
73
73
74
- if let Err ( _) = compare_predicate_entailment (
74
+ if let Err ( _) = compare_method_predicate_entailment (
75
75
tcx,
76
76
impl_m,
77
77
impl_m_span,
@@ -150,7 +150,7 @@ pub(crate) fn compare_impl_method<'tcx>(
150
150
/// Finally we register each of these predicates as an obligation and check that
151
151
/// they hold.
152
152
#[ instrument( level = "debug" , skip( tcx, impl_m_span, impl_trait_ref) ) ]
153
- fn compare_predicate_entailment < ' tcx > (
153
+ fn compare_method_predicate_entailment < ' tcx > (
154
154
tcx : TyCtxt < ' tcx > ,
155
155
impl_m : & ty:: AssocItem ,
156
156
impl_m_span : Span ,
@@ -337,7 +337,7 @@ fn compare_predicate_entailment<'tcx>(
337
337
if !errors. is_empty ( ) {
338
338
match check_implied_wf {
339
339
CheckImpliedWfMode :: Check => {
340
- return compare_predicate_entailment (
340
+ return compare_method_predicate_entailment (
341
341
tcx,
342
342
impl_m,
343
343
impl_m_span,
@@ -374,7 +374,7 @@ fn compare_predicate_entailment<'tcx>(
374
374
// becomes a hard error (i.e. ideally we'd just call `resolve_regions_and_report_errors`
375
375
match check_implied_wf {
376
376
CheckImpliedWfMode :: Check => {
377
- return compare_predicate_entailment (
377
+ return compare_method_predicate_entailment (
378
378
tcx,
379
379
impl_m,
380
380
impl_m_span,
@@ -407,7 +407,7 @@ enum CheckImpliedWfMode {
407
407
/// re-check with `Skip`, and emit a lint if it succeeds.
408
408
Check ,
409
409
/// 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
411
411
/// the reason that we had failed earlier during `Check` was due to the impl
412
412
/// having stronger requirements than the trait.
413
413
Skip ,
@@ -441,8 +441,41 @@ fn compare_asyncness<'tcx>(
441
441
Ok ( ( ) )
442
442
}
443
443
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.
444
477
#[ 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 > (
446
479
tcx : TyCtxt < ' tcx > ,
447
480
def_id : DefId ,
448
481
) -> Result < & ' tcx FxHashMap < DefId , Ty < ' tcx > > , ErrorGuaranteed > {
@@ -550,13 +583,13 @@ pub fn collect_trait_impl_trait_tys<'tcx>(
550
583
// Unify the whole function signature. We need to do this to fully infer
551
584
// the lifetimes of the return type, but do this after unifying just the
552
585
// return types, since we want to avoid duplicating errors from
553
- // `compare_predicate_entailment `.
586
+ // `compare_method_predicate_entailment `.
554
587
match ocx. eq ( & cause, param_env, trait_fty, impl_fty) {
555
588
Ok ( ( ) ) => { }
556
589
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
558
591
// 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
560
593
// when normalization fails.
561
594
let emitted = report_trait_method_mismatch (
562
595
infcx,
@@ -589,7 +622,7 @@ pub fn collect_trait_impl_trait_tys<'tcx>(
589
622
infcx. check_region_obligations_and_report_errors (
590
623
impl_m. def_id . expect_local ( ) ,
591
624
& outlives_environment,
592
- ) ;
625
+ ) ? ;
593
626
594
627
let mut collected_tys = FxHashMap :: default ( ) ;
595
628
for ( def_id, ( ty, substs) ) in collector. types {
@@ -1516,8 +1549,8 @@ fn compare_generic_param_kinds<'tcx>(
1516
1549
Ok ( ( ) )
1517
1550
}
1518
1551
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 (
1521
1554
tcx : TyCtxt < ' _ > ,
1522
1555
( impl_const_item_def, trait_const_item_def) : ( LocalDefId , DefId ) ,
1523
1556
) -> Result < ( ) , ErrorGuaranteed > {
@@ -1617,13 +1650,13 @@ pub(crate) fn raw_compare_const_impl(
1617
1650
return Err ( infcx. err_ctxt ( ) . report_fulfillment_errors ( & errors, None ) ) ;
1618
1651
}
1619
1652
1620
- // FIXME return `ErrorReported` if region obligations error?
1621
1653
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
+
1623
1656
Ok ( ( ) )
1624
1657
}
1625
1658
1626
- pub ( crate ) fn compare_ty_impl < ' tcx > (
1659
+ pub ( super ) fn compare_impl_ty < ' tcx > (
1627
1660
tcx : TyCtxt < ' tcx > ,
1628
1661
impl_ty : & ty:: AssocItem ,
1629
1662
impl_ty_span : Span ,
@@ -1645,7 +1678,7 @@ pub(crate) fn compare_ty_impl<'tcx>(
1645
1678
} ) ( ) ;
1646
1679
}
1647
1680
1648
- /// The equivalent of [compare_predicate_entailment ], but for associated types
1681
+ /// The equivalent of [compare_method_predicate_entailment ], but for associated types
1649
1682
/// instead of associated functions.
1650
1683
fn compare_type_predicate_entailment < ' tcx > (
1651
1684
tcx : TyCtxt < ' tcx > ,
@@ -1730,7 +1763,7 @@ fn compare_type_predicate_entailment<'tcx>(
1730
1763
infcx. check_region_obligations_and_report_errors (
1731
1764
impl_ty. def_id . expect_local ( ) ,
1732
1765
& outlives_environment,
1733
- ) ;
1766
+ ) ? ;
1734
1767
1735
1768
Ok ( ( ) )
1736
1769
}
@@ -1749,7 +1782,7 @@ fn compare_type_predicate_entailment<'tcx>(
1749
1782
/// from the impl could be overridden). We also can't normalize generic
1750
1783
/// associated types (yet) because they contain bound parameters.
1751
1784
#[ instrument( level = "debug" , skip( tcx) ) ]
1752
- pub fn check_type_bounds < ' tcx > (
1785
+ pub ( super ) fn check_type_bounds < ' tcx > (
1753
1786
tcx : TyCtxt < ' tcx > ,
1754
1787
trait_ty : & ty:: AssocItem ,
1755
1788
impl_ty : & ty:: AssocItem ,
@@ -1944,7 +1977,7 @@ pub fn check_type_bounds<'tcx>(
1944
1977
infcx. check_region_obligations_and_report_errors (
1945
1978
impl_ty. def_id . expect_local ( ) ,
1946
1979
& outlives_environment,
1947
- ) ;
1980
+ ) ? ;
1948
1981
1949
1982
let constraints = infcx. inner . borrow_mut ( ) . opaque_type_storage . take_opaque_types ( ) ;
1950
1983
for ( key, value) in constraints {
0 commit comments