From 8587e7715a421deac58da4c400d67966d1b25798 Mon Sep 17 00:00:00 2001 From: Kenny Kerr Date: Thu, 5 Sep 2024 14:20:25 -0500 Subject: [PATCH 1/4] test_reference_float --- .github/workflows/clippy.yml | 2 + .github/workflows/test.yml | 6 ++- crates/libs/bindgen/src/metadata.rs | 4 +- crates/tests/winrt/reference_float/Cargo.toml | 23 +++++++++++ crates/tests/winrt/reference_float/build.rs | 33 +++++++++++++++ .../winrt/reference_float/src/bindings.rs | 41 +++++++++++++++++++ crates/tests/winrt/reference_float/src/lib.rs | 14 +++++++ .../winrt/reference_float/src/metadata.idl | 16 ++++++++ 8 files changed, 136 insertions(+), 3 deletions(-) create mode 100644 crates/tests/winrt/reference_float/Cargo.toml create mode 100644 crates/tests/winrt/reference_float/build.rs create mode 100644 crates/tests/winrt/reference_float/src/bindings.rs create mode 100644 crates/tests/winrt/reference_float/src/lib.rs create mode 100644 crates/tests/winrt/reference_float/src/metadata.idl diff --git a/.github/workflows/clippy.yml b/.github/workflows/clippy.yml index 6c246d5c49..dbc7ff508f 100644 --- a/.github/workflows/clippy.yml +++ b/.github/workflows/clippy.yml @@ -221,6 +221,8 @@ jobs: run: cargo clippy -p test_query_signature - name: Clippy test_readme run: cargo clippy -p test_readme + - name: Clippy test_reference_float + run: cargo clippy -p test_reference_float - name: Clippy test_registry run: cargo clippy -p test_registry - name: Clippy test_registry_default diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 860482483e..6fcb232280 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -247,16 +247,18 @@ jobs: run: cargo test -p test_query_signature --target ${{ matrix.target }} ${{ matrix.etc }} - name: Test test_readme run: cargo test -p test_readme --target ${{ matrix.target }} ${{ matrix.etc }} + - name: Test test_reference_float + run: cargo test -p test_reference_float --target ${{ matrix.target }} ${{ matrix.etc }} - name: Test test_registry run: cargo test -p test_registry --target ${{ matrix.target }} ${{ matrix.etc }} - name: Test test_registry_default run: cargo test -p test_registry_default --target ${{ matrix.target }} ${{ matrix.etc }} - name: Test test_reserved run: cargo test -p test_reserved --target ${{ matrix.target }} ${{ matrix.etc }} - - name: Test test_resources - run: cargo test -p test_resources --target ${{ matrix.target }} ${{ matrix.etc }} - name: Clean run: cargo clean + - name: Test test_resources + run: cargo test -p test_resources --target ${{ matrix.target }} ${{ matrix.etc }} - name: Test test_result run: cargo test -p test_result --target ${{ matrix.target }} ${{ matrix.etc }} - name: Test test_return_handle diff --git a/crates/libs/bindgen/src/metadata.rs b/crates/libs/bindgen/src/metadata.rs index 544eebc45e..b22d0d16a3 100644 --- a/crates/libs/bindgen/src/metadata.rs +++ b/crates/libs/bindgen/src/metadata.rs @@ -551,7 +551,9 @@ pub fn type_has_float(ty: &Type) -> bool { match ty { Type::F32 | Type::F64 => true, Type::Win32Array(ty, _) => type_has_float(ty), - Type::TypeDef(def, _) => type_def_has_float(*def), + Type::TypeDef(def, generics) => { + type_def_has_float(*def) || generics.iter().any(type_has_float) + } _ => false, } } diff --git a/crates/tests/winrt/reference_float/Cargo.toml b/crates/tests/winrt/reference_float/Cargo.toml new file mode 100644 index 0000000000..3f8d568b27 --- /dev/null +++ b/crates/tests/winrt/reference_float/Cargo.toml @@ -0,0 +1,23 @@ +[package] +name = "test_reference_float" +version = "0.0.0" +edition = "2021" +publish = false + +[lib] +crate-type = ["cdylib"] +doc = false +doctest = false + +[build-dependencies.windows-bindgen] +workspace = true + +[dependencies.windows-core] +workspace = true + +[dependencies.windows] +workspace = true +features = [ + "implement", + "Foundation", +] diff --git a/crates/tests/winrt/reference_float/build.rs b/crates/tests/winrt/reference_float/build.rs new file mode 100644 index 0000000000..493a467691 --- /dev/null +++ b/crates/tests/winrt/reference_float/build.rs @@ -0,0 +1,33 @@ +fn main() { + let mut command = std::process::Command::new("midlrt.exe"); + command.args([ + "/winrt", + "/nomidl", + "/h", + "nul", + "/metadata_dir", + "../../../libs/bindgen/default", + "/reference", + "../../../libs/bindgen/default/Windows.winmd", + "/winmd", + "metadata.winmd", + "src/metadata.idl", + ]); + + if !command.status().unwrap().success() { + panic!("Failed to run midlrt"); + } + + windows_bindgen::bindgen([ + "--in", + "metadata.winmd", + "--out", + "src/bindings.rs", + "--filter", + "test_reference_float", + "--config", + "implement", + "no-bindgen-comment", + ]) + .unwrap(); +} diff --git a/crates/tests/winrt/reference_float/src/bindings.rs b/crates/tests/winrt/reference_float/src/bindings.rs new file mode 100644 index 0000000000..203d4bc8c6 --- /dev/null +++ b/crates/tests/winrt/reference_float/src/bindings.rs @@ -0,0 +1,41 @@ +#![allow( + non_snake_case, + non_upper_case_globals, + non_camel_case_types, + dead_code, + clippy::all +)] +#[repr(C)] +#[derive(Clone, Debug, PartialEq)] +pub struct RefWithFloat { + pub ReferenceFloat: Option>, + pub ReferenceWithFloat: Option>, +} +impl windows_core::TypeKind for RefWithFloat { + type TypeKind = windows_core::CloneType; +} +impl windows_core::RuntimeType for RefWithFloat { + const SIGNATURE :windows_core::imp::ConstBuffer = windows_core::imp::ConstBuffer::from_slice ( b"struct(test_reference_float.RefWithFloat;pinterface({61c17706-2d65-11e0-9ae8-d48564015472};f4);pinterface({61c17706-2d65-11e0-9ae8-d48564015472};struct(test_reference_float.WithFloat;f4)))" ) ; +} +impl Default for RefWithFloat { + fn default() -> Self { + unsafe { core::mem::zeroed() } + } +} +#[repr(C)] +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct WithFloat { + pub Value: f32, +} +impl windows_core::TypeKind for WithFloat { + type TypeKind = windows_core::CopyType; +} +impl windows_core::RuntimeType for WithFloat { + const SIGNATURE: windows_core::imp::ConstBuffer = + windows_core::imp::ConstBuffer::from_slice(b"struct(test_reference_float.WithFloat;f4)"); +} +impl Default for WithFloat { + fn default() -> Self { + unsafe { core::mem::zeroed() } + } +} diff --git a/crates/tests/winrt/reference_float/src/lib.rs b/crates/tests/winrt/reference_float/src/lib.rs new file mode 100644 index 0000000000..7cb93020cc --- /dev/null +++ b/crates/tests/winrt/reference_float/src/lib.rs @@ -0,0 +1,14 @@ +mod bindings; +use bindings::*; +use windows::{core::*, Foundation::*}; + +#[test] +fn test() -> Result<()> { + let mut container = RefWithFloat::default(); + container.ReferenceFloat = Some(PropertyValue::CreateSingle(1.23)?.cast()?); + + // TODO: https://github.com/microsoft/windows-rs/issues/292 is needed to provide a stock implementation + // of `IReference` for arbitrary types in order to test `container.ReferenceWithFloat`. + + Ok(()) +} diff --git a/crates/tests/winrt/reference_float/src/metadata.idl b/crates/tests/winrt/reference_float/src/metadata.idl new file mode 100644 index 0000000000..e49b1d9f97 --- /dev/null +++ b/crates/tests/winrt/reference_float/src/metadata.idl @@ -0,0 +1,16 @@ +// This tests that windows-bindgen avoids deriving `Eq` for types with floating point fields. +// https://github.com/microsoft/windows-rs/issues/3220 + +namespace test_reference_float +{ + struct WithFloat + { + Single Value; + }; + + struct RefWithFloat + { + Windows.Foundation.IReference ReferenceFloat; + Windows.Foundation.IReference ReferenceWithFloat; + }; +} From ed973c99ba35e6350aadfed5f12ce9a53a3f3f21 Mon Sep 17 00:00:00 2001 From: Kenny Kerr Date: Thu, 5 Sep 2024 14:23:34 -0500 Subject: [PATCH 2/4] simpler --- .../winrt/reference_float/src/bindings.rs | 22 ++----------------- crates/tests/winrt/reference_float/src/lib.rs | 5 +---- .../winrt/reference_float/src/metadata.idl | 8 +------ 3 files changed, 4 insertions(+), 31 deletions(-) diff --git a/crates/tests/winrt/reference_float/src/bindings.rs b/crates/tests/winrt/reference_float/src/bindings.rs index 203d4bc8c6..39ddc3745c 100644 --- a/crates/tests/winrt/reference_float/src/bindings.rs +++ b/crates/tests/winrt/reference_float/src/bindings.rs @@ -8,34 +8,16 @@ #[repr(C)] #[derive(Clone, Debug, PartialEq)] pub struct RefWithFloat { - pub ReferenceFloat: Option>, - pub ReferenceWithFloat: Option>, + pub Value: Option>, } impl windows_core::TypeKind for RefWithFloat { type TypeKind = windows_core::CloneType; } impl windows_core::RuntimeType for RefWithFloat { - const SIGNATURE :windows_core::imp::ConstBuffer = windows_core::imp::ConstBuffer::from_slice ( b"struct(test_reference_float.RefWithFloat;pinterface({61c17706-2d65-11e0-9ae8-d48564015472};f4);pinterface({61c17706-2d65-11e0-9ae8-d48564015472};struct(test_reference_float.WithFloat;f4)))" ) ; + const SIGNATURE :windows_core::imp::ConstBuffer = windows_core::imp::ConstBuffer::from_slice ( b"struct(test_reference_float.RefWithFloat;pinterface({61c17706-2d65-11e0-9ae8-d48564015472};f4))" ) ; } impl Default for RefWithFloat { fn default() -> Self { unsafe { core::mem::zeroed() } } } -#[repr(C)] -#[derive(Clone, Copy, Debug, PartialEq)] -pub struct WithFloat { - pub Value: f32, -} -impl windows_core::TypeKind for WithFloat { - type TypeKind = windows_core::CopyType; -} -impl windows_core::RuntimeType for WithFloat { - const SIGNATURE: windows_core::imp::ConstBuffer = - windows_core::imp::ConstBuffer::from_slice(b"struct(test_reference_float.WithFloat;f4)"); -} -impl Default for WithFloat { - fn default() -> Self { - unsafe { core::mem::zeroed() } - } -} diff --git a/crates/tests/winrt/reference_float/src/lib.rs b/crates/tests/winrt/reference_float/src/lib.rs index 7cb93020cc..2840f056d5 100644 --- a/crates/tests/winrt/reference_float/src/lib.rs +++ b/crates/tests/winrt/reference_float/src/lib.rs @@ -5,10 +5,7 @@ use windows::{core::*, Foundation::*}; #[test] fn test() -> Result<()> { let mut container = RefWithFloat::default(); - container.ReferenceFloat = Some(PropertyValue::CreateSingle(1.23)?.cast()?); - - // TODO: https://github.com/microsoft/windows-rs/issues/292 is needed to provide a stock implementation - // of `IReference` for arbitrary types in order to test `container.ReferenceWithFloat`. + container.Value = Some(PropertyValue::CreateSingle(1.23)?.cast()?); Ok(()) } diff --git a/crates/tests/winrt/reference_float/src/metadata.idl b/crates/tests/winrt/reference_float/src/metadata.idl index e49b1d9f97..f2e3ae1f8e 100644 --- a/crates/tests/winrt/reference_float/src/metadata.idl +++ b/crates/tests/winrt/reference_float/src/metadata.idl @@ -3,14 +3,8 @@ namespace test_reference_float { - struct WithFloat - { - Single Value; - }; - struct RefWithFloat { - Windows.Foundation.IReference ReferenceFloat; - Windows.Foundation.IReference ReferenceWithFloat; + Windows.Foundation.IReference Value; }; } From 7d80edc3a82c4e4b9754f6deb22857c4100f19f3 Mon Sep 17 00:00:00 2001 From: Kenny Kerr Date: Thu, 5 Sep 2024 14:25:21 -0500 Subject: [PATCH 3/4] test --- crates/tests/winrt/reference_float/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/tests/winrt/reference_float/src/lib.rs b/crates/tests/winrt/reference_float/src/lib.rs index 2840f056d5..8371b8a472 100644 --- a/crates/tests/winrt/reference_float/src/lib.rs +++ b/crates/tests/winrt/reference_float/src/lib.rs @@ -6,6 +6,7 @@ use windows::{core::*, Foundation::*}; fn test() -> Result<()> { let mut container = RefWithFloat::default(); container.Value = Some(PropertyValue::CreateSingle(1.23)?.cast()?); + assert_eq!(container.Value.unwrap().Value()?, 1.23); Ok(()) } From e79c3f5a9f66e8d43eced2642ddf48787f202ad7 Mon Sep 17 00:00:00 2001 From: Kenny Kerr Date: Thu, 5 Sep 2024 14:45:03 -0500 Subject: [PATCH 4/4] clippy --- crates/tests/winrt/reference_float/src/lib.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/tests/winrt/reference_float/src/lib.rs b/crates/tests/winrt/reference_float/src/lib.rs index 8371b8a472..3ec53ddfa0 100644 --- a/crates/tests/winrt/reference_float/src/lib.rs +++ b/crates/tests/winrt/reference_float/src/lib.rs @@ -1,3 +1,5 @@ +#![cfg(test)] + mod bindings; use bindings::*; use windows::{core::*, Foundation::*};