Record method resolution for index expressions

This commit is contained in:
Lukas Wirth 2023-01-27 11:15:05 +01:00
parent 54da0bfbf0
commit a7f81e3cdc
2 changed files with 12 additions and 6 deletions

View File

@ -17,7 +17,7 @@
resolver::resolver_for_expr,
ConstParamId, FieldId, ItemContainerId, Lookup,
};
use hir_expand::name::Name;
use hir_expand::name::{name, Name};
use stdx::always;
use syntax::ast::RangeOp;
@ -741,7 +741,6 @@ fn infer_expr_inner(&mut self, tgt_expr: ExprId, expected: &Expectation) -> Ty {
}
}
Expr::Index { base, index } => {
// FIXME: note down method resolution for the `index`/`index_mut` function
let base_ty = self.infer_expr_inner(*base, &Expectation::none());
let index_ty = self.infer_expr(*index, &Expectation::none());
@ -758,6 +757,15 @@ fn infer_expr_inner(&mut self, tgt_expr: ExprId, expected: &Expectation) -> Ty {
adj.apply(&mut self.table, base_ty)
});
self.write_expr_adj(*base, adj);
if let Some(func) =
self.db.trait_data(index_trait).method_by_name(&name!(index))
{
let substs = TyBuilder::subst_for_def(self.db, index_trait, None)
.push(self_ty.clone())
.push(index_ty.clone())
.build();
self.write_method_resolution(tgt_expr, func, substs.clone());
}
self.resolve_associated_type_with_params(
self_ty,
self.resolve_ops_index_output(),

View File

@ -2295,10 +2295,8 @@ fn unselected_projection_in_trait_env_no_cycle() {
// this is not a cycle
check_types(
r#"
//- /main.rs
trait Index {
type Output;
}
//- minicore: index
use core::ops::Index;
type Key<S: UnificationStoreBase> = <S as UnificationStoreBase>::Key;