@@ -355,6 +355,11 @@ where
355355 goal : Goal < I , Self > ,
356356 ) -> Result < Candidate < I > , NoSolution > ;
357357
358+ fn consider_builtin_try_as_dyn_candidate (
359+ ecx : & mut EvalCtxt < ' _ , D > ,
360+ goal : Goal < I , Self > ,
361+ ) -> Result < Candidate < I > , NoSolution > ;
362+
358363 /// Consider (possibly several) candidates to upcast or unsize a type to another
359364 /// type, excluding the coercion of a sized type into a `dyn Trait`.
360365 ///
@@ -554,6 +559,19 @@ where
554559 let cx = self . cx ( ) ;
555560 let trait_def_id = goal. predicate . trait_def_id ( cx) ;
556561
562+ // Builtin impls regularly are not `is_fully_generic_for_reflection`, so instead
563+ // of trying to handle these manually, we just reject all builtin impls in reflection
564+ // mode. We can probably lift this restriction for specific cases, but this is safer.
565+ // See `try_as_dyn_builtin_impl` for how just allowing all builtin impls is unsound.
566+ match self . typing_mode ( ) {
567+ TypingMode :: Reflection => return ,
568+ TypingMode :: Coherence
569+ | TypingMode :: Analysis { .. }
570+ | TypingMode :: Borrowck { .. }
571+ | TypingMode :: PostBorrowckAnalysis { .. }
572+ | TypingMode :: PostAnalysis => { }
573+ }
574+
557575 // N.B. When assembling built-in candidates for lang items that are also
558576 // `auto` traits, then the auto trait candidate that is assembled in
559577 // `consider_auto_trait_candidate` MUST be disqualified to remain sound.
@@ -646,6 +664,9 @@ where
646664 Some ( SolverTraitLangItem :: BikeshedGuaranteedNoDrop ) => {
647665 G :: consider_builtin_bikeshed_guaranteed_no_drop_candidate ( self , goal)
648666 }
667+ Some ( SolverTraitLangItem :: TryAsDyn ) => {
668+ G :: consider_builtin_try_as_dyn_candidate ( self , goal)
669+ }
649670 Some ( SolverTraitLangItem :: Field ) => G :: consider_builtin_field_candidate ( self , goal) ,
650671 _ => Err ( NoSolution ) ,
651672 }
@@ -832,6 +853,19 @@ where
832853 return ;
833854 }
834855
856+ // Builtin impls regularly are not `is_fully_generic_for_reflection`, so instead
857+ // of trying to handle these manually, we just reject all builtin impls in reflection
858+ // mode. We can probably lift this restriction for specific cases, but this is safer.
859+ // See `try_as_dyn_builtin_impl` for how just allowing all builtin impls is unsound.
860+ match self . typing_mode ( ) {
861+ TypingMode :: Reflection => return ,
862+ TypingMode :: Coherence
863+ | TypingMode :: Analysis { .. }
864+ | TypingMode :: Borrowck { .. }
865+ | TypingMode :: PostBorrowckAnalysis { .. }
866+ | TypingMode :: PostAnalysis => { }
867+ }
868+
835869 let self_ty = goal. predicate . self_ty ( ) ;
836870 let bounds = match self_ty. kind ( ) {
837871 ty:: Bool
0 commit comments