Prefer plain trait definitions over macros for salsa

This commit is contained in:
Wilfred Hughes 2024-06-13 15:38:02 -07:00
parent e059feadef
commit 3874681cb6
7 changed files with 37 additions and 33 deletions

View file

@ -73,7 +73,7 @@ use std::{
use base_db::{ use base_db::{
impl_intern_key, impl_intern_key,
salsa::{self, impl_intern_value_trivial}, salsa::{self, InternValueTrivial},
CrateId, CrateId,
}; };
use hir_expand::{ use hir_expand::{
@ -187,7 +187,7 @@ pub trait ItemTreeLoc {
macro_rules! impl_intern { macro_rules! impl_intern {
($id:ident, $loc:ident, $intern:ident, $lookup:ident) => { ($id:ident, $loc:ident, $intern:ident, $lookup:ident) => {
impl_intern_key!($id); impl_intern_key!($id);
impl_intern_value_trivial!($loc); impl InternValueTrivial for $loc {}
impl_intern_lookup!(DefDatabase, $id, $loc, $intern, $lookup); impl_intern_lookup!(DefDatabase, $id, $loc, $intern, $lookup);
}; };
} }
@ -535,7 +535,7 @@ pub struct TypeOrConstParamId {
pub parent: GenericDefId, pub parent: GenericDefId,
pub local_id: LocalTypeOrConstParamId, pub local_id: LocalTypeOrConstParamId,
} }
impl_intern_value_trivial!(TypeOrConstParamId); impl InternValueTrivial for TypeOrConstParamId {}
/// A TypeOrConstParamId with an invariant that it actually belongs to a type /// A TypeOrConstParamId with an invariant that it actually belongs to a type
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@ -597,7 +597,7 @@ pub struct LifetimeParamId {
pub local_id: LocalLifetimeParamId, pub local_id: LocalLifetimeParamId,
} }
pub type LocalLifetimeParamId = Idx<generics::LifetimeParamData>; pub type LocalLifetimeParamId = Idx<generics::LifetimeParamData>;
impl_intern_value_trivial!(LifetimeParamId); impl InternValueTrivial for LifetimeParamId {}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ItemContainerId { pub enum ItemContainerId {

View file

@ -31,7 +31,7 @@ use triomphe::Arc;
use std::{fmt, hash::Hash}; use std::{fmt, hash::Hash};
use base_db::{salsa::impl_intern_value_trivial, CrateId, FileId}; use base_db::{salsa::InternValueTrivial, CrateId, FileId};
use either::Either; use either::Either;
use span::{ use span::{
Edition, ErasedFileAstId, FileAstId, FileRange, HirFileIdRepr, Span, SpanAnchor, Edition, ErasedFileAstId, FileAstId, FileRange, HirFileIdRepr, Span, SpanAnchor,
@ -173,7 +173,7 @@ pub struct MacroCallLoc {
pub kind: MacroCallKind, pub kind: MacroCallKind,
pub ctxt: SyntaxContextId, pub ctxt: SyntaxContextId,
} }
impl_intern_value_trivial!(MacroCallLoc); impl InternValueTrivial for MacroCallLoc {}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct MacroDefId { pub struct MacroDefId {

View file

@ -5,7 +5,7 @@ use std::sync;
use base_db::{ use base_db::{
impl_intern_key, impl_intern_key,
salsa::{self, impl_intern_value_trivial}, salsa::{self, InternValueTrivial},
CrateId, Upcast, CrateId, Upcast,
}; };
use hir_def::{ use hir_def::{
@ -298,7 +298,8 @@ impl_intern_key!(InternedClosureId);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct InternedClosure(pub DefWithBodyId, pub ExprId); pub struct InternedClosure(pub DefWithBodyId, pub ExprId);
impl_intern_value_trivial!(InternedClosure);
impl InternValueTrivial for InternedClosure {}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct InternedCoroutineId(salsa::InternId); pub struct InternedCoroutineId(salsa::InternId);
@ -306,7 +307,7 @@ impl_intern_key!(InternedCoroutineId);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct InternedCoroutine(pub DefWithBodyId, pub ExprId); pub struct InternedCoroutine(pub DefWithBodyId, pub ExprId);
impl_intern_value_trivial!(InternedCoroutine); impl InternValueTrivial for InternedCoroutine {}
/// This exists just for Chalk, because Chalk just has a single `FnDefId` where /// This exists just for Chalk, because Chalk just has a single `FnDefId` where
/// we have different IDs for struct and enum variant constructors. /// we have different IDs for struct and enum variant constructors.

View file

@ -52,7 +52,7 @@ use std::{
hash::{BuildHasherDefault, Hash}, hash::{BuildHasherDefault, Hash},
}; };
use base_db::salsa::impl_intern_value_trivial; use base_db::salsa::InternValueTrivial;
use chalk_ir::{ use chalk_ir::{
fold::{Shift, TypeFoldable}, fold::{Shift, TypeFoldable},
interner::HasInterner, interner::HasInterner,
@ -606,7 +606,7 @@ pub enum ImplTraitId {
AssociatedTypeImplTrait(hir_def::TypeAliasId, ImplTraitIdx), AssociatedTypeImplTrait(hir_def::TypeAliasId, ImplTraitIdx),
AsyncBlockTypeImplTrait(hir_def::DefWithBodyId, ExprId), AsyncBlockTypeImplTrait(hir_def::DefWithBodyId, ExprId),
} }
impl_intern_value_trivial!(ImplTraitId); impl InternValueTrivial for ImplTraitId {}
#[derive(PartialEq, Eq, Debug, Hash)] #[derive(PartialEq, Eq, Debug, Hash)]
pub struct ImplTraits { pub struct ImplTraits {

View file

@ -11,7 +11,7 @@ use std::{
}; };
use base_db::{ use base_db::{
salsa::{impl_intern_value_trivial, Cycle}, salsa::{Cycle, InternValueTrivial},
CrateId, CrateId,
}; };
use chalk_ir::{ use chalk_ir::{
@ -1965,7 +1965,9 @@ pub enum CallableDefId {
StructId(StructId), StructId(StructId),
EnumVariantId(EnumVariantId), EnumVariantId(EnumVariantId),
} }
impl_intern_value_trivial!(CallableDefId);
impl InternValueTrivial for CallableDefId {}
impl_from!(FunctionId, StructId, EnumVariantId for CallableDefId); impl_from!(FunctionId, StructId, EnumVariantId for CallableDefId);
impl From<CallableDefId> for ModuleDefId { impl From<CallableDefId> for ModuleDefId {
fn from(def: CallableDefId) -> ModuleDefId { fn from(def: CallableDefId) -> ModuleDefId {

View file

@ -98,26 +98,27 @@ impl<A: InternValue + Eq + Hash + Debug + Clone, B: InternValue + Eq + Hash + De
} }
} }
/// Implement [`InternValue`] trivially, that is without actually mapping at all. pub trait InternValueTrivial
#[macro_export] where
macro_rules! impl_intern_value_trivial { Self: Eq + Hash + Debug + Clone,
($($ty:ty),*) => { {
$(
impl $crate::InternValue for $ty {
type Key = $ty;
#[inline]
fn into_key(&self) -> Self::Key {
self.clone()
}
#[inline]
fn with_key<F: FnOnce(&Self::Key) -> T, T>(&self, f: F) -> T {
f(self)
}
}
)*
};
} }
impl_intern_value_trivial!(String);
/// Implement [`InternValue`] trivially, that is without actually mapping at all.
impl<V: InternValueTrivial> InternValue for V {
type Key = Self;
#[inline]
fn into_key(&self) -> Self::Key {
self.clone()
}
#[inline]
fn with_key<F: FnOnce(&Self::Key) -> T, T>(&self, f: F) -> T {
f(self)
}
}
impl InternValueTrivial for String {}
#[derive(Debug)] #[derive(Debug)]
struct Slot<V> { struct Slot<V> {
/// DatabaseKeyIndex for this slot. /// DatabaseKeyIndex for this slot.

View file

@ -40,7 +40,7 @@ use std::panic::{self, UnwindSafe};
pub use crate::durability::Durability; pub use crate::durability::Durability;
pub use crate::intern_id::InternId; pub use crate::intern_id::InternId;
pub use crate::interned::{InternKey, InternValue}; pub use crate::interned::{InternKey, InternValue, InternValueTrivial};
pub use crate::runtime::Runtime; pub use crate::runtime::Runtime;
pub use crate::runtime::RuntimeId; pub use crate::runtime::RuntimeId;
pub use crate::storage::Storage; pub use crate::storage::Storage;