From 46b1b8fae46b08e0fe0217aea1fd98c1beb98905 Mon Sep 17 00:00:00 2001 From: Tadeo Hepperle <62739623+tadeohepperle@users.noreply.github.com> Date: Wed, 17 Jan 2024 13:38:38 +0100 Subject: [PATCH] Introduce more solid `no_std` testing (#199) * introduce more solid no-std tests * clippy fix bc of new rust version * include scale and bitvec in no std tests --- .github/workflows/rust.yml | 5 +- src/portable.rs | 2 +- test_suite/derive_tests_no_std/Cargo.toml | 13 +++-- test_suite/derive_tests_no_std/rust-toolchain | 1 + test_suite/derive_tests_no_std/src/main.rs | 54 +++++++++++++++---- 5 files changed, 61 insertions(+), 14 deletions(-) create mode 100644 test_suite/derive_tests_no_std/rust-toolchain diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 488b505e..409adcef 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -55,7 +55,10 @@ jobs: run: | cargo test --all --all-features + # We cannot do usual rust tests in `no_std`. Instead we perform tests in the main function. + # If any assert fails, we get an `Aborted (core dumped)` non-zero exit code. + # This should make the CI fail. - name: test no-std run: | cd ./test_suite/derive_tests_no_std - cargo build --no-default-features + cargo run --no-default-features diff --git a/src/portable.rs b/src/portable.rs index e9bc1da4..c19bb7ec 100644 --- a/src/portable.rs +++ b/src/portable.rs @@ -118,7 +118,7 @@ impl PortableRegistry { for param in ty.ty.type_params.iter_mut() { let Some(ty) = ¶m.ty else { continue }; let new_id = retain_type(ty.id, types, new_types, retained_mappings); - param.ty = Some(new_id).map(Into::into); + param.ty = Some(Into::into(new_id)); } // make sure any types inside this type are also retained and update the IDs: diff --git a/test_suite/derive_tests_no_std/Cargo.toml b/test_suite/derive_tests_no_std/Cargo.toml index 820c0efe..5f99b236 100644 --- a/test_suite/derive_tests_no_std/Cargo.toml +++ b/test_suite/derive_tests_no_std/Cargo.toml @@ -10,9 +10,16 @@ license = "Apache-2.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -scale-info = { path = "../..", default-features = false, features = ["derive"] } -scale = { package = "parity-scale-codec", version = "2", features = ["full"] } - +scale-info = { path = "../..", default-features = false, features = ["derive", "bit-vec", "decode"] } +scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive", "bit-vec"] } +bitvec = { version = "1", default-features = false, features = ["alloc"] } libc = { version = "0.2", default-features = false } +libc_alloc = { version = "1.0.6" } + +[profile.dev] +panic = "abort" + +[profile.release] +panic = "abort" [workspace] diff --git a/test_suite/derive_tests_no_std/rust-toolchain b/test_suite/derive_tests_no_std/rust-toolchain new file mode 100644 index 00000000..07ade694 --- /dev/null +++ b/test_suite/derive_tests_no_std/rust-toolchain @@ -0,0 +1 @@ +nightly \ No newline at end of file diff --git a/test_suite/derive_tests_no_std/src/main.rs b/test_suite/derive_tests_no_std/src/main.rs index e8a8fa0e..b1bfff08 100644 --- a/test_suite/derive_tests_no_std/src/main.rs +++ b/test_suite/derive_tests_no_std/src/main.rs @@ -12,27 +12,66 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. - +#![allow(internal_features)] +#![feature(lang_items, start)] +#![feature(alloc_error_handler)] #![no_std] +#[start] +fn start(_argc: isize, _argv: *const *const u8) -> isize { + test(); + 0 +} + +#[lang = "eh_personality"] +#[no_mangle] +pub extern "C" fn rust_eh_personality() {} + +#[panic_handler] +fn panic(_info: &core::panic::PanicInfo) -> ! { + unsafe { + libc::abort(); + } +} + +use libc_alloc::LibcAlloc; + +#[global_allocator] +static ALLOCATOR: LibcAlloc = LibcAlloc; + +////////////////////////////////////////////////////////////////////////////// + +// Note: Use the types in some way to make sure they are not pruned as dead code. +// If an assert fails we will get `Aborted (core dumped)`. +fn test() { + assert_eq!(UnitStruct::type_info().type_params.len(), 0); + assert_eq!(TupleStruct::type_info().type_params.len(), 0); + assert_eq!(Struct::::type_info().type_params.len(), 1); + assert_eq!(CLike::type_info().type_params.len(), 0); + assert_eq!(E::::type_info().type_params.len(), 1); +} + +use bitvec::{order::Lsb0, vec::BitVec}; +use scale::{Decode, Encode}; use scale_info::TypeInfo; #[allow(unused)] -#[derive(TypeInfo)] +#[derive(TypeInfo, Decode, Encode)] struct UnitStruct; #[allow(unused)] -#[derive(TypeInfo)] +#[derive(TypeInfo, Decode, Encode)] struct TupleStruct(u128, bool); #[allow(unused)] -#[derive(TypeInfo)] +#[derive(TypeInfo, Decode, Encode)] struct Struct { t: T, + bitvec: BitVec, } #[allow(unused)] -#[derive(TypeInfo)] +#[derive(TypeInfo, Decode, Encode)] enum CLike { A, B, @@ -40,12 +79,9 @@ enum CLike { } #[allow(unused)] -#[derive(TypeInfo)] +#[derive(TypeInfo, Decode, Encode)] enum E { A(T), B { b: T }, C, } - -fn main() { -}