Skip to content

Commit

Permalink
Add static method tactic
Browse files Browse the repository at this point in the history
  • Loading branch information
kilpkonn committed Dec 18, 2023
1 parent b00ff5f commit 4da835f
Showing 8 changed files with 569 additions and 93 deletions.
39 changes: 24 additions & 15 deletions crates/hir-def/src/attr.rs
Original file line number Diff line number Diff line change
@@ -418,27 +418,36 @@ impl AttrsWithOwner {
AttrDefId::GenericParamId(it) => match it {
GenericParamId::ConstParamId(it) => {
let src = it.parent().child_source(db);
RawAttrs::from_attrs_owner(
db.upcast(),
src.with_value(&src.value[it.local_id()]),
db.span_map(src.file_id).as_ref(),
)
match src.value.get(it.local_id()) {
Some(val) => RawAttrs::from_attrs_owner(
db.upcast(),
src.with_value(val),
db.span_map(src.file_id).as_ref(),
),
None => RawAttrs::EMPTY,
}
}
GenericParamId::TypeParamId(it) => {
let src = it.parent().child_source(db);
RawAttrs::from_attrs_owner(
db.upcast(),
src.with_value(&src.value[it.local_id()]),
db.span_map(src.file_id).as_ref(),
)
match src.value.get(it.local_id()) {
Some(val) => RawAttrs::from_attrs_owner(
db.upcast(),
src.with_value(val),
db.span_map(src.file_id).as_ref(),
),
None => RawAttrs::EMPTY,
}
}
GenericParamId::LifetimeParamId(it) => {
let src = it.parent.child_source(db);
RawAttrs::from_attrs_owner(
db.upcast(),
src.with_value(&src.value[it.local_id]),
db.span_map(src.file_id).as_ref(),
)
match src.value.get(it.local_id) {
Some(val) => RawAttrs::from_attrs_owner(
db.upcast(),
src.with_value(val),
db.span_map(src.file_id).as_ref(),
),
None => RawAttrs::EMPTY,
}
}
},
AttrDefId::ExternBlockId(it) => attrs_from_item_tree_loc(db, it),
102 changes: 100 additions & 2 deletions crates/hir/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1170,6 +1170,10 @@ impl Struct {
fn variant_data(self, db: &dyn HirDatabase) -> Arc<VariantData> {
db.struct_data(self.id).variant_data.clone()
}

pub fn is_unstable(self, db: &dyn HirDatabase) -> bool {
db.attrs(self.id.into()).is_unstable()
}
}

impl HasVisibility for Struct {
@@ -1212,6 +1216,10 @@ impl Union {
fn variant_data(self, db: &dyn HirDatabase) -> Arc<VariantData> {
db.union_data(self.id).variant_data.clone()
}

pub fn is_unstable(self, db: &dyn HirDatabase) -> bool {
db.attrs(self.id.into()).is_unstable()
}
}

impl HasVisibility for Union {
@@ -1301,6 +1309,10 @@ impl Enum {
pub fn layout(self, db: &dyn HirDatabase) -> Result<Layout, LayoutError> {
Adt::from(self).layout(db)
}

pub fn is_unstable(self, db: &dyn HirDatabase) -> bool {
db.attrs(self.id.into()).is_unstable()
}
}

impl HasVisibility for Enum {
@@ -1373,6 +1385,10 @@ impl Variant {
_ => parent_layout,
})
}

pub fn is_unstable(self, db: &dyn HirDatabase) -> bool {
db.variants_attrs(self.parent.into())[self.id].is_unstable()
}
}

/// Variants inherit visibility from the parent enum.
@@ -3081,7 +3097,7 @@ impl GenericDef {
.collect()
}

pub fn type_params(self, db: &dyn HirDatabase) -> Vec<TypeOrConstParam> {
pub fn type_or_const_params(self, db: &dyn HirDatabase) -> Vec<TypeOrConstParam> {
let generics = db.generic_params(self.into());
generics
.type_or_consts
@@ -3091,6 +3107,40 @@ impl GenericDef {
})
.collect()
}

pub fn type_params(self, db: &dyn HirDatabase) -> Vec<TypeParam> {
let generics = db.generic_params(self.into());
generics
.type_or_consts
.iter()
.filter_map(|(local_id, data)| match data {
hir_def::generics::TypeOrConstParamData::TypeParamData(_) => Some(TypeParam {
id: TypeParamId::from_unchecked(TypeOrConstParamId {
parent: self.into(),
local_id,
}),
}),
hir_def::generics::TypeOrConstParamData::ConstParamData(_) => None,
})
.collect()
}

pub fn const_params(self, db: &dyn HirDatabase) -> Vec<ConstParam> {
let generics = db.generic_params(self.into());
generics
.type_or_consts
.iter()
.filter_map(|(local_id, data)| match data {
hir_def::generics::TypeOrConstParamData::TypeParamData(_) => None,
hir_def::generics::TypeOrConstParamData::ConstParamData(_) => Some(ConstParam {
id: ConstParamId::from_unchecked(TypeOrConstParamId {
parent: self.into(),
local_id,
}),
}),
})
.collect()
}
}

