Skip to content
Open
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
4 changes: 2 additions & 2 deletions examples/cpp/method/example_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub mod foo {
/// Generated from: examples/cpp/method/example.h;l=14
#[inline(always)]
pub unsafe fn MyMethod(__this: *mut Self) {
self::bar::MyMethod(__this)
unsafe { self::bar::MyMethod(__this) }
}
}

Expand All @@ -62,7 +62,7 @@ pub mod foo {
/// Generated from: examples/cpp/method/example.h;l=14
#[inline(always)]
pub unsafe fn MyMethod(__this: *mut crate::foo::Bar) {
crate::detail::__rust_thunk___ZN3foo3Bar8MyMethodEv(__this)
unsafe { crate::detail::__rust_thunk___ZN3foo3Bar8MyMethodEv(__this) }
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions examples/cpp/unsafe_attributes/example_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub fn SafeSignatureWithoutAnnotation() {
/// Generated from: examples/cpp/unsafe_attributes/example.h;l=11
#[inline(always)]
pub unsafe fn SafeSignatureButAnnotatedUnsafe() {
crate::detail::__rust_thunk___Z31SafeSignatureButAnnotatedUnsafev()
unsafe { crate::detail::__rust_thunk___Z31SafeSignatureButAnnotatedUnsafev() }
}

/// # Safety
Expand All @@ -38,7 +38,7 @@ pub unsafe fn SafeSignatureButAnnotatedUnsafe() {
/// Generated from: examples/cpp/unsafe_attributes/example.h;l=13
#[inline(always)]
pub unsafe fn UnsafeSignatureWithoutAnnotation(__param_0: *mut ::ffi_11::c_void) {
crate::detail::__rust_thunk___Z32UnsafeSignatureWithoutAnnotationPv(__param_0)
unsafe { crate::detail::__rust_thunk___Z32UnsafeSignatureWithoutAnnotationPv(__param_0) }
}

/// Generated from: examples/cpp/unsafe_attributes/example.h;l=14
Expand All @@ -64,7 +64,7 @@ pub fn SafeBasedOnBoolean() {
/// Generated from: examples/cpp/unsafe_attributes/example.h;l=17
#[inline(always)]
pub unsafe fn UnsafeBasedOnBoolean() {
crate::detail::__rust_thunk___Z20UnsafeBasedOnBooleanv()
unsafe { crate::detail::__rust_thunk___Z20UnsafeBasedOnBooleanv() }
}

// Generated from: nowhere/llvm/src/libcxx/include/__type_traits/integral_constant.h;l=21
Expand Down
18 changes: 10 additions & 8 deletions examples/cpp/virtual/example_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl RustDerived {
/// Generated from: examples/cpp/virtual/example.h;l=16
#[inline(always)]
pub unsafe fn Method1(__this: *const Self) -> ::ffi_11::c_int {
self::rust_derived::Method1(__this)
unsafe { self::rust_derived::Method1(__this) }
}
/// # Safety
///
Expand All @@ -49,7 +49,7 @@ impl RustDerived {
/// Generated from: examples/cpp/virtual/example.h;l=18
#[inline(always)]
pub unsafe fn Upcast(__this: *mut Self) -> *mut ::base::ExampleBase {
self::rust_derived::Upcast(__this)
unsafe { self::rust_derived::Upcast(__this) }
}
}

Expand All @@ -61,7 +61,7 @@ impl<'__unelided> ::ctor::CtorNew<::ctor::RvalueReference<'__unelided, Self>> fo
fn ctor_new(args: ::ctor::RvalueReference<'__unelided, Self>) -> Self::CtorType {
let mut __param_0 = args;
unsafe {
::ctor::FnCtor::new(move |dest: *mut Self| {
::ctor::FnCtor::new(move |dest: *mut Self| unsafe {
crate::detail::__rust_thunk___ZN11RustDerivedC1EOS_(
dest as *mut ::core::ffi::c_void,
__param_0,
Expand All @@ -84,7 +84,7 @@ impl<'__unelided> ::ctor::CtorNew<(::ctor::RvalueReference<'__unelided, Self>,)>
impl ::ctor::PinnedDrop for RustDerived {
#[inline(always)]
unsafe fn pinned_drop<'a>(self: ::core::pin::Pin<&'a mut Self>) {
crate::detail::__rust_thunk___ZN11RustDerivedD1Ev(self)
unsafe { crate::detail::__rust_thunk___ZN11RustDerivedD1Ev(self) }
}
}

Expand All @@ -106,7 +106,7 @@ impl ::ctor::CtorNew<::definition::RustDerived> for RustDerived {
fn ctor_new(args: ::definition::RustDerived) -> Self::CtorType {
let mut rust = args;
unsafe {
::ctor::FnCtor::new(move |dest: *mut Self| {
::ctor::FnCtor::new(move |dest: *mut Self| unsafe {
crate::detail::__rust_thunk___ZN11RustDerivedC1EN10definition11RustDerivedE(
dest as *mut ::core::ffi::c_void,
&mut rust,
Expand All @@ -128,7 +128,9 @@ impl ::ctor::CtorNew<(::definition::RustDerived,)> for RustDerived {
unsafe impl ::operator::Delete for crate::RustDerived {
#[inline(always)]
unsafe fn delete(p: *mut Self) {
crate::detail::__crubit_operator_delete__11RustDerived___2f_2fthird_5fparty_2fcrubit_2fexamples_2fcpp_2fvirtual_3aexample_5flib(p);
unsafe {
crate::detail::__crubit_operator_delete__11RustDerived___2f_2fthird_5fparty_2fcrubit_2fexamples_2fcpp_2fvirtual_3aexample_5flib(p);
}
}
}

Expand All @@ -141,7 +143,7 @@ pub mod rust_derived {
/// Generated from: examples/cpp/virtual/example.h;l=16
#[inline(always)]
pub unsafe fn Method1(__this: *const crate::RustDerived) -> ::ffi_11::c_int {
crate::detail::__rust_thunk___ZNK11RustDerived7Method1Ev(__this)
unsafe { crate::detail::__rust_thunk___ZNK11RustDerived7Method1Ev(__this) }
}
/// # Safety
///
Expand All @@ -151,7 +153,7 @@ pub mod rust_derived {
/// Generated from: examples/cpp/virtual/example.h;l=18
#[inline(always)]
pub unsafe fn Upcast(__this: *mut crate::RustDerived) -> *mut ::base::ExampleBase {
crate::detail::__rust_thunk___ZN11RustDerived6UpcastEv(__this)
unsafe { crate::detail::__rust_thunk___ZN11RustDerived6UpcastEv(__this) }
}
}

Expand Down
2 changes: 1 addition & 1 deletion rs_bindings_from_cc/bazel_support/compile_rust.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def compile_rust(ctx, attr, src, extra_srcs, deps, crate_name, include_coverage,
aliases = aliases,
output = lib,
metadata = rmeta,
edition = "2018",
edition = "2024",
is_test = False,
rustc_env = {},
compile_data = depset([]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ pub fn generated_items_to_tokens<'db>(
quote! {
unsafe impl oops::Inherits<#base_name> for #derived_name {
unsafe fn upcast_ptr(derived: *const Self) -> *const #base_name {
#body
unsafe { #body }
}
}
__NEWLINE__
Expand All @@ -797,7 +797,7 @@ pub fn generated_items_to_tokens<'db>(
unsafe impl ::operator::Delete for #record_type {
#[inline(always)]
unsafe fn delete(p: *mut Self) {
#crate_root_path::detail::#thunk_ident(p);
unsafe { #crate_root_path::detail::#thunk_ident(p); }
}
}
__NEWLINE__
Expand Down Expand Up @@ -902,7 +902,7 @@ pub fn generated_items_to_tokens<'db>(
link_name.as_deref().map(|link_name| quote! { #[link_name = #link_name] });
let mut_kw = if *is_mut { Some(quote! { mut }) } else { None };
quote! {
extern "C" {
unsafe extern "C" {
#link_name_attr
#visibility static #mut_kw #ident: #type_tokens;
}
Expand Down
35 changes: 21 additions & 14 deletions rs_bindings_from_cc/generate_bindings/generate_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1146,21 +1146,25 @@ fn generate_func_body(
CrubitAbiTypeToRustExprTokens(&crubit_abi_type);
quote! {
::bridge_rust::unstable_return!(@ #crubit_abi_type_expr_tokens, #crubit_abi_type_tokens, |__return_abi_buffer| {
#crate_root_path::detail::#thunk_ident(
__return_abi_buffer,
#(#clone_prefixes #thunk_args #clone_suffixes ),*
);
unsafe {
#crate_root_path::detail::#thunk_ident(
__return_abi_buffer,
#(#clone_prefixes #thunk_args #clone_suffixes ),*
);
}
})
}
}
PassingConvention::Ctor => {
quote! {
::ctor::FnCtor::new(
move |dest: *mut #return_type_or_self| {
#crate_root_path::detail::#thunk_ident(
dest as *mut ::core::ffi::c_void
#( , #thunk_args )*
);
unsafe {
#crate_root_path::detail::#thunk_ident(
dest as *mut ::core::ffi::c_void
#( , #thunk_args )*
);
}
}
)
}
Expand Down Expand Up @@ -1192,11 +1196,9 @@ fn generate_func_body(
// return_type = RsTypeKind::Primitive(PrimitiveType::Unit);
let _ = return_type; // proof that we don't need to update it.
}
// Only need to wrap everything in an `unsafe { ... }` block if
// the *whole* api function is safe.
if !impl_kind.is_unsafe() {
body = quote! { unsafe { #body } };
}
// In Rust 2024, `unsafe fn` bodies are not considered unsafe blocks,
// so we must always wrap the thunk call in an `unsafe { ... }` block.
body = quote! { unsafe { #body } };
Ok(quote! {
#thunk_prepare
#body
Expand Down Expand Up @@ -1746,6 +1748,11 @@ pub fn generate_function(

let target_record = derived_record.clone().unwrap_or_else(|| record.clone());
let mod_name = db.record_to_associated_module_name(target_record)?;
let delegate_call = if impl_kind.is_unsafe() {
quote! { unsafe { self::#mod_name::#func_name(#( #method_delegation_args ),*) } }
} else {
quote! { self::#mod_name::#func_name(#( #method_delegation_args ),*) }
};

member_functions_map.insert(
derived_record.as_deref().unwrap_or(record.as_ref()).id,
Expand All @@ -1758,7 +1765,7 @@ pub fn generate_function(
fn #bracketed_func_name #fn_generic_params(
#( #api_params ),*
) #arrow #quoted_return_type #unsatisfied_where_clause {
self::#mod_name::#func_name(#( #method_delegation_args ),*)
#delegate_call
}
}],
);
Expand Down
6 changes: 4 additions & 2 deletions rs_bindings_from_cc/test/annotations/do_not_bind_rs_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ pub mod crubit {
__this: *mut Self,
mut __param_0: crate::crubit::test::ArgumentToBoundOverload,
) {
self::struct_with_do_not_bind_method::DoNotBindMethod(__this, __param_0)
unsafe { self::struct_with_do_not_bind_method::DoNotBindMethod(__this, __param_0) }
}
}

Expand Down Expand Up @@ -176,7 +176,9 @@ pub mod crubit {
__this: *mut crate::crubit::test::StructWithDoNotBindMethod,
mut __param_0: crate::crubit::test::ArgumentToBoundOverload,
) {
crate::detail::__rust_thunk___ZN6crubit4test25StructWithDoNotBindMethod15DoNotBindMethodENS0_23ArgumentToBoundOverloadE(__this,&mut __param_0)
unsafe {
crate::detail::__rust_thunk___ZN6crubit4test25StructWithDoNotBindMethod15DoNotBindMethodENS0_23ArgumentToBoundOverloadE(__this,&mut __param_0)
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions rs_bindings_from_cc/test/annotations/owned_ptr_rs_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl RawThing {
/// Generated from: rs_bindings_from_cc/test/annotations/owned_ptr.h;l=21
#[inline(always)]
pub unsafe fn Close(__this: *mut Self) {
self::raw_thing::Close(__this)
unsafe { self::raw_thing::Close(__this) }
}
}

Expand Down Expand Up @@ -95,7 +95,7 @@ pub mod raw_thing {
/// Generated from: rs_bindings_from_cc/test/annotations/owned_ptr.h;l=21
#[inline(always)]
pub unsafe fn Close(__this: *mut crate::RawThing) {
crate::detail::__rust_thunk___ZN5Thing5CloseEv(__this)
unsafe { crate::detail::__rust_thunk___ZN5Thing5CloseEv(__this) }
}
}

Expand Down
6 changes: 4 additions & 2 deletions rs_bindings_from_cc/test/annotations/owned_ptr_user_rs_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ pub fn MakeThing(value: ::ffi_11::c_int) -> *mut ::owned_ptr::RawThing {
/// Generated from: rs_bindings_from_cc/test/annotations/owned_ptr_user.h;l=18
#[inline(always)]
pub unsafe fn ThingToValue(thingptr: ::owned_ptr::OwnedThing) -> ::ffi_11::c_int {
crate::detail::__rust_thunk___Z12ThingToValueP5Thing(::core::mem::transmute(thingptr))
unsafe {
crate::detail::__rust_thunk___Z12ThingToValueP5Thing(::core::mem::transmute(thingptr))
}
}

/// # Safety
Expand All @@ -48,7 +50,7 @@ pub unsafe fn ThingToValue(thingptr: ::owned_ptr::OwnedThing) -> ::ffi_11::c_int
/// Generated from: rs_bindings_from_cc/test/annotations/owned_ptr_user.h;l=20
#[inline(always)]
pub unsafe fn GetThingValue(thingptr: *mut ::owned_ptr::RawThing) -> ::ffi_11::c_int {
crate::detail::__rust_thunk___Z13GetThingValueP5Thing(thingptr)
unsafe { crate::detail::__rust_thunk___Z13GetThingValueP5Thing(thingptr) }
}

// Generated from: nowhere/llvm/src/libcxx/include/__type_traits/integral_constant.h;l=21
Expand Down
10 changes: 7 additions & 3 deletions rs_bindings_from_cc/test/annotations/rust_name_rs_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub mod crubit {
b: ::ffi_11::c_int,
c: ::ffi_11::c_int,
) -> Self {
self::some_struct::ConstructorNewName(a, b, c)
unsafe { self::some_struct::ConstructorNewName(a, b, c) }
}
/// # Safety
///
Expand All @@ -84,7 +84,7 @@ pub mod crubit {
/// Expanded at: rs_bindings_from_cc/test/annotations/rust_name.h;l=21
#[inline(always)]
pub unsafe fn MethodNewName(__this: *const Self) {
self::some_struct::MethodNewName(__this)
unsafe { self::some_struct::MethodNewName(__this) }
}
}

Expand Down Expand Up @@ -131,7 +131,11 @@ pub mod crubit {
/// Expanded at: rs_bindings_from_cc/test/annotations/rust_name.h;l=21
#[inline(always)]
pub unsafe fn MethodNewName(__this: *const crate::crubit::test::SomeStruct) {
crate::detail::__rust_thunk___ZNK6crubit4test10SomeStruct13MethodOldNameEv(__this)
unsafe {
crate::detail::__rust_thunk___ZNK6crubit4test10SomeStruct13MethodOldNameEv(
__this,
)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#[inline(always)]
pub fn CoReturnReference() -> ::co::Co<'static, *mut ::ffi_11::c_int> {
unsafe {
::bridge_rust::unstable_return!(@::co::internal_crubit::CoCrubitAbi::new(|consume_result_into_buffer: ::co::internal_crubit::ConsumeResultIntoBufferFn,context: *mut::core::ffi::c_void|->*mut::ffi_11::c_int{ ::bridge_rust::unstable_return!(@::bridge_rust::transmute_abi::<*mut::ffi_11::c_int>(),::bridge_rust::TransmuteAbi<*mut::ffi_11::c_int>,|buffer: *mut u8|{ (consume_result_into_buffer.unwrap())(context,buffer,<::bridge_rust::TransmuteAbi<*mut::ffi_11::c_int>as::bridge_rust::CrubitAbi>::SIZE,); }) }),::co::internal_crubit::CoCrubitAbi<*mut::ffi_11::c_int>,|__return_abi_buffer|{ crate::detail::__rust_thunk___Z17CoReturnReferencev(__return_abi_buffer,); })
::bridge_rust::unstable_return!(@::co::internal_crubit::CoCrubitAbi::new(|consume_result_into_buffer: ::co::internal_crubit::ConsumeResultIntoBufferFn,context: *mut::core::ffi::c_void|->*mut::ffi_11::c_int{ ::bridge_rust::unstable_return!(@::bridge_rust::transmute_abi::<*mut::ffi_11::c_int>(),::bridge_rust::TransmuteAbi<*mut::ffi_11::c_int>,|buffer: *mut u8|{ (consume_result_into_buffer.unwrap())(context,buffer,<::bridge_rust::TransmuteAbi<*mut::ffi_11::c_int>as::bridge_rust::CrubitAbi>::SIZE,); }) }),::co::internal_crubit::CoCrubitAbi<*mut::ffi_11::c_int>,|__return_abi_buffer|{ unsafe{ crate::detail::__rust_thunk___Z17CoReturnReferencev(__return_abi_buffer,); } })
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ impl S {
/// Generated from: rs_bindings_from_cc/test/assume_lifetimes/member_function.h;l=9
#[inline(always)]
pub fn int_accessor<'__this>(&'__this self) -> ::cref::CRef<'__this, ::ffi_11::c_int> {
self::s::int_accessor(self)
unsafe { self::s::int_accessor(self) }
}
/// Generated from: rs_bindings_from_cc/test/assume_lifetimes/member_function.h;l=10
#[inline(always)]
pub fn me<'__this>(&'__this mut self) -> ::cref::CMut<'__this, crate::S> {
self::s::me(self)
unsafe { self::s::me(self) }
}
}

Expand Down
Loading