Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce more solid no_std testing #199

Merged
merged 3 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"] }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does it work with scale = { package = "parity-scale-codec", version = "2", default-features = false }?

Or why was it removed?

Copy link
Contributor Author

@tadeohepperle tadeohepperle Jan 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just removed it from the dependencies because it was not used before and is not needed.
Edit: I now added scale and bitvec as dependencies and make use of them in the no-std test, to check that scale-info works with them.


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() {
}
Loading