Skip to content

Commit

Permalink
Introduce more solid no_std testing (#199)
Browse files Browse the repository at this point in the history
* introduce more solid no-std tests

* clippy fix bc of new rust version

* include scale and bitvec in no std tests
  • Loading branch information
tadeohepperle authored Jan 17, 2024
1 parent 68c85ee commit 46b1b8f
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 14 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion src/portable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl PortableRegistry {
for param in ty.ty.type_params.iter_mut() {
let Some(ty) = &param.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:
Expand Down
13 changes: 10 additions & 3 deletions test_suite/derive_tests_no_std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
1 change: 1 addition & 0 deletions test_suite/derive_tests_no_std/rust-toolchain
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nightly
54 changes: 45 additions & 9 deletions test_suite/derive_tests_no_std/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,76 @@
// 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::<TupleStruct>::type_info().type_params.len(), 1);
assert_eq!(CLike::type_info().type_params.len(), 0);
assert_eq!(E::<CLike>::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: T,
bitvec: BitVec<u16, Lsb0>,
}

#[allow(unused)]
#[derive(TypeInfo)]
#[derive(TypeInfo, Decode, Encode)]
enum CLike {
A,
B,
C,
}

#[allow(unused)]
#[derive(TypeInfo)]
#[derive(TypeInfo, Decode, Encode)]
enum E<T> {
A(T),
B { b: T },
C,
}

fn main() {
}

0 comments on commit 46b1b8f

Please sign in to comment.