@@ -1575,7 +1575,9 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
1575
1575
return false ;
1576
1576
}
1577
1577
match result {
1578
- Ok ( Some ( SelectionCandidate :: ParamCandidate ( trait_ref) ) ) => !trait_ref. has_infer ( ) ,
1578
+ Ok ( Some ( SelectionCandidate :: ParamCandidate { predicate, .. } ) ) => {
1579
+ !predicate. has_infer ( )
1580
+ }
1579
1581
_ => true ,
1580
1582
}
1581
1583
}
@@ -1827,31 +1829,35 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
1827
1829
return DropVictim :: Yes ;
1828
1830
}
1829
1831
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.
1842
1832
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:?}" ) ,
1847
1842
( BuiltinCandidate { has_nested : false } | ConstDestructCandidate ( _) , _) => {
1848
1843
DropVictim :: Yes
1849
1844
}
1850
1845
( _, BuiltinCandidate { has_nested : false } | ConstDestructCandidate ( _) ) => {
1851
1846
DropVictim :: No
1852
1847
}
1853
1848
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
+ ) => {
1855
1861
let same_except_bound_vars = other. skip_binder ( ) . trait_ref
1856
1862
== victim. skip_binder ( ) . trait_ref
1857
1863
&& other. skip_binder ( ) . polarity == victim. skip_binder ( ) . polarity
@@ -1868,63 +1874,8 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
1868
1874
}
1869
1875
}
1870
1876
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 ,
1928
1879
1929
1880
( ProjectionCandidate ( i) , ProjectionCandidate ( j) )
1930
1881
| ( ObjectCandidate ( i) , ObjectCandidate ( j) ) => {
@@ -1937,44 +1888,18 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
1937
1888
bug ! ( "Have both object and projection candidate" )
1938
1889
}
1939
1890
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 ,
1959
1894
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 ,
1978
1903
1979
1904
( & ImplCandidate ( other_def) , & ImplCandidate ( victim_def) ) => {
1980
1905
// See if we can toss out `victim` based on specialization.
@@ -2054,49 +1979,25 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
2054
1979
}
2055
1980
}
2056
1981
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.
2071
1983
(
2072
1984
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
2084
1986
| BuiltinCandidate { has_nested : true }
2085
- | TraitAliasCandidate ,
2086
- ImplCandidate ( _)
2087
- | ClosureCandidate { .. }
2088
1987
| AsyncClosureCandidate
2089
1988
| AsyncFnKindHelperCandidate
2090
1989
| CoroutineCandidate
2091
1990
| FutureCandidate
2092
1991
| IteratorCandidate
2093
1992
| AsyncIteratorCandidate
2094
1993
| FnPointerCandidate { .. }
2095
- | BuiltinObjectCandidate
1994
+ | ClosureCandidate { .. }
1995
+ | TraitAliasCandidate
2096
1996
| BuiltinUnsizeCandidate
2097
1997
| TraitUpcastingUnsizeCandidate ( _)
2098
- | BuiltinCandidate { has_nested : true }
2099
- | TraitAliasCandidate ,
1998
+ | TransmutabilityCandidate
1999
+ | BuiltinObjectCandidate ,
2000
+ _,
2100
2001
) => DropVictim :: No ,
2101
2002
}
2102
2003
}
0 commit comments