Skip to content

Commit 0dbc091

Browse files
committed
add test for suggest_name
1 parent 82c1b31 commit 0dbc091

File tree

3 files changed

+35
-14
lines changed

3 files changed

+35
-14
lines changed

crates/hir/src/semantics.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use hir_expand::{
1616
name::{known, AsName},
1717
ExpansionInfo, MacroCallId,
1818
};
19-
use hir_ty::Interner;
2019
use itertools::Itertools;
2120
use rustc_hash::{FxHashMap, FxHashSet};
2221
use smallvec::{smallvec, SmallVec};
@@ -975,18 +974,11 @@ impl<'db> SemanticsImpl<'db> {
975974
}
976975

977976
fn resolve_method_call(&self, call: &ast::MethodCallExpr) -> Option<FunctionId> {
978-
self.analyze(call.syntax())?.resolve_method_call(self.db, call).map(|(id, _)| id)
977+
self.analyze(call.syntax())?.resolve_method_call(self.db, call)
979978
}
980979

981980
fn resolve_method_call_as_callable(&self, call: &ast::MethodCallExpr) -> Option<Callable> {
982-
let source_analyzer = self.analyze(call.syntax())?;
983-
let (func, subst) = source_analyzer.resolve_method_call(self.db, call)?;
984-
let ty = self.db.value_ty(func.into()).substitute(Interner, &subst);
985-
let resolver = source_analyzer.resolver;
986-
let ty = Type::new_with_resolver(self.db, &resolver, ty);
987-
let mut res = ty.as_callable(self.db)?;
988-
res.is_bound_method = true;
989-
Some(res)
981+
self.analyze(call.syntax())?.resolve_method_call_as_callable(self.db, call)
990982
}
991983

992984
fn resolve_field(&self, field: &ast::FieldExpr) -> Option<Field> {

crates/hir/src/source_analyzer.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ use syntax::{
4343

4444
use crate::{
4545
db::HirDatabase, semantics::PathResolution, Adt, AssocItem, BindingMode, BuiltinAttr,
46-
BuiltinType, Const, Field, Function, Local, Macro, ModuleDef, Static, Struct, ToolModule,
47-
Trait, Type, TypeAlias, Variant,
46+
BuiltinType, Callable, Const, Field, Function, Local, Macro, ModuleDef, Static, Struct,
47+
ToolModule, Trait, Type, TypeAlias, Variant,
4848
};
4949

5050
/// `SourceAnalyzer` is a convenience wrapper which exposes HIR API in terms of
@@ -239,15 +239,29 @@ impl SourceAnalyzer {
239239
)
240240
}
241241

242+
pub(crate) fn resolve_method_call_as_callable(
243+
&self,
244+
db: &dyn HirDatabase,
245+
call: &ast::MethodCallExpr,
246+
) -> Option<Callable> {
247+
let expr_id = self.expr_id(db, &call.clone().into())?;
248+
let (func, substs) = self.infer.as_ref()?.method_resolution(expr_id)?;
249+
let ty = db.value_ty(func.into()).substitute(Interner, &substs);
250+
let ty = Type::new_with_resolver(db, &self.resolver, ty);
251+
let mut res = ty.as_callable(db)?;
252+
res.is_bound_method = true;
253+
Some(res)
254+
}
255+
242256
pub(crate) fn resolve_method_call(
243257
&self,
244258
db: &dyn HirDatabase,
245259
call: &ast::MethodCallExpr,
246-
) -> Option<(FunctionId, Substitution)> {
260+
) -> Option<FunctionId> {
247261
let expr_id = self.expr_id(db, &call.clone().into())?;
248262
let (f_in_trait, substs) = self.infer.as_ref()?.method_resolution(expr_id)?;
249263
let f_in_impl = self.resolve_impl_method(db, f_in_trait, &substs);
250-
Some((f_in_impl.unwrap_or(f_in_trait), substs))
264+
f_in_impl.or(Some(f_in_trait))
251265
}
252266

253267
pub(crate) fn resolve_field(

crates/ide-assists/src/utils/suggest_name.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,21 @@ fn foo() { S.bar($01$0, 2) }
450450
);
451451
}
452452

453+
#[test]
454+
fn method_on_impl_trait() {
455+
check(
456+
r#"
457+
struct S;
458+
trait T {
459+
fn bar(&self, n: i32, m: u32);
460+
}
461+
impl T for S { fn bar(&self, n: i32, m: u32); }
462+
fn foo() { S.bar($01$0, 2) }
463+
"#,
464+
"n",
465+
);
466+
}
467+
453468
#[test]
454469
fn method_ufcs() {
455470
check(

0 commit comments

Comments
 (0)