[ty] Add type information to all_members API

Since we generally need (so far) to get the type information of each
suggestion to figure out its boundness anyway, we might as well expose
it here. Completions want to use this information to enhance the
metadata on each suggestion for a more pleasant user experience.

For the most part, this was pretty straight-forward. The most exciting
part was in computing the types for instance attributes. I'm not 100%
sure it's correct or is the best way to do it.
This commit is contained in:
Andrew Gallant 2025-07-08 14:43:50 -04:00 committed by Andrew Gallant
parent 79fe538458
commit fea84e8777
4 changed files with 269 additions and 102 deletions

View file

@ -73,7 +73,7 @@ impl<'db> SemanticModel<'db> {
.into_iter()
.map(|member| Completion {
name: member.name,
ty: None,
ty: member.ty,
builtin,
})
.collect()
@ -86,7 +86,7 @@ impl<'db> SemanticModel<'db> {
.into_iter()
.map(|member| Completion {
name: member.name,
ty: None,
ty: member.ty,
builtin: false,
})
.collect()
@ -122,7 +122,7 @@ impl<'db> SemanticModel<'db> {
all_declarations_and_bindings(self.db, file_scope.to_scope_id(self.db, self.file))
.map(|member| Completion {
name: member.name,
ty: None,
ty: member.ty,
builtin: false,
}),
);
@ -172,8 +172,8 @@ impl NameKind {
pub struct Completion<'db> {
/// The label shown to the user for this suggestion.
pub name: Name,
/// The type of this completion, if available.
pub ty: Option<Type<'db>>,
/// The type of this completion.
pub ty: Type<'db>,
/// Whether this suggestion came from builtins or not.
///
/// At time of writing (2025-06-26), this information

View file

@ -2048,7 +2048,11 @@ impl<'db> ClassLiteral<'db> {
/// A helper function for `instance_member` that looks up the `name` attribute only on
/// this class, not on its superclasses.
fn own_instance_member(self, db: &'db dyn Db, name: &str) -> PlaceAndQualifiers<'db> {
pub(crate) fn own_instance_member(
self,
db: &'db dyn Db,
name: &str,
) -> PlaceAndQualifiers<'db> {
// TODO: There are many things that are not yet implemented here:
// - `typing.Final`
// - Proper diagnostics

View file

@ -1,3 +1,6 @@
use std::cmp::Ordering;
use crate::module_resolver::resolve_module;
use crate::place::{Place, imported_symbol, place_from_bindings, place_from_declarations};
use crate::semantic_index::definition::DefinitionKind;
use crate::semantic_index::place::ScopeId;
@ -14,7 +17,7 @@ use rustc_hash::FxHashSet;
pub(crate) fn all_declarations_and_bindings<'db>(
db: &'db dyn Db,
scope_id: ScopeId<'db>,
) -> impl Iterator<Item = Member> + 'db {
) -> impl Iterator<Item = Member<'db>> + 'db {
let use_def_map = use_def_map(db, scope_id);
let table = place_table(db, scope_id);
@ -24,12 +27,12 @@ pub(crate) fn all_declarations_and_bindings<'db>(
place_from_declarations(db, declarations)
.ok()
.and_then(|result| {
result.place.ignore_possibly_unbound().and_then(|_| {
result.place.ignore_possibly_unbound().and_then(|ty| {
table
.place_expr(symbol_id)
.as_name()
.cloned()
.map(|name| Member { name })
.map(|name| Member { name, ty })
})
})
})
@ -39,23 +42,23 @@ pub(crate) fn all_declarations_and_bindings<'db>(
.filter_map(move |(symbol_id, bindings)| {
place_from_bindings(db, bindings)
.ignore_possibly_unbound()
.and_then(|_| {
.and_then(|ty| {
table
.place_expr(symbol_id)
.as_name()
.cloned()
.map(|name| Member { name })
.map(|name| Member { name, ty })
})
}),
)
}
struct AllMembers {
members: FxHashSet<Member>,
struct AllMembers<'db> {
members: FxHashSet<Member<'db>>,
}
impl AllMembers {
fn of<'db>(db: &'db dyn Db, ty: Type<'db>) -> Self {
impl<'db> AllMembers<'db> {
fn of(db: &'db dyn Db, ty: Type<'db>) -> Self {
let mut all_members = Self {
members: FxHashSet::default(),
};
@ -63,7 +66,7 @@ impl AllMembers {
all_members
}
fn extend_with_type<'db>(&mut self, db: &'db dyn Db, ty: Type<'db>) {
fn extend_with_type(&mut self, db: &'db dyn Db, ty: Type<'db>) {
match ty {
Type::Union(union) => self.members.extend(
union
@ -85,7 +88,6 @@ impl AllMembers {
Type::NominalInstance(instance) => {
let (class_literal, _specialization) = instance.class.class_literal(db);
self.extend_with_class_members(db, class_literal);
self.extend_with_instance_members(db, class_literal);
}
@ -191,6 +193,7 @@ impl AllMembers {
self.members.insert(Member {
name: place_table.place_expr(symbol_id).expect_name().clone(),
ty,
});
}
@ -198,71 +201,133 @@ impl AllMembers {
self.members.extend(
imported_modules(db, literal.importing_file(db))
.iter()
.filter_map(|submodule_name| submodule_name.relative_to(module_name))
.filter_map(|relative_submodule_name| {
Some(Member {
name: Name::from(relative_submodule_name.components().next()?),
})
.filter_map(|submodule_name| {
let module = resolve_module(db, submodule_name)?;
let ty = Type::module_literal(db, file, &module);
Some((submodule_name, ty))
})
.filter_map(|(submodule_name, ty)| {
let relative = submodule_name.relative_to(module_name)?;
Some((relative, ty))
})
.filter_map(|(relative_submodule_name, ty)| {
let name = Name::from(relative_submodule_name.components().next()?);
Some(Member { name, ty })
}),
);
}
}
}
fn extend_with_declarations_and_bindings(&mut self, db: &dyn Db, scope_id: ScopeId) {
self.members
.extend(all_declarations_and_bindings(db, scope_id));
}
fn extend_with_class_members<'db>(
&mut self,
db: &'db dyn Db,
class_literal: ClassLiteral<'db>,
) {
fn extend_with_class_members(&mut self, db: &'db dyn Db, class_literal: ClassLiteral<'db>) {
for parent in class_literal
.iter_mro(db, None)
.filter_map(ClassBase::into_class)
.map(|class| class.class_literal(db).0)
{
let parent_ty = Type::ClassLiteral(parent);
let parent_scope = parent.body_scope(db);
self.extend_with_declarations_and_bindings(db, parent_scope);
for Member { name, .. } in all_declarations_and_bindings(db, parent_scope) {
let result = parent_ty.member(db, name.as_str());
let Some(ty) = result.place.ignore_possibly_unbound() else {
continue;
};
self.members.insert(Member { name, ty });
}
}
}
fn extend_with_instance_members<'db>(
&mut self,
db: &'db dyn Db,
class_literal: ClassLiteral<'db>,
) {
fn extend_with_instance_members(&mut self, db: &'db dyn Db, class_literal: ClassLiteral<'db>) {
for parent in class_literal
.iter_mro(db, None)
.filter_map(ClassBase::into_class)
.map(|class| class.class_literal(db).0)
{
let parent_instance = Type::instance(db, parent.default_specialization(db));
let class_body_scope = parent.body_scope(db);
let file = class_body_scope.file(db);
let index = semantic_index(db, file);
for function_scope_id in attribute_scopes(db, class_body_scope) {
let place_table = index.place_table(function_scope_id);
self.members.extend(
place_table
.instance_attributes()
.cloned()
.map(|name| Member { name }),
);
for place_expr in place_table.places() {
let Some(name) = place_expr.as_instance_attribute() else {
continue;
};
let result = parent_instance.member(db, name.as_str());
let Some(ty) = result.place.ignore_possibly_unbound() else {
continue;
};
self.members.insert(Member {
name: name.clone(),
ty,
});
}
}
// This is very similar to `extend_with_class_members`,
// but uses the type of the class instance to query the
// class member. This gets us the right type for each
// member, e.g., `SomeClass.__delattr__` is not a bound
// method, but `instance_of_SomeClass.__delattr__` is.
for Member { name, .. } in all_declarations_and_bindings(db, class_body_scope) {
let result = parent_instance.member(db, name.as_str());
let Some(ty) = result.place.ignore_possibly_unbound() else {
continue;
};
self.members.insert(Member { name, ty });
}
}
}
}
#[derive(Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub struct Member {
/// A member of a type.
///
/// This represents a single item in (ideally) the list returned by
/// `dir(object)`.
///
/// The equality, comparison and hashing traits implemented for
/// this type are done so by taking only the name into account. At
/// present, this is because we assume the name is enough to uniquely
/// identify each attribute on an object. This is perhaps complicated
/// by overloads, but they only get represented by one member for
/// now. Moreover, it is convenient to be able to sort collections of
/// members, and a `Type` currently (as of 2025-07-09) has no way to do
/// ordered comparisons.
#[derive(Clone, Debug)]
pub struct Member<'db> {
pub name: Name,
pub ty: Type<'db>,
}
impl std::hash::Hash for Member<'_> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.name.hash(state);
}
}
impl Eq for Member<'_> {}
impl<'db> PartialEq for Member<'db> {
fn eq(&self, rhs: &Member<'db>) -> bool {
self.name == rhs.name
}
}
impl<'db> Ord for Member<'db> {
fn cmp(&self, rhs: &Member<'db>) -> Ordering {
self.name.cmp(&rhs.name)
}
}
impl<'db> PartialOrd for Member<'db> {
fn partial_cmp(&self, rhs: &Member<'db>) -> Option<Ordering> {
Some(self.cmp(rhs))
}
}
/// List all members of a given type: anything that would be valid when accessed
/// as an attribute on an object of the given type.
pub fn all_members<'db>(db: &'db dyn Db, ty: Type<'db>) -> FxHashSet<Member> {
pub fn all_members<'db>(db: &'db dyn Db, ty: Type<'db>) -> FxHashSet<Member<'db>> {
AllMembers::of(db, ty).members
}