@@ -29,6 +29,7 @@ use rustc_hir::{GenericArg, GenericArgs, OpaqueTyOrigin};
29
29
use rustc_infer:: infer:: { InferCtxt , InferOk , TyCtxtInferExt } ;
30
30
use rustc_infer:: traits:: ObligationCause ;
31
31
use rustc_middle:: middle:: stability:: AllowUnstable ;
32
+ use rustc_middle:: ty:: visit:: TypeVisitable ;
32
33
use rustc_middle:: ty:: GenericParamDefKind ;
33
34
use rustc_middle:: ty:: {
34
35
self , Const , GenericArgKind , GenericArgsRef , IsSuggestable , Ty , TyCtxt , TypeVisitableExt ,
@@ -38,9 +39,9 @@ use rustc_span::edit_distance::find_best_match_for_name;
38
39
use rustc_span:: symbol:: { kw, Ident , Symbol } ;
39
40
use rustc_span:: { sym, BytePos , Span , DUMMY_SP } ;
40
41
use rustc_target:: spec:: abi;
42
+ use rustc_trait_selection:: traits:: query:: normalize:: MaxEscapingBoundVarVisitor ;
41
43
use rustc_trait_selection:: traits:: wf:: object_region_bounds;
42
44
use rustc_trait_selection:: traits:: { self , NormalizeExt , ObligationCtxt } ;
43
- use rustc_type_ir:: fold:: { TypeFoldable , TypeFolder , TypeSuperFoldable } ;
44
45
45
46
use std:: fmt:: Display ;
46
47
use std:: slice;
@@ -1609,133 +1610,116 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
1609
1610
let cause = ObligationCause :: misc ( span, block. owner . def_id ) ;
1610
1611
1611
1612
let mut fulfillment_errors = Vec :: new ( ) ;
1612
- let mut applicable_candidates: Vec < _ > = infcx. probe ( |_| {
1613
- // Regions are not considered during selection.
1614
- let self_ty = self_ty
1615
- . fold_with ( & mut BoundVarEraser { tcx, universe : infcx. create_next_universe ( ) } ) ;
1616
-
1617
- struct BoundVarEraser < ' tcx > {
1618
- tcx : TyCtxt < ' tcx > ,
1619
- universe : ty:: UniverseIndex ,
1620
- }
1621
-
1622
- // FIXME(non_lifetime_binders): Don't assign the same universe to each placeholder.
1623
- impl < ' tcx > TypeFolder < TyCtxt < ' tcx > > for BoundVarEraser < ' tcx > {
1624
- fn interner ( & self ) -> TyCtxt < ' tcx > {
1625
- self . tcx
1626
- }
1613
+ infcx. probe ( |_| {
1614
+ let mut universes = if self_ty. has_escaping_bound_vars ( ) {
1615
+ let mut max_visitor =
1616
+ MaxEscapingBoundVarVisitor { outer_index : ty:: INNERMOST , escaping : 0 } ;
1617
+ self_ty. visit_with ( & mut max_visitor) ;
1618
+ vec ! [ None ; max_visitor. escaping]
1619
+ } else {
1620
+ vec ! [ ]
1621
+ } ;
1627
1622
1628
- fn fold_region ( & mut self , r : ty:: Region < ' tcx > ) -> ty:: Region < ' tcx > {
1629
- // FIXME(@lcnr): This is broken, erasing bound regions
1630
- // impacts selection as it results in different types.
1631
- if r. is_bound ( ) { self . tcx . lifetimes . re_erased } else { r }
1632
- }
1623
+ crate :: traits:: project:: with_replaced_escaping_bound_vars (
1624
+ infcx,
1625
+ & mut universes,
1626
+ self_ty,
1627
+ |self_ty| {
1628
+ let InferOk { value : self_ty, obligations } =
1629
+ infcx. at ( & cause, param_env) . normalize ( self_ty) ;
1633
1630
1634
- fn fold_ty ( & mut self , ty : Ty < ' tcx > ) -> Ty < ' tcx > {
1635
- match * ty. kind ( ) {
1636
- ty:: Bound ( _, bv) => Ty :: new_placeholder (
1637
- self . tcx ,
1638
- ty:: PlaceholderType { universe : self . universe , bound : bv } ,
1639
- ) ,
1640
- _ => ty. super_fold_with ( self ) ,
1641
- }
1642
- }
1631
+ let mut applicable_candidates: Vec < _ > = candidates
1632
+ . iter ( )
1633
+ . copied ( )
1634
+ . filter ( |& ( impl_, _) | {
1635
+ infcx. probe ( |_| {
1636
+ let ocx = ObligationCtxt :: new ( & infcx) ;
1637
+ ocx. register_obligations ( obligations. clone ( ) ) ;
1638
+
1639
+ let impl_args = infcx. fresh_args_for_item ( span, impl_) ;
1640
+ let impl_ty = tcx. type_of ( impl_) . instantiate ( tcx, impl_args) ;
1641
+ let impl_ty = ocx. normalize ( & cause, param_env, impl_ty) ;
1642
+
1643
+ // Check that the self types can be related.
1644
+ // FIXME(inherent_associated_types): Should we use `eq` here? Method probing uses
1645
+ // `sup` for this situtation, too. What for? To constrain inference variables?
1646
+ if ocx
1647
+ . sup ( & ObligationCause :: dummy ( ) , param_env, impl_ty, self_ty)
1648
+ . is_err ( )
1649
+ {
1650
+ return false ;
1651
+ }
1643
1652
1644
- fn fold_const (
1645
- & mut self ,
1646
- ct : ty:: Const < ' tcx > ,
1647
- ) -> <TyCtxt < ' tcx > as rustc_type_ir:: Interner >:: Const {
1648
- assert ! ( !ct. ty( ) . has_escaping_bound_vars( ) ) ;
1649
-
1650
- match ct. kind ( ) {
1651
- ty:: ConstKind :: Bound ( _, bv) => ty:: Const :: new_placeholder (
1652
- self . tcx ,
1653
- ty:: PlaceholderConst { universe : self . universe , bound : bv } ,
1654
- ct. ty ( ) ,
1655
- ) ,
1656
- _ => ct. super_fold_with ( self ) ,
1657
- }
1658
- }
1659
- }
1653
+ // Check whether the impl imposes obligations we have to worry about.
1654
+ let impl_bounds =
1655
+ tcx. predicates_of ( impl_) . instantiate ( tcx, impl_args) ;
1656
+ let impl_bounds = ocx. normalize ( & cause, param_env, impl_bounds) ;
1657
+ let impl_obligations = traits:: predicates_for_generics (
1658
+ |_, _| cause. clone ( ) ,
1659
+ param_env,
1660
+ impl_bounds,
1661
+ ) ;
1662
+ ocx. register_obligations ( impl_obligations) ;
1660
1663
1661
- let InferOk { value : self_ty, obligations } =
1662
- infcx. at ( & cause, param_env) . normalize ( self_ty) ;
1664
+ let mut errors = ocx. select_where_possible ( ) ;
1665
+ if !errors. is_empty ( ) {
1666
+ fulfillment_errors. append ( & mut errors) ;
1667
+ return false ;
1668
+ }
1663
1669
1664
- candidates
1665
- . iter ( )
1666
- . copied ( )
1667
- . filter ( |& ( impl_, _) | {
1668
- infcx. probe ( |_| {
1669
- let ocx = ObligationCtxt :: new ( & infcx) ;
1670
- ocx. register_obligations ( obligations. clone ( ) ) ;
1671
-
1672
- let impl_args = infcx. fresh_args_for_item ( span, impl_) ;
1673
- let impl_ty = tcx. type_of ( impl_) . instantiate ( tcx, impl_args) ;
1674
- let impl_ty = ocx. normalize ( & cause, param_env, impl_ty) ;
1675
-
1676
- // Check that the self types can be related.
1677
- // FIXME(inherent_associated_types): Should we use `eq` here? Method probing uses
1678
- // `sup` for this situtation, too. What for? To constrain inference variables?
1679
- if ocx. sup ( & ObligationCause :: dummy ( ) , param_env, impl_ty, self_ty) . is_err ( )
1680
- {
1681
- return false ;
1682
- }
1670
+ true
1671
+ } )
1672
+ } )
1673
+ . collect ( ) ;
1674
+
1675
+ if applicable_candidates. len ( ) > 1 {
1676
+ return Err ( self . complain_about_ambiguous_inherent_assoc_type (
1677
+ name,
1678
+ applicable_candidates
1679
+ . into_iter ( )
1680
+ . map ( |( _, ( candidate, _) ) | candidate)
1681
+ . collect ( ) ,
1682
+ span,
1683
+ ) ) ;
1684
+ }
1683
1685
1684
- // Check whether the impl imposes obligations we have to worry about.
1685
- let impl_bounds = tcx. predicates_of ( impl_) . instantiate ( tcx, impl_args) ;
1686
- let impl_bounds = ocx. normalize ( & cause, param_env, impl_bounds) ;
1687
- let impl_obligations = traits:: predicates_for_generics (
1688
- |_, _| cause. clone ( ) ,
1689
- param_env,
1690
- impl_bounds,
1686
+ if let Some ( ( impl_, ( assoc_item, def_scope) ) ) = applicable_candidates. pop ( ) {
1687
+ self . check_assoc_ty ( assoc_item, name, def_scope, block, span) ;
1688
+
1689
+ // FIXME(fmease): Currently creating throwaway `parent_args` to please
1690
+ // `create_args_for_associated_item`. Modify the latter instead (or sth. similar) to
1691
+ // not require the parent args logic.
1692
+ let parent_args = ty:: GenericArgs :: identity_for_item ( tcx, impl_) ;
1693
+ let args = self . create_args_for_associated_item (
1694
+ span,
1695
+ assoc_item,
1696
+ segment,
1697
+ parent_args,
1698
+ ) ;
1699
+ let args = tcx. mk_args_from_iter (
1700
+ std:: iter:: once ( ty:: GenericArg :: from ( self_ty) )
1701
+ . chain ( args. into_iter ( ) . skip ( parent_args. len ( ) ) ) ,
1691
1702
) ;
1692
- ocx. register_obligations ( impl_obligations) ;
1693
-
1694
- let mut errors = ocx. select_where_possible ( ) ;
1695
- if !errors. is_empty ( ) {
1696
- fulfillment_errors. append ( & mut errors) ;
1697
- return false ;
1698
- }
1699
-
1700
- true
1701
- } )
1702
- } )
1703
- . collect ( )
1704
- } ) ;
1705
-
1706
- if applicable_candidates. len ( ) > 1 {
1707
- return Err ( self . complain_about_ambiguous_inherent_assoc_type (
1708
- name,
1709
- applicable_candidates. into_iter ( ) . map ( |( _, ( candidate, _) ) | candidate) . collect ( ) ,
1710
- span,
1711
- ) ) ;
1712
- }
1713
-
1714
- if let Some ( ( impl_, ( assoc_item, def_scope) ) ) = applicable_candidates. pop ( ) {
1715
- self . check_assoc_ty ( assoc_item, name, def_scope, block, span) ;
1716
-
1717
- // FIXME(fmease): Currently creating throwaway `parent_args` to please
1718
- // `create_args_for_associated_item`. Modify the latter instead (or sth. similar) to
1719
- // not require the parent args logic.
1720
- let parent_args = ty:: GenericArgs :: identity_for_item ( tcx, impl_) ;
1721
- let args = self . create_args_for_associated_item ( span, assoc_item, segment, parent_args) ;
1722
- let args = tcx. mk_args_from_iter (
1723
- std:: iter:: once ( ty:: GenericArg :: from ( self_ty) )
1724
- . chain ( args. into_iter ( ) . skip ( parent_args. len ( ) ) ) ,
1725
- ) ;
1726
1703
1727
- let ty = Ty :: new_alias ( tcx, ty:: Inherent , ty:: AliasTy :: new ( tcx, assoc_item, args) ) ;
1704
+ let ty = Ty :: new_alias (
1705
+ tcx,
1706
+ ty:: Inherent ,
1707
+ ty:: AliasTy :: new ( tcx, assoc_item, args) ,
1708
+ ) ;
1728
1709
1729
- return Ok ( Some ( ( ty, assoc_item) ) ) ;
1730
- }
1710
+ return Ok ( Some ( ( ty, assoc_item) ) ) ;
1711
+ }
1731
1712
1732
- Err ( self . complain_about_inherent_assoc_type_not_found (
1733
- name,
1734
- self_ty,
1735
- candidates,
1736
- fulfillment_errors,
1737
- span,
1738
- ) )
1713
+ Err ( self . complain_about_inherent_assoc_type_not_found (
1714
+ name,
1715
+ self_ty,
1716
+ candidates,
1717
+ fulfillment_errors,
1718
+ span,
1719
+ ) )
1720
+ } ,
1721
+ )
1722
+ } )
1739
1723
}
1740
1724
1741
1725
fn lookup_assoc_ty (
0 commit comments