Skip to content

Commit 20ce4f6

Browse files
committed
Add static method tactic
1 parent 6db4b57 commit 20ce4f6

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
@@ -1190,6 +1190,10 @@ impl Struct {
11901190
fn variant_data(self, db: &dyn HirDatabase) -> Arc<VariantData> {
11911191
db.struct_data(self.id).variant_data.clone()
11921192
}
1193+
1194+
pub fn is_unstable(self, db: &dyn HirDatabase) -> bool {
1195+
db.attrs(self.id.into()).is_unstable()
1196+
}
11931197
}
11941198

11951199
impl HasVisibility for Struct {
@@ -1232,6 +1236,10 @@ impl Union {
12321236
fn variant_data(self, db: &dyn HirDatabase) -> Arc<VariantData> {
12331237
db.union_data(self.id).variant_data.clone()
12341238
}
1239+
1240+
pub fn is_unstable(self, db: &dyn HirDatabase) -> bool {
1241+
db.attrs(self.id.into()).is_unstable()
1242+
}
12351243
}
12361244

12371245
impl HasVisibility for Union {
@@ -1321,6 +1329,10 @@ impl Enum {
13211329
pub fn layout(self, db: &dyn HirDatabase) -> Result<Layout, LayoutError> {
13221330
Adt::from(self).layout(db)
13231331
}
1332+
1333+
pub fn is_unstable(self, db: &dyn HirDatabase) -> bool {
1334+
db.attrs(self.id.into()).is_unstable()
1335+
}
13241336
}
13251337

13261338
impl HasVisibility for Enum {
@@ -1396,6 +1408,10 @@ impl Variant {
13961408
_ => parent_layout,
13971409
})
13981410
}
1411+
1412+
pub fn is_unstable(self, db: &dyn HirDatabase) -> bool {
1413+
db.attrs(self.id.into()).is_unstable()
1414+
}
13991415
}
14001416

14011417
/// Variants inherit visibility from the parent enum.
@@ -3099,7 +3115,7 @@ impl GenericDef {
30993115
.collect()
31003116
}
31013117

3102-
pub fn type_params(self, db: &dyn HirDatabase) -> Vec<TypeOrConstParam> {
3118+
pub fn type_or_const_params(self, db: &dyn HirDatabase) -> Vec<TypeOrConstParam> {
31033119
let generics = db.generic_params(self.into());
31043120
generics
31053121
.type_or_consts
@@ -3109,6 +3125,40 @@ impl GenericDef {
31093125
})
31103126
.collect()
31113127
}
3128+
3129+
pub fn type_params(self, db: &dyn HirDatabase) -> Vec<TypeParam> {
3130+
let generics = db.generic_params(self.into());
3131+
generics
3132+
.type_or_consts
3133+
.iter()
3134+
.filter_map(|(local_id, data)| match data {
3135+
hir_def::generics::TypeOrConstParamData::TypeParamData(_) => Some(TypeParam {
3136+
id: TypeParamId::from_unchecked(TypeOrConstParamId {
3137+
parent: self.into(),
3138+
local_id,
3139+
}),
3140+
}),
3141+
hir_def::generics::TypeOrConstParamData::ConstParamData(_) => None,
3142+
})
3143+
.collect()
3144+
}
3145+
3146+
pub fn const_params(self, db: &dyn HirDatabase) -> Vec<ConstParam> {
3147+
let generics = db.generic_params(self.into());
3148+
generics
3149+
.type_or_consts
3150+
.iter()
3151+
.filter_map(|(local_id, data)| match data {
3152+
hir_def::generics::TypeOrConstParamData::TypeParamData(_) => None,
3153+
hir_def::generics::TypeOrConstParamData::ConstParamData(_) => Some(ConstParam {
3154+
id: ConstParamId::from_unchecked(TypeOrConstParamId {
3155+
parent: self.into(),
3156+
local_id,
3157+
}),
3158+
}),
3159+
})
3160+
.collect()
3161+
}
31123162
}
31133163

31143164
/// A single local definition.
@@ -3471,12 +3521,16 @@ impl TypeParam {
34713521
let ty = generic_arg_from_param(db, self.id.into())?;
34723522
let resolver = self.id.parent().resolver(db.upcast());
34733523
match ty.data(Interner) {
3474-
GenericArgData::Ty(it) => {
3524+
GenericArgData::Ty(it) if *it.kind(Interner) != TyKind::Error => {
34753525
Some(Type::new_with_resolver_inner(db, &resolver, it.clone()))
34763526
}
34773527
_ => None,
34783528
}
34793529
}
3530+
3531+
pub fn is_unstable(self, db: &dyn HirDatabase) -> bool {
3532+
db.attrs(GenericParamId::from(self.id).into()).is_unstable()
3533+
}
34803534
}
34813535

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

4017+
pub fn contains_reference(&self, db: &dyn HirDatabase) -> bool {
4018+
return go(db, self.env.krate, &self.ty);
4019+
4020+
fn go(db: &dyn HirDatabase, krate: CrateId, ty: &Ty) -> bool {
4021+
match ty.kind(Interner) {
4022+
// Reference itself
4023+
TyKind::Ref(_, _, _) => true,
4024+
4025+
// For non-phantom_data adts we check variants/fields as well as generic parameters
4026+
TyKind::Adt(adt_id, substitution)
4027+
if !db.struct_datum(krate, *adt_id).flags.phantom_data =>
4028+
{
4029+
let adt_datum = &db.struct_datum(krate, *adt_id);
4030+
let adt_datum_bound =
4031+
adt_datum.binders.clone().substitute(Interner, substitution);
4032+
adt_datum_bound
4033+
.variants
4034+
.into_iter()
4035+
.flat_map(|variant| variant.fields.into_iter())
4036+
.any(|ty| go(db, krate, &ty))
4037+
|| substitution
4038+
.iter(Interner)
4039+
.filter_map(|x| x.ty(Interner))
4040+
.any(|ty| go(db, krate, ty))
4041+
}
4042+
// And for `PhantomData<T>`, we check `T`.
4043+
TyKind::Adt(_, substitution)
4044+
| TyKind::Tuple(_, substitution)
4045+
| TyKind::OpaqueType(_, substitution)
4046+
| TyKind::AssociatedType(_, substitution)
4047+
| TyKind::FnDef(_, substitution) => substitution
4048+
.iter(Interner)
4049+
.filter_map(|x| x.ty(Interner))
4050+
.any(|ty| go(db, krate, ty)),
4051+
4052+
// For `[T]` or `*T` we check `T`
4053+
TyKind::Array(ty, _) | TyKind::Slice(ty) | TyKind::Raw(_, ty) => go(db, krate, ty),
4054+
4055+
// Consider everything else as not reference
4056+
_ => false,
4057+
}
4058+
}
4059+
}
4060+
39634061
pub fn as_reference(&self) -> Option<(Type, Mutability)> {
39644062
let (ty, _lt, m) = self.ty.as_reference()?;
39654063
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)