@@ -1577,7 +1577,9 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
1577
1577
return false ;
1578
1578
}
1579
1579
match result {
1580
- Ok ( Some ( SelectionCandidate :: ParamCandidate ( trait_ref) ) ) => !trait_ref. has_infer ( ) ,
1580
+ Ok ( Some ( SelectionCandidate :: ParamCandidate { predicate, .. } ) ) => {
1581
+ !predicate. has_infer ( )
1582
+ }
1581
1583
_ => true ,
1582
1584
}
1583
1585
}
@@ -1829,31 +1831,35 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
1829
1831
return DropVictim :: Yes ;
1830
1832
}
1831
1833
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.
1844
1834
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:?}" ) ,
1849
1844
( BuiltinCandidate { has_nested : false } | ConstDestructCandidate ( _) , _) => {
1850
1845
DropVictim :: Yes
1851
1846
}
1852
1847
( _, BuiltinCandidate { has_nested : false } | ConstDestructCandidate ( _) ) => {
1853
1848
DropVictim :: No
1854
1849
}
1855
1850
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
+ ) => {
1857
1863
let same_except_bound_vars = other. skip_binder ( ) . trait_ref
1858
1864
== victim. skip_binder ( ) . trait_ref
1859
1865
&& other. skip_binder ( ) . polarity == victim. skip_binder ( ) . polarity
@@ -1870,68 +1876,8 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
1870
1876
}
1871
1877
}
1872
1878
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 ,
1935
1881
1936
1882
( ProjectionCandidate ( i) , ProjectionCandidate ( j) )
1937
1883
| ( ObjectCandidate ( i) , ObjectCandidate ( j) ) => {
@@ -1944,44 +1890,18 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
1944
1890
bug ! ( "Have both object and projection candidate" )
1945
1891
}
1946
1892
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 ,
1966
1896
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 ,
1985
1905
1986
1906
( & ImplCandidate ( other_def) , & ImplCandidate ( victim_def) ) => {
1987
1907
// See if we can toss out `victim` based on specialization.
@@ -2061,49 +1981,25 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
2061
1981
}
2062
1982
}
2063
1983
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.
2078
1985
(
2079
1986
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
2091
1988
| BuiltinCandidate { has_nested : true }
2092
- | TraitAliasCandidate ,
2093
- ImplCandidate ( _)
2094
- | ClosureCandidate { .. }
2095
1989
| AsyncClosureCandidate
2096
1990
| AsyncFnKindHelperCandidate
2097
1991
| CoroutineCandidate
2098
1992
| FutureCandidate
2099
1993
| IteratorCandidate
2100
1994
| AsyncIteratorCandidate
2101
1995
| FnPointerCandidate { .. }
2102
- | BuiltinObjectCandidate
1996
+ | ClosureCandidate { .. }
1997
+ | TraitAliasCandidate
2103
1998
| BuiltinUnsizeCandidate
2104
1999
| TraitUpcastingUnsizeCandidate ( _)
2105
- | BuiltinCandidate { has_nested : true }
2106
- | TraitAliasCandidate ,
2000
+ | TransmutabilityCandidate
2001
+ | BuiltinObjectCandidate ,
2002
+ _,
2107
2003
) => DropVictim :: No ,
2108
2004
}
2109
2005
}
0 commit comments