Skip to content

Commit

Permalink
Update windows-bindgen to support unsafe_op_in_unsafe_fn (#3393)
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr authored Dec 13, 2024
1 parent 103d38b commit aca1a34
Show file tree
Hide file tree
Showing 501 changed files with 356,363 additions and 236,903 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/msrv-windows-registry.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
check:
strategy:
matrix:
rust: [1.60.0, stable, nightly]
rust: [1.74.0, stable, nightly]
runs-on: windows-latest
steps:
- name: Checkout
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/msrv-windows-result.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
check:
strategy:
matrix:
rust: [1.60.0, stable, nightly]
rust: [1.74.0, stable, nightly]
runs-on:
- windows-latest
- ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/msrv-windows-strings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
check:
strategy:
matrix:
rust: [1.60.0, stable, nightly]
rust: [1.74.0, stable, nightly]
runs-on: windows-latest
steps:
- name: Checkout
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/msrv-windows-version.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
check:
strategy:
matrix:
rust: [1.60.0, stable, nightly]
rust: [1.74.0, stable, nightly]
runs-on: windows-latest
steps:
- name: Checkout
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ exclude = [
[workspace.lints.rust]
rust_2018_idioms = { level = "warn", priority = -1 }
missing_docs = "warn"
unsafe_op_in_unsafe_fn = "warn"
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(windows_raw_dylib, windows_debugger_visualizer, windows_slim_errors)'] }

[workspace.dependencies]
Expand Down
32 changes: 19 additions & 13 deletions crates/libs/bindgen/src/types/cpp_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl CppFn {
pub unsafe fn #name<#generics T>(#params) -> windows_core::Result<T> #where_clause {
#link
let mut result__ = core::ptr::null_mut();
#name(#args).and_then(||windows_core::Type::from_abi(result__))
unsafe { #name(#args).and_then(||windows_core::Type::from_abi(result__)) }
}
}
}
Expand All @@ -122,7 +122,7 @@ impl CppFn {
#[inline]
pub unsafe fn #name<#generics T>(#params result__: *mut Option<T>) -> windows_core::Result<()> #where_clause {
#link
#name(#args).ok()
unsafe { #name(#args).ok() }
}
}
}
Expand All @@ -143,8 +143,10 @@ impl CppFn {
#[inline]
pub unsafe fn #name<#generics>(#params) -> windows_core::Result<#return_type> #where_clause {
#link
let mut result__ = core::mem::zeroed();
#name(#args).#map
unsafe {
let mut result__ = core::mem::zeroed();
#name(#args).#map
}
}
}
}
Expand All @@ -156,7 +158,7 @@ impl CppFn {
#[inline]
pub unsafe fn #name<#generics>(#params) -> windows_core::Result<()> #where_clause {
#link
#name(#args).ok()
unsafe { #name(#args).ok() }
}
}
}
Expand All @@ -175,9 +177,11 @@ impl CppFn {
#[inline]
pub unsafe fn #name<#generics>(#params) -> windows_core::Result<#return_type> #where_clause {
#link
let mut result__ = core::mem::zeroed();
#name(#args);
windows_core::Type::from_abi(result__)
unsafe {
let mut result__ = core::mem::zeroed();
#name(#args);
windows_core::Type::from_abi(result__)
}
}
}
} else {
Expand All @@ -195,9 +199,11 @@ impl CppFn {
#[inline]
pub unsafe fn #name<#generics>(#params) -> #return_type #where_clause {
#link
let mut result__ = core::mem::zeroed();
#name(#args);
#map
unsafe {
let mut result__ = core::mem::zeroed();
#name(#args);
#map
}
}
}
}
Expand All @@ -213,7 +219,7 @@ impl CppFn {
#[inline]
pub unsafe fn #name<#generics>(#params) -> windows_core::Result<#return_type> #where_clause {
#link
let result__ = #name(#args);
let result__ = unsafe { #name(#args) };
(!result__.is_invalid()).then_some(result__).ok_or_else(windows_core::Error::from_win32)
}
}
Expand All @@ -223,7 +229,7 @@ impl CppFn {
#[inline]
pub unsafe fn #name<#generics>(#params) #abi_return_type #where_clause {
#link
#name(#args)
unsafe { #name(#args) }
}
}
}
Expand Down
14 changes: 9 additions & 5 deletions crates/libs/bindgen/src/types/cpp_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,16 +270,20 @@ impl CppInterface {
if has_unknown_base {
quote! {
unsafe extern "system" fn #name<Identity: #impl_name, const OFFSET: isize> #signature {
let this: &Identity = &*((this as *const *const ()).offset(OFFSET) as *const Identity);
#upcall
unsafe {
let this: &Identity = &*((this as *const *const ()).offset(OFFSET) as *const Identity);
#upcall
}
}
}
} else {
quote! {
unsafe extern "system" fn #name<Identity: #impl_name> #signature {
let this = (this as *mut *mut core::ffi::c_void) as *const windows_core::ScopedHeap;
let this = &*((*this).this as *const Identity);
#upcall
unsafe {
let this = (this as *mut *mut core::ffi::c_void) as *const windows_core::ScopedHeap;
let this = &*((*this).this as *const Identity);
#upcall
}
}
}
}
Expand Down
38 changes: 23 additions & 15 deletions crates/libs/bindgen/src/types/cpp_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ impl CppMethod {
quote! {
pub unsafe fn #name<#generics T>(&self, #params) -> windows_core::Result<T> #where_clause {
let mut result__ = core::ptr::null_mut();
(windows_core::Interface::vtable(self).#vname)(windows_core::Interface::as_raw(self),#args).and_then(||windows_core::Type::from_abi(result__))
unsafe { (windows_core::Interface::vtable(self).#vname)(windows_core::Interface::as_raw(self),#args).and_then(||windows_core::Type::from_abi(result__)) }
}
}
}
Expand All @@ -293,7 +293,7 @@ impl CppMethod {

quote! {
pub unsafe fn #name<#generics T>(&self, #params result__: *mut Option<T>) -> windows_core::Result<()> #where_clause {
(windows_core::Interface::vtable(self).#vname)(windows_core::Interface::as_raw(self),#args).ok()
unsafe { (windows_core::Interface::vtable(self).#vname)(windows_core::Interface::as_raw(self),#args).ok() }
}
}
}
Expand All @@ -316,8 +316,10 @@ impl CppMethod {

quote! {
pub unsafe fn #name<#generics>(&self, #params) -> windows_core::Result<#return_type> #where_clause {
let mut result__ = core::mem::zeroed();
(windows_core::Interface::vtable(self).#vname)(windows_core::Interface::as_raw(self),#args).#map
unsafe {
let mut result__ = core::mem::zeroed();
(windows_core::Interface::vtable(self).#vname)(windows_core::Interface::as_raw(self),#args).#map
}
}
}
}
Expand All @@ -326,7 +328,7 @@ impl CppMethod {

quote! {
pub unsafe fn #name<#generics>(&self, #params) -> windows_core::Result<()> #where_clause {
(windows_core::Interface::vtable(self).#vname)(windows_core::Interface::as_raw(self),#args).ok()
unsafe { (windows_core::Interface::vtable(self).#vname)(windows_core::Interface::as_raw(self),#args).ok() }
}
}
}
Expand All @@ -342,9 +344,11 @@ impl CppMethod {

quote! {
pub unsafe fn #name<#generics>(&self, #params) -> windows_core::Result<#return_type> #where_clause {
let mut result__ = core::mem::zeroed();
(windows_core::Interface::vtable(self).#vname)(windows_core::Interface::as_raw(self), #args);
windows_core::Type::from_abi(result__)
unsafe {
let mut result__ = core::mem::zeroed();
(windows_core::Interface::vtable(self).#vname)(windows_core::Interface::as_raw(self), #args);
windows_core::Type::from_abi(result__)
}
}
}
} else {
Expand All @@ -359,9 +363,11 @@ impl CppMethod {

quote! {
pub unsafe fn #name<#generics>(&self, #params) -> #return_type #where_clause {
let mut result__ = core::mem::zeroed();
(windows_core::Interface::vtable(self).#vname)(windows_core::Interface::as_raw(self), #args);
#map
unsafe {
let mut result__ = core::mem::zeroed();
(windows_core::Interface::vtable(self).#vname)(windows_core::Interface::as_raw(self), #args);
#map
}
}
}
}
Expand All @@ -372,9 +378,11 @@ impl CppMethod {

quote! {
pub unsafe fn #name<#generics>(&self, #params) -> #return_type #where_clause {
let mut result__ = core::mem::zeroed();
(windows_core::Interface::vtable(self).#vname)(windows_core::Interface::as_raw(self), &mut result__, #args);
result__
unsafe {
let mut result__ = core::mem::zeroed();
(windows_core::Interface::vtable(self).#vname)(windows_core::Interface::as_raw(self), &mut result__, #args);
result__
}
}
}
}
Expand All @@ -383,7 +391,7 @@ impl CppMethod {

quote! {
pub unsafe fn #name<#generics>(&self, #params) #abi_return_type #where_clause {
(windows_core::Interface::vtable(self).#vname)(windows_core::Interface::as_raw(self), #args)
unsafe { (windows_core::Interface::vtable(self).#vname)(windows_core::Interface::as_raw(self), #args) }
}
}
}
Expand Down
64 changes: 36 additions & 28 deletions crates/libs/bindgen/src/types/delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,44 +128,52 @@ impl Delegate {
#named_phantoms
};
unsafe extern "system" fn QueryInterface(this: *mut core::ffi::c_void, iid: *const windows_core::GUID, interface: *mut *mut core::ffi::c_void) -> windows_core::HRESULT {
let this = this as *mut *mut core::ffi::c_void as *mut Self;

if iid.is_null() || interface.is_null() {
return windows_core::HRESULT(-2147467261); // E_POINTER
}

*interface = if *iid == <#name as windows_core::Interface>::IID ||
*iid == <windows_core::IUnknown as windows_core::Interface>::IID ||
*iid == <windows_core::imp::IAgileObject as windows_core::Interface>::IID {
&mut (*this).vtable as *mut _ as _
unsafe {
let this = this as *mut *mut core::ffi::c_void as *mut Self;

if iid.is_null() || interface.is_null() {
return windows_core::HRESULT(-2147467261); // E_POINTER
}

*interface = if *iid == <#name as windows_core::Interface>::IID ||
*iid == <windows_core::IUnknown as windows_core::Interface>::IID ||
*iid == <windows_core::imp::IAgileObject as windows_core::Interface>::IID {
&mut (*this).vtable as *mut _ as _
} else {
core::ptr::null_mut()
};

if (*interface).is_null() {
windows_core::HRESULT(-2147467262) // E_NOINTERFACE
} else {
core::ptr::null_mut()
};

if (*interface).is_null() {
windows_core::HRESULT(-2147467262) // E_NOINTERFACE
} else {
(*this).count.add_ref();
windows_core::HRESULT(0)
(*this).count.add_ref();
windows_core::HRESULT(0)
}
}
}
unsafe extern "system" fn AddRef(this: *mut core::ffi::c_void) -> u32 {
let this = this as *mut *mut core::ffi::c_void as *mut Self;
(*this).count.add_ref()
unsafe {
let this = this as *mut *mut core::ffi::c_void as *mut Self;
(*this).count.add_ref()
}
}
unsafe extern "system" fn Release(this: *mut core::ffi::c_void) -> u32 {
let this = this as *mut *mut core::ffi::c_void as *mut Self;
let remaining = (*this).count.release();
unsafe {
let this = this as *mut *mut core::ffi::c_void as *mut Self;
let remaining = (*this).count.release();

if remaining == 0 {
let _ = windows_core::imp::Box::from_raw(this);
}
if remaining == 0 {
let _ = windows_core::imp::Box::from_raw(this);
}

remaining
remaining
}
}
unsafe extern "system" fn Invoke(#invoke_vtbl) -> windows_core::HRESULT {
let this = &mut *(this as *mut *mut core::ffi::c_void as *mut Self);
#invoke_upcall
unsafe {
let this = &mut *(this as *mut *mut core::ffi::c_void as *mut Self);
#invoke_upcall
}
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions crates/libs/bindgen/src/types/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,8 +402,10 @@ impl Interface {

quote! {
unsafe extern "system" fn #name<#constraints Identity: #impl_name <#(#generics,)*>, const OFFSET: isize> (#signature) -> windows_core::HRESULT {
let this: &Identity = &*((this as *const *const ()).offset(OFFSET) as *const Identity);
#upcall
unsafe {
let this: &Identity = &*((this as *const *const ()).offset(OFFSET) as *const Identity);
#upcall
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/libs/bindgen/src/writer/cpp_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl Writer {
unsafe fn free(&mut self) {
if !self.is_invalid() {
#link
#free(self.0 #tail);
unsafe { #free(self.0 #tail); }
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/libs/core/src/as_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub trait AsImpl<T> {
/// The caller needs to ensure that `self` is actually implemented by the
/// implementation `T`.
unsafe fn as_impl(&self) -> &T {
self.as_impl_ptr().as_ref()
unsafe { self.as_impl_ptr().as_ref() }
}

/// Returns a pointer to the implementation object.
Expand Down
Loading

0 comments on commit aca1a34

Please sign in to comment.