@@ -2440,7 +2440,7 @@ impl Impl {
2440
2440
2441
2441
#[ derive( Clone , PartialEq , Eq , Debug ) ]
2442
2442
pub struct Type {
2443
- krate : CrateId ,
2443
+ krate : CrateId , // FIXME this is probably redundant with the TraitEnvironment
2444
2444
env : Arc < TraitEnvironment > ,
2445
2445
ty : Ty ,
2446
2446
}
@@ -2533,36 +2533,25 @@ impl Type {
2533
2533
/// Checks that particular type `ty` implements `std::future::Future`.
2534
2534
/// This function is used in `.await` syntax completion.
2535
2535
pub fn impls_future ( & self , db : & dyn HirDatabase ) -> bool {
2536
- // No special case for the type of async block, since Chalk can figure it out.
2537
-
2538
- let krate = self . krate ;
2539
-
2540
- let std_future_trait =
2541
- db. lang_item ( krate, SmolStr :: new_inline ( "future_trait" ) ) . and_then ( |it| it. as_trait ( ) ) ;
2536
+ let std_future_trait = db
2537
+ . lang_item ( self . krate , SmolStr :: new_inline ( "future_trait" ) )
2538
+ . and_then ( |it| it. as_trait ( ) ) ;
2542
2539
let std_future_trait = match std_future_trait {
2543
2540
Some ( it) => it,
2544
2541
None => return false ,
2545
2542
} ;
2546
2543
2547
2544
let canonical_ty =
2548
2545
Canonical { value : self . ty . clone ( ) , binders : CanonicalVarKinds :: empty ( Interner ) } ;
2549
- method_resolution:: implements_trait (
2550
- & canonical_ty,
2551
- db,
2552
- self . env . clone ( ) ,
2553
- krate,
2554
- std_future_trait,
2555
- )
2546
+ method_resolution:: implements_trait ( & canonical_ty, db, self . env . clone ( ) , std_future_trait)
2556
2547
}
2557
2548
2558
2549
/// Checks that particular type `ty` implements `std::ops::FnOnce`.
2559
2550
///
2560
2551
/// This function can be used to check if a particular type is callable, since FnOnce is a
2561
2552
/// supertrait of Fn and FnMut, so all callable types implements at least FnOnce.
2562
2553
pub fn impls_fnonce ( & self , db : & dyn HirDatabase ) -> bool {
2563
- let krate = self . krate ;
2564
-
2565
- let fnonce_trait = match FnTrait :: FnOnce . get_id ( db, krate) {
2554
+ let fnonce_trait = match FnTrait :: FnOnce . get_id ( db, self . krate ) {
2566
2555
Some ( it) => it,
2567
2556
None => return false ,
2568
2557
} ;
@@ -2573,7 +2562,6 @@ impl Type {
2573
2562
& canonical_ty,
2574
2563
db,
2575
2564
self . env . clone ( ) ,
2576
- krate,
2577
2565
fnonce_trait,
2578
2566
)
2579
2567
}
@@ -2744,9 +2732,8 @@ impl Type {
2744
2732
pub fn autoderef_ < ' a > ( & ' a self , db : & ' a dyn HirDatabase ) -> impl Iterator < Item = Ty > + ' a {
2745
2733
// There should be no inference vars in types passed here
2746
2734
let canonical = hir_ty:: replace_errors_with_variables ( & self . ty ) ;
2747
- let environment = self . env . env . clone ( ) ;
2748
- let ty = InEnvironment { goal : canonical, environment } ;
2749
- autoderef ( db, Some ( self . krate ) , ty) . map ( |canonical| canonical. value )
2735
+ let environment = self . env . clone ( ) ;
2736
+ autoderef ( db, environment, canonical) . map ( |canonical| canonical. value )
2750
2737
}
2751
2738
2752
2739
// This would be nicer if it just returned an iterator, but that runs into
@@ -2801,24 +2788,26 @@ impl Type {
2801
2788
pub fn iterate_method_candidates < T > (
2802
2789
& self ,
2803
2790
db : & dyn HirDatabase ,
2804
- krate : Crate ,
2791
+ scope : & SemanticsScope ,
2792
+ // FIXME this can be retrieved from `scope`, except autoimport uses this
2793
+ // to specify a different set, so the method needs to be split
2805
2794
traits_in_scope : & FxHashSet < TraitId > ,
2806
2795
with_local_impls : Option < Module > ,
2807
2796
name : Option < & Name > ,
2808
- mut callback : impl FnMut ( Type , Function ) -> Option < T > ,
2797
+ mut callback : impl FnMut ( Function ) -> Option < T > ,
2809
2798
) -> Option < T > {
2810
2799
let _p = profile:: span ( "iterate_method_candidates" ) ;
2811
2800
let mut slot = None ;
2812
2801
2813
2802
self . iterate_method_candidates_dyn (
2814
2803
db,
2815
- krate ,
2804
+ scope ,
2816
2805
traits_in_scope,
2817
2806
with_local_impls,
2818
2807
name,
2819
- & mut |ty , assoc_item_id| {
2808
+ & mut |assoc_item_id| {
2820
2809
if let AssocItemId :: FunctionId ( func) = assoc_item_id {
2821
- if let Some ( res) = callback ( self . derived ( ty . clone ( ) ) , func. into ( ) ) {
2810
+ if let Some ( res) = callback ( func. into ( ) ) {
2822
2811
slot = Some ( res) ;
2823
2812
return ControlFlow :: Break ( ( ) ) ;
2824
2813
}
@@ -2832,50 +2821,55 @@ impl Type {
2832
2821
fn iterate_method_candidates_dyn (
2833
2822
& self ,
2834
2823
db : & dyn HirDatabase ,
2835
- krate : Crate ,
2824
+ scope : & SemanticsScope ,
2836
2825
traits_in_scope : & FxHashSet < TraitId > ,
2837
2826
with_local_impls : Option < Module > ,
2838
2827
name : Option < & Name > ,
2839
- callback : & mut dyn FnMut ( & Ty , AssocItemId ) -> ControlFlow < ( ) > ,
2828
+ callback : & mut dyn FnMut ( AssocItemId ) -> ControlFlow < ( ) > ,
2840
2829
) {
2841
2830
// There should be no inference vars in types passed here
2842
2831
let canonical = hir_ty:: replace_errors_with_variables ( & self . ty ) ;
2843
2832
2844
- let env = self . env . clone ( ) ;
2845
- let krate = krate. id ;
2833
+ let krate = match scope. krate ( ) {
2834
+ Some ( k) => k,
2835
+ None => return ,
2836
+ } ;
2837
+ let environment = scope. resolver ( ) . generic_def ( ) . map_or_else (
2838
+ || Arc :: new ( TraitEnvironment :: empty ( krate. id ) ) ,
2839
+ |d| db. trait_environment ( d) ,
2840
+ ) ;
2846
2841
2847
2842
method_resolution:: iterate_method_candidates_dyn (
2848
2843
& canonical,
2849
2844
db,
2850
- env,
2851
- krate,
2845
+ environment,
2852
2846
traits_in_scope,
2853
2847
with_local_impls. and_then ( |b| b. id . containing_block ( ) ) . into ( ) ,
2854
2848
name,
2855
2849
method_resolution:: LookupMode :: MethodCall ,
2856
- & mut |ty , id| callback ( & ty . value , id) ,
2850
+ & mut |_adj , id| callback ( id) ,
2857
2851
) ;
2858
2852
}
2859
2853
2860
2854
pub fn iterate_path_candidates < T > (
2861
2855
& self ,
2862
2856
db : & dyn HirDatabase ,
2863
- krate : Crate ,
2857
+ scope : & SemanticsScope ,
2864
2858
traits_in_scope : & FxHashSet < TraitId > ,
2865
2859
with_local_impls : Option < Module > ,
2866
2860
name : Option < & Name > ,
2867
- mut callback : impl FnMut ( Type , AssocItem ) -> Option < T > ,
2861
+ mut callback : impl FnMut ( AssocItem ) -> Option < T > ,
2868
2862
) -> Option < T > {
2869
2863
let _p = profile:: span ( "iterate_path_candidates" ) ;
2870
2864
let mut slot = None ;
2871
2865
self . iterate_path_candidates_dyn (
2872
2866
db,
2873
- krate ,
2867
+ scope ,
2874
2868
traits_in_scope,
2875
2869
with_local_impls,
2876
2870
name,
2877
- & mut |ty , assoc_item_id| {
2878
- if let Some ( res) = callback ( self . derived ( ty . clone ( ) ) , assoc_item_id. into ( ) ) {
2871
+ & mut |assoc_item_id| {
2872
+ if let Some ( res) = callback ( assoc_item_id. into ( ) ) {
2879
2873
slot = Some ( res) ;
2880
2874
return ControlFlow :: Break ( ( ) ) ;
2881
2875
}
@@ -2888,27 +2882,31 @@ impl Type {
2888
2882
fn iterate_path_candidates_dyn (
2889
2883
& self ,
2890
2884
db : & dyn HirDatabase ,
2891
- krate : Crate ,
2885
+ scope : & SemanticsScope ,
2892
2886
traits_in_scope : & FxHashSet < TraitId > ,
2893
2887
with_local_impls : Option < Module > ,
2894
2888
name : Option < & Name > ,
2895
- callback : & mut dyn FnMut ( & Ty , AssocItemId ) -> ControlFlow < ( ) > ,
2889
+ callback : & mut dyn FnMut ( AssocItemId ) -> ControlFlow < ( ) > ,
2896
2890
) {
2897
2891
let canonical = hir_ty:: replace_errors_with_variables ( & self . ty ) ;
2898
2892
2899
- let env = self . env . clone ( ) ;
2900
- let krate = krate. id ;
2893
+ let krate = match scope. krate ( ) {
2894
+ Some ( k) => k,
2895
+ None => return ,
2896
+ } ;
2897
+ let environment = scope. resolver ( ) . generic_def ( ) . map_or_else (
2898
+ || Arc :: new ( TraitEnvironment :: empty ( krate. id ) ) ,
2899
+ |d| db. trait_environment ( d) ,
2900
+ ) ;
2901
2901
2902
- method_resolution:: iterate_method_candidates_dyn (
2902
+ method_resolution:: iterate_path_candidates (
2903
2903
& canonical,
2904
2904
db,
2905
- env,
2906
- krate,
2905
+ environment,
2907
2906
traits_in_scope,
2908
2907
with_local_impls. and_then ( |b| b. id . containing_block ( ) ) . into ( ) ,
2909
2908
name,
2910
- method_resolution:: LookupMode :: Path ,
2911
- & mut |ty, id| callback ( & ty. value , id) ,
2909
+ & mut |id| callback ( id) ,
2912
2910
) ;
2913
2911
}
2914
2912
0 commit comments