@@ -420,7 +420,22 @@ impl SourceAnalyzer {
420
420
let base_ty = self . ty_of_expr ( db, & index_expr. base ( ) ?) ?;
421
421
let index_ty = self . ty_of_expr ( db, & index_expr. index ( ) ?) ?;
422
422
423
- let ( op_trait, op_fn) = self . lang_trait_fn ( db, LangItem :: Index , & name ! [ index] ) ?;
423
+ let ( index_trait, index_fn) = self . lang_trait_fn ( db, LangItem :: Index , & name ! [ index] ) ?;
424
+ let ( op_trait, op_fn) = self
425
+ . infer
426
+ . as_ref ( )
427
+ . and_then ( |infer| {
428
+ let expr = self . expr_id ( db, & index_expr. clone ( ) . into ( ) ) ?;
429
+ let ( func, _) = infer. method_resolution ( expr) ?;
430
+ let ( index_mut_trait, index_mut_fn) =
431
+ self . lang_trait_fn ( db, LangItem :: IndexMut , & name ! [ index_mut] ) ?;
432
+ if func == index_mut_fn {
433
+ Some ( ( index_mut_trait, index_mut_fn) )
434
+ } else {
435
+ None
436
+ }
437
+ } )
438
+ . unwrap_or ( ( index_trait, index_fn) ) ;
424
439
// HACK: subst for all methods coincides with that for their trait because the methods
425
440
// don't have any generic parameters, so we skip building another subst for the methods.
426
441
let substs = hir_ty:: TyBuilder :: subst_for_def ( db, op_trait, None )
0 commit comments