Skip to content

Commit

Permalink
feat: add more methods to index types (#156)
Browse files Browse the repository at this point in the history
* feat: add more methods to index types

* chore: cargo update

```
    Updating git repository `https://github.com/oli-obk/ui_test`
    Updating crates.io index
     Locking 59 packages to latest compatible versions
    Updating allocator-api2 v0.2.18 -> v0.2.21
    Updating alloy-json-abi v0.8.10 -> v0.8.14
    Updating alloy-primitives v0.8.10 -> v0.8.14
    Updating alloy-sol-type-parser v0.8.10 -> v0.8.14
    Updating anstyle v1.0.9 -> v1.0.10
    Updating anyhow v1.0.91 -> v1.0.94
    Updating bstr v1.10.0 -> v1.11.0
    Updating bytes v1.8.0 -> v1.9.0
    Updating cargo-platform v0.1.8 -> v0.1.9
    Updating cc v1.1.31 -> v1.2.2
    Updating clap v4.5.20 -> v4.5.23
    Updating clap_builder v4.5.20 -> v4.5.23
    Updating clap_lex v0.7.2 -> v0.7.4
    Updating const-hex v1.13.1 -> v1.14.0
    Updating cpufeatures v0.2.14 -> v0.2.16
    Updating errno v0.3.9 -> v0.3.10
    Updating fastrand v2.1.1 -> v2.2.0
    Updating generator v0.8.3 -> v0.8.4
    Updating hashbrown v0.15.0 -> v0.15.2
    Updating impl-trait-for-tuples v0.2.2 -> v0.2.3
    Updating indexmap v2.6.0 -> v2.7.0
    Updating indicatif v0.17.8 -> v0.17.9
    Removing instant v0.1.13
      Adding itertools v0.13.0
    Updating itoa v1.0.11 -> v1.0.14
    Updating js-sys v0.3.72 -> v0.3.74
    Updating libc v0.2.161 -> v0.2.167
    Updating portable-atomic v1.9.0 -> v1.10.0
    Updating proc-macro2 v1.0.89 -> v1.0.92
    Updating regex-automata v0.4.8 -> v0.4.9
    Updating rustc-hash v2.0.0 -> v2.1.0
    Updating rustfix v0.8.6 -> v0.8.7
    Updating rustix v0.38.38 -> v0.38.41
    Updating scc v2.2.4 -> v2.2.5
    Updating semver-parser v0.10.2 -> v0.10.3
    Updating serde v1.0.214 -> v1.0.215
    Updating serde_derive v1.0.214 -> v1.0.215
    Updating serde_json v1.0.132 -> v1.0.133
    Updating syn v2.0.85 -> v2.0.90
    Updating tempfile v3.13.0 -> v3.14.0
    Updating thiserror v1.0.65 -> v1.0.69 (available: v2.0.4)
    Updating thiserror-impl v1.0.65 -> v1.0.69
    Updating time v0.3.36 -> v0.3.37
    Updating time-macros v0.2.18 -> v0.2.19
    Updating tracing v0.1.40 -> v0.1.41
    Updating tracing-attributes v0.1.27 -> v0.1.28
    Updating tracing-core v0.1.32 -> v0.1.33
    Updating tracing-error v0.2.0 -> v0.2.1
    Updating tracing-subscriber v0.3.18 -> v0.3.19
    Updating tracy-client-sys v0.24.1 -> v0.24.2
    Updating unicode-ident v1.0.13 -> v1.0.14
    Updating wasm-bindgen v0.2.95 -> v0.2.97
    Updating wasm-bindgen-backend v0.2.95 -> v0.2.97
    Updating wasm-bindgen-macro v0.2.95 -> v0.2.97
    Updating wasm-bindgen-macro-support v0.2.95 -> v0.2.97
    Updating wasm-bindgen-shared v0.2.95 -> v0.2.97
    Updating web-sys v0.3.72 -> v0.3.74
      Adding web-time v1.1.0
    Updating xshell v0.2.6 -> v0.2.7
    Updating xshell-macros v0.2.6 -> v0.2.7
```

* chore: clippy
  • Loading branch information
DaniPopes authored Dec 6, 2024
1 parent f853d03 commit ebd02f8
Show file tree
Hide file tree
Showing 8 changed files with 369 additions and 217 deletions.
328 changes: 170 additions & 158 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion benches/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ solar-parse.workspace = true

solang-parser = "=0.3.4"

slang_solidity = "=0.18.2"
slang_solidity = "=0.18.3"
semver.workspace = true

[dev-dependencies]
Expand Down
42 changes: 26 additions & 16 deletions benches/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,43 @@
use solar_parse::interface::Session;
use std::{hint::black_box, io::Write, path::PathBuf, process::Stdio};
use std::{
hint::black_box,
io::Write,
path::{Path, PathBuf},
process::Stdio,
};

pub const PARSERS: &[&dyn Parser] = &[&Solc, &Solar, &Solang, &Slang];

macro_rules! include_source {
($path:literal) => {
source_from_path($path, include_str!($path))
};
}

pub fn get_srcs() -> &'static [Source] {
static CACHE: std::sync::OnceLock<Vec<Source>> = std::sync::OnceLock::new();
CACHE.get_or_init(|| {
vec![
Source { name: "empty", path: "", src: "" },
include_source!("../../testdata/Counter.sol"),
include_source!("../../testdata/solidity/test/benchmarks/verifier.sol"),
include_source!("../../testdata/solidity/test/benchmarks/OptimizorClub.sol"),
include_source!("../../testdata/UniswapV3.sol"),
include_source!("../../testdata/Solarray.sol"),
include_source!("../../testdata/console.sol"),
include_source!("../../testdata/Vm.sol"),
include_source!("../../testdata/safeconsole.sol"),
include_source("../testdata/Counter.sol"),
include_source("../testdata/solidity/test/benchmarks/verifier.sol"),
include_source("../testdata/solidity/test/benchmarks/OptimizorClub.sol"),
include_source("../testdata/UniswapV3.sol"),
include_source("../testdata/Solarray.sol"),
include_source("../testdata/console.sol"),
include_source("../testdata/Vm.sol"),
include_source("../testdata/safeconsole.sol"),
]
})
}

fn include_source(path: &'static str) -> Source {
source_from_path(
path,
std::fs::read_to_string(Path::new(env!("CARGO_MANIFEST_DIR")).join(path))
.unwrap_or_else(|e| {
panic!("failed to read {path}: {e}; you may need to initialize submodules")
})
.leak(),
)
}

fn source_from_path(path: &'static str, src: &'static str) -> Source {
Source { name: std::path::Path::new(path).file_stem().unwrap().to_str().unwrap(), path, src }
Source { name: Path::new(path).file_stem().unwrap().to_str().unwrap(), path, src }
}

#[derive(Clone, Debug)]
Expand Down
1 change: 1 addition & 0 deletions crates/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ fn run_compiler_with(args: Args, f: impl FnOnce(&Compiler) -> Result + Send) ->
Box::new(human)
}
cli::ErrorFormat::Json | cli::ErrorFormat::RustcJson => {
// `io::Stderr` is not buffered.
let writer = Box::new(std::io::BufWriter::new(std::io::stderr()));
let json = JsonEmitter::new(writer, source_map.clone())
.pretty(args.pretty_json_err)
Expand Down
123 changes: 111 additions & 12 deletions crates/data-structures/src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ macro_rules! newtype_index {
impl $crate::index::Idx for $name {
#[inline(always)]
fn from_usize(value: usize) -> Self {
Self(<$crate::index::BaseIndex32 as $crate::index::Idx>::from_usize(value))
Self::from_usize(value)
}

#[inline(always)]
fn index(self) -> usize {
<$crate::index::BaseIndex32 as $crate::index::Idx>::index(self.0)
self.index()
}
}

Expand All @@ -49,6 +49,17 @@ macro_rules! newtype_index {
Self($crate::index::BaseIndex32::new(value))
}

/// Creates a new `$name` from the given `value`.
///
/// # Safety
///
/// The caller must ensure that `value` is less than or equal to `MAX`.
#[inline(always)]
#[must_use]
$vis const unsafe fn new_unchecked(value: u32) -> Self {
Self($crate::index::BaseIndex32::new_unchecked(value))
}

/// Creates a new `$name` from the given `value`.
///
/// Returns `None` if `value` exceeds `MAX`.
Expand All @@ -61,12 +72,53 @@ macro_rules! newtype_index {
}
}

/// Creates a new `$name` from the given `value`.
///
/// # Panics
///
/// Panics if `value` exceeds `MAX`.
#[inline(always)]
#[must_use]
$vis const fn from_usize(value: usize) -> Self {
Self($crate::index::BaseIndex32::from_usize(value))
}

/// Creates a new `$name` from the given `value`.
///
/// # Safety
///
/// The caller must ensure that `value` is less than or equal to `MAX`.
#[inline(always)]
#[must_use]
$vis const unsafe fn from_usize_unchecked(value: usize) -> Self {
Self($crate::index::BaseIndex32::from_usize_unchecked(value))
}

/// Creates a new `$name` from the given `value`.
///
/// Returns `None` if `value` exceeds `MAX`.
#[inline(always)]
#[must_use]
$vis const fn try_from_usize(value: usize) -> Option<Self> {
match $crate::index::BaseIndex32::try_from_usize(value) {
Some(value) => Some(Self(value)),
None => None,
}
}

/// Returns the underlying index value.
#[inline(always)]
#[must_use]
$vis const fn get(self) -> u32 {
self.0.get()
}

/// Returns the underlying index value.
#[inline(always)]
#[must_use]
$vis const fn index(self) -> usize {
self.0.index()
}
}

$crate::newtype_index!($($rest)*);
Expand Down Expand Up @@ -102,16 +154,12 @@ macro_rules! base_index {
impl Idx for $name {
#[inline(always)]
fn from_usize(value: usize) -> Self {
if value > Self::MAX_AS as usize {
index_overflow();
}
// SAFETY: `value` is less than or equal to `MAX`.
unsafe { Self::new_unchecked(value as $primitive) }
Self::from_usize(value)
}

#[inline(always)]
fn index(self) -> usize {
self.get() as usize
self.index()
}
}

Expand All @@ -129,12 +177,12 @@ macro_rules! base_index {
/// Panics if `value` exceeds `MAX`.
#[inline(always)]
#[must_use]
#[cfg_attr(debug_assertions, track_caller)]
pub const fn new(value: $primitive) -> Self {
if value > Self::MAX_AS {
index_overflow();
match Self::try_new(value) {
Some(value) => value,
None => index_overflow(),
}
// SAFETY: `value` is less than or equal to `MAX`.
unsafe { Self::new_unchecked(value) }
}

/// Creates a new `$name` from the given `value`.
Expand All @@ -151,6 +199,35 @@ macro_rules! base_index {
}
}

/// Creates a new `$name` from the given `value`.
///
/// # Panics
///
/// Panics if `value` exceeds `MAX`.
#[inline(always)]
#[must_use]
#[cfg_attr(debug_assertions, track_caller)]
pub const fn from_usize(value: usize) -> Self {
match Self::try_from_usize(value) {
Some(value) => value,
None => index_overflow(),
}
}

/// Creates a new `$name` from the given `value`.
///
/// Returns `None` if `value` exceeds `MAX`.
#[inline(always)]
#[must_use]
pub const fn try_from_usize(value: usize) -> Option<Self> {
if value > Self::MAX_AS as usize {
None
} else {
// SAFETY: `value` is less than or equal to `MAX`.
Some(unsafe { Self::new_unchecked(value as $primitive) })
}
}

/// Creates a new `$name` from the given `value`, without checking for overflow.
///
/// # Safety
Expand All @@ -159,6 +236,8 @@ macro_rules! base_index {
#[inline(always)]
#[must_use]
pub const unsafe fn new_unchecked(value: $primitive) -> Self {
debug_assert!(value <= Self::MAX_AS);

// SAFETY: guaranteed by the caller.
#[cfg(feature = "nightly")]
return unsafe { std::intrinsics::transmute_unchecked(value) };
Expand All @@ -167,6 +246,19 @@ macro_rules! base_index {
return unsafe { Self { value: std::num::NonZero::new_unchecked(value.unchecked_add(1)) } };
}


/// 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)]
#[must_use]
pub const unsafe fn from_usize_unchecked(value: usize) -> Self {
debug_assert!(value <= Self::MAX_AS as usize);
Self::new_unchecked(value as $primitive)
}

/// Returns the underlying index value.
#[inline(always)]
#[must_use]
Expand All @@ -183,6 +275,13 @@ macro_rules! base_index {
#[cfg(not(feature = "nightly"))]
return unsafe { self.value.get().unchecked_sub(1) };
}

/// Returns the underlying index value.
#[inline(always)]
#[must_use]
pub const fn index(self) -> usize {
self.get() as usize
}
}
};
}
Expand Down
1 change: 1 addition & 0 deletions crates/interface/src/diagnostics/emitter/human.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ impl HumanEmitter {
if color_choice == ColorChoice::Auto {
color_choice = AutoStream::choice(&stderr);
}
// `io::Stderr` is not buffered.
Self::new(io::BufWriter::new(stderr), color_choice)
}

Expand Down
Loading

0 comments on commit ebd02f8

Please sign in to comment.