@@ -13,11 +13,12 @@ use hir_def::{
13
13
ArithOp , Array , BinaryOp , ClosureKind , Expr , ExprId , LabelId , Literal , Statement , UnaryOp ,
14
14
} ,
15
15
generics:: TypeOrConstParamData ,
16
+ lang_item:: LangItem ,
16
17
path:: { GenericArg , GenericArgs } ,
17
18
resolver:: resolver_for_expr,
18
19
ConstParamId , FieldId , ItemContainerId , Lookup ,
19
20
} ;
20
- use hir_expand:: name:: Name ;
21
+ use hir_expand:: name:: { name , Name } ;
21
22
use stdx:: always;
22
23
use syntax:: ast:: RangeOp ;
23
24
@@ -157,7 +158,8 @@ impl<'a> InferenceContext<'a> {
157
158
}
158
159
159
160
// The ok-ish type that is expected from the last expression
160
- let ok_ty = self . resolve_associated_type ( try_ty. clone ( ) , self . resolve_ops_try_ok ( ) ) ;
161
+ let ok_ty =
162
+ self . resolve_associated_type ( try_ty. clone ( ) , self . resolve_ops_try_output ( ) ) ;
161
163
162
164
self . with_breakable_ctx ( BreakableKind :: Block , ok_ty. clone ( ) , None , |this| {
163
165
this. infer_expr ( * body, & Expectation :: has_type ( ok_ty) ) ;
@@ -331,11 +333,18 @@ impl<'a> InferenceContext<'a> {
331
333
derefed_callee. callable_sig ( self . db ) . map_or ( false , |sig| sig. is_varargs )
332
334
|| res. is_none ( ) ;
333
335
let ( param_tys, ret_ty) = match res {
334
- Some ( res ) => {
336
+ Some ( ( func , params , ret_ty ) ) => {
335
337
let adjustments = auto_deref_adjust_steps ( & derefs) ;
336
338
// FIXME: Handle call adjustments for Fn/FnMut
337
339
self . write_expr_adj ( * callee, adjustments) ;
338
- res
340
+ if let Some ( ( trait_, func) ) = func {
341
+ let subst = TyBuilder :: subst_for_def ( self . db , trait_, None )
342
+ . push ( callee_ty. clone ( ) )
343
+ . push ( TyBuilder :: tuple_with ( params. iter ( ) . cloned ( ) ) )
344
+ . build ( ) ;
345
+ self . write_method_resolution ( tgt_expr, func, subst. clone ( ) ) ;
346
+ }
347
+ ( params, ret_ty)
339
348
}
340
349
None => ( Vec :: new ( ) , self . err_ty ( ) ) , // FIXME diagnostic
341
350
} ;
@@ -587,7 +596,18 @@ impl<'a> InferenceContext<'a> {
587
596
}
588
597
Expr :: Try { expr } => {
589
598
let inner_ty = self . infer_expr_inner ( * expr, & Expectation :: none ( ) ) ;
590
- self . resolve_associated_type ( inner_ty, self . resolve_ops_try_ok ( ) )
599
+ if let Some ( trait_) = self . resolve_lang_trait ( LangItem :: Try ) {
600
+ if let Some ( func) = self . db . trait_data ( trait_) . method_by_name ( & name ! ( branch) ) {
601
+ let subst = TyBuilder :: subst_for_def ( self . db , trait_, None )
602
+ . push ( inner_ty. clone ( ) )
603
+ . build ( ) ;
604
+ self . write_method_resolution ( tgt_expr, func, subst. clone ( ) ) ;
605
+ }
606
+ let try_output = self . resolve_output_on ( trait_) ;
607
+ self . resolve_associated_type ( inner_ty, try_output)
608
+ } else {
609
+ self . err_ty ( )
610
+ }
591
611
}
592
612
Expr :: Cast { expr, type_ref } => {
593
613
// FIXME: propagate the "castable to" expectation (and find a test case that shows this is necessary)
@@ -626,6 +646,7 @@ impl<'a> InferenceContext<'a> {
626
646
Expr :: UnaryOp { expr, op } => {
627
647
let inner_ty = self . infer_expr_inner ( * expr, & Expectation :: none ( ) ) ;
628
648
let inner_ty = self . resolve_ty_shallow ( & inner_ty) ;
649
+ // FIXME: Note down method resolution her
629
650
match op {
630
651
UnaryOp :: Deref => {
631
652
autoderef:: deref ( & mut self . table , inner_ty) . unwrap_or_else ( || self . err_ty ( ) )
@@ -735,7 +756,7 @@ impl<'a> InferenceContext<'a> {
735
756
let base_ty = self . infer_expr_inner ( * base, & Expectation :: none ( ) ) ;
736
757
let index_ty = self . infer_expr ( * index, & Expectation :: none ( ) ) ;
737
758
738
- if let Some ( index_trait) = self . resolve_ops_index ( ) {
759
+ if let Some ( index_trait) = self . resolve_lang_trait ( LangItem :: Index ) {
739
760
let canonicalized = self . canonicalize ( base_ty. clone ( ) ) ;
740
761
let receiver_adjustments = method_resolution:: resolve_indexing_op (
741
762
self . db ,
@@ -748,6 +769,15 @@ impl<'a> InferenceContext<'a> {
748
769
adj. apply ( & mut self . table , base_ty)
749
770
} ) ;
750
771
self . write_expr_adj ( * base, adj) ;
772
+ if let Some ( func) =
773
+ self . db . trait_data ( index_trait) . method_by_name ( & name ! ( index) )
774
+ {
775
+ let substs = TyBuilder :: subst_for_def ( self . db , index_trait, None )
776
+ . push ( self_ty. clone ( ) )
777
+ . push ( index_ty. clone ( ) )
778
+ . build ( ) ;
779
+ self . write_method_resolution ( tgt_expr, func, substs. clone ( ) ) ;
780
+ }
751
781
self . resolve_associated_type_with_params (
752
782
self_ty,
753
783
self . resolve_ops_index_output ( ) ,
0 commit comments