Repository: rust-lang/rust-bindgen
Pull request #2661 (2023-10-11 16:06:22)

Add thiscall_abi to stable version Rust 1.73.
1.73 Changelog:
- https://github.com/rust-lang/rust/releases/tag/1.73.0

Stabilization issue:
- https://github.com/rust-lang/rust/pull/114562/

----
 Maybe there are some tests to be fixed. I'm not quite sure how to review those.

pvdrz: It would be good if you could add a test like https://github.com/rust-lang/rust-bindgen/blob/main/bindgen-tests/tests/headers/win32-thiscall_nightly.hpp and https://github.com/rust-lang/rust-bindgen/blob/main/bindgen-tests/tests/headers/win32-thiscall_1_0.hpp using `--rust-target=1.73`.
rodrigorc: Added a test case, copied from the nightly but with stable 1.73 instead.

BTW, a bit off-topic, the `test-one.sh` script does not apply the `--target` option chosen from the header file. That took me some time to notice...
rodrigorc: It looks like there was some test that failed. I think that is because they are not obeying the `--target` option, `thiscall` exists only in i686-pc-windows-* targets, I think.

The `win32-thiscall_nightly` test doesn't fail because the `.rs` file contains this line:
```
#![cfg(feature = "nightly")]
```
So if you use a non-nightly compiler, it will just skip the whole file and trivially succeed!

I guess I could do a similar thing in my new `.rs` file: `#![cfg(target = "i686-pc-windows-msvc")]` or something, but it looks a bit like cheating.
rodrigorc: Hi @pvdrz ! Any feedback about this PR? 
tgross35: @rodrigorc can you fix the macos CI test?
rodrigorc: > @rodrigorc can you fix the macos CI test?

