mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 11:31:15 +00:00
Merge #11540
11540: fix: Resolve private fields in type inference r=flodiebold a=Veykril Fixes https://github.com/rust-analyzer/rust-analyzer/issues/10253#issuecomment-920962927 (the same issue probably exists for method calls, but I think fixing that might be trickier) Visibility checks were introduced in https://github.com/rust-analyzer/rust-analyzer/issues/7841 for autoderef to work properly, so now we just record the first field we find unconditionally, and then overwrite it if autoderef manages to find another field in a later cycle. Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
This commit is contained in:
commit
6b55d2f10a
@ -13,8 +13,8 @@
|
|||||||
//! to certain types. To record this, we use the union-find implementation from
|
//! to certain types. To record this, we use the union-find implementation from
|
||||||
//! the `ena` crate, which is extracted from rustc.
|
//! the `ena` crate, which is extracted from rustc.
|
||||||
|
|
||||||
use std::ops::Index;
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
use std::{collections::hash_map::Entry, ops::Index};
|
||||||
|
|
||||||
use chalk_ir::{cast::Cast, DebruijnIndex, Mutability, Safety, Scalar, TypeFlags};
|
use chalk_ir::{cast::Cast, DebruijnIndex, Mutability, Safety, Scalar, TypeFlags};
|
||||||
use hir_def::{
|
use hir_def::{
|
||||||
@ -459,6 +459,12 @@ impl<'a> InferenceContext<'a> {
|
|||||||
self.result.field_resolutions.insert(expr, field);
|
self.result.field_resolutions.insert(expr, field);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn write_field_resolution_if_empty(&mut self, expr: ExprId, field: FieldId) {
|
||||||
|
if let Entry::Vacant(entry) = self.result.field_resolutions.entry(expr) {
|
||||||
|
entry.insert(field);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn write_variant_resolution(&mut self, id: ExprOrPatId, variant: VariantId) {
|
fn write_variant_resolution(&mut self, id: ExprOrPatId, variant: VariantId) {
|
||||||
self.result.variant_resolutions.insert(id, variant);
|
self.result.variant_resolutions.insert(id, variant);
|
||||||
}
|
}
|
||||||
|
@ -532,6 +532,11 @@ impl<'a> InferenceContext<'a> {
|
|||||||
.substitute(Interner, ¶meters),
|
.substitute(Interner, ¶meters),
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
|
// Write down the first field resolution even if it is not visible
|
||||||
|
// This aids IDE features for private fields like goto def and in
|
||||||
|
// case of autoderef finding an applicable field, this will be
|
||||||
|
// overwritten in a following cycle
|
||||||
|
self.write_field_resolution_if_empty(tgt_expr, field);
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -546,6 +551,11 @@ impl<'a> InferenceContext<'a> {
|
|||||||
.substitute(Interner, ¶meters),
|
.substitute(Interner, ¶meters),
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
|
// Write down the first field resolution even if it is not visible
|
||||||
|
// This aids IDE features for private fields like goto def and in
|
||||||
|
// case of autoderef finding an applicable field, this will be
|
||||||
|
// overwritten in a following cycle
|
||||||
|
self.write_field_resolution_if_empty(tgt_expr, field);
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -206,8 +206,23 @@ impl NameClass {
|
|||||||
|
|
||||||
let parent = name.syntax().parent()?;
|
let parent = name.syntax().parent()?;
|
||||||
|
|
||||||
let def = if let Some(item) = ast::Item::cast(parent.clone()) {
|
let definition = match_ast! {
|
||||||
match item {
|
match parent {
|
||||||
|
ast::Item(it) => classify_item(sema, it)?,
|
||||||
|
ast::IdentPat(it) => return classify_ident_pat(sema, it),
|
||||||
|
ast::Rename(it) => classify_rename(sema, it)?,
|
||||||
|
ast::SelfParam(it) => Definition::Local(sema.to_def(&it)?),
|
||||||
|
ast::RecordField(it) => Definition::Field(sema.to_def(&it)?),
|
||||||
|
ast::Variant(it) => Definition::Variant(sema.to_def(&it)?),
|
||||||
|
ast::TypeParam(it) => Definition::GenericParam(sema.to_def(&it)?.into()),
|
||||||
|
ast::ConstParam(it) => Definition::GenericParam(sema.to_def(&it)?.into()),
|
||||||
|
_ => return None,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
return Some(NameClass::Definition(definition));
|
||||||
|
|
||||||
|
fn classify_item(sema: &Semantics<RootDatabase>, item: ast::Item) -> Option<Definition> {
|
||||||
|
let definition = match item {
|
||||||
ast::Item::MacroRules(it) => {
|
ast::Item::MacroRules(it) => {
|
||||||
Definition::Macro(sema.to_def(&ast::Macro::MacroRules(it))?)
|
Definition::Macro(sema.to_def(&ast::Macro::MacroRules(it))?)
|
||||||
}
|
}
|
||||||
@ -229,14 +244,20 @@ impl NameClass {
|
|||||||
ast::Item::Struct(it) => Definition::Adt(hir::Adt::Struct(sema.to_def(&it)?)),
|
ast::Item::Struct(it) => Definition::Adt(hir::Adt::Struct(sema.to_def(&it)?)),
|
||||||
ast::Item::Union(it) => Definition::Adt(hir::Adt::Union(sema.to_def(&it)?)),
|
ast::Item::Union(it) => Definition::Adt(hir::Adt::Union(sema.to_def(&it)?)),
|
||||||
_ => return None,
|
_ => return None,
|
||||||
}
|
};
|
||||||
} else if let Some(it) = ast::IdentPat::cast(parent.clone()) {
|
Some(definition)
|
||||||
if let Some(def) = sema.resolve_bind_pat_to_const(&it) {
|
}
|
||||||
|
|
||||||
|
fn classify_ident_pat(
|
||||||
|
sema: &Semantics<RootDatabase>,
|
||||||
|
ident_pat: ast::IdentPat,
|
||||||
|
) -> Option<NameClass> {
|
||||||
|
if let Some(def) = sema.resolve_bind_pat_to_const(&ident_pat) {
|
||||||
return Some(NameClass::ConstReference(Definition::from(def)));
|
return Some(NameClass::ConstReference(Definition::from(def)));
|
||||||
}
|
}
|
||||||
|
|
||||||
let local = sema.to_def(&it)?;
|
let local = sema.to_def(&ident_pat)?;
|
||||||
let pat_parent = it.syntax().parent();
|
let pat_parent = ident_pat.syntax().parent();
|
||||||
if let Some(record_pat_field) = pat_parent.and_then(ast::RecordPatField::cast) {
|
if let Some(record_pat_field) = pat_parent.and_then(ast::RecordPatField::cast) {
|
||||||
if record_pat_field.name_ref().is_none() {
|
if record_pat_field.name_ref().is_none() {
|
||||||
if let Some(field) = sema.resolve_record_pat_field(&record_pat_field) {
|
if let Some(field) = sema.resolve_record_pat_field(&record_pat_field) {
|
||||||
@ -247,57 +268,23 @@ impl NameClass {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Some(NameClass::Definition(Definition::Local(local)))
|
||||||
|
}
|
||||||
|
|
||||||
Definition::Local(local)
|
fn classify_rename(
|
||||||
} else if let Some(it) = ast::Rename::cast(parent.clone()) {
|
sema: &Semantics<RootDatabase>,
|
||||||
if let Some(use_tree) = it.syntax().parent().and_then(ast::UseTree::cast) {
|
rename: ast::Rename,
|
||||||
|
) -> Option<Definition> {
|
||||||
|
if let Some(use_tree) = rename.syntax().parent().and_then(ast::UseTree::cast) {
|
||||||
let path = use_tree.path()?;
|
let path = use_tree.path()?;
|
||||||
let path_segment = path.segment()?;
|
sema.resolve_path(&path).map(Definition::from)
|
||||||
let name_ref = path_segment.name_ref()?;
|
|
||||||
let name_ref = if name_ref.self_token().is_some() {
|
|
||||||
use_tree
|
|
||||||
.syntax()
|
|
||||||
.parent()
|
|
||||||
.as_ref()
|
|
||||||
// Skip over UseTreeList
|
|
||||||
.and_then(|it| {
|
|
||||||
let use_tree = it.parent().and_then(ast::UseTree::cast)?;
|
|
||||||
let path = use_tree.path()?;
|
|
||||||
let path_segment = path.segment()?;
|
|
||||||
path_segment.name_ref()
|
|
||||||
})
|
|
||||||
.unwrap_or(name_ref)
|
|
||||||
} else {
|
|
||||||
name_ref
|
|
||||||
};
|
|
||||||
let name_ref_class = NameRefClass::classify(sema, &name_ref)?;
|
|
||||||
|
|
||||||
match name_ref_class {
|
|
||||||
NameRefClass::Definition(def) => def,
|
|
||||||
NameRefClass::FieldShorthand { local_ref: _, field_ref } => {
|
|
||||||
Definition::Field(field_ref)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
let extern_crate = it.syntax().parent().and_then(ast::ExternCrate::cast)?;
|
let extern_crate = rename.syntax().parent().and_then(ast::ExternCrate::cast)?;
|
||||||
let krate = sema.resolve_extern_crate(&extern_crate)?;
|
let krate = sema.resolve_extern_crate(&extern_crate)?;
|
||||||
let root_module = krate.root_module(sema.db);
|
let root_module = krate.root_module(sema.db);
|
||||||
Definition::Module(root_module)
|
Some(Definition::Module(root_module))
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
match_ast! {
|
|
||||||
match parent {
|
|
||||||
ast::SelfParam(it) => Definition::Local(sema.to_def(&it)?),
|
|
||||||
ast::RecordField(it) => Definition::Field(sema.to_def(&it)?),
|
|
||||||
ast::Variant(it) => Definition::Variant(sema.to_def(&it)?),
|
|
||||||
ast::TypeParam(it) => Definition::GenericParam(sema.to_def(&it)?.into()),
|
|
||||||
ast::ConstParam(it) => Definition::GenericParam(sema.to_def(&it)?.into()),
|
|
||||||
_ => return None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
Some(NameClass::Definition(def))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn classify_lifetime(
|
pub fn classify_lifetime(
|
||||||
@ -307,19 +294,14 @@ impl NameClass {
|
|||||||
let _p = profile::span("classify_lifetime").detail(|| lifetime.to_string());
|
let _p = profile::span("classify_lifetime").detail(|| lifetime.to_string());
|
||||||
let parent = lifetime.syntax().parent()?;
|
let parent = lifetime.syntax().parent()?;
|
||||||
|
|
||||||
match_ast! {
|
if let Some(it) = ast::LifetimeParam::cast(parent.clone()) {
|
||||||
match parent {
|
sema.to_def(&it).map(Into::into).map(Definition::GenericParam)
|
||||||
ast::LifetimeParam(it) => {
|
} else if let Some(it) = ast::Label::cast(parent.clone()) {
|
||||||
let def = sema.to_def(&it)?;
|
sema.to_def(&it).map(Definition::Label)
|
||||||
Some(NameClass::Definition(Definition::GenericParam(def.into())))
|
} else {
|
||||||
},
|
None
|
||||||
ast::Label(it) => {
|
|
||||||
let def = sema.to_def(&it)?;
|
|
||||||
Some(NameClass::Definition(Definition::Label(def)))
|
|
||||||
},
|
|
||||||
_ => None,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
.map(NameClass::Definition)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user