@@ -1803,25 +1803,19 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
1803
1803
1804
1804
// We prefer `Sized` candidates over everything.
1805
1805
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 ( ) {
1808
1808
// There should only ever be a single sized candidate
1809
1809
// as they would otherwise overlap.
1810
1810
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
+ }
1825
1819
}
1826
1820
1827
1821
// Before we consider where-bounds, we have to deduplicate them here and also
@@ -1950,7 +1944,7 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
1950
1944
// Don't use impl candidates which overlap with other candidates.
1951
1945
// This should pretty much only ever happen with malformed impls.
1952
1946
if candidates. iter ( ) . all ( |c| match c. candidate {
1953
- SizedCandidate
1947
+ SizedCandidate { has_nested : _ }
1954
1948
| BuiltinCandidate { has_nested : _ }
1955
1949
| TransmutabilityCandidate
1956
1950
| AutoImplCandidate
0 commit comments