Skip to content

Commit

Permalink
refactor(workspace): replace static-assertions with const fn expressi…
Browse files Browse the repository at this point in the history
…ons (#3596)
  • Loading branch information
StackOverflowExcept1on authored Jan 9, 2024
1 parent a184859 commit 4ad375b
Show file tree
Hide file tree
Showing 27 changed files with 26 additions and 66 deletions.
11 changes: 0 additions & 11 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ serde = "^1"
serde_json = "^1"
serde_yaml = "0.8.26"
sha-1 = "0.10.1"
static_assertions = "1"
subxt = "0.32.1"
subxt-metadata = "0.32.1"
subxt-codegen = "0.32.1"
Expand Down
1 change: 0 additions & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ gear-wasm-instrument.workspace = true
wasmparser.workspace = true
hex = { workspace = true, features = ["alloc"] }
hashbrown.workspace = true
static_assertions.workspace = true
paste.workspace = true
enum-iterator.workspace = true
byteorder.workspace = true
Expand Down
5 changes: 1 addition & 4 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,5 @@ pub mod reservation;
pub mod buffer;
pub mod str;

use core::mem::size_of;
use static_assertions::const_assert;

// This allows all casts from u32 into usize be safe.
const_assert!(size_of::<u32>() <= size_of::<usize>());
const _: () = assert!(core::mem::size_of::<u32>() <= core::mem::size_of::<usize>());
4 changes: 1 addition & 3 deletions core/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,7 @@ impl PageBuf {
/// Host pointer can be 64bit or less, to support both we use u64.
pub type HostPointer = u64;

static_assertions::const_assert!(
core::mem::size_of::<HostPointer>() >= core::mem::size_of::<usize>()
);
const _: () = assert!(core::mem::size_of::<HostPointer>() >= core::mem::size_of::<usize>());

/// Core memory error.
#[derive(Debug, Clone, Eq, PartialEq, derive_more::Display)]
Expand Down
2 changes: 1 addition & 1 deletion core/src/message/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub const MAX_PAYLOAD_SIZE: usize = 8 * 1024 * 1024;

// **WARNING**: do not remove this check until be sure that
// all `MAX_PAYLOAD_SIZE` conversions are safe!
static_assertions::const_assert!(MAX_PAYLOAD_SIZE <= u32::MAX as usize);
const _: () = assert!(MAX_PAYLOAD_SIZE <= u32::MAX as usize);

/// Payload size exceed error
#[derive(
Expand Down
12 changes: 6 additions & 6 deletions core/src/pages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ pub const WASM_PAGE_SIZE: usize = 0x10000;
/// native page size, so can vary.
pub const GEAR_PAGE_SIZE: usize = 0x4000;

static_assertions::const_assert!(WASM_PAGE_SIZE < u32::MAX as usize);
static_assertions::const_assert_eq!(WASM_PAGE_SIZE % GEAR_PAGE_SIZE, 0);
const _: () = assert!(WASM_PAGE_SIZE < u32::MAX as usize);
const _: () = assert!(WASM_PAGE_SIZE % GEAR_PAGE_SIZE == 0);

/// Errors when act with PageU32Size.
#[derive(Debug, Clone, derive_more::Display)]
Expand Down Expand Up @@ -64,7 +64,7 @@ pub struct GearPage(pub(crate) u32);
impl From<u16> for GearPage {
fn from(value: u16) -> Self {
// u16::MAX * GearPage::size() - 1 <= u32::MAX
static_assertions::const_assert!(GEAR_PAGE_SIZE <= 0x10000);
const _: () = assert!(GEAR_PAGE_SIZE <= 0x10000);
GearPage(value as u32)
}
}
Expand All @@ -77,7 +77,7 @@ impl From<GearPage> for u32 {

impl PageU32Size for GearPage {
fn size_non_zero() -> NonZeroU32 {
static_assertions::const_assert_ne!(GEAR_PAGE_SIZE, 0);
const _: () = assert!(GEAR_PAGE_SIZE != 0);
unsafe { NonZeroU32::new_unchecked(GEAR_PAGE_SIZE as u32) }
}

Expand All @@ -102,7 +102,7 @@ pub struct WasmPage(pub(crate) u32);
impl From<u16> for WasmPage {
fn from(value: u16) -> Self {
// u16::MAX * WasmPage::size() - 1 == u32::MAX
static_assertions::const_assert!(WASM_PAGE_SIZE == 0x10000);
const _: () = assert!(WASM_PAGE_SIZE == 0x10000);
WasmPage(value as u32)
}
}
Expand All @@ -121,7 +121,7 @@ impl PageNumber for WasmPage {

impl PageU32Size for WasmPage {
fn size_non_zero() -> NonZeroU32 {
static_assertions::const_assert_ne!(WASM_PAGE_SIZE, 0);
const _: () = assert!(WASM_PAGE_SIZE != 0);
unsafe { NonZeroU32::new_unchecked(WASM_PAGE_SIZE as u32) }
}

Expand Down
1 change: 0 additions & 1 deletion examples/stack-allocations/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ parity-scale-codec.workspace = true
gtest.workspace = true
rand_pcg.workspace = true
rand.workspace = true
static_assertions.workspace = true

[build-dependencies]
gear-wasm-builder.workspace = true
Expand Down
5 changes: 2 additions & 3 deletions examples/stack-allocations/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ mod tests {
use gtest::{Program, System};
use parity_scale_codec::Decode;
use rand::{Rng, SeedableRng};
use static_assertions::const_assert;

#[test]
fn stress() {
Expand All @@ -77,11 +76,11 @@ mod tests {
const MAX_NUMBER: u8 = 255;

// Check that check sum is less than u32::MAX
const_assert!(
const _: () = assert!(
MAX_ACTIONS_AMOUNT * MAX_NUMBER as usize * HANDLE_DATA_SIZE <= u32::MAX as usize
);
// Check that we can fit all the data in the stack (heuristic no more than 10 wasm pages)
const_assert!(MAX_ACTIONS_AMOUNT * HANDLE_DATA_SIZE <= 64 * 1024 * 10);
const _: () = assert!(MAX_ACTIONS_AMOUNT * HANDLE_DATA_SIZE <= 64 * 1024 * 10);

let from = 42;
let system = System::new();
Expand Down
1 change: 0 additions & 1 deletion gclient/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ thiserror.workspace = true
async-trait.workspace = true
url.workspace = true
wabt.workspace = true
static_assertions.workspace = true

[dev-dependencies]
tokio = { workspace = true, features = ["full"] }
Expand Down
2 changes: 1 addition & 1 deletion gclient/src/api/calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ impl GearApi {
) -> Result {
let program = self.0.api().gprog_at(program_id, block_hash).await?;

static_assertions::const_assert_eq!(WASM_PAGE_SIZE % GEAR_PAGE_SIZE, 0);
const _: () = assert!(WASM_PAGE_SIZE % GEAR_PAGE_SIZE == 0);
assert!(program.static_pages.0 > 0);
let static_page_count =
(program.static_pages.0 as usize - 1) * WASM_PAGE_SIZE / GEAR_PAGE_SIZE;
Expand Down
1 change: 0 additions & 1 deletion gcore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ gsys.workspace = true
gear-core-errors.workspace = true
gear-stack-buffer.workspace = true
codec = { workspace = true, optional = true }
static_assertions.workspace = true

[dev-dependencies]
hex-literal.workspace = true
Expand Down
5 changes: 1 addition & 4 deletions gcore/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,5 @@ pub use utils::ext;

pub use gsys::{BlockCount, BlockNumber, Gas, GasMultiplier, Percent, Value};

use core::mem::size_of;
use static_assertions::const_assert;

// This allows all casts from u32 into usize be safe.
const_assert!(size_of::<u32>() <= size_of::<usize>());
const _: () = assert!(core::mem::size_of::<u32>() <= core::mem::size_of::<usize>());
2 changes: 0 additions & 2 deletions gstd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ primitive-types = { workspace = true, features = ["scale-info"] }
scale-info = { workspace = true, features = ["derive"] }
futures = { workspace = true, features = ["alloc"] }

static_assertions.workspace = true

[features]
#! ## Default features

Expand Down
5 changes: 1 addition & 4 deletions gstd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,5 @@ pub use gstd_codegen::{async_init, async_main};
pub use prelude::*;
pub use reservations::*;

use core::mem::size_of;
use static_assertions::const_assert;

// This allows all casts from u32 into usize be safe.
const_assert!(size_of::<u32>() <= size_of::<usize>());
const _: () = assert!(core::mem::size_of::<u32>() <= core::mem::size_of::<usize>());
1 change: 0 additions & 1 deletion pallets/gear-program/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ scale-info = { workspace = true, features = ["derive"] }
primitive-types = { workspace = true, features = ["scale-info"] }
log.workspace = true
hashbrown.workspace = true
static_assertions.workspace = true

# Internal deps
common.workspace = true
Expand Down
1 change: 0 additions & 1 deletion pallets/gear/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ gear-wasm-instrument.workspace = true
derive_more.workspace = true
env_logger = { workspace = true, optional = true }
scopeguard.workspace = true
static_assertions.workspace = true

# Internal deps
common.workspace = true
Expand Down
4 changes: 2 additions & 2 deletions pallets/gear/src/schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1070,8 +1070,8 @@ impl<T: Config> Default for MemoryWeights<T> {
}

const KB_AMOUNT_IN_ONE_GEAR_PAGE: u64 = GEAR_PAGE_SIZE as u64 / KB_SIZE;
static_assertions::const_assert!(KB_AMOUNT_IN_ONE_GEAR_PAGE > 0);
static_assertions::const_assert!(GEAR_PAGE_SIZE as u64 % KB_SIZE == 0);
const _: () = assert!(KB_AMOUNT_IN_ONE_GEAR_PAGE > 0);
const _: () = assert!(GEAR_PAGE_SIZE as u64 % KB_SIZE == 0);

Self {
lazy_pages_signal_read: to_weight!(to_cost_per_gear_page!(lazy_pages_signal_read)),
Expand Down
2 changes: 0 additions & 2 deletions runtime-interface/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ sp-wasm-interface.workspace = true
byteorder.workspace = true
codec = { workspace = true }
log = { workspace = true, optional = true }
static_assertions.workspace = true


[target.'cfg(windows)'.dependencies]
winapi = { workspace = true, features = ["memoryapi"] }
Expand Down
4 changes: 1 addition & 3 deletions runtime-interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ mod gear_sandbox;
pub use gear_sandbox::init as sandbox_init;
pub use gear_sandbox::sandbox;

static_assertions::const_assert!(
core::mem::size_of::<HostPointer>() >= core::mem::size_of::<usize>()
);
const _: () = assert!(core::mem::size_of::<HostPointer>() >= core::mem::size_of::<usize>());

#[derive(Debug, Clone, Encode, Decode)]
#[codec(crate = codec)]
Expand Down
1 change: 0 additions & 1 deletion runtime/vara/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const-str.workspace = true
log.workspace = true
parity-scale-codec.workspace = true
scale-info = { workspace = true, features = ["derive"] }
static_assertions.workspace = true

# Frame deps
frame-support.workspace = true
Expand Down
5 changes: 2 additions & 3 deletions runtime/vara/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ use sp_std::{
#[cfg(feature = "std")]
use sp_version::NativeVersion;
use sp_version::RuntimeVersion;
use static_assertions::const_assert;

#[cfg(any(feature = "std", test))]
pub use frame_system::Call as SystemCall;
Expand Down Expand Up @@ -174,8 +173,8 @@ pub const BABE_GENESIS_EPOCH_CONFIG: sp_consensus_babe::BabeEpochConfiguration =

// We'll verify that WEIGHT_REF_TIME_PER_SECOND does not overflow, allowing us to use
// simple multiply and divide operators instead of saturating or checked ones.
const_assert!(WEIGHT_REF_TIME_PER_SECOND.checked_div(3).is_some());
const_assert!((WEIGHT_REF_TIME_PER_SECOND / 3).checked_mul(2).is_some());
const _: () = assert!(WEIGHT_REF_TIME_PER_SECOND.checked_div(3).is_some());
const _: () = assert!((WEIGHT_REF_TIME_PER_SECOND / 3).checked_mul(2).is_some());

/// We allow for 1/3 of block time for computations, with maximum proof size.
///
Expand Down
8 changes: 4 additions & 4 deletions scripts/src/clippy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ EOF
}

gear_clippy() {
# TODO #3452: remove `-A clippy::needless_pass_by_ref_mut` on next rust update
# TODO #3452: remove `-A clippy::needless_pass_by_ref_mut`, `-A clippy::assertions_on_constants` on next rust update
EXCLUDE_PACKAGES="--exclude vara-runtime --exclude runtime-fuzzer --exclude runtime-fuzzer-fuzz"
INCLUDE_PACKAGES="-p vara-runtime -p runtime-fuzzer -p runtime-fuzzer-fuzz"

__GEAR_WASM_BUILDER_NO_BUILD=1 SKIP_WASM_BUILD=1 SKIP_VARA_RUNTIME_WASM_BUILD=1 cargo clippy --workspace "$@" $EXCLUDE_PACKAGES -- --no-deps -D warnings -A clippy::needless_pass_by_ref_mut
__GEAR_WASM_BUILDER_NO_BUILD=1 SKIP_WASM_BUILD=1 SKIP_VARA_RUNTIME_WASM_BUILD=1 cargo clippy $INCLUDE_PACKAGES --all-features -- --no-deps -D warnings -A clippy::needless_pass_by_ref_mut
__GEAR_WASM_BUILDER_NO_BUILD=1 SKIP_WASM_BUILD=1 SKIP_VARA_RUNTIME_WASM_BUILD=1 cargo clippy --workspace "$@" $EXCLUDE_PACKAGES -- --no-deps -D warnings -A clippy::needless_pass_by_ref_mut -A clippy::assertions_on_constants
__GEAR_WASM_BUILDER_NO_BUILD=1 SKIP_WASM_BUILD=1 SKIP_VARA_RUNTIME_WASM_BUILD=1 cargo clippy $INCLUDE_PACKAGES --all-features -- --no-deps -D warnings -A clippy::needless_pass_by_ref_mut -A clippy::assertions_on_constants
}

examples_clippy() {
__GEAR_WASM_BUILDER_NO_BUILD=1 SKIP_WASM_BUILD=1 SKIP_VARA_RUNTIME_WASM_BUILD=1 cargo clippy -p "demo-*" -p test-syscalls --no-default-features "$@" -- --no-deps -D warnings -A clippy::needless_pass_by_ref_mut
__GEAR_WASM_BUILDER_NO_BUILD=1 SKIP_WASM_BUILD=1 SKIP_VARA_RUNTIME_WASM_BUILD=1 cargo clippy -p "demo-*" -p test-syscalls --no-default-features "$@" -- --no-deps -D warnings -A clippy::needless_pass_by_ref_mut -A clippy::assertions_on_constants
}
1 change: 0 additions & 1 deletion utils/runtime-fuzzer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ codec = { workspace = true, features = ["derive"] }
hex.workspace = true
log.workspace = true
sha-1.workspace = true
static_assertions.workspace = true

# Temporary deps for the reproducing crash script until #2313 is implemented
clap = { workspace = true, features = ["derive"] }
Expand Down
4 changes: 2 additions & 2 deletions utils/runtime-fuzzer/src/gear_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ use std::mem;
///
/// TODO: #3442
const MAX_PAYLOAD_SIZE: usize = 1024;
static_assertions::const_assert!(MAX_PAYLOAD_SIZE <= gear_core::message::MAX_PAYLOAD_SIZE);
const _: () = assert!(MAX_PAYLOAD_SIZE <= gear_core::message::MAX_PAYLOAD_SIZE);

/// Maximum salt size for the fuzzer - 512 bytes.
///
/// There's no need in large salts as we have only 35 extrinsics
/// for one run. Also small salt will make overall size of the
/// corpus smaller.
const MAX_SALT_SIZE: usize = 512;
static_assertions::const_assert!(MAX_SALT_SIZE <= gear_core::message::MAX_PAYLOAD_SIZE);
const _: () = assert!(MAX_SALT_SIZE <= gear_core::message::MAX_PAYLOAD_SIZE);

const ID_SIZE: usize = mem::size_of::<ProgramId>();
const GAS_AND_VALUE_SIZE: usize = mem::size_of::<(u64, u128)>();
Expand Down
1 change: 0 additions & 1 deletion utils/wasm-gen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ gsys.workspace = true
gear-utils.workspace = true
gear-core.workspace = true
wasmparser.workspace = true
static_assertions.workspace = true
thiserror.workspace = true

[dev-dependencies]
Expand Down
Loading

0 comments on commit 4ad375b

Please sign in to comment.