@@ -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,68 +1874,8 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
1868
1874
}
1869
1875
}
1870
1876
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 ,
1933
1879
1934
1880
( ProjectionCandidate ( i) , ProjectionCandidate ( j) )
1935
1881
| ( ObjectCandidate ( i) , ObjectCandidate ( j) ) => {
@@ -1942,44 +1888,18 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
1942
1888
bug ! ( "Have both object and projection candidate" )
1943
1889
}
1944
1890
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 ,
1964
1894
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 ,
1983
1903
1984
1904
( & ImplCandidate ( other_def) , & ImplCandidate ( victim_def) ) => {
1985
1905
// See if we can toss out `victim` based on specialization.
@@ -2059,49 +1979,25 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
2059
1979
}
2060
1980
}
2061
1981
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.
2076
1983
(
2077
1984
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
2089
1986
| BuiltinCandidate { has_nested : true }
2090
- | TraitAliasCandidate ,
2091
- ImplCandidate ( _)
2092
- | ClosureCandidate { .. }
2093
1987
| AsyncClosureCandidate
2094
1988
| AsyncFnKindHelperCandidate
2095
1989
| CoroutineCandidate
2096
1990
| FutureCandidate
2097
1991
| IteratorCandidate
2098
1992
| AsyncIteratorCandidate
2099
1993
| FnPointerCandidate { .. }
2100
- | BuiltinObjectCandidate
1994
+ | ClosureCandidate { .. }
1995
+ | TraitAliasCandidate
2101
1996
| BuiltinUnsizeCandidate
2102
1997
| TraitUpcastingUnsizeCandidate ( _)
2103
- | BuiltinCandidate { has_nested : true }
2104
- | TraitAliasCandidate ,
1998
+ | TransmutabilityCandidate
1999
+ | BuiltinObjectCandidate ,
2000
+ _,
2105
2001
) => DropVictim :: No ,
2106
2002
}
2107
2003
}
0 commit comments