@@ -10,8 +10,9 @@ use rustc_type_ir::solve::{
1010 RerunReason , RerunResultExt , SizedTraitKind ,
1111} ;
1212use rustc_type_ir:: {
13- self as ty, FieldInfo , Interner , MayBeErased , Movability , PredicatePolarity , TraitPredicate ,
14- TraitRef , TypeVisitableExt as _, TypingMode , Unnormalized , Upcast as _, elaborate,
13+ self as ty, ExistentialPredicate , FieldInfo , Interner , MayBeErased , Movability ,
14+ PredicatePolarity , TraitPredicate , TraitRef , TypeVisitableExt as _, TypingMode , Unnormalized ,
15+ Upcast as _, elaborate,
1516} ;
1617use tracing:: { debug, instrument, trace, warn} ;
1718
@@ -874,6 +875,49 @@ where
874875 }
875876 }
876877
878+ fn consider_builtin_try_as_dyn_candidate (
879+ ecx : & mut EvalCtxt < ' _ , D > ,
880+ goal : Goal < I , Self > ,
881+ ) -> Result < Candidate < I > , NoSolutionOrRerunNonErased > {
882+ if goal. predicate . polarity != ty:: PredicatePolarity :: Positive {
883+ return Err ( NoSolution . into ( ) ) ;
884+ }
885+ let cx = ecx. cx ( ) ;
886+
887+ ecx. probe_builtin_trait_candidate ( BuiltinImplSource :: Misc ) . enter ( |ecx| {
888+ let self_ty = goal. predicate . self_ty ( ) ;
889+ let ty_lifetime = goal. predicate . trait_ref . args . region_at ( 1 ) ;
890+ match self_ty. kind ( ) {
891+ ty:: Dynamic ( bounds, lifetime) => {
892+ for bound in bounds. iter ( ) {
893+ match bound. skip_binder ( ) {
894+ ExistentialPredicate :: Trait ( _) => { }
895+ // FIXME(try_as_dyn): check what kind of projections we can allow
896+ ExistentialPredicate :: Projection ( _) => return Err ( NoSolution . into ( ) ) ,
897+ // Auto traits do not affect lifetimes outside of specialization,
898+ // which is disabled in reflection.
899+ ExistentialPredicate :: AutoTrait ( _) => { }
900+ }
901+ }
902+ ecx. add_goal (
903+ GoalSource :: Misc ,
904+ goal. with ( cx, ty:: OutlivesPredicate ( ty_lifetime, lifetime) ) ,
905+ ) ;
906+ ecx. evaluate_added_goals_and_make_canonical_response ( Certainty :: Yes )
907+ }
908+
909+ ty:: Bound ( ..)
910+ | ty:: Infer (
911+ ty:: TyVar ( _) | ty:: FreshTy ( _) | ty:: FreshIntTy ( _) | ty:: FreshFloatTy ( _) ,
912+ ) => {
913+ panic ! ( "unexpected type `{self_ty:?}`" )
914+ }
915+
916+ _ => Err ( NoSolution . into ( ) ) ,
917+ }
918+ } )
919+ }
920+
877921 fn consider_builtin_field_candidate (
878922 ecx : & mut EvalCtxt < ' _ , D > ,
879923 goal : Goal < I , Self > ,
0 commit comments