Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Goto definition for index_mut #16709

Merged
merged 2 commits into from Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 16 additions & 1 deletion crates/hir/src/source_analyzer.rs
Expand Up @@ -420,7 +420,22 @@ impl SourceAnalyzer {
let base_ty = self.ty_of_expr(db, &index_expr.base()?)?;
let index_ty = self.ty_of_expr(db, &index_expr.index()?)?;

let (op_trait, op_fn) = self.lang_trait_fn(db, LangItem::Index, &name![index])?;
let (index_trait, index_fn) = self.lang_trait_fn(db, LangItem::Index, &name![index])?;
let (op_trait, op_fn) = self
.infer
.as_ref()
.and_then(|infer| {
let expr = self.expr_id(db, &index_expr.clone().into())?;
let (func, _) = infer.method_resolution(expr)?;
let (index_mut_trait, index_mut_fn) =
self.lang_trait_fn(db, LangItem::IndexMut, &name![index_mut])?;
if func == index_mut_fn {
Some((index_mut_trait, index_mut_fn))
} else {
None
}
})
.unwrap_or((index_trait, index_fn));
// HACK: subst for all methods coincides with that for their trait because the methods
// don't have any generic parameters, so we skip building another subst for the methods.
let substs = hir_ty::TyBuilder::subst_for_def(db, op_trait, None)
Expand Down
28 changes: 28 additions & 0 deletions crates/ide/src/goto_definition.rs
Expand Up @@ -1955,6 +1955,34 @@ fn f() {
);
}

#[test]
fn goto_index_mut_op() {
check(
r#"
//- minicore: index

struct Foo;
struct Bar;

impl core::ops::Index<usize> for Foo {
type Output = Bar;

fn index(&self, index: usize) -> &Self::Output {}
}

impl core::ops::IndexMut<usize> for Foo {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {}
//^^^^^^^^^
}

fn f() {
let mut foo = Foo;
foo[0]$0 = Bar;
}
"#,
);
}

#[test]
fn goto_prefix_op() {
check(
Expand Down