Skip to content

Commit e119cad

Browse files
committed
Add static method tactic
1 parent 3cb7b22 commit e119cad

File tree

8 files changed

+569
-93
lines changed

8 files changed

+569
-93
lines changed

crates/hir-def/src/attr.rs

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -382,27 +382,36 @@ impl AttrsWithOwner {
382382
AttrDefId::GenericParamId(it) => match it {
383383
GenericParamId::ConstParamId(it) => {
384384
let src = it.parent().child_source(db);
385-
RawAttrs::from_attrs_owner(
386-
db.upcast(),
387-
src.with_value(&src.value[it.local_id()]),
388-
db.span_map(src.file_id).as_ref(),
389-
)
385+
match src.value.get(it.local_id()) {
386+
Some(val) => RawAttrs::from_attrs_owner(
387+
db.upcast(),
388+
src.with_value(val),
389+
db.span_map(src.file_id).as_ref(),
390+
),
391+
None => RawAttrs::EMPTY,
392+
}
390393
}
391394
GenericParamId::TypeParamId(it) => {
392395
let src = it.parent().child_source(db);
393-
RawAttrs::from_attrs_owner(
394-
db.upcast(),
395-
src.with_value(&src.value[it.local_id()]),
396-
db.span_map(src.file_id).as_ref(),
397-
)
396+
match src.value.get(it.local_id()) {
397+
Some(val) => RawAttrs::from_attrs_owner(
398+
db.upcast(),
399+
src.with_value(val),
400+
db.span_map(src.file_id).as_ref(),
401+
),
402+
None => RawAttrs::EMPTY,
403+
}
398404
}
399405
GenericParamId::LifetimeParamId(it) => {
400406
let src = it.parent.child_source(db);
401-
RawAttrs::from_attrs_owner(
402-
db.upcast(),
403-
src.with_value(&src.value[it.local_id]),
404-
db.span_map(src.file_id).as_ref(),
405-
)
407+
match src.value.get(it.local_id) {
408+
Some(val) => RawAttrs::from_attrs_owner(
409+
db.upcast(),
410+
src.with_value(val),
411+
db.span_map(src.file_id).as_ref(),
412+
),
413+
None => RawAttrs::EMPTY,
414+
}
406415
}
407416
},
408417
AttrDefId::ExternBlockId(it) => attrs_from_item_tree_loc(db, it),

crates/hir/src/lib.rs

Lines changed: 100 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,6 +1186,10 @@ impl Struct {
11861186
fn variant_data(self, db: &dyn HirDatabase) -> Arc<VariantData> {
11871187
db.struct_data(self.id).variant_data.clone()
11881188
}
1189+
1190+
pub fn is_unstable(self, db: &dyn HirDatabase) -> bool {
1191+
db.attrs(self.id.into()).is_unstable()
1192+
}
11891193
}
11901194

11911195
impl HasVisibility for Struct {
@@ -1228,6 +1232,10 @@ impl Union {
12281232
fn variant_data(self, db: &dyn HirDatabase) -> Arc<VariantData> {
12291233
db.union_data(self.id).variant_data.clone()
12301234
}
1235+
1236+
pub fn is_unstable(self, db: &dyn HirDatabase) -> bool {
1237+
db.attrs(self.id.into()).is_unstable()
1238+
}
12311239
}
12321240

12331241
impl HasVisibility for Union {
@@ -1317,6 +1325,10 @@ impl Enum {
13171325
pub fn layout(self, db: &dyn HirDatabase) -> Result<Layout, LayoutError> {
13181326
Adt::from(self).layout(db)
13191327
}
1328+
1329+
pub fn is_unstable(self, db: &dyn HirDatabase) -> bool {
1330+
db.attrs(self.id.into()).is_unstable()
1331+
}
13201332
}
13211333

13221334
impl HasVisibility for Enum {
@@ -1392,6 +1404,10 @@ impl Variant {
13921404
_ => parent_layout,
13931405
})
13941406
}
1407+
1408+
pub fn is_unstable(self, db: &dyn HirDatabase) -> bool {
1409+
db.attrs(self.id.into()).is_unstable()
1410+
}
13951411
}
13961412

13971413
/// Variants inherit visibility from the parent enum.
@@ -2900,7 +2916,7 @@ impl GenericDef {
29002916
.collect()
29012917
}
29022918

2903-
pub fn type_params(self, db: &dyn HirDatabase) -> Vec<TypeOrConstParam> {
2919+
pub fn type_or_const_params(self, db: &dyn HirDatabase) -> Vec<TypeOrConstParam> {
29042920
let generics = db.generic_params(self.into());
29052921
generics
29062922
.type_or_consts
@@ -2910,6 +2926,40 @@ impl GenericDef {
29102926
})
29112927
.collect()
29122928
}
2929+
2930+
pub fn type_params(self, db: &dyn HirDatabase) -> Vec<TypeParam> {
2931+
let generics = db.generic_params(self.into());
2932+
generics
2933+
.type_or_consts
2934+
.iter()
2935+
.filter_map(|(local_id, data)| match data {
2936+
hir_def::generics::TypeOrConstParamData::TypeParamData(_) => Some(TypeParam {
2937+
id: TypeParamId::from_unchecked(TypeOrConstParamId {
2938+
parent: self.into(),
2939+
local_id,
2940+
}),
2941+
}),
2942+
hir_def::generics::TypeOrConstParamData::ConstParamData(_) => None,
2943+
})
2944+
.collect()
2945+
}
2946+
2947+
pub fn const_params(self, db: &dyn HirDatabase) -> Vec<ConstParam> {
2948+
let generics = db.generic_params(self.into());
2949+
generics
2950+
.type_or_consts
2951+
.iter()
2952+
.filter_map(|(local_id, data)| match data {
2953+
hir_def::generics::TypeOrConstParamData::TypeParamData(_) => None,
2954+
hir_def::generics::TypeOrConstParamData::ConstParamData(_) => Some(ConstParam {
2955+
id: ConstParamId::from_unchecked(TypeOrConstParamId {
2956+
parent: self.into(),
2957+
local_id,
2958+
}),
2959+
}),
2960+
})
2961+
.collect()
2962+
}
29132963
}
29142964

