@@ -30,7 +30,6 @@ use rustc_trait_selection::traits::misc::{
30
30
ConstParamTyImplementationError , type_allowed_to_implement_const_param_ty,
31
31
} ;
32
32
use rustc_trait_selection:: traits:: outlives_bounds:: InferCtxtExt as _;
33
- use rustc_trait_selection:: traits:: query:: evaluate_obligation:: InferCtxtExt as _;
34
33
use rustc_trait_selection:: traits:: {
35
34
self , FulfillmentError , Obligation , ObligationCause , ObligationCauseCode , ObligationCtxt ,
36
35
WellFormedLoc ,
@@ -1630,13 +1629,6 @@ fn check_fn_or_method<'tcx>(
1630
1629
}
1631
1630
}
1632
1631
1633
- /// The `arbitrary_self_types_pointers` feature implies `arbitrary_self_types`.
1634
- #[ derive( Clone , Copy , PartialEq ) ]
1635
- enum ArbitrarySelfTypesLevel {
1636
- Basic , // just arbitrary_self_types
1637
- WithPointers , // both arbitrary_self_types and arbitrary_self_types_pointers
1638
- }
1639
-
1640
1632
#[ instrument( level = "debug" , skip( wfcx) ) ]
1641
1633
fn check_method_receiver < ' tcx > (
1642
1634
wfcx : & WfCheckingCtxt < ' _ , ' tcx > ,
@@ -1669,55 +1661,21 @@ fn check_method_receiver<'tcx>(
1669
1661
return Ok ( ( ) ) ;
1670
1662
}
1671
1663
1672
- let arbitrary_self_types_level = if tcx. features ( ) . arbitrary_self_types_pointers ( ) {
1673
- Some ( ArbitrarySelfTypesLevel :: WithPointers )
1674
- } else if tcx. features ( ) . arbitrary_self_types ( ) {
1675
- Some ( ArbitrarySelfTypesLevel :: Basic )
1676
- } else {
1677
- None
1678
- } ;
1679
1664
let generics = tcx. generics_of ( method. def_id ) ;
1680
1665
1681
- let receiver_validity =
1682
- receiver_is_valid ( wfcx, span, receiver_ty, self_ty, arbitrary_self_types_level, generics) ;
1666
+ let arbitrary_self_types_pointers_enabled = tcx. features ( ) . arbitrary_self_types_pointers ( ) ;
1667
+ let receiver_validity = receiver_is_valid (
1668
+ wfcx,
1669
+ span,
1670
+ receiver_ty,
1671
+ self_ty,
1672
+ arbitrary_self_types_pointers_enabled,
1673
+ generics,
1674
+ ) ;
1683
1675
if let Err ( receiver_validity_err) = receiver_validity {
1684
- return Err ( match arbitrary_self_types_level {
1685
- // Wherever possible, emit a message advising folks that the features
1686
- // `arbitrary_self_types` or `arbitrary_self_types_pointers` might
1687
- // have helped.
1688
- None if receiver_is_valid (
1689
- wfcx,
1690
- span,
1691
- receiver_ty,
1692
- self_ty,
1693
- Some ( ArbitrarySelfTypesLevel :: Basic ) ,
1694
- generics,
1695
- )
1696
- . is_ok ( ) =>
1697
- {
1698
- // Report error; would have worked with `arbitrary_self_types`.
1699
- feature_err (
1700
- & tcx. sess ,
1701
- sym:: arbitrary_self_types,
1702
- span,
1703
- format ! (
1704
- "`{receiver_ty}` cannot be used as the type of `self` without \
1705
- the `arbitrary_self_types` feature",
1706
- ) ,
1707
- )
1708
- . with_help ( fluent:: hir_analysis_invalid_receiver_ty_help)
1709
- . emit ( )
1710
- }
1711
- None | Some ( ArbitrarySelfTypesLevel :: Basic )
1712
- if receiver_is_valid (
1713
- wfcx,
1714
- span,
1715
- receiver_ty,
1716
- self_ty,
1717
- Some ( ArbitrarySelfTypesLevel :: WithPointers ) ,
1718
- generics,
1719
- )
1720
- . is_ok ( ) =>
1676
+ return Err (
1677
+ if !arbitrary_self_types_pointers_enabled
1678
+ && receiver_is_valid ( wfcx, span, receiver_ty, self_ty, true , generics) . is_ok ( )
1721
1679
{
1722
1680
// Report error; would have worked with `arbitrary_self_types_pointers`.
1723
1681
feature_err (
@@ -1726,15 +1684,13 @@ fn check_method_receiver<'tcx>(
1726
1684
span,
1727
1685
format ! (
1728
1686
"`{receiver_ty}` cannot be used as the type of `self` without \
1729
- the `arbitrary_self_types_pointers` feature",
1687
+ the `arbitrary_self_types_pointers` feature",
1730
1688
) ,
1731
1689
)
1732
1690
. with_help ( fluent:: hir_analysis_invalid_receiver_ty_help)
1733
1691
. emit ( )
1734
- }
1735
- _ =>
1736
- // Report error; would not have worked with `arbitrary_self_types[_pointers]`.
1737
- {
1692
+ } else {
1693
+ // Report error; would not have worked with `arbitrary_self_types[_pointers]`.
1738
1694
match receiver_validity_err {
1739
1695
ReceiverValidityError :: DoesNotDeref => {
1740
1696
let hint = match receiver_ty
@@ -1748,7 +1704,7 @@ fn check_method_receiver<'tcx>(
1748
1704
wfcx,
1749
1705
span,
1750
1706
receiver_ty,
1751
- arbitrary_self_types_level ,
1707
+ arbitrary_self_types_pointers_enabled ,
1752
1708
) ;
1753
1709
None
1754
1710
}
@@ -1760,8 +1716,8 @@ fn check_method_receiver<'tcx>(
1760
1716
tcx. dcx ( ) . emit_err ( errors:: InvalidGenericReceiverTy { span, receiver_ty } )
1761
1717
}
1762
1718
}
1763
- }
1764
- } ) ;
1719
+ } ,
1720
+ ) ;
1765
1721
}
1766
1722
Ok ( ( ) )
1767
1723
}
@@ -1805,11 +1761,10 @@ fn receiver_is_valid<'tcx>(
1805
1761
span : Span ,
1806
1762
receiver_ty : Ty < ' tcx > ,
1807
1763
self_ty : Ty < ' tcx > ,
1808
- arbitrary_self_types_enabled : Option < ArbitrarySelfTypesLevel > ,
1764
+ arbitrary_self_types_pointers_enabled : bool ,
1809
1765
method_generics : & ty:: Generics ,
1810
1766
) -> Result < ( ) , ReceiverValidityError > {
1811
1767
let infcx = wfcx. infcx ;
1812
- let tcx = wfcx. tcx ( ) ;
1813
1768
let cause =
1814
1769
ObligationCause :: new ( span, wfcx. body_def_id , traits:: ObligationCauseCode :: MethodReceiver ) ;
1815
1770
@@ -1824,17 +1779,11 @@ fn receiver_is_valid<'tcx>(
1824
1779
1825
1780
confirm_type_is_not_a_method_generic_param ( receiver_ty, method_generics) ?;
1826
1781
1827
- let mut autoderef = Autoderef :: new ( infcx, wfcx. param_env , wfcx. body_def_id , span, receiver_ty) ;
1828
-
1829
- // The `arbitrary_self_types` feature allows custom smart pointer
1830
- // types to be method receivers, as identified by following the Receiver<Target=T>
1831
- // chain.
1832
- if arbitrary_self_types_enabled. is_some ( ) {
1833
- autoderef = autoderef. use_receiver_trait ( ) ;
1834
- }
1782
+ let mut autoderef = Autoderef :: new ( infcx, wfcx. param_env , wfcx. body_def_id , span, receiver_ty)
1783
+ . use_receiver_trait ( ) ;
1835
1784
1836
1785
// The `arbitrary_self_types_pointers` feature allows raw pointer receivers like `self: *const Self`.
1837
- if arbitrary_self_types_enabled == Some ( ArbitrarySelfTypesLevel :: WithPointers ) {
1786
+ if arbitrary_self_types_pointers_enabled {
1838
1787
autoderef = autoderef. include_raw_pointers ( ) ;
1839
1788
}
1840
1789
@@ -1857,58 +1806,12 @@ fn receiver_is_valid<'tcx>(
1857
1806
wfcx. register_obligations ( autoderef. into_obligations ( ) ) ;
1858
1807
return Ok ( ( ) ) ;
1859
1808
}
1860
-
1861
- // Without `feature(arbitrary_self_types)`, we require that each step in the
1862
- // deref chain implement `LegacyReceiver`.
1863
- if arbitrary_self_types_enabled. is_none ( ) {
1864
- let legacy_receiver_trait_def_id =
1865
- tcx. require_lang_item ( LangItem :: LegacyReceiver , Some ( span) ) ;
1866
- if !legacy_receiver_is_implemented (
1867
- wfcx,
1868
- legacy_receiver_trait_def_id,
1869
- cause. clone ( ) ,
1870
- potential_self_ty,
1871
- ) {
1872
- // We cannot proceed.
1873
- break ;
1874
- }
1875
-
1876
- // Register the bound, in case it has any region side-effects.
1877
- wfcx. register_bound (
1878
- cause. clone ( ) ,
1879
- wfcx. param_env ,
1880
- potential_self_ty,
1881
- legacy_receiver_trait_def_id,
1882
- ) ;
1883
- }
1884
1809
}
1885
1810
1886
1811
debug ! ( "receiver_is_valid: type `{:?}` does not deref to `{:?}`" , receiver_ty, self_ty) ;
1887
1812
Err ( ReceiverValidityError :: DoesNotDeref )
1888
1813
}
1889
1814
1890
- fn legacy_receiver_is_implemented < ' tcx > (
1891
- wfcx : & WfCheckingCtxt < ' _ , ' tcx > ,
1892
- legacy_receiver_trait_def_id : DefId ,
1893
- cause : ObligationCause < ' tcx > ,
1894
- receiver_ty : Ty < ' tcx > ,
1895
- ) -> bool {
1896
- let tcx = wfcx. tcx ( ) ;
1897
- let trait_ref = ty:: TraitRef :: new ( tcx, legacy_receiver_trait_def_id, [ receiver_ty] ) ;
1898
-
1899
- let obligation = Obligation :: new ( tcx, cause, wfcx. param_env , trait_ref) ;
1900
-
1901
- if wfcx. infcx . predicate_must_hold_modulo_regions ( & obligation) {
1902
- true
1903
- } else {
1904
- debug ! (
1905
- "receiver_is_implemented: type `{:?}` does not implement `LegacyReceiver` trait" ,
1906
- receiver_ty
1907
- ) ;
1908
- false
1909
- }
1910
- }
1911
-
1912
1815
/// Determine whether it _would have_ been possible to use `receiver_ty` as
1913
1816
/// a self type if it had been `Sized`, so we can show a specialized diagnostic.
1914
1817
/// This is because it's an easy mistake to `impl<T> Receiver` instead of
@@ -1917,16 +1820,13 @@ fn emit_receiver_not_implemented_errors<'tcx>(
1917
1820
wfcx : & WfCheckingCtxt < ' _ , ' tcx > ,
1918
1821
span : Span ,
1919
1822
receiver_ty : Ty < ' tcx > ,
1920
- arbitrary_self_types_enabled : Option < ArbitrarySelfTypesLevel > ,
1823
+ arbitrary_self_types_pointers_enabled : bool ,
1921
1824
) {
1922
1825
let infcx = wfcx. infcx ;
1923
1826
let mut autoderef = Autoderef :: new ( infcx, wfcx. param_env , wfcx. body_def_id , span, receiver_ty)
1924
1827
. use_receiver_trait ( ) ;
1925
1828
1926
- if arbitrary_self_types_enabled. is_some ( ) {
1927
- autoderef = autoderef. use_receiver_trait ( ) ;
1928
- }
1929
- if arbitrary_self_types_enabled == Some ( ArbitrarySelfTypesLevel :: WithPointers ) {
1829
+ if arbitrary_self_types_pointers_enabled {
1930
1830
autoderef = autoderef. include_raw_pointers ( ) ;
1931
1831
}
1932
1832
0 commit comments