/// A single local definition.
@@ -3452,12 +3502,16 @@ impl TypeParam {
let ty = generic_arg_from_param(db, self.id.into())?;
let resolver = self.id.parent().resolver(db.upcast());
match ty.data(Interner) {
GenericArgData::Ty(it) => {
GenericArgData::Ty(it) if *it.kind(Interner) != TyKind::Error => {
Some(Type::new_with_resolver_inner(db, &resolver, it.clone()))
}
_ => None,
}
}

pub fn is_unstable(self, db: &dyn HirDatabase) -> bool {
db.attrs(GenericParamId::from(self.id).into()).is_unstable()
}
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
@@ -3940,6 +3994,50 @@ impl Type {
matches!(self.ty.kind(Interner), TyKind::Ref(..))
}

pub fn contains_reference(&self, db: &dyn HirDatabase) -> bool {
return go(db, self.env.krate, &self.ty);

fn go(db: &dyn HirDatabase, krate: CrateId, ty: &Ty) -> bool {
match ty.kind(Interner) {
// Reference itself
TyKind::Ref(_, _, _) => true,

// For non-phantom_data adts we check variants/fields as well as generic parameters
TyKind::Adt(adt_id, substitution)
if !db.struct_datum(krate, *adt_id).flags.phantom_data =>
{
let adt_datum = &db.struct_datum(krate, *adt_id);
let adt_datum_bound =
adt_datum.binders.clone().substitute(Interner, substitution);
adt_datum_bound
.variants
.into_iter()
.flat_map(|variant| variant.fields.into_iter())
.any(|ty| go(db, krate, &ty))
|| substitution
.iter(Interner)
.filter_map(|x| x.ty(Interner))
.any(|ty| go(db, krate, ty))
}
// And for `PhantomData<T>`, we check `T`.
TyKind::Adt(_, substitution)
| TyKind::Tuple(_, substitution)
| TyKind::OpaqueType(_, substitution)
| TyKind::AssociatedType(_, substitution)
| TyKind::FnDef(_, substitution) => substitution
.iter(Interner)
.filter_map(|x| x.ty(Interner))
.any(|ty| go(db, krate, ty)),

// For `[T]` or `*T` we check `T`
TyKind::Array(ty, _) | TyKind::Slice(ty) | TyKind::Raw(_, ty) => go(db, krate, ty),

// Consider everything else as not reference
_ => false,
}
}
}

pub fn as_reference(&self) -> Option<(Type, Mutability)> {
let (ty, _lt, m) = self.ty.as_reference()?;
let m = Mutability::from_mutable(matches!(m, hir_ty::Mutability::Mut));
7 changes: 7 additions & 0 deletions crates/hir/src/term_search/mod.rs
Original file line number Diff line number Diff line change
@@ -47,6 +47,8 @@ struct LookupTable {
round_scopedef_hits: FxHashSet<ScopeDef>,
/// Amount of rounds since scopedef was first used.
rounds_since_sopedef_hit: FxHashMap<ScopeDef, u32>,
/// Types queried but not present
types_wishlist: FxHashSet<Type>,
}

impl LookupTable {
@@ -149,6 +151,10 @@ impl LookupTable {
fn exhausted_scopedefs(&self) -> &FxHashSet<ScopeDef> {
&self.exhausted_scopedefs
}

fn take_types_wishlist(&mut self) -> FxHashSet<Type> {
std::mem::take(&mut self.types_wishlist)
}
}

/// # Term search
@@ -205,6 +211,7 @@ pub fn term_search<DB: HirDatabase>(
solutions.extend(tactics::free_function(sema.db, &module, &defs, &mut lookup, goal));
solutions.extend(tactics::impl_method(sema.db, &module, &defs, &mut lookup, goal));
solutions.extend(tactics::struct_projection(sema.db, &module, &defs, &mut lookup, goal));
solutions.extend(tactics::impl_static_method(sema.db, &module, &defs, &mut lookup, goal));

// Break after 1 round after successful solution
if solution_found {
Loading

0 comments on commit 4da835f

Please sign in to comment.