29152965
/// A single local definition.
@@ -3272,12 +3322,16 @@ impl TypeParam {
32723322
let ty = generic_arg_from_param(db, self.id.into())?;
32733323
let resolver = self.id.parent().resolver(db.upcast());
32743324
match ty.data(Interner) {
3275-
GenericArgData::Ty(it) => {
3325+
GenericArgData::Ty(it) if *it.kind(Interner) != TyKind::Error => {
32763326
Some(Type::new_with_resolver_inner(db, &resolver, it.clone()))
32773327
}
32783328
_ => None,
32793329
}
32803330
}
3331+
3332+
pub fn is_unstable(self, db: &dyn HirDatabase) -> bool {
3333+
db.attrs(GenericParamId::from(self.id).into()).is_unstable()
3334+
}
32813335
}
32823336

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

3818+
pub fn contains_reference(&self, db: &dyn HirDatabase) -> bool {
3819+
return go(db, self.env.krate, &self.ty);
3820+
3821+
fn go(db: &dyn HirDatabase, krate: CrateId, ty: &Ty) -> bool {
3822+
match ty.kind(Interner) {
3823+
// Reference itself
3824+
TyKind::Ref(_, _, _) => true,
3825+
3826+
// For non-phantom_data adts we check variants/fields as well as generic parameters
3827+
TyKind::Adt(adt_id, substitution)
3828+
if !db.struct_datum(krate, *adt_id).flags.phantom_data =>
3829+
{
3830+
let adt_datum = &db.struct_datum(krate, *adt_id);
3831+
let adt_datum_bound =
3832+
adt_datum.binders.clone().substitute(Interner, substitution);
3833+
adt_datum_bound
3834+
.variants
3835+
.into_iter()
3836+
.flat_map(|variant| variant.fields.into_iter())
3837+
.any(|ty| go(db, krate, &ty))
3838+
|| substitution
3839+
.iter(Interner)
3840+
.filter_map(|x| x.ty(Interner))
3841+
.any(|ty| go(db, krate, ty))
3842+
}
3843+
// And for `PhantomData<T>`, we check `T`.
3844+
TyKind::Adt(_, substitution)
3845+
| TyKind::Tuple(_, substitution)
3846+
| TyKind::OpaqueType(_, substitution)
3847+
| TyKind::AssociatedType(_, substitution)
3848+
| TyKind::FnDef(_, substitution) => substitution
3849+
.iter(Interner)
3850+
.filter_map(|x| x.ty(Interner))
3851+
.any(|ty| go(db, krate, ty)),
3852+
3853+
// For `[T]` or `*T` we check `T`
3854+
TyKind::Array(ty, _) | TyKind::Slice(ty) | TyKind::Raw(_, ty) => go(db, krate, ty),
3855+
3856+
// Consider everything else as not reference
3857+
_ => false,
3858+
}
3859+
}
3860+
}
3861+
37643862
pub fn as_reference(&self) -> Option<(Type, Mutability)> {
37653863
let (ty, _lt, m) = self.ty.as_reference()?;
37663864
let m = Mutability::from_mutable(matches!(m, hir_ty::Mutability::Mut));

crates/hir/src/term_search/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ struct LookupTable {
4747
round_scopedef_hits: FxHashSet<ScopeDef>,
4848
/// Amount of rounds since scopedef was first used.
4949
rounds_since_sopedef_hit: FxHashMap<ScopeDef, u32>,
50+
/// Types queried but not present
51+
types_wishlist: FxHashSet<Type>,
5052
}
5153

5254
impl LookupTable {
@@ -149,6 +151,10 @@ impl LookupTable {
149151
fn exhausted_scopedefs(&self) -> &FxHashSet<ScopeDef> {
150152
&self.exhausted_scopedefs
151153
}
154+
155+
fn take_types_wishlist(&mut self) -> FxHashSet<Type> {
156+
std::mem::take(&mut self.types_wishlist)
157+
}
152158
}
153159

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

209216
// Break after 1 round after successful solution
210217
if solution_found {

0 commit comments

Comments
 (0)