Skip to content

Commit f975424

Browse files
committed
Use salsa::tracked methods and remove _impl functions
1 parent 7ecd47e commit f975424

File tree

4 files changed

+145
-231
lines changed

4 files changed

+145
-231
lines changed

crates/hir-analysis/src/ty/ty_def.rs

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@ pub struct TyId<'db> {
3636
pub data: TyData<'db>,
3737
}
3838

39+
#[salsa::tracked]
3940
impl<'db> TyId<'db> {
4041
/// Returns the kind of the type.
41-
pub fn kind(self, db: &'db dyn HirAnalysisDb) -> &'db Kind {
42-
ty_kind(db, self)
42+
#[salsa::tracked(return_ref)]
43+
pub fn kind(self, db: &'db dyn HirAnalysisDb) -> Kind {
44+
self.data(db).kind(db)
4345
}
4446

4547
/// Returns the current arguments of the type.
@@ -133,8 +135,17 @@ impl<'db> TyId<'db> {
133135
!matches!(self.kind(db), Kind::Abs(_, _))
134136
}
135137

136-
pub fn pretty_print(self, db: &'db dyn HirAnalysisDb) -> &'db str {
137-
pretty_print_ty(db, self)
138+
#[salsa::tracked(return_ref)]
139+
pub fn pretty_print(self, db: &'db dyn HirAnalysisDb) -> String {
140+
match self.data(db) {
141+
TyData::TyVar(var) => var.pretty_print(),
142+
TyData::TyParam(param) => param.pretty_print(db),
143+
TyData::TyApp(_, _) => pretty_print_ty_app(db, self),
144+
TyData::TyBase(ty_con) => ty_con.pretty_print(db),
145+
TyData::ConstTy(const_ty) => const_ty.pretty_print(db),
146+
TyData::Never => "!".to_string(),
147+
TyData::Invalid(..) => "<invalid>".to_string(),
148+
}
138149
}
139150

140151
pub fn is_inherent_impl_allowed(self, db: &dyn HirAnalysisDb, ingot: IngotId) -> bool {
@@ -610,11 +621,6 @@ pub struct ApplicableTyProp<'db> {
610621
pub const_ty: Option<TyId<'db>>,
611622
}
612623

613-
#[salsa::tracked(return_ref)]
614-
pub fn ty_kind<'db>(db: &'db dyn HirAnalysisDb, ty: TyId<'db>) -> Kind {
615-
ty.data(db).kind(db)
616-
}
617-
618624
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
619625
pub enum TyData<'db> {
620626
/// Type variable.
@@ -1084,7 +1090,7 @@ impl HasKind for AdtDef<'_> {
10841090
fn kind(&self, db: &dyn HirAnalysisDb) -> Kind {
10851091
let mut kind = Kind::Star;
10861092
for param in self.params(db).iter().rev() {
1087-
kind = Kind::abs(ty_kind(db, *param).clone(), kind);
1093+
kind = Kind::abs(param.kind(db).clone(), kind);
10881094
}
10891095

10901096
kind
@@ -1095,7 +1101,7 @@ impl HasKind for FuncDef<'_> {
10951101
fn kind(&self, db: &dyn HirAnalysisDb) -> Kind {
10961102
let mut kind = Kind::Star;
10971103
for param in self.params(db).iter().rev() {
1098-
kind = Kind::abs(ty_kind(db, *param).clone(), kind);
1104+
kind = Kind::abs(param.kind(db).clone(), kind);
10991105
}
11001106

11011107
kind
@@ -1164,19 +1170,6 @@ where
11641170
collector.keys
11651171
}
11661172

1167-
#[salsa::tracked(return_ref)]
1168-
pub(crate) fn pretty_print_ty<'db>(db: &'db dyn HirAnalysisDb, ty: TyId<'db>) -> String {
1169-
match ty.data(db) {
1170-
TyData::TyVar(var) => var.pretty_print(),
1171-
TyData::TyParam(param) => param.pretty_print(db),
1172-
TyData::TyApp(_, _) => pretty_print_ty_app(db, ty),
1173-
TyData::TyBase(ty_con) => ty_con.pretty_print(db),
1174-
TyData::ConstTy(const_ty) => const_ty.pretty_print(db),
1175-
TyData::Never => "!".to_string(),
1176-
TyData::Invalid(..) => "<invalid>".to_string(),
1177-
}
1178-
}
1179-
11801173
fn pretty_print_ty_app<'db>(db: &'db dyn HirAnalysisDb, ty: TyId<'db>) -> String {
11811174
use PrimTy::*;
11821175
use TyBase::*;

crates/hir/src/hir_def/item.rs

Lines changed: 77 additions & 168 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,8 @@ pub struct TopLevelMod<'db> {
360360
pub ingot: IngotId<'db>,
361361
pub(crate) file: InputFile,
362362
}
363+
364+
#[salsa::tracked]
363365
impl<'db> TopLevelMod<'db> {
364366
pub fn lazy_span(self) -> LazyTopModSpan<'db> {
365367
LazyTopModSpan::new(self)
@@ -413,207 +415,114 @@ impl<'db> TopLevelMod<'db> {
413415

414416
/// Returns all items in the top level module including ones in nested
415417
/// modules.
416-
pub fn all_items(self, db: &'db dyn HirDb) -> &'db Vec<ItemKind<'db>> {
417-
all_items_in_top_mod(db, self)
418+
#[salsa::tracked(return_ref)]
419+
pub fn all_items(self, db: &'db dyn HirDb) -> Vec<ItemKind<'db>> {
420+
self.children_nested(db).collect()
418421
}
419422

420423
/// Returns all structs in the top level module including ones in nested
421424
/// modules.
422-
pub fn all_structs(self, db: &'db dyn HirDb) -> &'db Vec<Struct<'db>> {
423-
all_structs_in_top_mod(db, self)
425+
#[salsa::tracked(return_ref)]
426+
pub fn all_structs(self, db: &'db dyn HirDb) -> Vec<Struct<'db>> {
427+
self.all_items(db)
428+
.iter()
429+
.filter_map(|item| match item {
430+
ItemKind::Struct(struct_) => Some(*struct_),
431+
_ => None,
432+
})
433+
.collect()
424434
}
425435

426436
/// Returns all enums in the top level module including ones in nested
427437
/// modules.
428-
pub fn all_enums(self, db: &'db dyn HirDb) -> &'db Vec<Enum<'db>> {
429-
all_enums_in_top_mod(db, self)
438+
#[salsa::tracked(return_ref)]
439+
pub fn all_enums(self, db: &'db dyn HirDb) -> Vec<Enum<'db>> {
440+
self.all_items(db)
441+
.iter()
442+
.filter_map(|item| match item {
443+
ItemKind::Enum(enum_) => Some(*enum_),
444+
_ => None,
445+
})
446+
.collect()
430447
}
431448

432449
/// Returns all contracts in the top level module including ones in nested
433450
/// modules.
434-
pub fn all_contracts(self, db: &'db dyn HirDb) -> &'db Vec<Contract<'db>> {
435-
all_contracts_in_top_mod(db, self)
451+
#[salsa::tracked(return_ref)]
452+
pub fn all_contracts(self, db: &'db dyn HirDb) -> Vec<Contract<'db>> {
453+
self.all_items(db)
454+
.iter()
455+
.filter_map(|item| match item {
456+
ItemKind::Contract(contract) => Some(*contract),
457+
_ => None,
458+
})
459+
.collect()
436460
}
437461

438462
/// Returns all type aliases in the top level module including ones in
439463
/// nested modules.
440-
pub fn all_type_aliases(self, db: &'db dyn HirDb) -> &'db Vec<TypeAlias<'db>> {
441-
all_type_aliases_in_top_mod(db, self)
464+
#[salsa::tracked(return_ref)]
465+
pub fn all_type_aliases(self, db: &'db dyn HirDb) -> Vec<TypeAlias<'db>> {
466+
self.all_items(db)
467+
.iter()
468+
.filter_map(|item| match item {
469+
ItemKind::TypeAlias(alias) => Some(*alias),
470+
_ => None,
471+
})
472+
.collect()
442473
}
443474

444475
/// Returns all traits in the top level module including ones in nested
445476
/// modules.
446-
pub fn all_traits(self, db: &'db dyn HirDb) -> &'db Vec<Trait<'db>> {
447-
all_traits_in_top_mod(db, self)
477+
#[salsa::tracked(return_ref)]
478+
pub fn all_traits(self, db: &'db dyn HirDb) -> Vec<Trait<'db>> {
479+
self.all_items(db)
480+
.iter()
481+
.filter_map(|item| match item {
482+
ItemKind::Trait(trait_) => Some(*trait_),
483+
_ => None,
484+
})
485+
.collect()
448486
}
449487

450-
pub fn all_funcs(self, db: &'db dyn HirDb) -> &'db Vec<Func<'db>> {
451-
all_funcs_in_top_mod(db, self)
488+
#[salsa::tracked(return_ref)]
489+
pub fn all_funcs(self, db: &'db dyn HirDb) -> Vec<Func<'db>> {
490+
self.all_items(db)
491+
.iter()
492+
.filter_map(|item| match item {
493+
ItemKind::Func(func_) => Some(*func_),
494+
_ => None,
495+
})
496+
.collect()
452497
}
453498

454499
/// Returns all traits in the top level module including ones in nested
455500
/// modules.
456-
pub fn all_impl_traits(self, db: &'db dyn HirDb) -> &'db Vec<ImplTrait<'db>> {
457-
all_impl_trait_in_top_mod(db, self)
501+
#[salsa::tracked(return_ref)]
502+
pub fn all_impl_traits(self, db: &'db dyn HirDb) -> Vec<ImplTrait<'db>> {
503+
self.all_items(db)
504+
.iter()
505+
.filter_map(|item| match item {
506+
ItemKind::ImplTrait(impl_trait) => Some(*impl_trait),
507+
_ => None,
508+
})
509+
.collect()
458510
}
459511

460512
/// Returns all impls in the top level module including ones in nested
461513
/// modules.
462-
pub fn all_impls(self, db: &'db dyn HirDb) -> &'db Vec<Impl<'db>> {
463-
all_impl_in_top_mod(db, self)
514+
#[salsa::tracked(return_ref)]
515+
pub fn all_impls(self, db: &'db dyn HirDb) -> Vec<Impl<'db>> {
516+
self.all_items(db)
517+
.iter()
518+
.filter_map(|item| match item {
519+
ItemKind::Impl(impl_) => Some(*impl_),
520+
_ => None,
521+
})
522+
.collect()
464523
}
465524
}
466525

467-
#[salsa::tracked(return_ref)]
468-
pub fn all_top_modules_in_ingot<'db>(
469-
db: &'db dyn HirDb,
470-
ingot: IngotId<'db>,
471-
) -> Vec<TopLevelMod<'db>> {
472-
let tree = ingot.module_tree(db);
473-
tree.all_modules().collect()
474-
}
475-
476-
#[salsa::tracked(return_ref)]
477-
pub fn all_enums_in_ingot<'db>(db: &'db dyn HirDb, ingot: IngotId<'db>) -> Vec<Enum<'db>> {
478-
ingot
479-
.all_modules(db)
480-
.iter()
481-
.flat_map(|top_mod| top_mod.all_enums(db).iter().copied())
482-
.collect()
483-
}
484-
485-
#[salsa::tracked(return_ref)]
486-
pub fn all_impl_traits_in_ingot<'db>(
487-
db: &'db dyn HirDb,
488-
ingot: IngotId<'db>,
489-
) -> Vec<ImplTrait<'db>> {
490-
ingot
491-
.all_modules(db)
492-
.iter()
493-
.flat_map(|top_mod| top_mod.all_impl_traits(db).iter().copied())
494-
.collect()
495-
}
496-
497-
#[salsa::tracked(return_ref)]
498-
pub fn all_impls_in_ingot<'db>(db: &'db dyn HirDb, ingot: IngotId<'db>) -> Vec<Impl<'db>> {
499-
ingot
500-
.all_modules(db)
501-
.iter()
502-
.flat_map(|top_mod| top_mod.all_impls(db).iter().copied())
503-
.collect()
504-
}
505-
506-
#[salsa::tracked(return_ref)]
507-
pub fn all_items_in_top_mod<'db>(
508-
db: &'db dyn HirDb,
509-
top_mod: TopLevelMod<'db>,
510-
) -> Vec<ItemKind<'db>> {
511-
top_mod.children_nested(db).collect()
512-
}
513-
514-
#[salsa::tracked(return_ref)]
515-
pub fn all_structs_in_top_mod<'db>(
516-
db: &'db dyn HirDb,
517-
top_mod: TopLevelMod<'db>,
518-
) -> Vec<Struct<'db>> {
519-
all_items_in_top_mod(db, top_mod)
520-
.iter()
521-
.filter_map(|item| match item {
522-
ItemKind::Struct(struct_) => Some(*struct_),
523-
_ => None,
524-
})
525-
.collect()
526-
}
527-
528-
#[salsa::tracked(return_ref)]
529-
pub fn all_enums_in_top_mod<'db>(db: &'db dyn HirDb, top_mod: TopLevelMod<'db>) -> Vec<Enum<'db>> {
530-
all_items_in_top_mod(db, top_mod)
531-
.iter()
532-
.filter_map(|item| match item {
533-
ItemKind::Enum(enum_) => Some(*enum_),
534-
_ => None,
535-
})
536-
.collect()
537-
}
538-
539-
#[salsa::tracked(return_ref)]
540-
pub fn all_type_aliases_in_top_mod<'db>(
541-
db: &'db dyn HirDb,
542-
top_mod: TopLevelMod<'db>,
543-
) -> Vec<TypeAlias<'db>> {
544-
all_items_in_top_mod(db, top_mod)
545-
.iter()
546-
.filter_map(|item| match item {
547-
ItemKind::TypeAlias(alias) => Some(*alias),
548-
_ => None,
549-
})
550-
.collect()
551-
}
552-
553-
#[salsa::tracked(return_ref)]
554-
pub fn all_contracts_in_top_mod<'db>(
555-
db: &'db dyn HirDb,
556-
top_mod: TopLevelMod<'db>,
557-
) -> Vec<Contract<'db>> {
558-
all_items_in_top_mod(db, top_mod)
559-
.iter()
560-
.filter_map(|item| match item {
561-
ItemKind::Contract(contract) => Some(*contract),
562-
_ => None,
563-
})
564-
.collect()
565-
}
566-
567-
#[salsa::tracked(return_ref)]
568-
pub fn all_traits_in_top_mod<'db>(
569-
db: &'db dyn HirDb,
570-
top_mod: TopLevelMod<'db>,
571-
) -> Vec<Trait<'db>> {
572-
all_items_in_top_mod(db, top_mod)
573-
.iter()
574-
.filter_map(|item| match item {
575-
ItemKind::Trait(trait_) => Some(*trait_),
576-
_ => None,
577-
})
578-
.collect()
579-
}
580-
581-
#[salsa::tracked(return_ref)]
582-
pub fn all_funcs_in_top_mod<'db>(db: &'db dyn HirDb, top_mod: TopLevelMod<'db>) -> Vec<Func<'db>> {
583-
all_items_in_top_mod(db, top_mod)
584-
.iter()
585-
.filter_map(|item| match item {
586-
ItemKind::Func(func_) => Some(*func_),
587-
_ => None,
588-
})
589-
.collect()
590-
}
591-
592-
#[salsa::tracked(return_ref)]
593-
pub fn all_impl_in_top_mod<'db>(db: &'db dyn HirDb, top_mod: TopLevelMod<'db>) -> Vec<Impl<'db>> {
594-
all_items_in_top_mod(db, top_mod)
595-
.iter()
596-
.filter_map(|item| match item {
597-
ItemKind::Impl(impl_) => Some(*impl_),
598-
_ => None,
599-
})
600-
.collect()
601-
}
602-
603-
#[salsa::tracked(return_ref)]
604-
pub fn all_impl_trait_in_top_mod<'db>(
605-
db: &'db dyn HirDb,
606-
top_mod: TopLevelMod<'db>,
607-
) -> Vec<ImplTrait<'db>> {
608-
all_items_in_top_mod(db, top_mod)
609-
.iter()
610-
.filter_map(|item| match item {
611-
ItemKind::ImplTrait(impl_trait) => Some(*impl_trait),
612-
_ => None,
613-
})
614-
.collect()
615-
}
616-
617526
#[salsa::tracked]
618527
pub struct Mod<'db> {
619528
#[id]

0 commit comments

Comments
 (0)