Skip to content

Commit

Permalink
Adds a workaround and test for MAKEINTRESOURCE style constants (#2007)
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr committed Sep 6, 2022
1 parent 13d0062 commit 4097440
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 10 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Expand Up @@ -173,6 +173,7 @@ jobs:
cargo clippy -p test_not_dll &&
cargo clippy -p test_properties &&
cargo clippy -p test_query_signature &&
cargo clippy -p test_resources &&
cargo clippy -p test_return_struct &&
cargo clippy -p test_string_param &&
cargo clippy -p test_structs &&
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Expand Up @@ -157,6 +157,7 @@ jobs:
cargo test --target ${{ matrix.target }} -p test_not_dll &&
cargo test --target ${{ matrix.target }} -p test_properties &&
cargo test --target ${{ matrix.target }} -p test_query_signature &&
cargo test --target ${{ matrix.target }} -p test_resources &&
cargo test --target ${{ matrix.target }} -p test_return_struct &&
cargo test --target ${{ matrix.target }} -p test_string_param &&
cargo test --target ${{ matrix.target }} -p test_structs &&
Expand Down
9 changes: 7 additions & 2 deletions crates/libs/bindgen/src/constants.rs
Expand Up @@ -8,7 +8,9 @@ pub fn gen(gen: &Gen, def: Field) -> TokenStream {
let features = gen.cfg_features(&cfg);

if let Some(constant) = gen.reader.field_constant(def) {
if ty == gen.reader.constant_type(constant) {
let constant_type = gen.reader.constant_type(constant);

if ty == constant_type {
let value = gen.typed_value(&gen.reader.constant_value(constant));
quote! {
#doc
Expand All @@ -19,8 +21,11 @@ pub fn gen(gen: &Gen, def: Field) -> TokenStream {
let kind = gen.type_default_name(&ty);
let value = gen.value(&gen.reader.constant_value(constant));

let value = if gen.reader.type_underlying_type(&ty) == gen.reader.constant_type(constant) {
let value = if gen.reader.type_underlying_type(&ty) == constant_type {
value
// TODO: workaround for https://github.com/microsoft/win32metadata/issues/1029
} else if ty == Type::PCWSTR && value.0.starts_with('-') {
quote! { #value as u16 as _ }
} else {
quote! { #value as _ }
};
Expand Down
8 changes: 4 additions & 4 deletions crates/libs/sys/src/Windows/Win32/UI/Controls/mod.rs
Expand Up @@ -3604,13 +3604,13 @@ pub const TCS_TOOLTIPS: u32 = 16384u32;
#[doc = "*Required features: `\"Win32_UI_Controls\"`*"]
pub const TCS_VERTICAL: u32 = 128u32;
#[doc = "*Required features: `\"Win32_UI_Controls\"`*"]
pub const TD_ERROR_ICON: ::windows_sys::core::PCWSTR = -2i32 as _;
pub const TD_ERROR_ICON: ::windows_sys::core::PCWSTR = -2i32 as u16 as _;
#[doc = "*Required features: `\"Win32_UI_Controls\"`*"]
pub const TD_INFORMATION_ICON: ::windows_sys::core::PCWSTR = -3i32 as _;
pub const TD_INFORMATION_ICON: ::windows_sys::core::PCWSTR = -3i32 as u16 as _;
#[doc = "*Required features: `\"Win32_UI_Controls\"`*"]
pub const TD_SHIELD_ICON: ::windows_sys::core::PCWSTR = -4i32 as _;
pub const TD_SHIELD_ICON: ::windows_sys::core::PCWSTR = -4i32 as u16 as _;
#[doc = "*Required features: `\"Win32_UI_Controls\"`*"]
pub const TD_WARNING_ICON: ::windows_sys::core::PCWSTR = -1i32 as _;
pub const TD_WARNING_ICON: ::windows_sys::core::PCWSTR = -1i32 as u16 as _;
#[doc = "*Required features: `\"Win32_UI_Controls\"`*"]
pub const TMTVS_RESERVEDHIGH: u32 = 19999u32;
#[doc = "*Required features: `\"Win32_UI_Controls\"`*"]
Expand Down
8 changes: 4 additions & 4 deletions crates/libs/windows/src/Windows/Win32/UI/Controls/mod.rs
Expand Up @@ -6232,13 +6232,13 @@ pub const TCS_TOOLTIPS: u32 = 16384u32;
#[doc = "*Required features: `\"Win32_UI_Controls\"`*"]
pub const TCS_VERTICAL: u32 = 128u32;
#[doc = "*Required features: `\"Win32_UI_Controls\"`*"]
pub const TD_ERROR_ICON: ::windows::core::PCWSTR = ::windows::core::PCWSTR(-2i32 as _);
pub const TD_ERROR_ICON: ::windows::core::PCWSTR = ::windows::core::PCWSTR(-2i32 as u16 as _);
#[doc = "*Required features: `\"Win32_UI_Controls\"`*"]
pub const TD_INFORMATION_ICON: ::windows::core::PCWSTR = ::windows::core::PCWSTR(-3i32 as _);
pub const TD_INFORMATION_ICON: ::windows::core::PCWSTR = ::windows::core::PCWSTR(-3i32 as u16 as _);
#[doc = "*Required features: `\"Win32_UI_Controls\"`*"]
pub const TD_SHIELD_ICON: ::windows::core::PCWSTR = ::windows::core::PCWSTR(-4i32 as _);
pub const TD_SHIELD_ICON: ::windows::core::PCWSTR = ::windows::core::PCWSTR(-4i32 as u16 as _);
#[doc = "*Required features: `\"Win32_UI_Controls\"`*"]
pub const TD_WARNING_ICON: ::windows::core::PCWSTR = ::windows::core::PCWSTR(-1i32 as _);
pub const TD_WARNING_ICON: ::windows::core::PCWSTR = ::windows::core::PCWSTR(-1i32 as u16 as _);
#[doc = "*Required features: `\"Win32_UI_Controls\"`*"]
pub const TMTVS_RESERVEDHIGH: u32 = 19999u32;
#[doc = "*Required features: `\"Win32_UI_Controls\"`*"]
Expand Down
21 changes: 21 additions & 0 deletions crates/tests/resources/Cargo.toml
@@ -0,0 +1,21 @@
[package]
name = "test_resources"
version = "0.0.0"
authors = ["Microsoft"]
edition = "2018"

[dependencies.windows]
path = "../../libs/windows"
features = [
"Win32_Foundation",
"Win32_UI_Controls",
"Win32_UI_WindowsAndMessaging",
]

[dependencies.windows-sys]
path = "../../libs/sys"
features = [
"Win32_Foundation",
"Win32_UI_Controls",
"Win32_UI_WindowsAndMessaging",
]
1 change: 1 addition & 0 deletions crates/tests/resources/src/lib.rs
@@ -0,0 +1 @@

16 changes: 16 additions & 0 deletions crates/tests/resources/tests/sys.rs
@@ -0,0 +1,16 @@
use windows_sys::{Win32::Foundation::*, Win32::UI::Controls::*, Win32::UI::WindowsAndMessaging::*};

/// These tests ensure `MAKEINTRESOURCEW` style constants an in particular negative constants like TD_ERROR_ICON
/// work as expected.
#[test]
fn sys() {
unsafe {
assert_eq!(IDI_APPLICATION as u16, 32512);
assert_ne!(LoadIconW(0, IDI_APPLICATION), 0);
assert_eq!(GetLastError(), 0);

assert_eq!(TD_ERROR_ICON as i16, -2);
assert_eq!(LoadIconW(0, TD_ERROR_ICON), 0);
assert_eq!(GetLastError(), ERROR_RESOURCE_TYPE_NOT_FOUND);
}
}
16 changes: 16 additions & 0 deletions crates/tests/resources/tests/win.rs
@@ -0,0 +1,16 @@
use windows::{core::*, Win32::Foundation::*, Win32::UI::Controls::*, Win32::UI::WindowsAndMessaging::*};

/// These tests ensure `MAKEINTRESOURCEW` style constants an in particular negative constants like TD_ERROR_ICON
/// work as expected.
#[test]
fn win() -> Result<()> {
unsafe {
assert_eq!(IDI_APPLICATION.0 as u16, 32512);
LoadIconW(HINSTANCE::default(), IDI_APPLICATION)?;

assert_eq!(TD_ERROR_ICON.0 as i16, -2);
assert_eq!(LoadIconW(HINSTANCE::default(), TD_ERROR_ICON).unwrap_err().code(), ERROR_RESOURCE_TYPE_NOT_FOUND.into());

Ok(())
}
}

0 comments on commit 4097440

Please sign in to comment.