Skip to content

Commit a7f81e3

Browse files
committed
Record method resolution for index expressions
1 parent 54da0bf commit a7f81e3

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

crates/hir-ty/src/infer/expr.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use hir_def::{
1717
resolver::resolver_for_expr,
1818
ConstParamId, FieldId, ItemContainerId, Lookup,
1919
};
20-
use hir_expand::name::Name;
20+
use hir_expand::name::{name, Name};
2121
use stdx::always;
2222
use syntax::ast::RangeOp;
2323

@@ -741,7 +741,6 @@ impl<'a> InferenceContext<'a> {
741741
}
742742
}
743743
Expr::Index { base, index } => {
744-
// FIXME: note down method resolution for the `index`/`index_mut` function
745744
let base_ty = self.infer_expr_inner(*base, &Expectation::none());
746745
let index_ty = self.infer_expr(*index, &Expectation::none());
747746

@@ -758,6 +757,15 @@ impl<'a> InferenceContext<'a> {
758757
adj.apply(&mut self.table, base_ty)
759758
});
760759
self.write_expr_adj(*base, adj);
760+
if let Some(func) =
761+
self.db.trait_data(index_trait).method_by_name(&name!(index))
762+
{
763+
let substs = TyBuilder::subst_for_def(self.db, index_trait, None)
764+
.push(self_ty.clone())
765+
.push(index_ty.clone())
766+
.build();
767+
self.write_method_resolution(tgt_expr, func, substs.clone());
768+
}
761769
self.resolve_associated_type_with_params(
762770
self_ty,
763771
self.resolve_ops_index_output(),

crates/hir-ty/src/tests/traits.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2295,10 +2295,8 @@ fn unselected_projection_in_trait_env_no_cycle() {
22952295
// this is not a cycle
22962296
check_types(
22972297
r#"
2298-
//- /main.rs
2299-
trait Index {
2300-
type Output;
2301-
}
2298+
//- minicore: index
2299+
use core::ops::Index;
23022300
23032301
type Key<S: UnificationStoreBase> = <S as UnificationStoreBase>::Key;
23042302

0 commit comments

Comments
 (0)