@@ -29,7 +29,6 @@ use rustc_trait_selection::regions::{InferCtxtRegionExt, OutlivesEnvironmentBuil
2929use rustc_trait_selection:: traits:: misc:: {
3030 ConstParamTyImplementationError , type_allowed_to_implement_const_param_ty,
3131} ;
32- use rustc_trait_selection:: traits:: query:: evaluate_obligation:: InferCtxtExt as _;
3332use rustc_trait_selection:: traits:: {
3433 self , FulfillmentError , Obligation , ObligationCause , ObligationCauseCode , ObligationCtxt ,
3534 WellFormedLoc ,
@@ -1720,13 +1719,6 @@ fn check_sized_if_body<'tcx>(
17201719 }
17211720}
17221721
1723- /// The `arbitrary_self_types_pointers` feature implies `arbitrary_self_types`.
1724- #[ derive( Clone , Copy , PartialEq ) ]
1725- enum ArbitrarySelfTypesLevel {
1726- Basic , // just arbitrary_self_types
1727- WithPointers , // both arbitrary_self_types and arbitrary_self_types_pointers
1728- }
1729-
17301722#[ instrument( level = "debug" , skip( wfcx) ) ]
17311723fn check_method_receiver < ' tcx > (
17321724 wfcx : & WfCheckingCtxt < ' _ , ' tcx > ,
@@ -1759,55 +1751,21 @@ fn check_method_receiver<'tcx>(
17591751 return Ok ( ( ) ) ;
17601752 }
17611753
1762- let arbitrary_self_types_level = if tcx. features ( ) . arbitrary_self_types_pointers ( ) {
1763- Some ( ArbitrarySelfTypesLevel :: WithPointers )
1764- } else if tcx. features ( ) . arbitrary_self_types ( ) {
1765- Some ( ArbitrarySelfTypesLevel :: Basic )
1766- } else {
1767- None
1768- } ;
17691754 let generics = tcx. generics_of ( method. def_id ) ;
17701755
1771- let receiver_validity =
1772- receiver_is_valid ( wfcx, span, receiver_ty, self_ty, arbitrary_self_types_level, generics) ;
1756+ let arbitrary_self_types_pointers_enabled = tcx. features ( ) . arbitrary_self_types_pointers ( ) ;
1757+ let receiver_validity = receiver_is_valid (
1758+ wfcx,
1759+ span,
1760+ receiver_ty,
1761+ self_ty,
1762+ arbitrary_self_types_pointers_enabled,
1763+ generics,
1764+ ) ;
17731765 if let Err ( receiver_validity_err) = receiver_validity {
1774- return Err ( match arbitrary_self_types_level {
1775- // Wherever possible, emit a message advising folks that the features
1776- // `arbitrary_self_types` or `arbitrary_self_types_pointers` might
1777- // have helped.
1778- None if receiver_is_valid (
1779- wfcx,
1780- span,
1781- receiver_ty,
1782- self_ty,
1783- Some ( ArbitrarySelfTypesLevel :: Basic ) ,
1784- generics,
1785- )
1786- . is_ok ( ) =>
1787- {
1788- // Report error; would have worked with `arbitrary_self_types`.
1789- feature_err (
1790- & tcx. sess ,
1791- sym:: arbitrary_self_types,
1792- span,
1793- format ! (
1794- "`{receiver_ty}` cannot be used as the type of `self` without \
1795- the `arbitrary_self_types` feature",
1796- ) ,
1797- )
1798- . with_help ( fluent:: hir_analysis_invalid_receiver_ty_help)
1799- . emit ( )
1800- }
1801- None | Some ( ArbitrarySelfTypesLevel :: Basic )
1802- if receiver_is_valid (
1803- wfcx,
1804- span,
1805- receiver_ty,
1806- self_ty,
1807- Some ( ArbitrarySelfTypesLevel :: WithPointers ) ,
1808- generics,
1809- )
1810- . is_ok ( ) =>
1766+ return Err (
1767+ if !arbitrary_self_types_pointers_enabled
1768+ && receiver_is_valid ( wfcx, span, receiver_ty, self_ty, true , generics) . is_ok ( )
18111769 {
18121770 // Report error; would have worked with `arbitrary_self_types_pointers`.
18131771 feature_err (
@@ -1816,17 +1774,15 @@ fn check_method_receiver<'tcx>(
18161774 span,
18171775 format ! (
18181776 "`{receiver_ty}` cannot be used as the type of `self` without \
1819- the `arbitrary_self_types_pointers` feature",
1777+ the `arbitrary_self_types_pointers` feature",
18201778 ) ,
18211779 )
18221780 . with_help ( fluent:: hir_analysis_invalid_receiver_ty_help)
18231781 . emit ( )
1824- }
1825- _ =>
1826- // Report error; would not have worked with `arbitrary_self_types[_pointers]`.
1827- {
1782+ } else {
1783+ // Report error; would not have worked with `arbitrary_self_types_pointers`.
18281784 match receiver_validity_err {
1829- ReceiverValidityError :: DoesNotDeref if arbitrary_self_types_level . is_some ( ) => {
1785+ ReceiverValidityError :: DoesNotDeref => {
18301786 let hint = match receiver_ty
18311787 . builtin_deref ( false )
18321788 . unwrap_or ( receiver_ty)
@@ -1840,18 +1796,12 @@ fn check_method_receiver<'tcx>(
18401796
18411797 tcx. dcx ( ) . emit_err ( errors:: InvalidReceiverTy { span, receiver_ty, hint } )
18421798 }
1843- ReceiverValidityError :: DoesNotDeref => {
1844- tcx. dcx ( ) . emit_err ( errors:: InvalidReceiverTyNoArbitrarySelfTypes {
1845- span,
1846- receiver_ty,
1847- } )
1848- }
18491799 ReceiverValidityError :: MethodGenericParamUsed => {
18501800 tcx. dcx ( ) . emit_err ( errors:: InvalidGenericReceiverTy { span, receiver_ty } )
18511801 }
18521802 }
1853- }
1854- } ) ;
1803+ } ,
1804+ ) ;
18551805 }
18561806 Ok ( ( ) )
18571807}
@@ -1895,11 +1845,10 @@ fn receiver_is_valid<'tcx>(
18951845 span : Span ,
18961846 receiver_ty : Ty < ' tcx > ,
18971847 self_ty : Ty < ' tcx > ,
1898- arbitrary_self_types_enabled : Option < ArbitrarySelfTypesLevel > ,
1848+ arbitrary_self_types_pointers_enabled : bool ,
18991849 method_generics : & ty:: Generics ,
19001850) -> Result < ( ) , ReceiverValidityError > {
19011851 let infcx = wfcx. infcx ;
1902- let tcx = wfcx. tcx ( ) ;
19031852 let cause =
19041853 ObligationCause :: new ( span, wfcx. body_def_id , traits:: ObligationCauseCode :: MethodReceiver ) ;
19051854
@@ -1914,17 +1863,11 @@ fn receiver_is_valid<'tcx>(
19141863
19151864 confirm_type_is_not_a_method_generic_param ( receiver_ty, method_generics) ?;
19161865
1917- let mut autoderef = Autoderef :: new ( infcx, wfcx. param_env , wfcx. body_def_id , span, receiver_ty) ;
1918-
1919- // The `arbitrary_self_types` feature allows custom smart pointer
1920- // types to be method receivers, as identified by following the Receiver<Target=T>
1921- // chain.
1922- if arbitrary_self_types_enabled. is_some ( ) {
1923- autoderef = autoderef. use_receiver_trait ( ) ;
1924- }
1866+ let mut autoderef = Autoderef :: new ( infcx, wfcx. param_env , wfcx. body_def_id , span, receiver_ty)
1867+ . use_receiver_trait ( ) ;
19251868
19261869 // The `arbitrary_self_types_pointers` feature allows raw pointer receivers like `self: *const Self`.
1927- if arbitrary_self_types_enabled == Some ( ArbitrarySelfTypesLevel :: WithPointers ) {
1870+ if arbitrary_self_types_pointers_enabled {
19281871 autoderef = autoderef. include_raw_pointers ( ) ;
19291872 }
19301873
@@ -1947,58 +1890,12 @@ fn receiver_is_valid<'tcx>(
19471890 wfcx. register_obligations ( autoderef. into_obligations ( ) ) ;
19481891 return Ok ( ( ) ) ;
19491892 }
1950-
1951- // Without `feature(arbitrary_self_types)`, we require that each step in the
1952- // deref chain implement `LegacyReceiver`.
1953- if arbitrary_self_types_enabled. is_none ( ) {
1954- let legacy_receiver_trait_def_id =
1955- tcx. require_lang_item ( LangItem :: LegacyReceiver , Some ( span) ) ;
1956- if !legacy_receiver_is_implemented (
1957- wfcx,
1958- legacy_receiver_trait_def_id,
1959- cause. clone ( ) ,
1960- potential_self_ty,
1961- ) {
1962- // We cannot proceed.
1963- break ;
1964- }
1965-
1966- // Register the bound, in case it has any region side-effects.
1967- wfcx. register_bound (
1968- cause. clone ( ) ,
1969- wfcx. param_env ,
1970- potential_self_ty,
1971- legacy_receiver_trait_def_id,
1972- ) ;
1973- }
19741893 }
19751894
19761895 debug ! ( "receiver_is_valid: type `{:?}` does not deref to `{:?}`" , receiver_ty, self_ty) ;
19771896 Err ( ReceiverValidityError :: DoesNotDeref )
19781897}
19791898
1980- fn legacy_receiver_is_implemented < ' tcx > (
1981- wfcx : & WfCheckingCtxt < ' _ , ' tcx > ,
1982- legacy_receiver_trait_def_id : DefId ,
1983- cause : ObligationCause < ' tcx > ,
1984- receiver_ty : Ty < ' tcx > ,
1985- ) -> bool {
1986- let tcx = wfcx. tcx ( ) ;
1987- let trait_ref = ty:: TraitRef :: new ( tcx, legacy_receiver_trait_def_id, [ receiver_ty] ) ;
1988-
1989- let obligation = Obligation :: new ( tcx, cause, wfcx. param_env , trait_ref) ;
1990-
1991- if wfcx. infcx . predicate_must_hold_modulo_regions ( & obligation) {
1992- true
1993- } else {
1994- debug ! (
1995- "receiver_is_implemented: type `{:?}` does not implement `LegacyReceiver` trait" ,
1996- receiver_ty
1997- ) ;
1998- false
1999- }
2000- }
2001-
20021899fn check_variances_for_type_defn < ' tcx > (
20031900 tcx : TyCtxt < ' tcx > ,
20041901 item : & ' tcx hir:: Item < ' tcx > ,
0 commit comments