Skip to content

Commit

Permalink
Auto merge of #17417 - Wilfred:intern_macros_salsa, r=Veykril
Browse files Browse the repository at this point in the history
refactor: Prefer plain trait definitions over macros for impl_intern_value_trivial

`impl_intern_value_trivial` can be defined with a trait directly, so prefer that over a macro definition.
  • Loading branch information
bors committed Jun 14, 2024
2 parents f8305b5 + 3874681 commit 6b8b8ff
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 32 deletions.
8 changes: 4 additions & 4 deletions crates/hir-def/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ use std::{

use base_db::{
impl_intern_key,
salsa::{self, impl_intern_value_trivial},
salsa::{self, InternValueTrivial},
CrateId,
};
use hir_expand::{
Expand Down Expand Up @@ -187,7 +187,7 @@ pub trait ItemTreeLoc {
macro_rules! impl_intern {
($id:ident, $loc:ident, $intern:ident, $lookup:ident) => {
impl_intern_key!($id);
impl_intern_value_trivial!($loc);
impl InternValueTrivial for $loc {}
impl_intern_lookup!(DefDatabase, $id, $loc, $intern, $lookup);
};
}
Expand Down Expand Up @@ -535,7 +535,7 @@ pub struct TypeOrConstParamId {
pub parent: GenericDefId,
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
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
Expand Down Expand Up @@ -597,7 +597,7 @@ pub struct LifetimeParamId {
pub local_id: LocalLifetimeParamId,
}
pub type LocalLifetimeParamId = Idx<generics::LifetimeParamData>;
impl_intern_value_trivial!(LifetimeParamId);
impl InternValueTrivial for LifetimeParamId {}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ItemContainerId {
Expand Down
4 changes: 2 additions & 2 deletions crates/hir-expand/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use triomphe::Arc;

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 span::{
Edition, ErasedFileAstId, FileAstId, FileRange, HirFileIdRepr, Span, SpanAnchor,
Expand Down Expand Up @@ -173,7 +173,7 @@ pub struct MacroCallLoc {
pub kind: MacroCallKind,
pub ctxt: SyntaxContextId,
}
impl_intern_value_trivial!(MacroCallLoc);
impl InternValueTrivial for MacroCallLoc {}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct MacroDefId {
Expand Down
7 changes: 4 additions & 3 deletions crates/hir-ty/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::sync;

use base_db::{
impl_intern_key,
salsa::{self, impl_intern_value_trivial},
salsa::{self, InternValueTrivial},
CrateId, Upcast,
};
use hir_def::{
Expand Down Expand Up @@ -298,15 +298,16 @@ impl_intern_key!(InternedClosureId);

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct InternedClosure(pub DefWithBodyId, pub ExprId);
impl_intern_value_trivial!(InternedClosure);

impl InternValueTrivial for InternedClosure {}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct InternedCoroutineId(salsa::InternId);
impl_intern_key!(InternedCoroutineId);

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
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
/// we have different IDs for struct and enum variant constructors.
Expand Down
4 changes: 2 additions & 2 deletions crates/hir-ty/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ use std::{
hash::{BuildHasherDefault, Hash},
};

use base_db::salsa::impl_intern_value_trivial;
use base_db::salsa::InternValueTrivial;
use chalk_ir::{
fold::{Shift, TypeFoldable},
interner::HasInterner,
Expand Down Expand Up @@ -606,7 +606,7 @@ pub enum ImplTraitId {
AssociatedTypeImplTrait(hir_def::TypeAliasId, ImplTraitIdx),
AsyncBlockTypeImplTrait(hir_def::DefWithBodyId, ExprId),
}
impl_intern_value_trivial!(ImplTraitId);
impl InternValueTrivial for ImplTraitId {}

#[derive(PartialEq, Eq, Debug, Hash)]
pub struct ImplTraits {
Expand Down
6 changes: 4 additions & 2 deletions crates/hir-ty/src/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::{
};

use base_db::{
salsa::{impl_intern_value_trivial, Cycle},
salsa::{Cycle, InternValueTrivial},
CrateId,
};
use chalk_ir::{
Expand Down Expand Up @@ -1965,7 +1965,9 @@ pub enum CallableDefId {
StructId(StructId),
EnumVariantId(EnumVariantId),
}
impl_intern_value_trivial!(CallableDefId);

impl InternValueTrivial for CallableDefId {}

impl_from!(FunctionId, StructId, EnumVariantId for CallableDefId);
impl From<CallableDefId> for ModuleDefId {
fn from(def: CallableDefId) -> ModuleDefId {
Expand Down
37 changes: 19 additions & 18 deletions crates/salsa/src/interned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,26 +98,27 @@ impl<A: InternValue + Eq + Hash + Debug + Clone, B: InternValue + Eq + Hash + De
}
}

pub trait InternValueTrivial
where
Self: Eq + Hash + Debug + Clone,
{
}

/// Implement [`InternValue`] trivially, that is without actually mapping at all.
#[macro_export]
macro_rules! impl_intern_value_trivial {
($($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<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_intern_value_trivial!(String);

impl InternValueTrivial for String {}

#[derive(Debug)]
struct Slot<V> {
/// DatabaseKeyIndex for this slot.
Expand Down
2 changes: 1 addition & 1 deletion crates/salsa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use std::panic::{self, UnwindSafe};

pub use crate::durability::Durability;
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::RuntimeId;
pub use crate::storage::Storage;
Expand Down

0 comments on commit 6b8b8ff

Please sign in to comment.