@@ -2,14 +2,13 @@ use std::cell::LazyCell;
2
2
use std:: ops:: { ControlFlow , Deref } ;
3
3
4
4
use hir:: intravisit:: { self , Visitor } ;
5
- use itertools:: Itertools ;
6
5
use rustc_data_structures:: fx:: { FxHashSet , FxIndexMap , FxIndexSet } ;
7
6
use rustc_errors:: codes:: * ;
8
7
use rustc_errors:: { Applicability , ErrorGuaranteed , pluralize, struct_span_code_err} ;
8
+ use rustc_hir:: ItemKind ;
9
9
use rustc_hir:: def:: { DefKind , Res } ;
10
10
use rustc_hir:: def_id:: { DefId , LocalDefId , LocalModDefId } ;
11
11
use rustc_hir:: lang_items:: LangItem ;
12
- use rustc_hir:: { GenericParamKind , ItemKind } ;
13
12
use rustc_infer:: infer:: outlives:: env:: OutlivesEnvironment ;
14
13
use rustc_infer:: infer:: { self , InferCtxt , TyCtxtInferExt } ;
15
14
use rustc_macros:: LintDiagnostic ;
@@ -379,7 +378,7 @@ fn check_trait_item<'tcx>(
379
378
_ => ( None , trait_item. span ) ,
380
379
} ;
381
380
check_dyn_incompatible_self_trait_by_name ( tcx, trait_item) ;
382
- let mut res = check_associated_item ( tcx, def_id, span, method_sig, None ) ;
381
+ let mut res = check_associated_item ( tcx, def_id, span, method_sig) ;
383
382
384
383
if matches ! ( trait_item. kind, hir:: TraitItemKind :: Fn ( ..) ) {
385
384
for & assoc_ty_def_id in tcx. associated_types_for_impl_traits_in_associated_fn ( def_id) {
@@ -388,7 +387,6 @@ fn check_trait_item<'tcx>(
388
387
assoc_ty_def_id. expect_local ( ) ,
389
388
tcx. def_span ( assoc_ty_def_id) ,
390
389
None ,
391
- None ,
392
390
) ) ;
393
391
}
394
392
}
@@ -906,13 +904,7 @@ fn check_impl_item<'tcx>(
906
904
hir:: ImplItemKind :: Type ( ty) if ty. span != DUMMY_SP => ( None , ty. span ) ,
907
905
_ => ( None , impl_item. span ) ,
908
906
} ;
909
- check_associated_item (
910
- tcx,
911
- impl_item. owner_id . def_id ,
912
- span,
913
- method_sig,
914
- Some ( impl_item. generics ) ,
915
- )
907
+ check_associated_item ( tcx, impl_item. owner_id . def_id , span, method_sig)
916
908
}
917
909
918
910
fn check_param_wf ( tcx : TyCtxt < ' _ > , param : & hir:: GenericParam < ' _ > ) -> Result < ( ) , ErrorGuaranteed > {
@@ -1045,7 +1037,6 @@ fn check_associated_item(
1045
1037
item_id : LocalDefId ,
1046
1038
span : Span ,
1047
1039
sig_if_method : Option < & hir:: FnSig < ' _ > > ,
1048
- generics : Option < & hir:: Generics < ' _ > > ,
1049
1040
) -> Result < ( ) , ErrorGuaranteed > {
1050
1041
let loc = Some ( WellFormedLoc :: Ty ( item_id) ) ;
1051
1042
enter_wf_checking_ctxt ( tcx, span, item_id, |wfcx| {
@@ -1078,7 +1069,7 @@ fn check_associated_item(
1078
1069
hir_sig. decl ,
1079
1070
item. def_id . expect_local ( ) ,
1080
1071
) ;
1081
- check_method_receiver ( wfcx, hir_sig, item, self_ty, generics )
1072
+ check_method_receiver ( wfcx, hir_sig, item, self_ty)
1082
1073
}
1083
1074
ty:: AssocKind :: Type => {
1084
1075
if let ty:: AssocItemContainer :: TraitContainer = item. container {
@@ -1700,7 +1691,6 @@ fn check_method_receiver<'tcx>(
1700
1691
fn_sig : & hir:: FnSig < ' _ > ,
1701
1692
method : ty:: AssocItem ,
1702
1693
self_ty : Ty < ' tcx > ,
1703
- generics : Option < & hir:: Generics < ' _ > > ,
1704
1694
) -> Result < ( ) , ErrorGuaranteed > {
1705
1695
let tcx = wfcx. tcx ( ) ;
1706
1696
@@ -1734,6 +1724,7 @@ fn check_method_receiver<'tcx>(
1734
1724
} else {
1735
1725
None
1736
1726
} ;
1727
+ let generics = tcx. generics_of ( method. def_id ) ;
1737
1728
1738
1729
let receiver_validity =
1739
1730
receiver_is_valid ( wfcx, span, receiver_ty, self_ty, arbitrary_self_types_level, generics) ;
@@ -1821,19 +1812,11 @@ enum ReceiverValidityError {
1821
1812
/// method's type params.
1822
1813
fn confirm_type_is_not_a_method_generic_param (
1823
1814
ty : Ty < ' _ > ,
1824
- method_generics : Option < & hir :: Generics < ' _ > > ,
1815
+ method_generics : & ty :: Generics ,
1825
1816
) -> Result < ( ) , ReceiverValidityError > {
1826
1817
if let ty:: Param ( param) = ty. kind ( ) {
1827
- if let Some ( generics) = method_generics {
1828
- if generics
1829
- . params
1830
- . iter ( )
1831
- . filter ( |g| matches ! ( g. kind, GenericParamKind :: Type { .. } ) )
1832
- . map ( |g| g. name . ident ( ) . name )
1833
- . contains ( & param. name )
1834
- {
1835
- return Err ( ReceiverValidityError :: MethodGenericParamUsed ) ;
1836
- }
1818
+ if ( param. index as usize ) >= method_generics. parent_count {
1819
+ return Err ( ReceiverValidityError :: MethodGenericParamUsed ) ;
1837
1820
}
1838
1821
}
1839
1822
Ok ( ( ) )
@@ -1854,7 +1837,7 @@ fn receiver_is_valid<'tcx>(
1854
1837
receiver_ty : Ty < ' tcx > ,
1855
1838
self_ty : Ty < ' tcx > ,
1856
1839
arbitrary_self_types_enabled : Option < ArbitrarySelfTypesLevel > ,
1857
- generics : Option < & hir :: Generics < ' _ > > ,
1840
+ method_generics : & ty :: Generics ,
1858
1841
) -> Result < ( ) , ReceiverValidityError > {
1859
1842
let infcx = wfcx. infcx ;
1860
1843
let tcx = wfcx. tcx ( ) ;
@@ -1870,7 +1853,7 @@ fn receiver_is_valid<'tcx>(
1870
1853
return Ok ( ( ) ) ;
1871
1854
}
1872
1855
1873
- confirm_type_is_not_a_method_generic_param ( receiver_ty, generics ) ?;
1856
+ confirm_type_is_not_a_method_generic_param ( receiver_ty, method_generics ) ?;
1874
1857
1875
1858
let mut autoderef = Autoderef :: new ( infcx, wfcx. param_env , wfcx. body_def_id , span, receiver_ty) ;
1876
1859
@@ -1888,7 +1871,7 @@ fn receiver_is_valid<'tcx>(
1888
1871
potential_self_ty, self_ty
1889
1872
) ;
1890
1873
1891
- confirm_type_is_not_a_method_generic_param ( potential_self_ty, generics ) ?;
1874
+ confirm_type_is_not_a_method_generic_param ( potential_self_ty, method_generics ) ?;
1892
1875
1893
1876
// Check if the self type unifies. If it does, then commit the result
1894
1877
// since it may have region side-effects.
0 commit comments