Skip to content

Commit

Permalink
Merge pull request #1061 from sbillig/salsa-update
Browse files Browse the repository at this point in the history
Salsa 3, part 3
  • Loading branch information
g-r-a-n-t authored Feb 26, 2025
2 parents 348e46c + 113d67c commit 1c40071
Show file tree
Hide file tree
Showing 43 changed files with 519 additions and 482 deletions.
262 changes: 172 additions & 90 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ dir-test = "0.4"
rustc-hash = "2.1.0"
num-bigint = "0.4"
paste = "1.0.15"
salsa = { git = "https://github.com/salsa-rs/salsa", rev = "d32b3c1995b632d894250a667adb79c846b3da02" }
smallvec = { version = "1.13.2", features = ["union"] }
salsa = { git = "https://github.com/salsa-rs/salsa", rev = "99be5d9917c3dd88e19735a82ef6bf39ba84bd7e" }
smallvec = { version = "2.0.0-alpha.10" }
wasm-bindgen-test = "0.3"
glob = "0.3.2"
semver = "1.0.24"
Expand Down
3 changes: 2 additions & 1 deletion crates/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ description = "Provides HIR definition and lowering for Fe lang."

[dependencies]
camino.workspace = true
indexmap = "2.7"
paste.workspace = true
salsa.workspace = true
semver.workspace = true
smol_str = "0.3.2"

parser.workspace = true
ordermap = "0.5.5"
rustc-hash.workspace = true
119 changes: 41 additions & 78 deletions crates/common/src/indexmap.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
use std::{
hash::{BuildHasher, Hash, RandomState},
hash::Hash,
ops::{Deref, DerefMut},
};

use rustc_hash::FxBuildHasher;
use salsa::Update;

#[derive(Debug, Clone)]
pub struct IndexMap<K, V, S = RandomState>(indexmap::IndexMap<K, V, S>);
type OrderMap<K, V> = ordermap::OrderMap<K, V, FxBuildHasher>;
type OrderSet<V> = ordermap::OrderSet<V, FxBuildHasher>;

#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct IndexMap<K, V>(OrderMap<K, V>);

impl<K, V> IndexMap<K, V> {
pub fn new() -> Self {
Self(indexmap::IndexMap::new())
Self(OrderMap::default())
}

pub fn with_capacity(n: usize) -> Self {
Self(indexmap::IndexMap::with_capacity(n))
Self(OrderMap::with_capacity_and_hasher(n, FxBuildHasher {}))
}
}

Expand All @@ -24,83 +28,56 @@ impl<K, V> Default for IndexMap<K, V> {
}
}

