mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-03 07:04:49 +00:00
add test for suggest_name
This commit is contained in:
parent
82c1b313bc
commit
0dbc091fee
3 changed files with 35 additions and 14 deletions
|
@ -16,7 +16,6 @@ use hir_expand::{
|
||||||
name::{known, AsName},
|
name::{known, AsName},
|
||||||
ExpansionInfo, MacroCallId,
|
ExpansionInfo, MacroCallId,
|
||||||
};
|
};
|
||||||
use hir_ty::Interner;
|
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use rustc_hash::{FxHashMap, FxHashSet};
|
use rustc_hash::{FxHashMap, FxHashSet};
|
||||||
use smallvec::{smallvec, SmallVec};
|
use smallvec::{smallvec, SmallVec};
|
||||||
|
@ -975,18 +974,11 @@ impl<'db> SemanticsImpl<'db> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn resolve_method_call(&self, call: &ast::MethodCallExpr) -> Option<FunctionId> {
|
fn resolve_method_call(&self, call: &ast::MethodCallExpr) -> Option<FunctionId> {
|
||||||
self.analyze(call.syntax())?.resolve_method_call(self.db, call).map(|(id, _)| id)
|
self.analyze(call.syntax())?.resolve_method_call(self.db, call)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn resolve_method_call_as_callable(&self, call: &ast::MethodCallExpr) -> Option<Callable> {
|
fn resolve_method_call_as_callable(&self, call: &ast::MethodCallExpr) -> Option<Callable> {
|
||||||
let source_analyzer = self.analyze(call.syntax())?;
|
self.analyze(call.syntax())?.resolve_method_call_as_callable(self.db, call)
|
||||||
let (func, subst) = source_analyzer.resolve_method_call(self.db, call)?;
|
|
||||||
let ty = self.db.value_ty(func.into()).substitute(Interner, &subst);
|
|
||||||
let resolver = source_analyzer.resolver;
|
|
||||||
let ty = Type::new_with_resolver(self.db, &resolver, ty);
|
|
||||||
let mut res = ty.as_callable(self.db)?;
|
|
||||||
res.is_bound_method = true;
|
|
||||||
Some(res)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn resolve_field(&self, field: &ast::FieldExpr) -> Option<Field> {
|
fn resolve_field(&self, field: &ast::FieldExpr) -> Option<Field> {
|
||||||
|
|
|
@ -43,8 +43,8 @@ use syntax::{
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
db::HirDatabase, semantics::PathResolution, Adt, AssocItem, BindingMode, BuiltinAttr,
|
db::HirDatabase, semantics::PathResolution, Adt, AssocItem, BindingMode, BuiltinAttr,
|
||||||
BuiltinType, Const, Field, Function, Local, Macro, ModuleDef, Static, Struct, ToolModule,
|
BuiltinType, Callable, Const, Field, Function, Local, Macro, ModuleDef, Static, Struct,
|
||||||
Trait, Type, TypeAlias, Variant,
|
ToolModule, Trait, Type, TypeAlias, Variant,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// `SourceAnalyzer` is a convenience wrapper which exposes HIR API in terms of
|
/// `SourceAnalyzer` is a convenience wrapper which exposes HIR API in terms of
|
||||||
|
@ -239,15 +239,29 @@ impl SourceAnalyzer {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn resolve_method_call_as_callable(
|
||||||
|
&self,
|
||||||
|
db: &dyn HirDatabase,
|
||||||
|
call: &ast::MethodCallExpr,
|
||||||
|
) -> Option<Callable> {
|
||||||
|
let expr_id = self.expr_id(db, &call.clone().into())?;
|
||||||
|
let (func, substs) = self.infer.as_ref()?.method_resolution(expr_id)?;
|
||||||
|
let ty = db.value_ty(func.into()).substitute(Interner, &substs);
|
||||||
|
let ty = Type::new_with_resolver(db, &self.resolver, ty);
|
||||||
|
let mut res = ty.as_callable(db)?;
|
||||||
|
res.is_bound_method = true;
|
||||||
|
Some(res)
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn resolve_method_call(
|
pub(crate) fn resolve_method_call(
|
||||||
&self,
|
&self,
|
||||||
db: &dyn HirDatabase,
|
db: &dyn HirDatabase,
|
||||||
call: &ast::MethodCallExpr,
|
call: &ast::MethodCallExpr,
|
||||||
) -> Option<(FunctionId, Substitution)> {
|
) -> Option<FunctionId> {
|
||||||
let expr_id = self.expr_id(db, &call.clone().into())?;
|
let expr_id = self.expr_id(db, &call.clone().into())?;
|
||||||
let (f_in_trait, substs) = self.infer.as_ref()?.method_resolution(expr_id)?;
|
let (f_in_trait, substs) = self.infer.as_ref()?.method_resolution(expr_id)?;
|
||||||
let f_in_impl = self.resolve_impl_method(db, f_in_trait, &substs);
|
let f_in_impl = self.resolve_impl_method(db, f_in_trait, &substs);
|
||||||
Some((f_in_impl.unwrap_or(f_in_trait), substs))
|
f_in_impl.or(Some(f_in_trait))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn resolve_field(
|
pub(crate) fn resolve_field(
|
||||||
|
|
|
@ -450,6 +450,21 @@ fn foo() { S.bar($01$0, 2) }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn method_on_impl_trait() {
|
||||||
|
check(
|
||||||
|
r#"
|
||||||
|
struct S;
|
||||||
|
trait T {
|
||||||
|
fn bar(&self, n: i32, m: u32);
|
||||||
|
}
|
||||||
|
impl T for S { fn bar(&self, n: i32, m: u32); }
|
||||||
|
fn foo() { S.bar($01$0, 2) }
|
||||||
|
"#,
|
||||||
|
"n",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn method_ufcs() {
|
fn method_ufcs() {
|
||||||
check(
|
check(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue