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..80b27147c8 --- /dev/null +++ b/.github/workflows/slim_errors.yml @@ -0,0 +1,40 @@ +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 + 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::()); + } +}