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

Use conventional testing for windows_slim_errors #3132

Merged
merged 2 commits into from
Jun 28, 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
1 change: 1 addition & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
rustflags = [
# "--cfg", "windows_debugger_visualizer",
# "--cfg", "windows_raw_dylib",
# "--cfg", "windows_slim_errors",
# "-C", "target-feature=+crt-static",
]
10 changes: 0 additions & 10 deletions .github/workflows/msrv-windows-result.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
kennykerr marked this conversation as resolved.
Show resolved Hide resolved
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
40 changes: 40 additions & 0 deletions .github/workflows/slim_errors.yml
Original file line number Diff line number Diff line change
@@ -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
kennykerr marked this conversation as resolved.
Show resolved Hide resolved

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
3 changes: 0 additions & 3 deletions crates/libs/result/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ workspace = true
default-target = "x86_64-pc-windows-msvc"
targets = []

[dependencies]
static_assertions = "1.0"
kennykerr marked this conversation as resolved.
Show resolved Hide resolved

[dependencies.windows-targets]
version = "0.52.5"
path = "../targets"
9 changes: 0 additions & 9 deletions crates/libs/result/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)))]
Expand Down Expand Up @@ -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::<core::result::Result<(), Error>>(),
size_of::<HRESULT>()
);
}
3 changes: 0 additions & 3 deletions crates/libs/result/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,3 @@ pub use hresult::HRESULT;

/// A specialized [`Result`] type that provides Windows error information.
pub type Result<T> = core::result::Result<T, Error>;

#[cfg(test)]
mod tests;
18 changes: 0 additions & 18 deletions crates/libs/result/src/tests.rs

This file was deleted.

4 changes: 4 additions & 0 deletions crates/tests/result/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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)'] }
10 changes: 8 additions & 2 deletions crates/tests/result/tests/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
7 changes: 6 additions & 1 deletion crates/tests/result/tests/hresult.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
kennykerr marked this conversation as resolved.
Show resolved Hide resolved
}

#[test]
Expand Down
12 changes: 12 additions & 0 deletions crates/tests/result/tests/slim_errors.rs
Original file line number Diff line number Diff line change
@@ -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::<Result<()>>(), size_of::<HRESULT>());
}
}