@@ -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,68 +1874,8 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
18681874 }
18691875 }
18701876
1871- // Drop otherwise equivalent non-const fn pointer candidates
1872- ( FnPointerCandidate { .. } , FnPointerCandidate { fn_host_effect } ) => {
1873- DropVictim :: drop_if ( * fn_host_effect == self . tcx ( ) . consts . true_ )
1874- }
1875-
1876- (
1877- ParamCandidate ( ref other_cand) ,
1878- ImplCandidate ( ..)
1879- | AutoImplCandidate
1880- | ClosureCandidate { .. }
1881- | AsyncClosureCandidate
1882- | AsyncFnKindHelperCandidate
1883- | CoroutineCandidate
1884- | FutureCandidate
1885- | IteratorCandidate
1886- | AsyncIteratorCandidate
1887- | FnPointerCandidate { .. }
1888- | BuiltinObjectCandidate
1889- | BuiltinUnsizeCandidate
1890- | TraitUpcastingUnsizeCandidate ( _)
1891- | BuiltinCandidate { .. }
1892- | TraitAliasCandidate
1893- | ObjectCandidate ( _)
1894- | ProjectionCandidate ( _) ,
1895- ) => {
1896- // We have a where clause so don't go around looking
1897- // for impls. Arbitrarily give param candidates priority
1898- // over projection and object candidates.
1899- //
1900- // Global bounds from the where clause should be ignored
1901- // here (see issue #50825).
1902- DropVictim :: drop_if ( !is_global ( other_cand) )
1903- }
1904- ( ObjectCandidate ( _) | ProjectionCandidate ( _) , ParamCandidate ( ref victim_cand) ) => {
1905- // Prefer these to a global where-clause bound
1906- // (see issue #50825).
1907- if is_global ( victim_cand) { DropVictim :: Yes } else { DropVictim :: No }
1908- }
1909- (
1910- ImplCandidate ( _)
1911- | AutoImplCandidate
1912- | ClosureCandidate { .. }
1913- | AsyncClosureCandidate
1914- | AsyncFnKindHelperCandidate
1915- | CoroutineCandidate
1916- | FutureCandidate
1917- | IteratorCandidate
1918- | AsyncIteratorCandidate
1919- | FnPointerCandidate { .. }
1920- | BuiltinObjectCandidate
1921- | BuiltinUnsizeCandidate
1922- | TraitUpcastingUnsizeCandidate ( _)
1923- | BuiltinCandidate { has_nested : true }
1924- | TraitAliasCandidate ,
1925- ParamCandidate ( ref victim_cand) ,
1926- ) => {
1927- // Prefer these to a global where-clause bound
1928- // (see issue #50825).
1929- DropVictim :: drop_if (
1930- is_global ( victim_cand) && other. evaluation . must_apply_modulo_regions ( ) ,
1931- )
1932- }
1877+ ( ParamCandidate { is_global : false , .. } , _) => DropVictim :: Yes ,
1878+ ( _, ParamCandidate { is_global : false , .. } ) => DropVictim :: No ,
19331879
19341880 ( ProjectionCandidate ( i) , ProjectionCandidate ( j) )
19351881 | ( ObjectCandidate ( i) , ObjectCandidate ( j) ) => {
@@ -1942,44 +1888,18 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
19421888 bug ! ( "Have both object and projection candidate" )
19431889 }
19441890
1945- // Arbitrarily give projection and object candidates priority.
1946- (
1947- ObjectCandidate ( _) | ProjectionCandidate ( _) ,
1948- ImplCandidate ( ..)
1949- | AutoImplCandidate
1950- | ClosureCandidate { .. }
1951- | AsyncClosureCandidate
1952- | AsyncFnKindHelperCandidate
1953- | CoroutineCandidate
1954- | FutureCandidate
1955- | IteratorCandidate
1956- | AsyncIteratorCandidate
1957- | FnPointerCandidate { .. }
1958- | BuiltinObjectCandidate
1959- | BuiltinUnsizeCandidate
1960- | TraitUpcastingUnsizeCandidate ( _)
1961- | BuiltinCandidate { .. }
1962- | TraitAliasCandidate ,
1963- ) => DropVictim :: Yes ,
1891+ // Arbitrarily give projection candidates priority.
1892+ ( ProjectionCandidate ( _) , _) => DropVictim :: Yes ,
1893+ ( _, ProjectionCandidate ( _) ) => DropVictim :: No ,
19641894
1965- (
1966- ImplCandidate ( ..)
1967- | AutoImplCandidate
1968- | ClosureCandidate { .. }
1969- | AsyncClosureCandidate
1970- | AsyncFnKindHelperCandidate
1971- | CoroutineCandidate
1972- | FutureCandidate
1973- | IteratorCandidate
1974- | AsyncIteratorCandidate
1975- | FnPointerCandidate { .. }
1976- | BuiltinObjectCandidate
1977- | BuiltinUnsizeCandidate
1978- | TraitUpcastingUnsizeCandidate ( _)
1979- | BuiltinCandidate { .. }
1980- | TraitAliasCandidate ,
1981- ObjectCandidate ( _) | ProjectionCandidate ( _) ,
1982- ) => 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 ,
19831903
19841904 ( & ImplCandidate ( other_def) , & ImplCandidate ( victim_def) ) => {
19851905 // See if we can toss out `victim` based on specialization.
@@ -2059,49 +1979,25 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
20591979 }
20601980 }
20611981
2062- ( AutoImplCandidate , ImplCandidate ( _) ) | ( ImplCandidate ( _) , AutoImplCandidate ) => {
2063- DropVictim :: No
2064- }
2065-
2066- ( AutoImplCandidate , _) | ( _, AutoImplCandidate ) => {
2067- bug ! (
2068- "default implementations shouldn't be recorded \
2069- when there are other global candidates: {:?} {:?}",
2070- other,
2071- victim
2072- ) ;
2073- }
2074-
2075- // Everything else is ambiguous
1982+ // Treat all non-trivial builtin impls and user-defined impls the same way.
20761983 (
20771984 ImplCandidate ( _)
2078- | ClosureCandidate { .. }
2079- | AsyncClosureCandidate
2080- | AsyncFnKindHelperCandidate
2081- | CoroutineCandidate
2082- | FutureCandidate
2083- | IteratorCandidate
2084- | AsyncIteratorCandidate
2085- | FnPointerCandidate { .. }
2086- | BuiltinObjectCandidate
2087- | BuiltinUnsizeCandidate
2088- | TraitUpcastingUnsizeCandidate ( _)
1985+ | AutoImplCandidate
20891986 | BuiltinCandidate { has_nested : true }
2090- | TraitAliasCandidate ,
2091- ImplCandidate ( _)
2092- | ClosureCandidate { .. }
20931987 | AsyncClosureCandidate
20941988 | AsyncFnKindHelperCandidate
20951989 | CoroutineCandidate
20961990 | FutureCandidate
20971991 | IteratorCandidate
20981992 | AsyncIteratorCandidate
20991993 | FnPointerCandidate { .. }
2100- | BuiltinObjectCandidate
1994+ | ClosureCandidate { .. }
1995+ | TraitAliasCandidate
21011996 | BuiltinUnsizeCandidate
21021997 | TraitUpcastingUnsizeCandidate ( _)
2103- | BuiltinCandidate { has_nested : true }
2104- | TraitAliasCandidate ,
1998+ | TransmutabilityCandidate
1999+ | BuiltinObjectCandidate ,
2000+ _,
21052001 ) => DropVictim :: No ,
21062002 }
21072003 }
0 commit comments