@@ -1575,7 +1575,9 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
15751575 return false ;
15761576 }
15771577 match result {
1578- Ok ( Some ( SelectionCandidate :: ParamCandidate ( trait_ref) ) ) => !trait_ref. has_infer ( ) ,
1578+ Ok ( Some ( SelectionCandidate :: ParamCandidate { predicate, .. } ) ) => {
1579+ !predicate. has_infer ( )
1580+ }
15791581 _ => true ,
15801582 }
15811583 }
@@ -1827,31 +1829,35 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
18271829 return DropVictim :: Yes ;
18281830 }
18291831
1830- // Check if a bound would previously have been removed when normalizing
1831- // the param_env so that it can be given the lowest priority. See
1832- // #50825 for the motivation for this.
1833- let is_global =
1834- |cand : & ty:: PolyTraitPredicate < ' tcx > | cand. is_global ( ) && !cand. has_bound_vars ( ) ;
1835-
1836- // (*) Prefer `BuiltinCandidate { has_nested: false }`, `PointeeCandidate`,
1837- // `DiscriminantKindCandidate`, `ConstDestructCandidate`
1838- // to anything else.
1839- //
1840- // This is a fix for #53123 and prevents winnowing from accidentally extending the
1841- // lifetime of a variable.
18421832 match ( & other. candidate , & victim. candidate ) {
1843- // FIXME(@jswrenn): this should probably be more sophisticated
1844- ( TransmutabilityCandidate , _) | ( _, TransmutabilityCandidate ) => DropVictim :: No ,
1845-
1846- // (*)
1833+ // Prefer `BuiltinCandidate { has_nested: false }`, `ConstDestructCandidate`
1834+ // to anything else.
1835+ //
1836+ // This is a fix for #53123 and prevents winnowing from accidentally extending the
1837+ // lifetime of a variable.
1838+ (
1839+ BuiltinCandidate { has_nested : false } | ConstDestructCandidate ( _) ,
1840+ BuiltinCandidate { has_nested : false } | ConstDestructCandidate ( _) ,
1841+ ) => bug ! ( "two trivial builtin candidates: {other:?} {victim:?}" ) ,
18471842 ( BuiltinCandidate { has_nested : false } | ConstDestructCandidate ( _) , _) => {
18481843 DropVictim :: Yes
18491844 }
18501845 ( _, BuiltinCandidate { has_nested : false } | ConstDestructCandidate ( _) ) => {
18511846 DropVictim :: No
18521847 }
18531848
1854- ( ParamCandidate ( other) , ParamCandidate ( victim) ) => {
1849+ // Global bounds from the where clause should be ignored
1850+ // here (see issue #50825).
1851+ ( ParamCandidate { is_global : true , .. } , ParamCandidate { is_global : true , .. } ) => {
1852+ DropVictim :: No
1853+ }
1854+ ( _, ParamCandidate { is_global : true , .. } ) => DropVictim :: Yes ,
1855+ ( ParamCandidate { is_global : true , .. } , _) => DropVictim :: No ,
1856+
1857+ (
1858+ ParamCandidate { is_global : false , predicate : other } ,
1859+ ParamCandidate { is_global : false , predicate : victim } ,
1860+ ) => {
18551861 let same_except_bound_vars = other. skip_binder ( ) . trait_ref
18561862 == victim. skip_binder ( ) . trait_ref
18571863 && other. skip_binder ( ) . polarity == victim. skip_binder ( ) . polarity
@@ -1868,63 +1874,8 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
18681874 }
18691875 }
18701876
1871- (
1872- ParamCandidate ( ref other_cand) ,
1873- ImplCandidate ( ..)
1874- | AutoImplCandidate
1875- | ClosureCandidate { .. }
1876- | AsyncClosureCandidate
1877- | AsyncFnKindHelperCandidate
1878- | CoroutineCandidate
1879- | FutureCandidate
1880- | IteratorCandidate
1881- | AsyncIteratorCandidate
1882- | FnPointerCandidate { .. }
1883- | BuiltinObjectCandidate
1884- | BuiltinUnsizeCandidate
1885- | TraitUpcastingUnsizeCandidate ( _)
1886- | BuiltinCandidate { .. }
1887- | TraitAliasCandidate
1888- | ObjectCandidate ( _)
1889- | ProjectionCandidate ( _) ,
1890- ) => {
1891- // We have a where clause so don't go around looking
1892- // for impls. Arbitrarily give param candidates priority
1893- // over projection and object candidates.
1894- //
1895- // Global bounds from the where clause should be ignored
1896- // here (see issue #50825).
1897- DropVictim :: drop_if ( !is_global ( other_cand) )
1898- }
1899- ( ObjectCandidate ( _) | ProjectionCandidate ( _) , ParamCandidate ( ref victim_cand) ) => {
1900- // Prefer these to a global where-clause bound
1901- // (see issue #50825).
1902- if is_global ( victim_cand) { DropVictim :: Yes } else { DropVictim :: No }
1903- }
1904- (
1905- ImplCandidate ( _)
1906- | AutoImplCandidate
1907- | ClosureCandidate { .. }
1908- | AsyncClosureCandidate
1909- | AsyncFnKindHelperCandidate
1910- | CoroutineCandidate
1911- | FutureCandidate
1912- | IteratorCandidate
1913- | AsyncIteratorCandidate
1914- | FnPointerCandidate { .. }
1915- | BuiltinObjectCandidate
1916- | BuiltinUnsizeCandidate
1917- | TraitUpcastingUnsizeCandidate ( _)
1918- | BuiltinCandidate { has_nested : true }
1919- | TraitAliasCandidate ,
1920- ParamCandidate ( ref victim_cand) ,
1921- ) => {
1922- // Prefer these to a global where-clause bound
1923- // (see issue #50825).
1924- DropVictim :: drop_if (
1925- is_global ( victim_cand) && other. evaluation . must_apply_modulo_regions ( ) ,
1926- )
1927- }
1877+ ( ParamCandidate { is_global : false , .. } , _) => DropVictim :: Yes ,
1878+ ( _, ParamCandidate { is_global : false , .. } ) => DropVictim :: No ,
19281879
19291880 ( ProjectionCandidate ( i) , ProjectionCandidate ( j) )
19301881 | ( ObjectCandidate ( i) , ObjectCandidate ( j) ) => {
@@ -1937,44 +1888,18 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
19371888 bug ! ( "Have both object and projection candidate" )
19381889 }
19391890
1940- // Arbitrarily give projection and object candidates priority.
1941- (
1942- ObjectCandidate ( _) | ProjectionCandidate ( _) ,
1943- ImplCandidate ( ..)
1944- | AutoImplCandidate
1945- | ClosureCandidate { .. }
1946- | AsyncClosureCandidate
1947- | AsyncFnKindHelperCandidate
1948- | CoroutineCandidate
1949- | FutureCandidate
1950- | IteratorCandidate
1951- | AsyncIteratorCandidate
1952- | FnPointerCandidate { .. }
1953- | BuiltinObjectCandidate
1954- | BuiltinUnsizeCandidate
1955- | TraitUpcastingUnsizeCandidate ( _)
1956- | BuiltinCandidate { .. }
1957- | TraitAliasCandidate ,
1958- ) => DropVictim :: Yes ,
1891+ // Arbitrarily give projection candidates priority.
1892+ ( ProjectionCandidate ( _) , _) => DropVictim :: Yes ,
1893+ ( _, ProjectionCandidate ( _) ) => DropVictim :: No ,
19591894
1960- (
1961- ImplCandidate ( ..)
1962- | AutoImplCandidate
1963- | ClosureCandidate { .. }
1964- | AsyncClosureCandidate
1965- | AsyncFnKindHelperCandidate
1966- | CoroutineCandidate
1967- | FutureCandidate
1968- | IteratorCandidate
1969- | AsyncIteratorCandidate
1970- | FnPointerCandidate { .. }
1971- | BuiltinObjectCandidate
1972- | BuiltinUnsizeCandidate
1973- | TraitUpcastingUnsizeCandidate ( _)
1974- | BuiltinCandidate { .. }
1975- | TraitAliasCandidate ,
1976- ObjectCandidate ( _) | ProjectionCandidate ( _) ,
1977- ) => DropVictim :: No ,
1895+ // Need to prioritize builtin trait object impls as
1896+ // `<dyn Any as Any>::type_id` should use the vtable method
1897+ // and not the method provided by the user-defined impl
1898+ // `impl<T: ?Sized> Any for T { .. }`.
1899+ //
1900+ // cc #57893
1901+ ( ObjectCandidate ( _) , _) => DropVictim :: Yes ,
1902+ ( _, ObjectCandidate ( _) ) => DropVictim :: No ,
19781903
19791904 ( & ImplCandidate ( other_def) , & ImplCandidate ( victim_def) ) => {
19801905 // See if we can toss out `victim` based on specialization.
@@ -2054,49 +1979,25 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
20541979 }
20551980 }
20561981
2057- ( AutoImplCandidate , ImplCandidate ( _) ) | ( ImplCandidate ( _) , AutoImplCandidate ) => {
2058- DropVictim :: No
2059- }
2060-
2061- ( AutoImplCandidate , _) | ( _, AutoImplCandidate ) => {
2062- bug ! (
2063- "default implementations shouldn't be recorded \
2064- when there are other global candidates: {:?} {:?}",
2065- other,
2066- victim
2067- ) ;
2068- }
2069-
2070- // Everything else is ambiguous
1982+ // Treat all non-trivial builtin impls and user-defined impls the same way.
20711983 (
20721984 ImplCandidate ( _)
2073- | ClosureCandidate { .. }
2074- | AsyncClosureCandidate
2075- | AsyncFnKindHelperCandidate
2076- | CoroutineCandidate
2077- | FutureCandidate
2078- | IteratorCandidate
2079- | AsyncIteratorCandidate
2080- | FnPointerCandidate { .. }
2081- | BuiltinObjectCandidate
2082- | BuiltinUnsizeCandidate
2083- | TraitUpcastingUnsizeCandidate ( _)
1985+ | AutoImplCandidate
20841986 | BuiltinCandidate { has_nested : true }
2085- | TraitAliasCandidate ,
2086- ImplCandidate ( _)
2087- | ClosureCandidate { .. }
20881987 | AsyncClosureCandidate
20891988 | AsyncFnKindHelperCandidate
20901989 | CoroutineCandidate
20911990 | FutureCandidate
20921991 | IteratorCandidate
20931992 | AsyncIteratorCandidate
20941993 | FnPointerCandidate { .. }
2095- | BuiltinObjectCandidate
1994+ | ClosureCandidate { .. }
1995+ | TraitAliasCandidate
20961996 | BuiltinUnsizeCandidate
20971997 | TraitUpcastingUnsizeCandidate ( _)
2098- | BuiltinCandidate { has_nested : true }
2099- | TraitAliasCandidate ,
1998+ | TransmutabilityCandidate
1999+ | BuiltinObjectCandidate ,
2000+ _,
21002001 ) => DropVictim :: No ,
21012002 }
21022003 }
0 commit comments