I just force-pushed a fix for that. I'm not totally sure if that is the proper solution, but I run it changing some things here and there and it looked mostly ok, but I don't have a macos, so I can't be sure about that part.

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index e3f2c9e9..5f39caa1 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,178 +7,184 @@
   - [Removed](#removed)
   - [Fixed](#fixed)
   - [Security](#security)
-- [0.69.1 (2023-11-02)](#0691-2023-11-02)
-  - [Fixed](#fixed-1)
-- [0.69.0 (2023-11-01)](#0690-2023-11-01)
+- [0.69.2 (2024-01-13)](#0692-2024-01-13)
   - [Added](#added-1)
   - [Changed](#changed-1)
   - [Removed](#removed-1)
-  - [Fixed](#fixed-2)
+  - [Fixed](#fixed-1)
   - [Security](#security-1)
-- [0.68.1](#0681)
-  - [Fixed](#fixed-3)
-- [0.68.0](#0680)
+- [0.69.1 (2023-11-02)](#0691-2023-11-02)
+  - [Fixed](#fixed-2)
+- [0.69.0 (2023-11-01)](#0690-2023-11-01)
   - [Added](#added-2)
   - [Changed](#changed-2)
   - [Removed](#removed-2)
+  - [Fixed](#fixed-3)
+  - [Security](#security-2)
+- [0.68.1](#0681)
   - [Fixed](#fixed-4)
-- [0.67.0](#0670)
-- [0.66.1](#0661)
-  - [Removed](#removed-3)
-- [0.66.0](#0660)
+- [0.68.0](#0680)
   - [Added](#added-3)
   - [Changed](#changed-3)
-  - [Removed](#removed-4)
-- [0.65.1](#0651)
+  - [Removed](#removed-3)
   - [Fixed](#fixed-5)
-- [0.65.0](#0650)
+- [0.67.0](#0670)
+- [0.66.1](#0661)
+  - [Removed](#removed-4)
+- [0.66.0](#0660)
   - [Added](#added-4)
   - [Changed](#changed-4)
   - [Removed](#removed-5)
-- [0.64.0](#0640)
+- [0.65.1](#0651)
+  - [Fixed](#fixed-6)
+- [0.65.0](#0650)
   - [Added](#added-5)
   - [Changed](#changed-5)
-- [0.63.0](#0630)
+  - [Removed](#removed-6)
+- [0.64.0](#0640)
   - [Added](#added-6)
   - [Changed](#changed-6)
-  - [Removed](#removed-6)
-- [0.62.0](#0620)
+- [0.63.0](#0630)
   - [Added](#added-7)
   - [Changed](#changed-7)
-  - [Fixed](#fixed-6)
-- [0.61.0](#0610)
+  - [Removed](#removed-7)
+- [0.62.0](#0620)
   - [Added](#added-8)
   - [Changed](#changed-8)
   - [Fixed](#fixed-7)
-- [0.60.1](#0601)
-  - [Fixed](#fixed-8)
-- [0.60.0](#0600)
+- [0.61.0](#0610)
   - [Added](#added-9)
-  - [Fixed](#fixed-9)
   - [Changed](#changed-9)
-  - [Removed](#removed-7)
+  - [Fixed](#fixed-8)
+- [0.60.1](#0601)
+  - [Fixed](#fixed-9)
+- [0.60.0](#0600)
+  - [Added](#added-10)
+  - [Fixed](#fixed-10)
+  - [Changed](#changed-10)
+  - [Removed](#removed-8)
 - [0.59.2](#0592)
 - [0.59.1](#0591)
-  - [Fixed](#fixed-10)
-- [0.59.0](#0590)
-  - [Added](#added-10)
   - [Fixed](#fixed-11)
-  - [Changed](#changed-10)
-- [0.58.1](#0581)
+- [0.59.0](#0590)
   - [Added](#added-11)
-- [0.58.0](#0580)
-  - [Added](#added-12)
   - [Fixed](#fixed-12)
   - [Changed](#changed-11)
-  - [Deprecated](#deprecated)
-  - [Removed](#removed-8)
-  - [Fixed](#fixed-13)
-  - [Security](#security-2)
-- [0.57.0](#0570)
+- [0.58.1](#0581)
+  - [Added](#added-12)
+- [0.58.0](#0580)
   - [Added](#added-13)
+  - [Fixed](#fixed-13)
+  - [Changed](#changed-12)
+  - [Deprecated](#deprecated)
+  - [Removed](#removed-9)
   - [Fixed](#fixed-14)
-- [0.56.0](#0560)
+  - [Security](#security-3)
+- [0.57.0](#0570)
   - [Added](#added-14)
-  - [Changed](#changed-12)
   - [Fixed](#fixed-15)
-- [0.55.1](#0551)
-  - [Fixed](#fixed-16)
-- [0.55.0](#0550)
-  - [Removed](#removed-9)
+- [0.56.0](#0560)
   - [Added](#added-15)
   - [Changed](#changed-13)
+  - [Fixed](#fixed-16)
+- [0.55.1](#0551)
   - [Fixed](#fixed-17)
-- [0.54.1](#0541)
+- [0.55.0](#0550)
+  - [Removed](#removed-10)
   - [Added](#added-16)
   - [Changed](#changed-14)
   - [Fixed](#fixed-18)
-- [0.54.0](#0540)
+- [0.54.1](#0541)
   - [Added](#added-17)
   - [Changed](#changed-15)
   - [Fixed](#fixed-19)
-- [0.53.3](#0533)
+- [0.54.0](#0540)
   - [Added](#added-18)
+  - [Changed](#changed-16)
   - [Fixed](#fixed-20)
+- [0.53.3](#0533)
+  - [Added](#added-19)
+  - [Fixed](#fixed-21)
 - [0.53.2](#0532)
-  - [Changed](#changed-16)
+  - [Changed](#changed-17)
 - [0.53.1](#0531)
-  - [Added](#added-19)
-- [0.53.0](#0530)
   - [Added](#added-20)
-  - [Changed](#changed-17)
-  - [Fixed](#fixed-21)
-- [0.52.0](#0520)
+- [0.53.0](#0530)
   - [Added](#added-21)
   - [Changed](#changed-18)
   - [Fixed](#fixed-22)
-- [0.51.1](#0511)
-  - [Fixed](#fixed-23)
+- [0.52.0](#0520)
+  - [Added](#added-22)
   - [Changed](#changed-19)
-- [0.51.0](#0510)
+  - [Fixed](#fixed-23)
+- [0.51.1](#0511)
   - [Fixed](#fixed-24)
   - [Changed](#changed-20)
-  - [Added](#added-22)
-- [0.50.0](#0500)
+- [0.51.0](#0510)
+  - [Fixed](#fixed-25)
+  - [Changed](#changed-21)
   - [Added](#added-23)
-- [0.49.3](#0493)
+- [0.50.0](#0500)
   - [Added](#added-24)
+- [0.49.3](#0493)
+  - [Added](#added-25)
 - [0.49.2](#0492)
-  - [Changed](#changed-21)
-- [0.49.1](#0491)
-  - [Fixed](#fixed-25)
   - [Changed](#changed-22)
-- [0.49.0](#0490)
-  - [Added](#added-25)
+- [0.49.1](#0491)
   - [Fixed](#fixed-26)
   - [Changed](#changed-23)
-- [0.48.1](#0481)
+- [0.49.0](#0490)
+  - [Added](#added-26)
   - [Fixed](#fixed-27)
-- [0.48.0](#0480)
   - [Changed](#changed-24)
+- [0.48.1](#0481)
   - [Fixed](#fixed-28)
-- [0.47.4](#0474)
-  - [Added](#added-26)
-- [0.47.3](#0473)
+- [0.48.0](#0480)
   - [Changed](#changed-25)
-- [0.47.2](#0472)
   - [Fixed](#fixed-29)
-- [0.47.1](#0471)
+- [0.47.4](#0474)
+  - [Added](#added-27)
+- [0.47.3](#0473)
   - [Changed](#changed-26)
+- [0.47.2](#0472)
   - [Fixed](#fixed-30)
-- [0.47.0](#0470)
+- [0.47.1](#0471)
   - [Changed](#changed-27)
   - [Fixed](#fixed-31)
-- [0.33.1 .. 0.46.0](#0331--0460)
-  - [Added](#added-27)
-  - [Removed](#removed-10)
+- [0.47.0](#0470)
   - [Changed](#changed-28)
   - [Fixed](#fixed-32)
-- [0.33.1](#0331)
+- [0.33.1 .. 0.46.0](#0331--0460)
+  - [Added](#added-28)
+  - [Removed](#removed-11)
+  - [Changed](#changed-29)
   - [Fixed](#fixed-33)
+- [0.33.1](#0331)
+  - [Fixed](#fixed-34)
 - [0.33.0](#0330)
 - [0.32.2](#0322)
-  - [Fixed](#fixed-34)
-- [0.32.1](#0321)
   - [Fixed](#fixed-35)
-- [0.32.0](#0320)
-  - [Added](#added-28)
-  - [Changed](#changed-29)
+- [0.32.1](#0321)
   - [Fixed](#fixed-36)
-- [0.31.0](#0310)
+- [0.32.0](#0320)
   - [Added](#added-29)
   - [Changed](#changed-30)
-  - [Deprecated](#deprecated-1)
-  - [Removed](#removed-11)
   - [Fixed](#fixed-37)
-- [0.30.0](#0300)
+- [0.31.0](#0310)
   - [Added](#added-30)
   - [Changed](#changed-31)
-  - [Deprecated](#deprecated-2)
+  - [Deprecated](#deprecated-1)
+  - [Removed](#removed-12)
   - [Fixed](#fixed-38)
-- [0.29.0](#0290)
+- [0.30.0](#0300)
   - [Added](#added-31)
   - [Changed](#changed-32)
+  - [Deprecated](#deprecated-2)
   - [Fixed](#fixed-39)
+- [0.29.0](#0290)
+  - [Added](#added-32)
+  - [Changed](#changed-33)
+  - [Fixed](#fixed-40)
 
 <!-- END doctoc generated TOC please keep comment here to allow auto update -->
 
@@ -190,6 +196,14 @@
 ## Fixed
 ## Security
 
+# 0.69.2 (2024-01-13)
+## Added
+## Changed
+## Removed
+## Fixed
+- Fixed generation of extern "C" blocks with llvm 18+. See #2689.
+## Security
+
 # 0.69.1 (2023-11-02)
 
 ## Fixed
diff --git a/bindgen/codegen/struct_layout.rs b/bindgen/codegen/struct_layout.rs
index 83d78361..a62da695 100644
--- a/bindgen/codegen/struct_layout.rs
+++ b/bindgen/codegen/struct_layout.rs
@@ -212,7 +212,10 @@ impl<'a> StructLayoutTracker<'a> {
                     0
                 } else if !self.is_packed {
                     self.padding_bytes(field_layout)
-                } else if let Some(l) = self.known_type_layout {
+                } else if let Some(mut l) = self.known_type_layout {
+                    if field_layout.align < l.align {
+                        l.align = field_layout.align;
+                    }
                     self.padding_bytes(l)
                 } else {
                     0
diff --git a/bindgen/features.rs b/bindgen/features.rs
index 67c6fb45..e5e20680 100644
--- a/bindgen/features.rs
+++ b/bindgen/features.rs
@@ -95,9 +95,9 @@ macro_rules! define_rust_targets {
 // not stable.
 define_rust_targets! {
     Nightly => {
-        thiscall_abi: #42202,
         vectorcall_abi,
     },
+    Stable_1_73(73) => { thiscall_abi: #42202 },
     Stable_1_71(71) => { c_unwind_abi: #106075 },
     Stable_1_68(68) => { abi_efiapi: #105795 },
     Stable_1_64(64) => { core_ffi_c: #94503 },


Test patch:
diff --git a/bindgen-tests/tests/expectations/tests/issue-2695.rs b/bindgen-tests/tests/expectations/tests/issue-2695.rs
new file mode 100644
index 00000000..ed559fb6
--- /dev/null
+++ b/bindgen-tests/tests/expectations/tests/issue-2695.rs
@@ -0,0 +1,45 @@
+#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
+#[repr(C, packed(2))]
+#[derive(Debug, Default, Copy, Clone)]
+pub struct Test {
+    pub x: ::std::os::raw::c_ulong,
+    pub a: ::std::os::raw::c_char,
+    pub b: ::std::os::raw::c_char,
+    pub c: ::std::os::raw::c_char,
+    pub __bindgen_padding_0: u8,
+}
+#[test]
+fn bindgen_test_layout_Test() {
+    const UNINIT: ::std::mem::MaybeUninit<Test> = ::std::mem::MaybeUninit::uninit();
+    let ptr = UNINIT.as_ptr();
+    assert_eq!(
+        ::std::mem::size_of::<Test>(),
+        12usize,
+        concat!("Size of: ", stringify!(Test)),
+    );
+    assert_eq!(
+        ::std::mem::align_of::<Test>(),
+        2usize,
+        concat!("Alignment of ", stringify!(Test)),
+    );
+    assert_eq!(
+        unsafe { ::std::ptr::addr_of!((*ptr).x) as usize - ptr as usize },
+        0usize,
+        concat!("Offset of field: ", stringify!(Test), "::", stringify!(x)),
+    );
+    assert_eq!(
+        unsafe { ::std::ptr::addr_of!((*ptr).a) as usize - ptr as usize },
+        8usize,
+        concat!("Offset of field: ", stringify!(Test), "::", stringify!(a)),
+    );
+    assert_eq!(
+        unsafe { ::std::ptr::addr_of!((*ptr).b) as usize - ptr as usize },
+        9usize,
+        concat!("Offset of field: ", stringify!(Test), "::", stringify!(b)),
+    );
+    assert_eq!(
+        unsafe { ::std::ptr::addr_of!((*ptr).c) as usize - ptr as usize },
+        10usize,
+        concat!("Offset of field: ", stringify!(Test), "::", stringify!(c)),
+    );
+}
diff --git a/bindgen-tests/tests/expectations/tests/win32-thiscall_1_73.rs b/bindgen-tests/tests/expectations/tests/win32-thiscall_1_73.rs
new file mode 100644
index 00000000..2faedf18
--- /dev/null
+++ b/bindgen-tests/tests/expectations/tests/win32-thiscall_1_73.rs
@@ -0,0 +1,41 @@
+#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
+#![cfg(target = "i686-pc-windows-msvc")]
+#[repr(C)]
+#[derive(Debug, Default, Copy, Clone)]
+pub struct Foo {
+    pub _address: u8,
+}
+#[test]
+fn bindgen_test_layout_Foo() {
+    assert_eq!(
+        ::std::mem::size_of::<Foo>(),
+        1usize,
+        concat!("Size of: ", stringify!(Foo)),
+    );
+    assert_eq!(
+        ::std::mem::align_of::<Foo>(),
+        1usize,
+        concat!("Alignment of ", stringify!(Foo)),
+    );
+}
+extern "thiscall" {
+    #[link_name = "\u{1}?test@Foo@@QAEXXZ"]
+    pub fn Foo_test(this: *mut Foo);
+}
+extern "thiscall" {
+    #[link_name = "\u{1}?test2@Foo@@QAEHH@Z"]
+    pub fn Foo_test2(
+        this: *mut Foo,
+        var: ::std::os::raw::c_int,
+    ) -> ::std::os::raw::c_int;
+}
+impl Foo {
+    #[inline]
+    pub unsafe fn test(&mut self) {
+        Foo_test(self)
+    }
+    #[inline]
+    pub unsafe fn test2(&mut self, var: ::std::os::raw::c_int) -> ::std::os::raw::c_int {
+        Foo_test2(self, var)
+    }
+}
diff --git a/bindgen-tests/tests/headers/issue-2695.h b/bindgen-tests/tests/headers/issue-2695.h
new file mode 100644
index 00000000..4fbcc39b
--- /dev/null
+++ b/bindgen-tests/tests/headers/issue-2695.h
@@ -0,0 +1,10 @@
+// bindgen-flags: --explicit-padding
+
+#pragma pack(2)
+
+struct Test {
+        unsigned long x;
+        char a;
+        char b;
+        char c;
+};
diff --git a/bindgen-tests/tests/headers/win32-thiscall_1_73.hpp b/bindgen-tests/tests/headers/win32-thiscall_1_73.hpp
new file mode 100644
index 00000000..8c240055
--- /dev/null
+++ b/bindgen-tests/tests/headers/win32-thiscall_1_73.hpp
@@ -0,0 +1,7 @@
+// bindgen-flags: --rust-target=1.73 --raw-line '#![cfg(target = "i686-pc-windows-msvc")]' -- --target=i686-pc-windows-msvc
+
+class Foo {
+  public:
+    void test();
+    int test2(int var);
+};