@@ -1803,25 +1803,19 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
18031803
18041804 // We prefer `Sized` candidates over everything.
18051805 let mut sized_candidates =
1806- candidates. iter ( ) . filter ( |c| matches ! ( c. candidate, SizedCandidate ) ) ;
1807- if let Some ( _sized_candidate ) = sized_candidates. next ( ) {
1806+ candidates. iter ( ) . filter ( |c| matches ! ( c. candidate, SizedCandidate { has_nested : _ } ) ) ;
1807+ if let Some ( sized_candidate ) = sized_candidates. next ( ) {
18081808 // There should only ever be a single sized candidate
18091809 // as they would otherwise overlap.
18101810 debug_assert_eq ! ( sized_candidates. next( ) , None ) ;
1811- return Some ( SizedCandidate ) ;
1812- }
1813-
1814- // We prefer trivial builtin candidates, i.e. builtin impls without any nested
1815- // requirements, over all others. This is a fix for #53123 and prevents winnowing
1816- // from accidentally extending the lifetime of a variable.
1817- let mut trivial_builtin = candidates
1818- . iter ( )
1819- . filter ( |c| matches ! ( c. candidate, BuiltinCandidate { has_nested: false } ) ) ;
1820- if let Some ( _trivial) = trivial_builtin. next ( ) {
1821- // There should only ever be a single trivial builtin candidate
1822- // as they would otherwise overlap.
1823- debug_assert_eq ! ( trivial_builtin. next( ) , None ) ;
1824- return Some ( BuiltinCandidate { has_nested : false } ) ;
1811+ // Only prefer the built-in `Sized` candidate if its nested goals are certain.
1812+ // Otherwise, we may encounter failure later on if inference causes this candidate
1813+ // to not hold, but a where clause would've applied instead.
1814+ if sized_candidate. evaluation . must_apply_modulo_regions ( ) {
1815+ return Some ( sized_candidate. candidate . clone ( ) ) ;
1816+ } else {
1817+ return None ;
1818+ }
18251819 }
18261820
18271821 // Before we consider where-bounds, we have to deduplicate them here and also
@@ -1950,7 +1944,7 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
19501944 // Don't use impl candidates which overlap with other candidates.
19511945 // This should pretty much only ever happen with malformed impls.
19521946 if candidates. iter ( ) . all ( |c| match c. candidate {
1953- SizedCandidate
1947+ SizedCandidate { has_nested : _ }
19541948 | BuiltinCandidate { has_nested : _ }
19551949 | TransmutabilityCandidate
19561950 | AutoImplCandidate
0 commit comments