impl<K, V, S> IntoIterator for IndexMap<K, V, S> {
type Item = <indexmap::IndexMap<K, V, S> as IntoIterator>::Item;
type IntoIter = <indexmap::IndexMap<K, V, S> as IntoIterator>::IntoIter;
impl<K, V> IntoIterator for IndexMap<K, V> {
type Item = <OrderMap<K, V> as IntoIterator>::Item;
type IntoIter = <OrderMap<K, V> as IntoIterator>::IntoIter;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}

impl<'a, K, V, S> IntoIterator for &'a IndexMap<K, V, S> {
type Item = <&'a indexmap::IndexMap<K, V, S> as IntoIterator>::Item;
type IntoIter = <&'a indexmap::IndexMap<K, V, S> as IntoIterator>::IntoIter;
impl<'a, K, V> IntoIterator for &'a IndexMap<K, V> {
type Item = <&'a OrderMap<K, V> as IntoIterator>::Item;
type IntoIter = <&'a OrderMap<K, V> as IntoIterator>::IntoIter;
fn into_iter(self) -> Self::IntoIter {
(&self.0).into_iter()
}
}

impl<'a, K, V, S> IntoIterator for &'a mut IndexMap<K, V, S> {
type Item = <&'a mut indexmap::IndexMap<K, V, S> as IntoIterator>::Item;
type IntoIter = <&'a mut indexmap::IndexMap<K, V, S> as IntoIterator>::IntoIter;
impl<'a, K, V> IntoIterator for &'a mut IndexMap<K, V> {
type Item = <&'a mut OrderMap<K, V> as IntoIterator>::Item;
type IntoIter = <&'a mut OrderMap<K, V> as IntoIterator>::IntoIter;
fn into_iter(self) -> Self::IntoIter {
(&mut self.0).into_iter()
}
}

impl<K, V, S> PartialEq for IndexMap<K, V, S>
where
K: Eq + Hash,
V: PartialEq,
S: BuildHasher,
{
fn eq(&self, other: &Self) -> bool {
self.0.eq(&other.0)
}
}

impl<K, V, S> Eq for IndexMap<K, V, S>
where
K: Eq + Hash,
V: Eq,
S: BuildHasher,
{
}

impl<K, V, S> FromIterator<(K, V)> for IndexMap<K, V, S>
impl<K, V> FromIterator<(K, V)> for IndexMap<K, V>
where
K: Hash + Eq,
S: BuildHasher + Default,
{
fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self {
Self(indexmap::IndexMap::from_iter(iter))
Self(OrderMap::from_iter(iter))
}
}

impl<K, V, S> Deref for IndexMap<K, V, S>
where
S: BuildHasher,
{
type Target = indexmap::IndexMap<K, V, S>;
impl<K, V> Deref for IndexMap<K, V> {
type Target = OrderMap<K, V>;
fn deref(&self) -> &Self::Target {
&self.0
}
}

impl<K, V, S> DerefMut for IndexMap<K, V, S>
where
S: BuildHasher,
{
impl<K, V> DerefMut for IndexMap<K, V> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

unsafe impl<K, V, S> Update for IndexMap<K, V, S>
unsafe impl<K, V> Update for IndexMap<K, V>
where
K: Update + Eq + Hash,
V: Update,
S: BuildHasher,
{
unsafe fn maybe_update(old_pointer: *mut Self, new_map: Self) -> bool {
let old_map = unsafe { &mut *old_pointer };
Expand Down Expand Up @@ -131,15 +108,15 @@ where
}

#[derive(Debug, Clone)]
pub struct IndexSet<V, S = RandomState>(indexmap::IndexSet<V, S>);
pub struct IndexSet<V>(OrderSet<V>);

impl<V> IndexSet<V> {
pub fn new() -> Self {
Self(indexmap::IndexSet::new())
Self(OrderSet::default())
}

pub fn with_capacity(n: usize) -> Self {
Self(indexmap::IndexSet::with_capacity(n))
Self(OrderSet::with_capacity_and_hasher(n, FxBuildHasher {}))
}
}

Expand All @@ -149,72 +126,58 @@ impl<V> Default for IndexSet<V> {
}
}

impl<V, S> IntoIterator for IndexSet<V, S> {
type Item = <indexmap::IndexSet<V, S> as IntoIterator>::Item;
type IntoIter = <indexmap::IndexSet<V, S> as IntoIterator>::IntoIter;
impl<V> IntoIterator for IndexSet<V> {
type Item = <OrderSet<V> as IntoIterator>::Item;
type IntoIter = <OrderSet<V> as IntoIterator>::IntoIter;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}

impl<'a, V, S> IntoIterator for &'a IndexSet<V, S> {
type Item = <&'a indexmap::IndexSet<V, S> as IntoIterator>::Item;
type IntoIter = <&'a indexmap::IndexSet<V, S> as IntoIterator>::IntoIter;
impl<'a, V> IntoIterator for &'a IndexSet<V> {
type Item = <&'a OrderSet<V> as IntoIterator>::Item;
type IntoIter = <&'a OrderSet<V> as IntoIterator>::IntoIter;
fn into_iter(self) -> Self::IntoIter {
(&self.0).into_iter()
}
}

impl<V, S> PartialEq for IndexSet<V, S>
impl<V> PartialEq for IndexSet<V>
where
V: Hash + Eq,
S: BuildHasher,
{
fn eq(&self, other: &Self) -> bool {
self.0.eq(&other.0)
}
}

impl<V, S> Eq for IndexSet<V, S>
where
V: Eq + Hash,
S: BuildHasher,
{
}
impl<V> Eq for IndexSet<V> where V: Eq + Hash {}

impl<V, S> FromIterator<V> for IndexSet<V, S>
impl<V> FromIterator<V> for IndexSet<V>
where
V: Hash + Eq,
S: BuildHasher + Default,
{
fn from_iter<T: IntoIterator<Item = V>>(iter: T) -> Self {
Self(indexmap::IndexSet::from_iter(iter))
Self(OrderSet::from_iter(iter))
}
}

impl<V, S> Deref for IndexSet<V, S>
where
S: BuildHasher,
{
type Target = indexmap::IndexSet<V, S>;
impl<V> Deref for IndexSet<V> {
type Target = OrderSet<V>;
fn deref(&self) -> &Self::Target {
&self.0
}
}

impl<V, S> DerefMut for IndexSet<V, S>
where
S: BuildHasher,
{
impl<V> DerefMut for IndexSet<V> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

unsafe impl<V, S> Update for IndexSet<V, S>
unsafe impl<V> Update for IndexSet<V>
where
V: Update + Eq + Hash,
S: BuildHasher,
{
unsafe fn maybe_update(old_pointer: *mut Self, new_set: Self) -> bool {
let old_set = unsafe { &mut *old_pointer };
Expand Down
1 change: 0 additions & 1 deletion crates/hir-analysis/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ num-bigint.workspace = true
rustc-hash.workspace = true
salsa.workspace = true
smallvec.workspace = true
smallvec2 = { package = "smallvec", version = "2.0.0-alpha.10" }

common.workspace = true
test-utils.workspace = true
Expand Down
3 changes: 2 additions & 1 deletion crates/hir-analysis/src/name_resolution/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ use hir::{
hir_def::{IdentId, TopLevelMod},
span::DynLazySpan,
};
use salsa::Update;

use super::NameRes;
use crate::HirAnalysisDb;

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Update)]
pub enum NameResDiag<'db> {
/// The definition conflicts with other definitions.
Conflict(IdentId<'db>, Vec<DynLazySpan<'db>>),
Expand Down
12 changes: 7 additions & 5 deletions crates/hir-analysis/src/name_resolution/import_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ use std::{
mem,
};

use common::indexmap::IndexMap;
use hir::{
hir_def::{prim_ty::PrimTy, scope_graph::ScopeId, IdentId, IngotId, Use},
span::DynLazySpan,
};
use itertools::Itertools;
use rustc_hash::{FxHashMap, FxHashSet};
use salsa::Update;

use super::{
diagnostics::NameResDiag,
Expand Down Expand Up @@ -679,11 +681,11 @@ impl<'db> ImportResolver<'db> {
}
}

#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[derive(Clone, Debug, Default, PartialEq, Eq, Update)]
pub struct ResolvedImports<'db> {
pub named_resolved: FxHashMap<ScopeId<'db>, NamedImportSet<'db>>,
pub glob_resolved: FxHashMap<ScopeId<'db>, GlobImportSet<'db>>,
pub unnamed_resolved: FxHashMap<ScopeId<'db>, Vec<NameResBucket<'db>>>,
pub named_resolved: IndexMap<ScopeId<'db>, NamedImportSet<'db>>,
pub glob_resolved: IndexMap<ScopeId<'db>, GlobImportSet<'db>>,
pub unnamed_resolved: IndexMap<ScopeId<'db>, Vec<NameResBucket<'db>>>,
}

pub(super) trait Importer<'db> {
Expand Down Expand Up @@ -727,7 +729,7 @@ impl<'db> Importer<'db> for DefaultImporter {

pub type NamedImportSet<'db> = FxHashMap<IdentId<'db>, NameResBucket<'db>>;

#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[derive(Clone, Debug, Default, PartialEq, Eq, Update)]
pub struct GlobImportSet<'db> {
imported: FxHashMap<Use<'db>, FxHashMap<IdentId<'db>, Vec<NameRes<'db>>>>,
}
Expand Down
13 changes: 7 additions & 6 deletions crates/hir-analysis/src/name_resolution/name_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use hir::{
span::DynLazySpan,
};
use rustc_hash::{FxHashMap, FxHashSet};
use salsa::Update;

use super::{
import_resolver::Importer,
Expand Down Expand Up @@ -88,7 +89,7 @@ impl Default for QueryDirective {
/// The struct contains the lookup result of a name query.
/// The results can contain more than one name resolutions which belong to
/// different name domains.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[derive(Clone, Debug, Default, PartialEq, Eq, Update)]
pub struct NameResBucket<'db> {
pub(super) bucket: FxHashMap<NameDomain, NameResolutionResult<'db, NameRes<'db>>>,
}
Expand Down Expand Up @@ -240,7 +241,7 @@ impl<'db> From<NameRes<'db>> for NameResBucket<'db> {
}

/// The struct contains the lookup result of a name query.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[derive(Clone, Debug, PartialEq, Eq, Hash, Update)]
pub struct NameRes<'db> {
/// The kind of the resolution.
pub kind: NameResKind<'db>,
Expand Down Expand Up @@ -400,7 +401,7 @@ impl<'db> NameRes<'db> {
}
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, derive_more::From)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, derive_more::From, Update)]
pub enum NameResKind<'db> {
/// The name is resolved to a scope.
Scope(ScopeId<'db>),
Expand All @@ -427,7 +428,7 @@ impl<'db> NameResKind<'db> {
/// The name derivation indicates where a name resolution comes from.
/// Name derivation is used to track the origin of a resolution, and to
/// determine the shadowing rules.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[derive(Clone, Debug, PartialEq, Eq, Hash, Update)]
pub enum NameDerivation<'db> {
/// Derived from a definition in the current scope.
Def,
Expand Down Expand Up @@ -732,7 +733,7 @@ impl<'db, 'a> NameResolver<'db, 'a> {
}
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Update)]
pub enum NameResolutionError<'db> {
/// The name is not found.
NotFound,
Expand Down Expand Up @@ -775,7 +776,7 @@ impl fmt::Display for NameResolutionError<'_> {
impl std::error::Error for NameResolutionError<'_> {}

bitflags! {
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Update)]
/// Each resolved name is associated with a domain that indicates which domain
/// the name belongs to.
/// The multiple same names can be introduced in a same scope as long as they
Expand Down
Loading

0 comments on commit 1c40071

Please # to comment.