Skip to content

Commit

Permalink
fix: revert MSRV changes (alloy-rs#789)
Browse files Browse the repository at this point in the history
* Revert "chore: bump MSRV to 1.81 & use `core::error::Error` in place of `std` (alloy-rs#780)"

This reverts commit 7b84276.

* Revert "chore: address MSRV TODOs for 1.81 (alloy-rs#781)"

This reverts commit d37a5aa.
  • Loading branch information
DaniPopes authored and ZelionD committed Nov 11, 2024
1 parent c7a9564 commit b2eef09
Show file tree
Hide file tree
Showing 14 changed files with 75 additions and 33 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
rust: [
"stable",
"nightly",
"1.81", # MSRV
"1.79", # MSRV
]
flags: [
# No features
Expand All @@ -35,10 +35,10 @@ jobs:
include:
# MSRV features
- os: "ubuntu-latest"
rust: "1.81" # MSRV
rust: "1.79" # MSRV
flags: "--features json"
- os: "windows-latest"
rust: "1.81" # MSRV
rust: "1.79" # MSRV
flags: "--features json"
# All features
- os: "ubuntu-latest"
Expand All @@ -57,10 +57,10 @@ jobs:
cache-on-failure: true
# Only run tests on latest stable and above
- name: build
if: ${{ matrix.rust == '1.81' }} # MSRV
if: ${{ matrix.rust == '1.79' }} # MSRV
run: cargo build --workspace ${{ matrix.flags }}
- name: test
if: ${{ matrix.rust != '1.81' }} # MSRV
if: ${{ matrix.rust != '1.79' }} # MSRV
run: cargo test --workspace ${{ matrix.flags }}

miri:
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ resolver = "2"
[workspace.package]
version = "0.8.9"
edition = "2021"
rust-version = "1.81"
rust-version = "1.79"
authors = ["Alloy Contributors"]
license = "MIT OR Apache-2.0"
homepage = "https://github.com/alloy-rs/core"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ When updating this, also update:
- .github/workflows/ci.yml
-->

The current MSRV (minimum supported rust version) is 1.81.
The current MSRV (minimum supported rust version) is 1.79.

Alloy will keep a rolling MSRV policy of **at least** two versions behind the
latest stable release (so if the latest stable release is 1.58, we would
Expand Down
2 changes: 1 addition & 1 deletion clippy.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
msrv = "1.81"
msrv = "1.79"
3 changes: 2 additions & 1 deletion crates/dyn-abi/src/coerce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,8 @@ enum Error {
EmptyHexStringWithoutPrefix,
}

impl core::error::Error for Error {}
#[cfg(feature = "std")]
impl std::error::Error for Error {}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand Down
4 changes: 2 additions & 2 deletions crates/primitives/src/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub enum ToSqlError {
Overflow(usize, Type),
}

impl core::error::Error for ToSqlError {}
impl std::error::Error for ToSqlError {}

/// Convert to Postgres types.
///
Expand Down Expand Up @@ -225,7 +225,7 @@ pub enum FromSqlError {
ParseError(Type),
}

impl core::error::Error for FromSqlError {}
impl std::error::Error for FromSqlError {}

impl<'a, const BITS: usize, const LIMBS: usize> FromSql<'a> for Signed<BITS, LIMBS> {
fn accepts(ty: &Type) -> bool {
Expand Down
3 changes: 2 additions & 1 deletion crates/primitives/src/signed/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ impl fmt::Display for ParseSignedError {
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct BigIntConversionError;

impl core::error::Error for BigIntConversionError {}
#[cfg(feature = "std")]
impl std::error::Error for BigIntConversionError {}

impl fmt::Display for BigIntConversionError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand Down
3 changes: 2 additions & 1 deletion crates/sol-type-parser/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ pub type Result<T, E = Error> = core::result::Result<T, E>;
#[derive(Clone, PartialEq, Eq)]
pub struct Error(Repr);

impl core::error::Error for Error {}
#[cfg(feature = "std")]
impl std::error::Error for Error {}

impl fmt::Debug for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand Down
3 changes: 2 additions & 1 deletion crates/sol-type-parser/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ impl fmt::Display for CustomError {
}
}

impl core::error::Error for CustomError {}
#[cfg(feature = "std")]
impl std::error::Error for CustomError {}

pub type Input<'a> = winnow::Stateful<&'a str, RecursionCheck>;

Expand Down
20 changes: 6 additions & 14 deletions crates/sol-types/src/abi/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,6 @@ impl Encoder {
}
}

/// Return a reference to the encoded words.
#[inline]
pub fn words(&self) -> &[Word] {
&self.buf
}

/// Finish the encoding process, returning the encoded words.
///
/// Use `into_bytes` instead to flatten the words into bytes.
Expand All @@ -59,18 +53,16 @@ impl Encoder {
self.buf
}

/// Return a reference to the encoded bytes.
#[inline]
pub fn bytes(&self) -> &[u8] {
// SAFETY: `#[repr(transparent)] FixedBytes<N>([u8; N])`
unsafe { &*(self.words() as *const [Word] as *const [[u8; 32]]) }.as_flattened()
}

/// Finish the encoding process, returning the encoded bytes.
#[inline]
pub fn into_bytes(self) -> Vec<u8> {
// TODO: remove once `Vec::into_flattened` is stabilized.
// unsafe { mem::transmute::<Vec<_>, Vec<[u8; 32]>>(self.buf) }.into_flattened()

// SAFETY: `#[repr(transparent)] FixedBytes<N>([u8; N])`
unsafe { mem::transmute::<Vec<Word>, Vec<[u8; 32]>>(self.finish()) }.into_flattened()
crate::impl_core::into_flattened::<u8, 32>(unsafe {
mem::transmute::<Vec<Word>, Vec<[u8; 32]>>(self.buf)
})
}

/// Determine the current suffix offset.
Expand Down
40 changes: 40 additions & 0 deletions crates/sol-types/src/impl_core.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
//! Modified implementations of unstable libcore functions.

use alloc::vec::Vec;
use core::mem::{self, MaybeUninit};

trait Ext {
const IS_ZST: bool;
}

impl<T> Ext for T {
const IS_ZST: bool = mem::size_of::<Self>() == 0;
}

/// [`core::array::try_from_fn`]
#[inline]
pub(crate) fn try_from_fn<F, T, E, const N: usize>(mut cb: F) -> Result<[T; N], E>
Expand Down Expand Up @@ -79,3 +88,34 @@ pub(crate) unsafe fn array_assume_init<T, const N: usize>(array: [MaybeUninit<T>
unsafe fn transpose<T, const N: usize>(array: [MaybeUninit<T>; N]) -> MaybeUninit<[T; N]> {
mem::transmute_copy::<[MaybeUninit<T>; N], MaybeUninit<[T; N]>>(&mem::ManuallyDrop::new(&array))
}

// TODO(MSRV-1.80): remove
/// [`Vec::into_flattened`].
#[inline]
pub(crate) fn into_flattened<T, const N: usize>(vec: Vec<[T; N]>) -> Vec<T> {
let (ptr, len, cap) = into_raw_parts(vec);
let (new_len, new_cap) = if T::IS_ZST {
(len.checked_mul(N).expect("vec len overflow"), usize::MAX)
} else {
// SAFETY:
// - `cap * N` cannot overflow because the allocation is already in
// the address space.
// - Each `[T; N]` has `N` valid elements, so there are `len * N`
// valid elements in the allocation.
unsafe { (len.checked_mul(N).unwrap_unchecked(), cap.checked_mul(N).unwrap_unchecked()) }
};
// SAFETY:
// - `ptr` was allocated by `self`
// - `ptr` is well-aligned because `[T; N]` has the same alignment as `T`.
// - `new_cap` refers to the same sized allocation as `cap` because
// `new_cap * size_of::<T>()` == `cap * size_of::<[T; N]>()`
// - `len` <= `cap`, so `len * N` <= `cap * N`.
unsafe { Vec::from_raw_parts(ptr.cast(), new_len, new_cap) }
}

/// [`Vec::into_raw_parts`]
#[inline(always)]
fn into_raw_parts<T>(vec: Vec<T>) -> (*mut T, usize, usize) {
let mut me = mem::ManuallyDrop::new(vec);
(me.as_mut_ptr(), me.len(), me.capacity())
}
6 changes: 4 additions & 2 deletions crates/sol-types/src/types/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ impl fmt::Display for Revert {
}
}

impl core::error::Error for Revert {}
#[cfg(feature = "std")]
impl std::error::Error for Revert {}

impl AsRef<str> for Revert {
#[inline]
Expand Down Expand Up @@ -263,7 +264,8 @@ impl fmt::Display for Panic {
}
}

impl core::error::Error for Panic {}
#[cfg(feature = "std")]
impl std::error::Error for Panic {}

impl SolError for Panic {
type Parameters<'a> = (crate::sol_data::Uint<256>,);
Expand Down
8 changes: 6 additions & 2 deletions crates/sol-types/src/types/interface/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ use crate::{alloc::string::ToString, Error, Panic, Result, Revert, SolError};
use alloc::{string::String, vec::Vec};
use core::{convert::Infallible, fmt, iter::FusedIterator, marker::PhantomData};

#[cfg(feature = "std")]
use std::error::Error as StdError;

mod event;
pub use event::SolEventInterface;

Expand Down Expand Up @@ -208,9 +211,10 @@ impl<T: fmt::Display> fmt::Display for ContractError<T> {
}
}

impl<T: core::error::Error + 'static> core::error::Error for ContractError<T> {
#[cfg(feature = "std")]
impl<T: StdError + 'static> StdError for ContractError<T> {
#[inline]
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
match self {
Self::CustomError(error) => Some(error),
Self::Panic(panic) => Some(panic),
Expand Down
2 changes: 1 addition & 1 deletion crates/syn-solidity/tests/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl Drop for GitPatcher<'_> {
}
}

fn parse_file(path: &Path) -> Result<File, Box<dyn core::error::Error>> {
fn parse_file(path: &Path) -> Result<File, Box<dyn std::error::Error>> {
let solidity = fs::read_to_string(path)?;
syn::parse_str(&solidity).map_err(Into::into)
}
Expand Down

0 comments on commit b2eef09

Please sign in to comment.