From 6b40d9960ac59c4a1be90c1d31de736561178eff Mon Sep 17 00:00:00 2001 From: Kenny Kerr Date: Wed, 26 Jun 2024 18:37:54 -0500 Subject: [PATCH 1/2] conventional testing for windows_slim_errors --- .cargo/config.toml | 1 + .github/workflows/msrv-windows-result.yml | 10 ------ .github/workflows/slim_errors.yml | 41 +++++++++++++++++++++++ crates/libs/result/Cargo.toml | 3 -- crates/libs/result/src/error.rs | 9 ----- crates/libs/result/src/lib.rs | 3 -- crates/libs/result/src/tests.rs | 18 ---------- crates/tests/result/Cargo.toml | 4 +++ crates/tests/result/tests/error.rs | 10 ++++-- crates/tests/result/tests/hresult.rs | 7 +++- crates/tests/result/tests/slim_errors.rs | 12 +++++++ 11 files changed, 72 insertions(+), 46 deletions(-) create mode 100644 .github/workflows/slim_errors.yml delete mode 100644 crates/libs/result/src/tests.rs create mode 100644 crates/tests/result/tests/slim_errors.rs diff --git a/.cargo/config.toml b/.cargo/config.toml index cff486357e..081f460391 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -2,5 +2,6 @@ rustflags = [ # "--cfg", "windows_debugger_visualizer", # "--cfg", "windows_raw_dylib", + # "--cfg", "windows_slim_errors", # "-C", "target-feature=+crt-static", ] diff --git a/.github/workflows/msrv-windows-result.yml b/.github/workflows/msrv-windows-result.yml index 602c22b9d1..baef56a15a 100644 --- a/.github/workflows/msrv-windows-result.yml +++ b/.github/workflows/msrv-windows-result.yml @@ -29,13 +29,3 @@ jobs: run: cargo check -p windows-result --all-features - name: Check Default Features run: cargo check -p windows-result - - name: Check Slim Errors - shell: pwsh - run: | - $ErrorActionPreference = 'Stop' - $env:RUSTFLAGS = '--cfg=windows_slim_errors' - - # This will show the size of Error, which lets us confirm that RUSTFLAGS was set. - cargo test -p windows-result --lib -- --nocapture --test-threads=1 - - cargo check -p windows-result diff --git a/.github/workflows/slim_errors.yml b/.github/workflows/slim_errors.yml new file mode 100644 index 0000000000..f971137aed --- /dev/null +++ b/.github/workflows/slim_errors.yml @@ -0,0 +1,41 @@ +name: slim_errors + +on: + pull_request: + push: + paths-ignore: + - '.github/ISSUE_TEMPLATE/**' + branches: + - master + +env: + RUSTFLAGS: -Dwarnings --cfg windows_slim_errors + +jobs: + check: + strategy: + matrix: + include: + - target: x86_64-pc-windows-msvc + - target: i686-pc-windows-msvc + - target: x86_64-pc-windows-gnu + - target: i686-pc-windows-gnu + runs-on: + - windows-latest + - ubuntu-latest + runs-on: ${{ matrix.runs-on }} + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Update toolchain + run: rustup update --no-self-update nightly && rustup default nightly-${{ matrix.target }} + + - name: Add toolchain target + run: rustup target add ${{ matrix.target }} + + - name: Fix environment + uses: ./.github/actions/fix-environment + + - name: Test + run: cargo test -p test_result diff --git a/crates/libs/result/Cargo.toml b/crates/libs/result/Cargo.toml index 6c65a5e8f0..f553397c3c 100644 --- a/crates/libs/result/Cargo.toml +++ b/crates/libs/result/Cargo.toml @@ -21,9 +21,6 @@ workspace = true default-target = "x86_64-pc-windows-msvc" targets = [] -[dependencies] -static_assertions = "1.0" - [dependencies.windows-targets] version = "0.52.5" path = "../targets" diff --git a/crates/libs/result/src/error.rs b/crates/libs/result/src/error.rs index 64d6c4c3bb..9c448bc6e8 100644 --- a/crates/libs/result/src/error.rs +++ b/crates/libs/result/src/error.rs @@ -257,8 +257,6 @@ impl Ord for Error { } } -static_assertions::assert_impl_all!(Error: Send, Sync); - use error_info::*; #[cfg(all(windows, not(windows_slim_errors)))] @@ -395,11 +393,4 @@ mod error_info { core::ptr::null_mut() } } - - // If we are using "slim" Error objects, then we can rely on Result<()> - // having a representation that is equivalent to HRESULT. - static_assertions::const_assert_eq!( - size_of::>(), - size_of::() - ); } diff --git a/crates/libs/result/src/lib.rs b/crates/libs/result/src/lib.rs index 3207acfdc4..c636a52c53 100644 --- a/crates/libs/result/src/lib.rs +++ b/crates/libs/result/src/lib.rs @@ -36,6 +36,3 @@ pub use hresult::HRESULT; /// A specialized [`Result`] type that provides Windows error information. pub type Result = core::result::Result; - -#[cfg(test)] -mod tests; diff --git a/crates/libs/result/src/tests.rs b/crates/libs/result/src/tests.rs deleted file mode 100644 index 7e38f3b24b..0000000000 --- a/crates/libs/result/src/tests.rs +++ /dev/null @@ -1,18 +0,0 @@ -use crate::*; - -#[test] -fn show_sizes() { - use core::mem::size_of; - - macro_rules! show_size { - ($t:ty) => { - println!("size_of {} = {}", stringify!($t), size_of::<$t>()); - }; - } - - println!("sizes:"); - show_size!(Error); - show_size!(Result<()>); - show_size!(Result); - show_size!(Result); -} diff --git a/crates/tests/result/Cargo.toml b/crates/tests/result/Cargo.toml index ba88c24086..d724747228 100644 --- a/crates/tests/result/Cargo.toml +++ b/crates/tests/result/Cargo.toml @@ -16,3 +16,7 @@ path = "../../libs/targets" [dependencies] helpers = { package = "test_helpers", path = "../helpers" } +static_assertions = "1.0" + +[lints.rust] +unexpected_cfgs = { level = "warn", check-cfg = ['cfg(windows_slim_errors)'] } diff --git a/crates/tests/result/tests/error.rs b/crates/tests/result/tests/error.rs index 43b5b20af5..ee79f59faa 100644 --- a/crates/tests/result/tests/error.rs +++ b/crates/tests/result/tests/error.rs @@ -24,8 +24,14 @@ fn new() { let e = Error::new(E_INVALIDARG, "test message"); assert_eq!(e.code(), E_INVALIDARG); - assert!(!e.as_ptr().is_null()); - assert_eq!(e.message(), "test message"); + + if cfg!(windows_slim_errors) { + assert!(e.as_ptr().is_null()); + assert_eq!(e.message(), "The parameter is incorrect."); + } else { + assert!(!e.as_ptr().is_null()); + assert_eq!(e.message(), "test message"); + } } #[test] diff --git a/crates/tests/result/tests/hresult.rs b/crates/tests/result/tests/hresult.rs index 51d8f3cd4d..9cea3032a1 100644 --- a/crates/tests/result/tests/hresult.rs +++ b/crates/tests/result/tests/hresult.rs @@ -80,7 +80,12 @@ fn from_result() { let result: Result<()> = Err(Error::new(E_INVALIDARG, "test message")); let err = HRESULT::from(result).ok().unwrap_err(); assert_eq!(err.code(), E_INVALIDARG); - assert_eq!(err.message(), "test message"); + + if cfg!(windows_slim_errors) { + assert_eq!(err.message(), "The parameter is incorrect."); + } else { + assert_eq!(err.message(), "test message"); + } } #[test] diff --git a/crates/tests/result/tests/slim_errors.rs b/crates/tests/result/tests/slim_errors.rs new file mode 100644 index 0000000000..95f9955eba --- /dev/null +++ b/crates/tests/result/tests/slim_errors.rs @@ -0,0 +1,12 @@ +use windows_result::*; + +#[test] +fn show_sizes() { + use core::mem::size_of; + + static_assertions::assert_impl_all!(Error: Send, Sync); + + if cfg!(windows_slim_errors) { + assert_eq!(size_of::>(), size_of::()); + } +} From 7514b079d68c03d9959c9c5239c0967d3e9a3042 Mon Sep 17 00:00:00 2001 From: Kenny Kerr Date: Wed, 26 Jun 2024 18:49:02 -0500 Subject: [PATCH 2/2] windows only --- .github/workflows/slim_errors.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/slim_errors.yml b/.github/workflows/slim_errors.yml index f971137aed..80b27147c8 100644 --- a/.github/workflows/slim_errors.yml +++ b/.github/workflows/slim_errors.yml @@ -22,7 +22,6 @@ jobs: - target: i686-pc-windows-gnu runs-on: - windows-latest - - ubuntu-latest runs-on: ${{ matrix.runs-on }} steps: - name: Checkout