@@ -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 ;
@@ -378,7 +377,7 @@ fn check_trait_item<'tcx>(
378
377
_ => ( None , trait_item. span ) ,
379
378
} ;
380
379
check_dyn_incompatible_self_trait_by_name ( tcx, trait_item) ;
381
- let mut res = check_associated_item ( tcx, def_id, span, method_sig, None ) ;
380
+ let mut res = check_associated_item ( tcx, def_id, span, method_sig) ;
382
381
383
382
if matches ! ( trait_item. kind, hir:: TraitItemKind :: Fn ( ..) ) {
384
383
for & assoc_ty_def_id in tcx. associated_types_for_impl_traits_in_associated_fn ( def_id) {
@@ -387,7 +386,6 @@ fn check_trait_item<'tcx>(
387
386
assoc_ty_def_id. expect_local ( ) ,
388
387
tcx. def_span ( assoc_ty_def_id) ,
389
388
None ,
390
- None ,
391
389
) ) ;
392
390
}
393
391
}
@@ -905,13 +903,7 @@ fn check_impl_item<'tcx>(
905
903
hir:: ImplItemKind :: Type ( ty) if ty. span != DUMMY_SP => ( None , ty. span ) ,
906
904
_ => ( None , impl_item. span ) ,
907
905
} ;
908
- check_associated_item (
909
- tcx,
910
- impl_item. owner_id . def_id ,
911
- span,
912
- method_sig,
913
- Some ( impl_item. generics ) ,
914
- )
906
+ check_associated_item ( tcx, impl_item. owner_id . def_id , span, method_sig)
915
907
}
916
908
917
909
fn check_param_wf ( tcx : TyCtxt < ' _ > , param : & hir:: GenericParam < ' _ > ) -> Result < ( ) , ErrorGuaranteed > {
@@ -1049,7 +1041,6 @@ fn check_associated_item(
1049
1041
item_id : LocalDefId ,
1050
1042
span : Span ,
1051
1043
sig_if_method : Option < & hir:: FnSig < ' _ > > ,
1052
- generics : Option < & hir:: Generics < ' _ > > ,
1053
1044
) -> Result < ( ) , ErrorGuaranteed > {
1054
1045
let loc = Some ( WellFormedLoc :: Ty ( item_id) ) ;
1055
1046
enter_wf_checking_ctxt ( tcx, span, item_id, |wfcx| {
@@ -1082,7 +1073,7 @@ fn check_associated_item(
1082
1073
hir_sig. decl ,
1083
1074
item. def_id . expect_local ( ) ,
1084
1075
) ;
1085
- check_method_receiver ( wfcx, hir_sig, item, self_ty, generics )
1076
+ check_method_receiver ( wfcx, hir_sig, item, self_ty)
1086
1077
}
1087
1078
ty:: AssocKind :: Type => {
1088
1079
if let ty:: AssocItemContainer :: TraitContainer = item. container {
@@ -1680,7 +1671,6 @@ fn check_method_receiver<'tcx>(
1680
1671
fn_sig : & hir:: FnSig < ' _ > ,
1681
1672
method : ty:: AssocItem ,
1682
1673
self_ty : Ty < ' tcx > ,
1683
- generics : Option < & hir:: Generics < ' _ > > ,
1684
1674
) -> Result < ( ) , ErrorGuaranteed > {
1685
1675
let tcx = wfcx. tcx ( ) ;
1686
1676
@@ -1714,6 +1704,7 @@ fn check_method_receiver<'tcx>(
1714
1704
} else {
1715
1705
None
1716
1706
} ;
1707
+ let generics = tcx. generics_of ( method. def_id ) ;
1717
1708
1718
1709
let receiver_validity =
1719
1710
receiver_is_valid ( wfcx, span, receiver_ty, self_ty, arbitrary_self_types_level, generics) ;
@@ -1801,19 +1792,11 @@ enum ReceiverValidityError {
1801
1792
/// method's type params.
1802
1793
fn confirm_type_is_not_a_method_generic_param (
1803
1794
ty : Ty < ' _ > ,
1804
- method_generics : Option < & hir :: Generics < ' _ > > ,
1795
+ method_generics : & ty :: Generics ,
1805
1796
) -> Result < ( ) , ReceiverValidityError > {
1806
1797
if let ty:: Param ( param) = ty. kind ( ) {
1807
- if let Some ( generics) = method_generics {
1808
- if generics
1809
- . params
1810
- . iter ( )
1811
- . filter ( |g| matches ! ( g. kind, GenericParamKind :: Type { .. } ) )
1812
- . map ( |g| g. name . ident ( ) . name )
1813
- . contains ( & param. name )
1814
- {
1815
- return Err ( ReceiverValidityError :: MethodGenericParamUsed ) ;
1816
- }
1798
+ if ( param. index as usize ) >= method_generics. parent_count {
1799
+ return Err ( ReceiverValidityError :: MethodGenericParamUsed ) ;
1817
1800
}
1818
1801
}
1819
1802
Ok ( ( ) )
@@ -1834,7 +1817,7 @@ fn receiver_is_valid<'tcx>(
1834
1817
receiver_ty : Ty < ' tcx > ,
1835
1818
self_ty : Ty < ' tcx > ,
1836
1819
arbitrary_self_types_enabled : Option < ArbitrarySelfTypesLevel > ,
1837
- generics : Option < & hir :: Generics < ' _ > > ,
1820
+ method_generics : & ty :: Generics ,
1838
1821
) -> Result < ( ) , ReceiverValidityError > {
1839
1822
let infcx = wfcx. infcx ;
1840
1823
let tcx = wfcx. tcx ( ) ;
@@ -1850,7 +1833,7 @@ fn receiver_is_valid<'tcx>(
1850
1833
return Ok ( ( ) ) ;
1851
1834
}
1852
1835
1853
- confirm_type_is_not_a_method_generic_param ( receiver_ty, generics ) ?;
1836
+ confirm_type_is_not_a_method_generic_param ( receiver_ty, method_generics ) ?;
1854
1837
1855
1838
let mut autoderef = Autoderef :: new ( infcx, wfcx. param_env , wfcx. body_def_id , span, receiver_ty) ;
1856
1839
@@ -1868,7 +1851,7 @@ fn receiver_is_valid<'tcx>(
1868
1851
potential_self_ty, self_ty
1869
1852
) ;
1870
1853
1871
- confirm_type_is_not_a_method_generic_param ( potential_self_ty, generics ) ?;
1854
+ confirm_type_is_not_a_method_generic_param ( potential_self_ty, method_generics ) ?;
1872
1855
1873
1856
// Check if the self type unifies. If it does, then commit the result
1874
1857
// since it may have region side-effects.
0 commit comments