mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-11-15 17:28:09 +00:00
Implement autoderef for field accesses
This commit is contained in:
parent
a6071c9f4c
commit
7bb279b365
6 changed files with 185 additions and 43 deletions
|
@ -23,31 +23,35 @@ pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) -> Ca
|
||||||
}
|
}
|
||||||
|
|
||||||
fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty) -> Cancelable<()> {
|
fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty) -> Cancelable<()> {
|
||||||
// TODO: autoderef etc.
|
for receiver in receiver.autoderef(ctx.db) {
|
||||||
match receiver {
|
match receiver {
|
||||||
Ty::Adt { def_id, .. } => {
|
Ty::Adt { def_id, .. } => {
|
||||||
match def_id.resolve(ctx.db)? {
|
match def_id.resolve(ctx.db)? {
|
||||||
Def::Struct(s) => {
|
Def::Struct(s) => {
|
||||||
let variant_data = s.variant_data(ctx.db)?;
|
let variant_data = s.variant_data(ctx.db)?;
|
||||||
for field in variant_data.fields() {
|
for field in variant_data.fields() {
|
||||||
CompletionItem::new(CompletionKind::Reference, field.name().to_string())
|
CompletionItem::new(
|
||||||
|
CompletionKind::Reference,
|
||||||
|
field.name().to_string(),
|
||||||
|
)
|
||||||
.kind(CompletionItemKind::Field)
|
.kind(CompletionItemKind::Field)
|
||||||
.add_to(acc);
|
.add_to(acc);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
// TODO unions
|
||||||
|
_ => {}
|
||||||
}
|
}
|
||||||
// TODO unions
|
|
||||||
_ => {}
|
|
||||||
}
|
}
|
||||||
}
|
Ty::Tuple(fields) => {
|
||||||
Ty::Tuple(fields) => {
|
for (i, _ty) in fields.iter().enumerate() {
|
||||||
for (i, _ty) in fields.iter().enumerate() {
|
CompletionItem::new(CompletionKind::Reference, i.to_string())
|
||||||
CompletionItem::new(CompletionKind::Reference, i.to_string())
|
.kind(CompletionItemKind::Field)
|
||||||
.kind(CompletionItemKind::Field)
|
.add_to(acc);
|
||||||
.add_to(acc);
|
}
|
||||||
}
|
}
|
||||||
}
|
_ => {}
|
||||||
_ => {}
|
};
|
||||||
};
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -87,6 +91,21 @@ mod tests {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_struct_field_completion_autoderef() {
|
||||||
|
check_ref_completion(
|
||||||
|
r"
|
||||||
|
struct A { the_field: u32 }
|
||||||
|
impl A {
|
||||||
|
fn foo(&self) {
|
||||||
|
self.<|>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
",
|
||||||
|
r#"the_field"#,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_no_struct_field_completion_for_method_call() {
|
fn test_no_struct_field_completion_for_method_call() {
|
||||||
check_ref_completion(
|
check_ref_completion(
|
||||||
|
|
|
@ -56,7 +56,7 @@ pub trait HirDatabase: SyntaxDatabase
|
||||||
use fn crate::ty::type_for_def;
|
use fn crate::ty::type_for_def;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn type_for_field(def_id: DefId, field: Name) -> Cancelable<Ty> {
|
fn type_for_field(def_id: DefId, field: Name) -> Cancelable<Option<Ty>> {
|
||||||
type TypeForFieldQuery;
|
type TypeForFieldQuery;
|
||||||
use fn crate::ty::type_for_field;
|
use fn crate::ty::type_for_field;
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,6 +13,7 @@
|
||||||
//! the union-find implementation from the `ena` crate, which is extracted from
|
//! the union-find implementation from the `ena` crate, which is extracted from
|
||||||
//! rustc.
|
//! rustc.
|
||||||
|
|
||||||
|
mod autoderef;
|
||||||
mod primitive;
|
mod primitive;
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests;
|
mod tests;
|
||||||
|
@ -36,6 +37,14 @@ use crate::{
|
||||||
expr::{Body, Expr, ExprId, PatId, UnaryOp, BinaryOp, Statement},
|
expr::{Body, Expr, ExprId, PatId, UnaryOp, BinaryOp, Statement},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
fn transpose<T>(x: Cancelable<Option<T>>) -> Option<Cancelable<T>> {
|
||||||
|
match x {
|
||||||
|
Ok(Some(t)) => Some(Ok(t)),
|
||||||
|
Ok(None) => None,
|
||||||
|
Err(e) => Some(Err(e)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// The ID of a type variable.
|
/// The ID of a type variable.
|
||||||
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
|
||||||
pub struct TypeVarId(u32);
|
pub struct TypeVarId(u32);
|
||||||
|
@ -357,6 +366,14 @@ impl Ty {
|
||||||
});
|
});
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn builtin_deref(&self) -> Option<Ty> {
|
||||||
|
match self {
|
||||||
|
Ty::Ref(t, _) => Some(Ty::clone(t)),
|
||||||
|
Ty::RawPtr(t, _) => Some(Ty::clone(t)),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for Ty {
|
impl fmt::Display for Ty {
|
||||||
|
@ -443,7 +460,11 @@ pub(super) fn type_for_def(db: &impl HirDatabase, def_id: DefId) -> Cancelable<T
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn type_for_field(db: &impl HirDatabase, def_id: DefId, field: Name) -> Cancelable<Ty> {
|
pub(super) fn type_for_field(
|
||||||
|
db: &impl HirDatabase,
|
||||||
|
def_id: DefId,
|
||||||
|
field: Name,
|
||||||
|
) -> Cancelable<Option<Ty>> {
|
||||||
let def = def_id.resolve(db)?;
|
let def = def_id.resolve(db)?;
|
||||||
let variant_data = match def {
|
let variant_data = match def {
|
||||||
Def::Struct(s) => {
|
Def::Struct(s) => {
|
||||||
|
@ -459,12 +480,13 @@ pub(super) fn type_for_field(db: &impl HirDatabase, def_id: DefId, field: Name)
|
||||||
};
|
};
|
||||||
let module = def_id.module(db)?;
|
let module = def_id.module(db)?;
|
||||||
let impl_block = def_id.impl_block(db)?;
|
let impl_block = def_id.impl_block(db)?;
|
||||||
let type_ref = if let Some(tr) = variant_data.get_field_type_ref(&field) {
|
let type_ref = ctry!(variant_data.get_field_type_ref(&field));
|
||||||
tr
|
Ok(Some(Ty::from_hir(
|
||||||
} else {
|
db,
|
||||||
return Ok(Ty::Unknown);
|
&module,
|
||||||
};
|
impl_block.as_ref(),
|
||||||
Ty::from_hir(db, &module, impl_block.as_ref(), &type_ref)
|
&type_ref,
|
||||||
|
)?))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The result of type inference: A mapping from expressions and patterns to types.
|
/// The result of type inference: A mapping from expressions and patterns to types.
|
||||||
|
@ -802,7 +824,9 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
|
||||||
let (ty, def_id) = self.resolve_variant(path.as_ref())?;
|
let (ty, def_id) = self.resolve_variant(path.as_ref())?;
|
||||||
for field in fields {
|
for field in fields {
|
||||||
let field_ty = if let Some(def_id) = def_id {
|
let field_ty = if let Some(def_id) = def_id {
|
||||||
self.db.type_for_field(def_id, field.name.clone())?
|
self.db
|
||||||
|
.type_for_field(def_id, field.name.clone())?
|
||||||
|
.unwrap_or(Ty::Unknown)
|
||||||
} else {
|
} else {
|
||||||
Ty::Unknown
|
Ty::Unknown
|
||||||
};
|
};
|
||||||
|
@ -815,15 +839,20 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
|
||||||
}
|
}
|
||||||
Expr::Field { expr, name } => {
|
Expr::Field { expr, name } => {
|
||||||
let receiver_ty = self.infer_expr(*expr, &Expectation::none())?;
|
let receiver_ty = self.infer_expr(*expr, &Expectation::none())?;
|
||||||
let ty = match receiver_ty {
|
let ty = receiver_ty
|
||||||
Ty::Tuple(fields) => {
|
.autoderef(self.db)
|
||||||
let i = name.to_string().parse::<usize>().ok();
|
.find_map(|derefed_ty| match derefed_ty {
|
||||||
i.and_then(|i| fields.get(i).cloned())
|
// this is more complicated than necessary because type_for_field is cancelable
|
||||||
.unwrap_or(Ty::Unknown)
|
Ty::Tuple(fields) => {
|
||||||
}
|
let i = name.to_string().parse::<usize>().ok();
|
||||||
Ty::Adt { def_id, .. } => self.db.type_for_field(def_id, name.clone())?,
|
i.and_then(|i| fields.get(i).cloned()).map(Ok)
|
||||||
_ => Ty::Unknown,
|
}
|
||||||
};
|
Ty::Adt { def_id, .. } => {
|
||||||
|
transpose(self.db.type_for_field(def_id, name.clone()))
|
||||||
|
}
|
||||||
|
_ => None,
|
||||||
|
})
|
||||||
|
.unwrap_or(Ok(Ty::Unknown))?;
|
||||||
self.insert_type_vars(ty)
|
self.insert_type_vars(ty)
|
||||||
}
|
}
|
||||||
Expr::Try { expr } => {
|
Expr::Try { expr } => {
|
||||||
|
@ -848,12 +877,11 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
|
||||||
let inner_ty = self.infer_expr(*expr, &Expectation::none())?;
|
let inner_ty = self.infer_expr(*expr, &Expectation::none())?;
|
||||||
match op {
|
match op {
|
||||||
Some(UnaryOp::Deref) => {
|
Some(UnaryOp::Deref) => {
|
||||||
match inner_ty {
|
if let Some(derefed_ty) = inner_ty.builtin_deref() {
|
||||||
// builtin deref:
|
derefed_ty
|
||||||
Ty::Ref(ref_inner, _) => (*ref_inner).clone(),
|
} else {
|
||||||
Ty::RawPtr(ptr_inner, _) => (*ptr_inner).clone(),
|
|
||||||
// TODO Deref::deref
|
// TODO Deref::deref
|
||||||
_ => Ty::Unknown,
|
Ty::Unknown
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => Ty::Unknown,
|
_ => Ty::Unknown,
|
||||||
|
|
21
crates/ra_hir/src/ty/autoderef.rs
Normal file
21
crates/ra_hir/src/ty/autoderef.rs
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
//! In certain situations, rust automatically inserts derefs as necessary: For
|
||||||
|
//! example, field accesses `foo.bar` still work when `foo` is actually a
|
||||||
|
//! reference to a type with the field `bar`. This is an approximation of the
|
||||||
|
//! logic in rustc (which lives in librustc_typeck/check/autoderef.rs).
|
||||||
|
|
||||||
|
use ra_syntax::algo::generate;
|
||||||
|
|
||||||
|
use crate::HirDatabase;
|
||||||
|
use super::Ty;
|
||||||
|
|
||||||
|
impl Ty {
|
||||||
|
/// Iterates over the possible derefs of `ty`.
|
||||||
|
pub fn autoderef<'a>(self, db: &'a impl HirDatabase) -> impl Iterator<Item = Ty> + 'a {
|
||||||
|
generate(Some(self), move |ty| ty.autoderef_step(db))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn autoderef_step(&self, _db: &impl HirDatabase) -> Option<Ty> {
|
||||||
|
// TODO Deref::deref
|
||||||
|
self.builtin_deref()
|
||||||
|
}
|
||||||
|
}
|
|
@ -95,7 +95,7 @@ fn test() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn infer_refs_and_ptrs() {
|
fn infer_refs() {
|
||||||
check_inference(
|
check_inference(
|
||||||
r#"
|
r#"
|
||||||
fn test(a: &u32, b: &mut u32, c: *const u32, d: *mut u32) {
|
fn test(a: &u32, b: &mut u32, c: *const u32, d: *mut u32) {
|
||||||
|
@ -180,6 +180,37 @@ fn test() {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn infer_field_autoderef() {
|
||||||
|
check_inference(
|
||||||
|
r#"
|
||||||
|
struct A {
|
||||||
|
b: B,
|
||||||
|
}
|
||||||
|
struct B;
|
||||||
|
|
||||||
|
fn test1(a: A) {
|
||||||
|
let a1 = a;
|
||||||
|
a1.b;
|
||||||
|
let a2 = &a;
|
||||||
|
a2.b;
|
||||||
|
let a3 = &mut a;
|
||||||
|
a3.b;
|
||||||
|
let a4 = &&&&&&&a;
|
||||||
|
a4.b;
|
||||||
|
let a5 = &mut &&mut &&mut a;
|
||||||
|
a5.b;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test2(a1: *const A, a2: *mut A) {
|
||||||
|
a1.b;
|
||||||
|
a2.b;
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
"field_autoderef.txt",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
fn infer(content: &str) -> String {
|
fn infer(content: &str) -> String {
|
||||||
let (db, _, file_id) = MockDatabase::with_single_file(content);
|
let (db, _, file_id) = MockDatabase::with_single_file(content);
|
||||||
let source_file = db.source_file(file_id);
|
let source_file = db.source_file(file_id);
|
||||||
|
|
43
crates/ra_hir/src/ty/tests/data/field_autoderef.txt
Normal file
43
crates/ra_hir/src/ty/tests/data/field_autoderef.txt
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
[44; 45) 'a': A
|
||||||
|
[50; 213) '{ ...5.b; }': ()
|
||||||
|
[60; 62) 'a1': A
|
||||||
|
[65; 66) 'a': A
|
||||||
|
[72; 74) 'a1': A
|
||||||
|
[72; 76) 'a1.b': B
|
||||||
|
[86; 88) 'a2': &A
|
||||||
|
[91; 93) '&a': &A
|
||||||
|
[92; 93) 'a': A
|
||||||
|
[99; 101) 'a2': &A
|
||||||
|
[99; 103) 'a2.b': B
|
||||||
|
[113; 115) 'a3': &mut A
|
||||||
|
[118; 124) '&mut a': &mut A
|
||||||
|
[123; 124) 'a': A
|
||||||
|
[130; 132) 'a3': &mut A
|
||||||
|
[130; 134) 'a3.b': B
|
||||||
|
[144; 146) 'a4': &&&&&&&A
|
||||||
|
[149; 157) '&&&&&&&a': &&&&&&&A
|
||||||
|
[150; 157) '&&&&&&a': &&&&&&A
|
||||||
|
[151; 157) '&&&&&a': &&&&&A
|
||||||
|
[152; 157) '&&&&a': &&&&A
|
||||||
|
[153; 157) '&&&a': &&&A
|
||||||
|
[154; 157) '&&a': &&A
|
||||||
|
[155; 157) '&a': &A
|
||||||
|
[156; 157) 'a': A
|
||||||
|
[163; 165) 'a4': &&&&&&&A
|
||||||
|
[163; 167) 'a4.b': B
|
||||||
|
[177; 179) 'a5': &mut &&mut &&mut A
|
||||||
|
[182; 200) '&mut &...&mut a': &mut &&mut &&mut A
|
||||||
|
[187; 200) '&&mut &&mut a': &&mut &&mut A
|
||||||
|
[188; 200) '&mut &&mut a': &mut &&mut A
|
||||||
|
[193; 200) '&&mut a': &&mut A
|
||||||
|
[194; 200) '&mut a': &mut A
|
||||||
|
[199; 200) 'a': A
|
||||||
|
[206; 208) 'a5': &mut &&mut &&mut A
|
||||||
|
[206; 210) 'a5.b': B
|
||||||
|
[224; 226) 'a1': *const A
|
||||||
|
[238; 240) 'a2': *mut A
|
||||||
|
[250; 273) '{ ...2.b; }': ()
|
||||||
|
[256; 258) 'a1': *const A
|
||||||
|
[256; 260) 'a1.b': B
|
||||||
|
[266; 268) 'a2': *mut A
|
||||||
|
[266; 270) 'a2.b': B
|
Loading…
Reference in a new issue