@@ -6,9 +6,10 @@ use derive_where::derive_where;
66use rustc_type_ir:: inherent:: * ;
77use rustc_type_ir:: lang_items:: TraitSolverLangItem ;
88use rustc_type_ir:: {
9- self as ty, Interner , TypeFoldable , TypeVisitableExt as _, TypingMode , Upcast as _, elaborate,
9+ self as ty, Interner , TypeFoldable , TypeFolder , TypeSuperFoldable , TypeVisitableExt as _,
10+ TypingMode , Upcast as _, elaborate,
1011} ;
11- use tracing:: { debug , instrument} ;
12+ use tracing:: instrument;
1213
1314use super :: has_only_region_constraints;
1415use super :: trait_goals:: TraitGoalProvenVia ;
@@ -316,8 +317,7 @@ where
316317 } ;
317318
318319 if normalized_self_ty. is_ty_var ( ) {
319- debug ! ( "self type has been normalized to infer" ) ;
320- return self . forced_ambiguity ( MaybeCause :: Ambiguity ) . into_iter ( ) . collect ( ) ;
320+ return self . try_assemble_bounds_via_registered_opaque ( goal, normalized_self_ty) ;
321321 }
322322
323323 let goal: Goal < I , G > =
@@ -828,6 +828,60 @@ where
828828 }
829829 }
830830
831+ fn try_assemble_bounds_via_registered_opaque < G : GoalKind < D > > (
832+ & mut self ,
833+ goal : Goal < I , G > ,
834+ self_ty : I :: Ty ,
835+ ) -> Vec < Candidate < I > > {
836+ //println!("for goal {goal:#?} and {self_ty:?}, we found an alias: {:#?}", self.find_sup_as_registered_opaque(self_ty));
837+
838+ let Some ( alias_ty) = self . find_sup_as_registered_opaque ( self_ty) else {
839+ return self . forced_ambiguity ( MaybeCause :: Ambiguity ) . into_iter ( ) . collect ( ) ;
840+ } ;
841+
842+ let mut candidates = vec ! [ ] ;
843+ for item_bound in
844+ self . cx ( ) . item_self_bounds ( alias_ty. def_id ) . iter_instantiated ( self . cx ( ) , alias_ty. args )
845+ {
846+ // TODO: comment
847+ let assumption =
848+ item_bound. fold_with ( & mut ReplaceOpaque { cx : self . cx ( ) , alias_ty, self_ty } ) ;
849+ candidates. extend ( G :: probe_and_match_goal_against_assumption (
850+ self ,
851+ CandidateSource :: AliasBound ,
852+ goal,
853+ assumption,
854+ |ecx| ecx. evaluate_added_goals_and_make_canonical_response ( Certainty :: AMBIGUOUS ) ,
855+ ) ) ;
856+ }
857+
858+ struct ReplaceOpaque < I : Interner > {
859+ cx : I ,
860+ alias_ty : ty:: AliasTy < I > ,
861+ self_ty : I :: Ty ,
862+ }
863+ impl < I : Interner > TypeFolder < I > for ReplaceOpaque < I > {
864+ fn cx ( & self ) -> I {
865+ self . cx
866+ }
867+ fn fold_ty ( & mut self , ty : I :: Ty ) -> I :: Ty {
868+ if let ty:: Alias ( ty:: Opaque , alias_ty) = ty. kind ( ) {
869+ if alias_ty == self . alias_ty {
870+ return self . self_ty ;
871+ }
872+ }
873+ ty. super_fold_with ( self )
874+ }
875+ }
876+
877+ // TODO:
878+ if candidates. is_empty ( ) {
879+ candidates. extend ( self . forced_ambiguity ( MaybeCause :: Ambiguity ) ) ;
880+ }
881+
882+ candidates
883+ }
884+
831885 /// Assemble and merge candidates for goals which are related to an underlying trait
832886 /// goal. Right now, this is normalizes-to and host effect goals.
833887 ///
0 commit comments