Skip to content

Commit

Permalink
Auto merge of #16696 - ShoyuVanilla:fix-goto-deref-mut, r=Veykril
Browse files Browse the repository at this point in the history
fix: Goto definition for `deref_mut`

Fixes #16520

https://github.com/rust-lang/rust-analyzer/blob/a3236be9d7a8179ac4a997858138a4d6c260a451/crates/hir/src/source_analyzer.rs#L375-L393

As we can see from the above, current implementation routes all dereferencing prefix operations to `Deref::deref` implementation, not regarding mutabilities.

https://github.com/rust-lang/rust-analyzer/blob/a3236be9d7a8179ac4a997858138a4d6c260a451/crates/hir-ty/src/infer/mutability.rs#L134-L151

Since we are resolving them already in mutability inferences, we can use those results for proper `deref` / `deref_mut` routing.
  • Loading branch information
bors committed Feb 27, 2024
2 parents a3236be + 6124431 commit a41cec9
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
30 changes: 25 additions & 5 deletions crates/hir/src/source_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,14 +377,34 @@ impl SourceAnalyzer {
db: &dyn HirDatabase,
prefix_expr: &ast::PrefixExpr,
) -> Option<FunctionId> {
let (lang_item, fn_name) = match prefix_expr.op_kind()? {
ast::UnaryOp::Deref => (LangItem::Deref, name![deref]),
ast::UnaryOp::Not => (LangItem::Not, name![not]),
ast::UnaryOp::Neg => (LangItem::Neg, name![neg]),
let (op_trait, op_fn) = match prefix_expr.op_kind()? {
ast::UnaryOp::Deref => {
// This can be either `Deref::deref` or `DerefMut::deref_mut`.
// Since deref kind is inferenced and stored in `InferenceResult.method_resolution`,
// use that result to find out which one it is.
let (deref_trait, deref) =
self.lang_trait_fn(db, LangItem::Deref, &name![deref])?;
self.infer
.as_ref()
.and_then(|infer| {
let expr = self.expr_id(db, &prefix_expr.clone().into())?;
let (func, _) = infer.method_resolution(expr)?;
let (deref_mut_trait, deref_mut) =
self.lang_trait_fn(db, LangItem::DerefMut, &name![deref_mut])?;
if func == deref_mut {
Some((deref_mut_trait, deref_mut))
} else {
None
}
})
.unwrap_or((deref_trait, deref))
}
ast::UnaryOp::Not => self.lang_trait_fn(db, LangItem::Not, &name![not])?,
ast::UnaryOp::Neg => self.lang_trait_fn(db, LangItem::Neg, &name![neg])?,
};

let ty = self.ty_of_expr(db, &prefix_expr.expr()?)?;

let (op_trait, op_fn) = self.lang_trait_fn(db, lang_item, &fn_name)?;
// 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).push(ty.clone()).build();
Expand Down
27 changes: 27 additions & 0 deletions crates/ide/src/goto_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1977,6 +1977,33 @@ fn f() {
);
}

#[test]
fn goto_deref_mut() {
check(
r#"
//- minicore: deref, deref_mut
struct Foo;
struct Bar;
impl core::ops::Deref for Foo {
type Target = Bar;
fn deref(&self) -> &Self::Target {}
}
impl core::ops::DerefMut for Foo {
fn deref_mut(&mut self) -> &mut Self::Target {}
//^^^^^^^^^
}
fn f() {
let a = Foo;
$0*a = Bar;
}
"#,
);
}

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

0 comments on commit a41cec9

Please sign in to comment.