@@ -1577,7 +1577,9 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
15771577 return false ;
15781578 }
15791579 match result {
1580- Ok ( Some ( SelectionCandidate :: ParamCandidate ( trait_ref) ) ) => !trait_ref. has_infer ( ) ,
1580+ Ok ( Some ( SelectionCandidate :: ParamCandidate { predicate, .. } ) ) => {
1581+ !predicate. has_infer ( )
1582+ }
15811583 _ => true ,
15821584 }
15831585 }
@@ -1829,31 +1831,35 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
18291831 return DropVictim :: Yes ;
18301832 }
18311833
1832- // Check if a bound would previously have been removed when normalizing
1833- // the param_env so that it can be given the lowest priority. See
1834- // #50825 for the motivation for this.
1835- let is_global =
1836- |cand : & ty:: PolyTraitPredicate < ' tcx > | cand. is_global ( ) && !cand. has_bound_vars ( ) ;
1837-
1838- // (*) Prefer `BuiltinCandidate { has_nested: false }`, `PointeeCandidate`,
1839- // `DiscriminantKindCandidate`, `ConstDestructCandidate`
1840- // to anything else.
1841- //
1842- // This is a fix for #53123 and prevents winnowing from accidentally extending the
1843- // lifetime of a variable.
18441834 match ( & other. candidate , & victim. candidate ) {
1845- // FIXME(@jswrenn): this should probably be more sophisticated
1846- ( TransmutabilityCandidate , _) | ( _, TransmutabilityCandidate ) => DropVictim :: No ,
1847-
1848- // (*)
1835+ // Prefer `BuiltinCandidate { has_nested: false }`, `ConstDestructCandidate`
1836+ // to anything else.
1837+ //
1838+ // This is a fix for #53123 and prevents winnowing from accidentally extending the
1839+ // lifetime of a variable.
1840+ (
1841+ BuiltinCandidate { has_nested : false } | ConstDestructCandidate ( _) ,
1842+ BuiltinCandidate { has_nested : false } | ConstDestructCandidate ( _) ,
1843+ ) => bug ! ( "two trivial builtin candidates: {other:?} {victim:?}" ) ,
18491844 ( BuiltinCandidate { has_nested : false } | ConstDestructCandidate ( _) , _) => {
18501845 DropVictim :: Yes
18511846 }
18521847 ( _, BuiltinCandidate { has_nested : false } | ConstDestructCandidate ( _) ) => {
18531848 DropVictim :: No
18541849 }
18551850
1856- ( ParamCandidate ( other) , ParamCandidate ( victim) ) => {
1851+ // Global bounds from the where clause should be ignored
1852+ // here (see issue #50825).
1853+ ( ParamCandidate { is_global : true , .. } , ParamCandidate { is_global : true , .. } ) => {
1854+ DropVictim :: No
1855+ }
1856+ ( _, ParamCandidate { is_global : true , .. } ) => DropVictim :: Yes ,
1857+ ( ParamCandidate { is_global : true , .. } , _) => DropVictim :: No ,
1858+
1859+ (
1860+ ParamCandidate { is_global : false , predicate : other } ,
1861+ ParamCandidate { is_global : false , predicate : victim } ,
1862+ ) => {
18571863 let same_except_bound_vars = other. skip_binder ( ) . trait_ref
18581864 == victim. skip_binder ( ) . trait_ref
18591865 && other. skip_binder ( ) . polarity == victim. skip_binder ( ) . polarity
@@ -1870,68 +1876,8 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
18701876 }
18711877 }
18721878
1873- // Drop otherwise equivalent non-const fn pointer candidates
1874- ( FnPointerCandidate { .. } , FnPointerCandidate { fn_host_effect } ) => {
1875- DropVictim :: drop_if ( * fn_host_effect == self . tcx ( ) . consts . true_ )
1876- }
1877-
1878- (
1879- ParamCandidate ( ref other_cand) ,
1880- ImplCandidate ( ..)
1881- | AutoImplCandidate
1882- | ClosureCandidate { .. }
1883- | AsyncClosureCandidate
1884- | AsyncFnKindHelperCandidate
1885- | CoroutineCandidate
1886- | FutureCandidate
1887- | IteratorCandidate
1888- | AsyncIteratorCandidate
1889- | FnPointerCandidate { .. }
1890- | BuiltinObjectCandidate
1891- | BuiltinUnsizeCandidate
1892- | TraitUpcastingUnsizeCandidate ( _)
1893- | BuiltinCandidate { .. }
1894- | TraitAliasCandidate
1895- | ObjectCandidate ( _)
1896- | ProjectionCandidate ( _) ,
1897- ) => {
1898- // We have a where clause so don't go around looking
1899- // for impls. Arbitrarily give param candidates priority
1900- // over projection and object candidates.
1901- //
1902- // Global bounds from the where clause should be ignored
1903- // here (see issue #50825).
1904- DropVictim :: drop_if ( !is_global ( other_cand) )
1905- }
1906- ( ObjectCandidate ( _) | ProjectionCandidate ( _) , ParamCandidate ( ref victim_cand) ) => {
1907- // Prefer these to a global where-clause bound
1908- // (see issue #50825).
1909- if is_global ( victim_cand) { DropVictim :: Yes } else { DropVictim :: No }
1910- }
1911- (
1912- ImplCandidate ( _)
1913- | AutoImplCandidate
1914- | ClosureCandidate { .. }
1915- | AsyncClosureCandidate
1916- | AsyncFnKindHelperCandidate
1917- | CoroutineCandidate
1918- | FutureCandidate
1919- | IteratorCandidate
1920- | AsyncIteratorCandidate
1921- | FnPointerCandidate { .. }
1922- | BuiltinObjectCandidate
1923- | BuiltinUnsizeCandidate
1924- | TraitUpcastingUnsizeCandidate ( _)
1925- | BuiltinCandidate { has_nested : true }
1926- | TraitAliasCandidate ,
1927- ParamCandidate ( ref victim_cand) ,
1928- ) => {
1929- // Prefer these to a global where-clause bound
1930- // (see issue #50825).
1931- DropVictim :: drop_if (
1932- is_global ( victim_cand) && other. evaluation . must_apply_modulo_regions ( ) ,
1933- )
1934- }
1879+ ( ParamCandidate { is_global : false , .. } , _) => DropVictim :: Yes ,
1880+ ( _, ParamCandidate { is_global : false , .. } ) => DropVictim :: No ,
19351881
19361882 ( ProjectionCandidate ( i) , ProjectionCandidate ( j) )
19371883 | ( ObjectCandidate ( i) , ObjectCandidate ( j) ) => {
@@ -1944,44 +1890,18 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
19441890 bug ! ( "Have both object and projection candidate" )
19451891 }
19461892
1947- // Arbitrarily give projection and object candidates priority.
1948- (
1949- ObjectCandidate ( _) | ProjectionCandidate ( _) ,
1950- ImplCandidate ( ..)
1951- | AutoImplCandidate
1952- | ClosureCandidate { .. }
1953- | AsyncClosureCandidate
1954- | AsyncFnKindHelperCandidate
1955- | CoroutineCandidate
1956- | FutureCandidate
1957- | IteratorCandidate
1958- | AsyncIteratorCandidate
1959- | FnPointerCandidate { .. }
1960- | BuiltinObjectCandidate
1961- | BuiltinUnsizeCandidate
1962- | TraitUpcastingUnsizeCandidate ( _)
1963- | BuiltinCandidate { .. }
1964- | TraitAliasCandidate ,
1965- ) => DropVictim :: Yes ,
1893+ // Arbitrarily give projection candidates priority.
1894+ ( ProjectionCandidate ( _) , _) => DropVictim :: Yes ,
1895+ ( _, ProjectionCandidate ( _) ) => DropVictim :: No ,
19661896
1967- (
1968- ImplCandidate ( ..)
1969- | AutoImplCandidate
1970- | ClosureCandidate { .. }
1971- | AsyncClosureCandidate
1972- | AsyncFnKindHelperCandidate
1973- | CoroutineCandidate
1974- | FutureCandidate
1975- | IteratorCandidate
1976- | AsyncIteratorCandidate
1977- | FnPointerCandidate { .. }
1978- | BuiltinObjectCandidate
1979- | BuiltinUnsizeCandidate
1980- | TraitUpcastingUnsizeCandidate ( _)
1981- | BuiltinCandidate { .. }
1982- | TraitAliasCandidate ,
1983- ObjectCandidate ( _) | ProjectionCandidate ( _) ,
1984- ) => DropVictim :: No ,
1897+ // Need to prioritize builtin trait object impls as
1898+ // `<dyn Any as Any>::type_id` should use the vtable method
1899+ // and not the method provided by the user-defined impl
1900+ // `impl<T: ?Sized> Any for T { .. }`.
1901+ //
1902+ // cc #57893
1903+ ( ObjectCandidate ( _) , _) => DropVictim :: Yes ,
1904+ ( _, ObjectCandidate ( _) ) => DropVictim :: No ,
19851905
19861906 ( & ImplCandidate ( other_def) , & ImplCandidate ( victim_def) ) => {
19871907 // See if we can toss out `victim` based on specialization.
@@ -2061,49 +1981,25 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
20611981 }
20621982 }
20631983
2064- ( AutoImplCandidate , ImplCandidate ( _) ) | ( ImplCandidate ( _) , AutoImplCandidate ) => {
2065- DropVictim :: No
2066- }
2067-
2068- ( AutoImplCandidate , _) | ( _, AutoImplCandidate ) => {
2069- bug ! (
2070- "default implementations shouldn't be recorded \
2071- when there are other global candidates: {:?} {:?}",
2072- other,
2073- victim
2074- ) ;
2075- }
2076-
2077- // Everything else is ambiguous
1984+ // Treat all non-trivial builtin impls and user-defined impls the same way.
20781985 (
20791986 ImplCandidate ( _)
2080- | ClosureCandidate { .. }
2081- | AsyncClosureCandidate
2082- | AsyncFnKindHelperCandidate
2083- | CoroutineCandidate
2084- | FutureCandidate
2085- | IteratorCandidate
2086- | AsyncIteratorCandidate
2087- | FnPointerCandidate { .. }
2088- | BuiltinObjectCandidate
2089- | BuiltinUnsizeCandidate
2090- | TraitUpcastingUnsizeCandidate ( _)
1987+ | AutoImplCandidate
20911988 | BuiltinCandidate { has_nested : true }
2092- | TraitAliasCandidate ,
2093- ImplCandidate ( _)
2094- | ClosureCandidate { .. }
20951989 | AsyncClosureCandidate
20961990 | AsyncFnKindHelperCandidate
20971991 | CoroutineCandidate
20981992 | FutureCandidate
20991993 | IteratorCandidate
21001994 | AsyncIteratorCandidate
21011995 | FnPointerCandidate { .. }
2102- | BuiltinObjectCandidate
1996+ | ClosureCandidate { .. }
1997+ | TraitAliasCandidate
21031998 | BuiltinUnsizeCandidate
21041999 | TraitUpcastingUnsizeCandidate ( _)
2105- | BuiltinCandidate { has_nested : true }
2106- | TraitAliasCandidate ,
2000+ | TransmutabilityCandidate
2001+ | BuiltinObjectCandidate ,
2002+ _,
21072003 ) => DropVictim :: No ,
21082004 }
21092005 }
0 commit comments