rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
94.93k stars 12.24k forks source link

compilation failure with "cargo check --tests" for espidf #125714

Open benstockil opened 1 month ago

benstockil commented 1 month ago

Running cargo check --tests fails to compile test for the esp-idf-template (tested on mcu esp32-c3, default template options).

   Compiling test v0.0.0 (/home/ben/.local/share/rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/test)
error[E0308]: mismatched types
   --> /home/ben/.local/share/rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/test/src/test_result.rs:102:18
    |
101 |         None => match status.signal() {
    |                       --------------- this expression has type `Option<i32>`
102 |             Some(libc::SIGABRT) => TestResult::TrFailed,
    |                  ^^^^^^^^^^^^^ expected `i32`, found `usize`

For more information about this error, try `rustc --explain E0308`.
error: could not compile `test` (lib) due to 1 previous error

https://github.com/rust-lang/libc/pull/3658 introduced these constants for espidf, where previously the error was a missing constant altogether (https://github.com/rust-lang/rust-analyzer/issues/16552).

Steps to Reproduce

Meta

rustc --version --verbose:

rustc 1.80.0-nightly (da159eb33 2024-05-28)
binary: rustc
commit-hash: da159eb331b27df528185c616b394bb0e1d2a4bd
commit-date: 2024-05-28
host: x86_64-unknown-linux-gnu
release: 1.80.0-nightly
LLVM version: 18.1.6
Vollbrecht commented 1 month ago

First of a short general remark, while we can push for making it possible to build crossplatform test this way they will never be runnable. For us the way forward is to build tests and use an external test harness, in the concrete case we are using the embedded-test crate for such adventures.

A current workaround to don't have rust-analyzer go insane on trying to build that tests the normal way is: Explicitly tell it inside your Cargo.toml by defining a section - depending on if you are building a lib, bin or a test - with the harness=false field. That will tell the system to not use the inbuilt rust test harness.

For your example project you would add a section like this

[[bin]]
name = "your project name"
harness = false

That said this answer is only trying to answer to the underlying issue of building test against espidf as a crossplatform target and not going into the detail of defining the right or wrong things inside rust libc