Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: cleanups, use features from 1.79, remove unnecessary aliases #30

Merged
merged 1 commit into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
fail-fast: false
matrix:
include:
- rust: 1.74 # MSRV
- rust: 1.79 # MSRV
- rust: stable
- rust: nightly
- rust: nightly
Expand All @@ -38,10 +38,10 @@ jobs:
cache-on-failure: true
# Only run tests on latest stable and above
- name: build
if: ${{ matrix.rust == '1.74' }} # MSRV
if: ${{ matrix.rust == '1.79' }} # MSRV
run: cargo build ${{ matrix.flags }}
- name: test
if: ${{ matrix.rust != '1.74' }} # MSRV
if: ${{ matrix.rust != '1.79' }} # MSRV
run: cargo test --workspace ${{ matrix.flags }}

clippy:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
.idea
/*.sol
/*.yul
/tmp/
17 changes: 2 additions & 15 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ resolver = "2"
[workspace.package]
version = "0.0.0"
edition = "2021"
rust-version = "1.74" # MSRV
rust-version = "1.79" # MSRV
authors = ["DaniPopes <57450786+DaniPopes@users.noreply.github.com>"]
license = "MIT OR Apache-2.0"
repository = "https://github.com/paradigmxyz/sulk"
Expand All @@ -33,6 +33,7 @@ unreachable-pub = "warn"
unused-must-use = "warn"
redundant-lifetimes = "warn"
unnameable-types = "warn"
unused-crate-dependencies = "warn"

[workspace.lints.rustdoc]
all = "warn"
Expand Down Expand Up @@ -133,6 +134,7 @@ walkdir = "2.4"
ahash = "0.8"
bitflags = "2.4"
bumpalo = "3.14"
dashmap = "6.0"
hex = { package = "const-hex", version = "1.10" }
index_vec = "0.1.3"
indexmap = "2.2"
Expand All @@ -141,6 +143,8 @@ itoa = "1.0"
libc = "0.2"
md-5 = "0.10"
memchr = "2.7"
paste = "1.0"
petgraph = "0.6"
rustc-hash = "2.0.0"
scoped-tls = "1.0.1"
semver = "1.0"
Expand Down
2 changes: 1 addition & 1 deletion benches/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ semver.workspace = true
[dev-dependencies]
criterion = "0.5"
iai-callgrind = "0.11"
paste = "1.0"
paste.workspace = true

[features]
ci = []
Expand Down
2 changes: 1 addition & 1 deletion clippy.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
msrv = "1.74"
msrv = "1.79"
allow-dbg-in-tests = true
28 changes: 28 additions & 0 deletions crates/ast/src/ast/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ pub struct Item {
}

impl Item {
/// Returns the name of the item, if any.
pub fn name(&self) -> Option<Ident> {
self.kind.name()
}

/// Returns the description of the item.
pub fn description(&self) -> &'static str {
self.kind.description()
Expand Down Expand Up @@ -70,6 +75,21 @@ pub enum ItemKind {
}

impl ItemKind {
/// Returns the name of the item, if any.
pub fn name(&self) -> Option<Ident> {
match self {
Self::Pragma(_) | Self::Import(_) | Self::Using(_) => None,
Self::Contract(item) => Some(item.name),
Self::Function(item) => item.header.name,
Self::Variable(item) => item.name,
Self::Struct(item) => Some(item.name),
Self::Enum(item) => Some(item.name),
Self::Udvt(item) => Some(item.name),
Self::Error(item) => Some(item.name),
Self::Event(item) => Some(item.name),
}
}

/// Returns the description of the item.
pub fn description(&self) -> &'static str {
match self {
Expand Down Expand Up @@ -192,6 +212,13 @@ pub struct ImportDirective {
pub items: ImportItems,
}

impl ImportDirective {
/// Returns `true` if the import directive imports all items from the target.
pub fn imports_all(&self) -> bool {
matches!(self.items, ImportItems::Glob(None) | ImportItems::Plain(None))
}
}

/// The path of an import directive.
#[derive(Clone, Debug)]
pub enum ImportItems {
Expand Down Expand Up @@ -324,6 +351,7 @@ pub struct ItemFunction {
#[derive(Clone, Debug, Default)]
pub struct FunctionHeader {
/// The name of the function.
/// Only `None` if this is a constructor, fallback, or receive function.
pub name: Option<Ident>,
/// The parameters of the function.
pub parameters: ParameterList,
Expand Down
5 changes: 2 additions & 3 deletions crates/ast/src/ast/lit.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use alloy_primitives::Address;
use std::fmt;
use sulk_data_structures::sync::Lrc;
use std::{fmt, sync::Arc};
use sulk_interface::{kw, Span, Symbol};

/// A literal: `hex"1234"`, `5.6 ether`.
Expand All @@ -25,7 +24,7 @@ pub enum LitKind {
///
/// Note that even if this is a string or unicode string literal, invalid UTF-8 sequences
/// are allowed, and as such this cannot be a `str` or `Symbol`.
Str(StrKind, Lrc<[u8]>),
Str(StrKind, Arc<[u8]>),
/// A decimal or hexadecimal number literal.
Number(num_bigint::BigInt),
/// A rational number literal.
Expand Down
7 changes: 5 additions & 2 deletions crates/data-structures/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@ rustdoc-args = ["--cfg", "docsrs"]
workspace = true

[dependencies]
ahash.workspace = true
index_vec.workspace = true
indexmap.workspace = true
parking_lot.workspace = true
rustc-hash.workspace = true
smallvec = { workspace = true, features = ["union"] }

[features]
nightly = ["smallvec/specialization", "smallvec/may_dangle", "parking_lot/nightly"]
nightly = [
"smallvec/specialization",
"smallvec/may_dangle",
"parking_lot/nightly",
]
59 changes: 26 additions & 33 deletions crates/data-structures/src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,16 @@ pub use index_vec::{
/// Creates a new index to use with [`::index_vec`].
#[macro_export]
macro_rules! newtype_index {
($(#[$attr:meta])* $vis:vis struct $name:ident) => {
() => {};
($(#[$attr:meta])* $vis:vis struct $name:ident; $($rest:tt)*) => {
$(#[$attr])*
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
$vis struct $name($crate::index::BaseIndex32);

impl std::fmt::Display for $name {
#[inline(always)]
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}

impl std::fmt::Debug for $name {
#[inline(always)]
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
write!(f, "{}({:?})", stringify!($name), self.0)
}
}

Expand Down Expand Up @@ -57,13 +50,14 @@ macro_rules! newtype_index {
self.0.get()
}
}

$crate::newtype_index!($($rest)*);
};
}

// TODO: Use generic NonZero<_>.
// NOTE: The max MUST be less than the maximum value of the underlying integer.
macro_rules! base_index {
($(#[$attr:meta])* $name:ident($primitive:ident || $non_zero:ident <= $max:literal)) => {
($(#[$attr:meta])* $name:ident($primitive:ident <= $max:literal)) => {
/// A specialized wrapper around a primitive number.
///
$(#[$attr])*
Expand All @@ -76,18 +70,10 @@ macro_rules! base_index {
#[cfg(feature = "nightly")]
value: $primitive,
#[cfg(not(feature = "nightly"))]
value: std::num::$non_zero,
}

impl fmt::Display for $name {
#[inline(always)]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.value.fmt(f)
}
value: std::num::NonZero<$primitive>,
}

impl fmt::Debug for $name {
#[inline(always)]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.value.fmt(f)
}
Expand All @@ -99,7 +85,8 @@ macro_rules! base_index {
if value > Self::MAX_AS as usize {
index_overflow();
}
Self::new(value as $primitive)
// SAFETY: `value` is less than or equal to `MAX`.
unsafe { Self::new_unchecked(value as $primitive) }
}

#[inline(always)]
Expand All @@ -120,20 +107,29 @@ macro_rules! base_index {
/// # Panics
///
/// Panics if `value` exceeds `MAX`.
#[inline]
#[inline(always)]
pub const fn new(value: $primitive) -> Self {
if value > Self::MAX_AS {
index_overflow();
}
// SAFETY: `value` is less than or equal to `MAX`.
unsafe { Self::new_unchecked(value) }
}

/// Creates a new `$name` from the given `value`, without checking for overflow.
///
/// # Safety
///
/// The caller must ensure that `value` is less than or equal to `MAX`.
#[inline(always)]
pub const unsafe fn new_unchecked(value: $primitive) -> Self {
// SAFETY: guaranteed by the caller.
unsafe {
Self {
#[cfg(feature = "nightly")]
value,
#[cfg(not(feature = "nightly"))]
value: std::num::$non_zero::new_unchecked(match value.checked_add(1) {
Some(value) => value,
None => std::hint::unreachable_unchecked(),
}),
value: std::num::NonZero::new_unchecked(value.unchecked_add(1)),
}
}
}
Expand All @@ -144,18 +140,15 @@ macro_rules! base_index {
#[cfg(feature = "nightly")]
return self.value;

// SAFETY: non-zero minus one doesn't overflow.
#[cfg(not(feature = "nightly"))]
return match self.value.get().checked_sub(1) {
Some(value) => value,
// SAFETY: Non-zero.
None => unsafe { std::hint::unreachable_unchecked() },
};
return unsafe { self.value.get().unchecked_sub(1) };
}
}
};
}

base_index!(BaseIndex32(u32 || NonZeroU32 <= 0xFFFF_FF00));
base_index!(BaseIndex32(u32 <= 0xFFFF_FF00));

#[inline(never)]
#[cold]
Expand Down
15 changes: 0 additions & 15 deletions crates/data-structures/src/map.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,18 @@
//! Map types.
//!
//! - use [`IndexMap`] over [`HashMap`] for deterministic iteration order
//! - use [`AHasher`] for big keys and [`FxHasher`] for everything else
//! - AHash docs make it look like it's faster than everything, but FxHash beats it for all
//! primitives and small (<=16 bytes, if not more) strings and byte arrays

use indexmap::{IndexMap, IndexSet};
use std::{
collections::{HashMap, HashSet},
hash::BuildHasherDefault,
};

pub use ahash::{self, AHasher};
pub use rustc_hash::{self, FxBuildHasher, FxHasher};

/// [`HashMap`] entry type.
pub type StdEntry<'a, K, V> = std::collections::hash_map::Entry<'a, K, V>;
/// [`IndexMap`] entry type.
pub type IndexEntry<'a, K, V> = indexmap::map::Entry<'a, K, V>;

/// A [`HashMap`] using [`AHasher`] as its hasher.
pub type AHashMap<K, V> = ahash::HashMap<K, V>;
/// A [`HashSet`] using [`AHasher`] as its hasher.
pub type AHashSet<V> = ahash::HashSet<V>;
/// An [`IndexMap`] using [`AHasher`] as its hasher.
pub type AIndexMap<K, V> = IndexMap<K, V, ahash::RandomState>;
/// An [`IndexSet`] using [`AHasher`] as its hasher.
pub type AIndexSet<V> = IndexSet<V, ahash::RandomState>;

/// A [`HashMap`] using [`FxHasher`] as its hasher.
pub type FxHashMap<K, V> = HashMap<K, V, BuildHasherDefault<FxHasher>>;
/// A [`HashSet`] using [`FxHasher`] as its hasher.
Expand Down
5 changes: 0 additions & 5 deletions crates/data-structures/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,3 @@ pub use parking_lot::{
MappedRwLockWriteGuard as MappedWriteGuard, Mutex as Lock, RwLock,
RwLockReadGuard as ReadGuard, RwLockWriteGuard as WriteGuard,
};

pub use std::sync::{
atomic::{AtomicBool, AtomicU32, AtomicU64, AtomicUsize},
Arc as Lrc, OnceLock, Weak,
};
Loading