Add CRUBIT_OWNER and document the CRUBIT_VIEW and CRUBIT_OWNER annotations Adds `CRUBIT_OWNER`, which expands to `[[gsl::Owner]]` under Clang, next to the existing `CRUBIT_VIEW` (`[[gsl::Pointer]]`). `annotations.h` now documents both as record-kind annotations: a view borrows what its pointer-like members point to, and an owner owns them. Generated bindings do not change: Crubit does not classify records by these annotations yet. Clang's dangling diagnostics (`-Wdangling-gsl` and related warnings) already key on `gsl::Owner` and `gsl::Pointer`, so annotating a record can change C++ diagnostics. PiperOrigin-RevId: 1036817249
diff --git a/rs_bindings_from_cc/test/annotations/BUILD b/rs_bindings_from_cc/test/annotations/BUILD index 11e5ec6..56ce489 100644 --- a/rs_bindings_from_cc/test/annotations/BUILD +++ b/rs_bindings_from_cc/test/annotations/BUILD
@@ -223,3 +223,49 @@ golden_cc = "thread_safe_api_impl.cc", golden_rs = "thread_safe_rs_api.rs", ) + +# CRUBIT_VIEW / CRUBIT_OWNER: today's bindings, without and with `unsafe_view`. +crubit_test_cc_library( + name = "view_owner", + hdrs = ["view_owner.h"], + deps = [ + "//support:annotations", + ], +) + +rust_api_from_cpp( + name = "view_owner_rust", + cpp_target = ":view_owner", +) + +golden_test( + name = "view_owner_golden_test", + basename = "view_owner", + cc_library = "view_owner", + golden_cc = "view_owner_api_impl.cc", + golden_rs = "view_owner_rs_api.rs", +) + +crubit_test_cc_library( + name = "view_owner_unsafe", + hdrs = ["view_owner_unsafe.h"], + aspect_hints = [ + "//features:unsafe_view", + ], + deps = [ + "//support:annotations", + ], +) + +rust_api_from_cpp( + name = "view_owner_unsafe_rust", + cpp_target = ":view_owner_unsafe", +) + +golden_test( + name = "view_owner_unsafe_golden_test", + basename = "view_owner_unsafe", + cc_library = "view_owner_unsafe", + golden_cc = "view_owner_unsafe_api_impl.cc", + golden_rs = "view_owner_unsafe_rs_api.rs", +)
diff --git a/rs_bindings_from_cc/test/annotations/view_owner.h b/rs_bindings_from_cc/test/annotations/view_owner.h new file mode 100644 index 0000000..21cbf77 --- /dev/null +++ b/rs_bindings_from_cc/test/annotations/view_owner.h
@@ -0,0 +1,54 @@ +// Part of the Crubit project, under the Apache License v2.0 with LLVM +// Exceptions. See /LICENSE for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +#ifndef THIRD_PARTY_CRUBIT_RS_BINDINGS_FROM_CC_TEST_ANNOTATIONS_VIEW_OWNER_H_ +#define THIRD_PARTY_CRUBIT_RS_BINDINGS_FROM_CC_TEST_ANNOTATIONS_VIEW_OWNER_H_ + +#include "support/annotations.h" + +namespace crubit::test { + +// CRUBIT_VIEW and CRUBIT_OWNER do not yet change the bindings (see +// annotations.h). This golden records today's behaviour; `view_owner_unsafe` +// shows the one effect, under the `unsafe_view` feature. + +// The pointers are private, so that a public pointer field does not make the +// records unsafe on its own. + +// A view: borrows what `data_` points to. +class CRUBIT_VIEW View final { + public: + int Get() const { return *data_; } + + private: + const int* data_ = nullptr; +}; + +// An owner: owns what `data_` points to. +class CRUBIT_OWNER Owner final { + public: + Owner() = default; + Owner(const Owner&) = delete; + Owner& operator=(const Owner&) = delete; + ~Owner() { delete data_; } + + private: + int* data_ = nullptr; +}; + +// Neither annotation, for comparison. +class Plain final { + public: + int Get() const { return *data_; } + + private: + const int* data_ = nullptr; +}; + +inline int ReadView(View v) { return v.Get(); } +inline int ReadPlain(Plain p) { return p.Get(); } + +} // namespace crubit::test + +#endif // THIRD_PARTY_CRUBIT_RS_BINDINGS_FROM_CC_TEST_ANNOTATIONS_VIEW_OWNER_H_
diff --git a/rs_bindings_from_cc/test/annotations/view_owner_api_impl.cc b/rs_bindings_from_cc/test/annotations/view_owner_api_impl.cc new file mode 100644 index 0000000..90a0900 --- /dev/null +++ b/rs_bindings_from_cc/test/annotations/view_owner_api_impl.cc
@@ -0,0 +1,82 @@ +// Part of the Crubit project, under the Apache License v2.0 with LLVM +// Exceptions. See /LICENSE for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +// Automatically @generated Rust bindings for the following C++ target: +// //rs_bindings_from_cc/test/annotations:view_owner + +#include "support/internal/cxx20_backports.h" +#include "support/internal/offsetof.h" +#include "support/internal/sizeof.h" +#include "support/internal/slot.h" + +#include <cstddef> +#include <memory> + +// Public headers of the C++ library being wrapped. +#include "rs_bindings_from_cc/test/annotations/view_owner.h" + +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wthread-safety-analysis" +#pragma clang diagnostic ignored "-Wdeprecated-declarations" + +static_assert(CRUBIT_SIZEOF(class crubit::test::View) == 8); +static_assert(alignof(class crubit::test::View) == 8); + +extern "C" void __rust_thunk___ZN6crubit4test4ViewC1Ev( + class crubit::test::View* __this) { + crubit::construct_at(__this); +} + +extern "C" int __rust_thunk___ZNK6crubit4test4View3GetEv( + class crubit::test::View const* __this) { + return __this->Get(); +} + +static_assert((int (::crubit::test::View::*)() const) & + ::crubit::test::View::Get); + +static_assert(CRUBIT_SIZEOF(class crubit::test::Owner) == 8); +static_assert(alignof(class crubit::test::Owner) == 8); + +extern "C" void __rust_thunk___ZN6crubit4test5OwnerC1Ev( + class crubit::test::Owner* __this) { + crubit::construct_at(__this); +} + +extern "C" void __rust_thunk___ZN6crubit4test5OwnerD1Ev( + class crubit::test::Owner* __this) { + std::destroy_at(__this); +} + +static_assert(CRUBIT_SIZEOF(class crubit::test::Plain) == 8); +static_assert(alignof(class crubit::test::Plain) == 8); + +extern "C" void __rust_thunk___ZN6crubit4test5PlainC1Ev( + class crubit::test::Plain* __this) { + crubit::construct_at(__this); +} + +extern "C" int __rust_thunk___ZNK6crubit4test5Plain3GetEv( + class crubit::test::Plain const* __this) { + return __this->Get(); +} + +static_assert((int (::crubit::test::Plain::*)() const) & + ::crubit::test::Plain::Get); + +extern "C" int __rust_thunk___ZN6crubit4test8ReadViewENS0_4ViewE( + class crubit::test::View* v) { + return crubit::test::ReadView(crubit::UnsafeTakeValue(v)); +} + +static_assert((int (*)(class crubit::test::View)) & ::crubit::test::ReadView); + +extern "C" int __rust_thunk___ZN6crubit4test9ReadPlainENS0_5PlainE( + class crubit::test::Plain* p) { + return crubit::test::ReadPlain(crubit::UnsafeTakeValue(p)); +} + +static_assert((int (*)(class crubit::test::Plain)) & ::crubit::test::ReadPlain); + +#pragma clang diagnostic pop
diff --git a/rs_bindings_from_cc/test/annotations/view_owner_rs_api.rs b/rs_bindings_from_cc/test/annotations/view_owner_rs_api.rs new file mode 100644 index 0000000..0fc201e --- /dev/null +++ b/rs_bindings_from_cc/test/annotations/view_owner_rs_api.rs
@@ -0,0 +1,264 @@ +// Part of the Crubit project, under the Apache License v2.0 with LLVM +// Exceptions. See /LICENSE for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +// Automatically @generated Rust bindings for the following C++ target: +// //rs_bindings_from_cc/test/annotations:view_owner + +#![rustfmt::skip] +#![feature(cfi_encoding, custom_inner_attributes, impl_trait_in_assoc_type, negative_impls)] +#![allow(stable_features)] +#![allow(improper_ctypes)] +#![allow(nonstandard_style)] +#![allow(unused)] +#![allow(deprecated)] +#![allow(unknown_lints, suspicious_runtime_symbol_definitions)] +#![deny(warnings)] +pub mod crubit { + pub mod test { + // CRUBIT_VIEW and CRUBIT_OWNER do not yet change the bindings (see + // annotations.h). This golden records today's behaviour; `view_owner_unsafe` + // shows the one effect, under the `unsafe_view` feature. + + // The pointers are private, so that a public pointer field does not make the + // records unsafe on its own. + + /// A view: borrows what `data_` points to. + #[derive(Clone, Copy, ::ctor::MoveAndAssignViaCopy)] + #[cfi_encoding = "N6crubit4test4ViewE"] + #[repr(C, align(8))] + #[cfg_attr(not(doc), doc = "CRUBIT_ANNOTATE: cpp_type=crubit :: test :: View")] + #[cfg_attr(not(doc), doc = "CRUBIT_ANNOTATE: cpp_move_constructible=")] + pub struct View { + __non_field_data: [::core::mem::MaybeUninit<u8>; 0], + /// Reason for representing this field as a blob of bytes: + /// Types of non-public C++ fields can be elided away + pub(crate) data_: [::core::mem::MaybeUninit<u8>; 8], + } + /// Cannot infer `Send` because: + /// * field `data_` is represented as opaque bytes + impl !Send for View {} + /// Cannot infer `Sync` because: + /// * field `data_` is represented as opaque bytes + impl !Sync for View {} + unsafe impl ::cxx::ExternType for View { + type Id = ::cxx::type_id!("crubit :: test :: View"); + type Kind = ::cxx::kind::Trivial; + } + forward_declare::unsafe_define!( + forward_declare::symbol!("crubit :: test :: View"), + crate::crubit::test::View + ); + impl View { + /// # Safety + /// + /// The caller must ensure that the following unsafe arguments are not misused by the function: + /// * `__this`: raw pointer + #[inline(always)] + pub unsafe fn Get(__this: *const Self) -> ::ffi_11::c_int { + unsafe { self::view::Get(__this) } + } + } + + impl Default for View { + #[inline(always)] + fn default() -> Self { + let mut tmp = ::core::mem::MaybeUninit::<Self>::zeroed(); + unsafe { + crate::detail::__rust_thunk___ZN6crubit4test4ViewC1Ev(&raw mut tmp as *mut _); + tmp.assume_init() + } + } + } + + pub mod view { + /// # Safety + /// + /// The caller must ensure that the following unsafe arguments are not misused by the function: + /// * `__this`: raw pointer + #[cfg_attr(not(doc), doc = "CRUBIT_ANNOTATE: do_not_bind=")] + #[inline(always)] + pub unsafe fn Get(__this: *const crate::crubit::test::View) -> ::ffi_11::c_int { + unsafe { crate::detail::__rust_thunk___ZNK6crubit4test4View3GetEv(__this) } + } + } + + /// An owner: owns what `data_` points to. + /// + /// Not Rust-movable: type is neither copyable nor movable + #[::ctor::recursively_pinned(PinnedDrop)] + #[cfi_encoding = "N6crubit4test5OwnerE"] + #[repr(C, align(8))] + #[cfg_attr(not(doc), doc = "CRUBIT_ANNOTATE: cpp_type=crubit :: test :: Owner")] + pub struct Owner { + __non_field_data: [::core::cell::Cell<::core::mem::MaybeUninit<u8>>; 0], + /// Reason for representing this field as a blob of bytes: + /// Types of non-public C++ fields can be elided away + pub(crate) data_: [::core::cell::Cell<::core::mem::MaybeUninit<u8>>; 8], + } + /// Cannot infer `Send` because: + /// * field `data_` is represented as opaque bytes + impl !Send for Owner {} + /// Cannot infer `Sync` because: + /// * field `data_` is represented as opaque bytes + impl !Sync for Owner {} + unsafe impl ::cxx::ExternType for Owner { + type Id = ::cxx::type_id!("crubit :: test :: Owner"); + type Kind = ::cxx::kind::Opaque; + } + forward_declare::unsafe_define!( + forward_declare::symbol!("crubit :: test :: Owner"), + crate::crubit::test::Owner + ); + + impl ::ctor::CtorNew<()> for Owner { + type CtorType = ::ctor::Ctor![Self]; + type Error = ::ctor::Infallible; + #[inline(always)] + fn ctor_new(args: ()) -> Self::CtorType { + unsafe { + ::ctor::FnCtor::new(move |__crubit_dest: *mut Self| { + let () = args; + crate::detail::__rust_thunk___ZN6crubit4test5OwnerC1Ev( + __crubit_dest as *mut ::core::ffi::c_void, + ); + }) + } + } + } + + impl ::ctor::PinnedDrop for Owner { + #[inline(always)] + unsafe fn pinned_drop<'a>(self: ::core::pin::Pin<&'a mut Self>) { + unsafe { crate::detail::__rust_thunk___ZN6crubit4test5OwnerD1Ev(self) } + } + } + + /// Neither annotation, for comparison. + #[derive(Clone, Copy, ::ctor::MoveAndAssignViaCopy)] + #[cfi_encoding = "N6crubit4test5PlainE"] + #[repr(C, align(8))] + #[cfg_attr(not(doc), doc = "CRUBIT_ANNOTATE: cpp_type=crubit :: test :: Plain")] + #[cfg_attr(not(doc), doc = "CRUBIT_ANNOTATE: cpp_move_constructible=")] + pub struct Plain { + __non_field_data: [::core::mem::MaybeUninit<u8>; 0], + /// Reason for representing this field as a blob of bytes: + /// Types of non-public C++ fields can be elided away + pub(crate) data_: [::core::mem::MaybeUninit<u8>; 8], + } + /// Cannot infer `Send` because: + /// * field `data_` is represented as opaque bytes + impl !Send for Plain {} + /// Cannot infer `Sync` because: + /// * field `data_` is represented as opaque bytes + impl !Sync for Plain {} + unsafe impl ::cxx::ExternType for Plain { + type Id = ::cxx::type_id!("crubit :: test :: Plain"); + type Kind = ::cxx::kind::Trivial; + } + forward_declare::unsafe_define!( + forward_declare::symbol!("crubit :: test :: Plain"), + crate::crubit::test::Plain + ); + impl Plain { + /// # Safety + /// + /// The caller must ensure that the following unsafe arguments are not misused by the function: + /// * `__this`: raw pointer + #[inline(always)] + pub unsafe fn Get(__this: *const Self) -> ::ffi_11::c_int { + unsafe { self::plain::Get(__this) } + } + } + + impl Default for Plain { + #[inline(always)] + fn default() -> Self { + let mut tmp = ::core::mem::MaybeUninit::<Self>::zeroed(); + unsafe { + crate::detail::__rust_thunk___ZN6crubit4test5PlainC1Ev(&raw mut tmp as *mut _); + tmp.assume_init() + } + } + } + + pub mod plain { + /// # Safety + /// + /// The caller must ensure that the following unsafe arguments are not misused by the function: + /// * `__this`: raw pointer + #[cfg_attr(not(doc), doc = "CRUBIT_ANNOTATE: do_not_bind=")] + #[inline(always)] + pub unsafe fn Get(__this: *const crate::crubit::test::Plain) -> ::ffi_11::c_int { + unsafe { crate::detail::__rust_thunk___ZNK6crubit4test5Plain3GetEv(__this) } + } + } + + #[inline(always)] + pub fn ReadView(v: crate::crubit::test::View) -> ::ffi_11::c_int { + let mut v = ::core::mem::MaybeUninit::new(v); + unsafe { + crate::detail::__rust_thunk___ZN6crubit4test8ReadViewENS0_4ViewE(v.as_mut_ptr()) + } + } + + #[inline(always)] + pub fn ReadPlain(p: crate::crubit::test::Plain) -> ::ffi_11::c_int { + let mut p = ::core::mem::MaybeUninit::new(p); + unsafe { + crate::detail::__rust_thunk___ZN6crubit4test9ReadPlainENS0_5PlainE(p.as_mut_ptr()) + } + } + } +} + +// namespace crubit::test + +mod detail { + #[allow(unused_imports)] + use super::*; + unsafe extern "C" { + pub(crate) unsafe fn __rust_thunk___ZN6crubit4test4ViewC1Ev( + __this: *mut ::core::ffi::c_void, + ); + pub(crate) unsafe fn __rust_thunk___ZNK6crubit4test4View3GetEv( + __this: *const crate::crubit::test::View, + ) -> ::ffi_11::c_int; + pub(crate) unsafe fn __rust_thunk___ZN6crubit4test5OwnerC1Ev( + __this: *mut ::core::ffi::c_void, + ); + pub(crate) unsafe fn __rust_thunk___ZN6crubit4test5OwnerD1Ev<'a>( + __this: ::core::pin::Pin<&'a mut crate::crubit::test::Owner>, + ); + pub(crate) unsafe fn __rust_thunk___ZN6crubit4test5PlainC1Ev( + __this: *mut ::core::ffi::c_void, + ); + pub(crate) unsafe fn __rust_thunk___ZNK6crubit4test5Plain3GetEv( + __this: *const crate::crubit::test::Plain, + ) -> ::ffi_11::c_int; + pub(crate) unsafe fn __rust_thunk___ZN6crubit4test8ReadViewENS0_4ViewE( + v: *mut crate::crubit::test::View, + ) -> ::ffi_11::c_int; + pub(crate) unsafe fn __rust_thunk___ZN6crubit4test9ReadPlainENS0_5PlainE( + p: *mut crate::crubit::test::Plain, + ) -> ::ffi_11::c_int; + } +} + +const _: () = { + assert!(::core::mem::size_of::<crate::crubit::test::View>() == 8); + assert!(::core::mem::align_of::<crate::crubit::test::View>() == 8); + static_assertions::assert_impl_all!(crate::crubit::test::View: Copy,Clone); + static_assertions::assert_not_impl_any!(crate::crubit::test::View: Drop); + assert!(::core::mem::offset_of!(crate::crubit::test::View, data_) == 0); + assert!(::core::mem::size_of::<crate::crubit::test::Owner>() == 8); + assert!(::core::mem::align_of::<crate::crubit::test::Owner>() == 8); + static_assertions::assert_impl_all!(crate::crubit::test::Owner: Drop); + static_assertions::assert_not_impl_any!(crate::crubit::test::Owner: Copy); + assert!(::core::mem::offset_of!(crate::crubit::test::Owner, data_) == 0); + assert!(::core::mem::size_of::<crate::crubit::test::Plain>() == 8); + assert!(::core::mem::align_of::<crate::crubit::test::Plain>() == 8); + static_assertions::assert_impl_all!(crate::crubit::test::Plain: Copy,Clone); + static_assertions::assert_not_impl_any!(crate::crubit::test::Plain: Drop); + assert!(::core::mem::offset_of!(crate::crubit::test::Plain, data_) == 0); +};
diff --git a/rs_bindings_from_cc/test/annotations/view_owner_unsafe.h b/rs_bindings_from_cc/test/annotations/view_owner_unsafe.h new file mode 100644 index 0000000..649621a --- /dev/null +++ b/rs_bindings_from_cc/test/annotations/view_owner_unsafe.h
@@ -0,0 +1,38 @@ +// Part of the Crubit project, under the Apache License v2.0 with LLVM +// Exceptions. See /LICENSE for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +#ifndef THIRD_PARTY_CRUBIT_RS_BINDINGS_FROM_CC_TEST_ANNOTATIONS_VIEW_OWNER_UNSAFE_H_ +#define THIRD_PARTY_CRUBIT_RS_BINDINGS_FROM_CC_TEST_ANNOTATIONS_VIEW_OWNER_UNSAFE_H_ + +#include "support/annotations.h" + +namespace crubit::test { + +// Built with the `unsafe_view` feature: the CRUBIT_VIEW record is unsafe, the +// CRUBIT_OWNER record is not. + +class CRUBIT_VIEW UnsafeView final { + public: + int Get() const { return *data_; } + + private: + const int* data_ = nullptr; +}; + +class CRUBIT_OWNER SafeOwner final { + public: + SafeOwner() = default; + SafeOwner(const SafeOwner&) = delete; + SafeOwner& operator=(const SafeOwner&) = delete; + ~SafeOwner() { delete data_; } + + private: + int* data_ = nullptr; +}; + +inline int ReadUnsafeView(UnsafeView v) { return v.Get(); } + +} // namespace crubit::test + +#endif // THIRD_PARTY_CRUBIT_RS_BINDINGS_FROM_CC_TEST_ANNOTATIONS_VIEW_OWNER_UNSAFE_H_
diff --git a/rs_bindings_from_cc/test/annotations/view_owner_unsafe_api_impl.cc b/rs_bindings_from_cc/test/annotations/view_owner_unsafe_api_impl.cc new file mode 100644 index 0000000..825dcb7 --- /dev/null +++ b/rs_bindings_from_cc/test/annotations/view_owner_unsafe_api_impl.cc
@@ -0,0 +1,60 @@ +// Part of the Crubit project, under the Apache License v2.0 with LLVM +// Exceptions. See /LICENSE for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +// Automatically @generated Rust bindings for the following C++ target: +// //rs_bindings_from_cc/test/annotations:view_owner_unsafe + +#include "support/internal/cxx20_backports.h" +#include "support/internal/offsetof.h" +#include "support/internal/sizeof.h" +#include "support/internal/slot.h" + +#include <cstddef> +#include <memory> + +// Public headers of the C++ library being wrapped. +#include "rs_bindings_from_cc/test/annotations/view_owner_unsafe.h" + +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wthread-safety-analysis" +#pragma clang diagnostic ignored "-Wdeprecated-declarations" + +static_assert(CRUBIT_SIZEOF(class crubit::test::UnsafeView) == 8); +static_assert(alignof(class crubit::test::UnsafeView) == 8); + +extern "C" void __rust_thunk___ZN6crubit4test10UnsafeViewC1Ev( + class crubit::test::UnsafeView* __this) { + crubit::construct_at(__this); +} + +extern "C" int __rust_thunk___ZNK6crubit4test10UnsafeView3GetEv( + class crubit::test::UnsafeView const* __this) { + return __this->Get(); +} + +static_assert((int (::crubit::test::UnsafeView::*)() const) & + ::crubit::test::UnsafeView::Get); + +static_assert(CRUBIT_SIZEOF(class crubit::test::SafeOwner) == 8); +static_assert(alignof(class crubit::test::SafeOwner) == 8); + +extern "C" void __rust_thunk___ZN6crubit4test9SafeOwnerC1Ev( + class crubit::test::SafeOwner* __this) { + crubit::construct_at(__this); +} + +extern "C" void __rust_thunk___ZN6crubit4test9SafeOwnerD1Ev( + class crubit::test::SafeOwner* __this) { + std::destroy_at(__this); +} + +extern "C" int __rust_thunk___ZN6crubit4test14ReadUnsafeViewENS0_10UnsafeViewE( + class crubit::test::UnsafeView* v) { + return crubit::test::ReadUnsafeView(crubit::UnsafeTakeValue(v)); +} + +static_assert((int (*)(class crubit::test::UnsafeView)) & + ::crubit::test::ReadUnsafeView); + +#pragma clang diagnostic pop
diff --git a/rs_bindings_from_cc/test/annotations/view_owner_unsafe_rs_api.rs b/rs_bindings_from_cc/test/annotations/view_owner_unsafe_rs_api.rs new file mode 100644 index 0000000..c61465f --- /dev/null +++ b/rs_bindings_from_cc/test/annotations/view_owner_unsafe_rs_api.rs
@@ -0,0 +1,187 @@ +// Part of the Crubit project, under the Apache License v2.0 with LLVM +// Exceptions. See /LICENSE for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +// Automatically @generated Rust bindings for the following C++ target: +// //rs_bindings_from_cc/test/annotations:view_owner_unsafe + +#![rustfmt::skip] +#![feature(cfi_encoding, custom_inner_attributes, impl_trait_in_assoc_type, negative_impls)] +#![allow(stable_features)] +#![allow(improper_ctypes)] +#![allow(nonstandard_style)] +#![allow(unused)] +#![allow(deprecated)] +#![allow(unknown_lints, suspicious_runtime_symbol_definitions)] +#![deny(warnings)] +pub mod crubit { + pub mod test { + /// Built with the `unsafe_view` feature: the CRUBIT_VIEW record is unsafe, the + /// CRUBIT_OWNER record is not. + /// + /// # Safety + /// + /// To call a function that accepts this type, you must uphold these requirements: + /// * The C++ type is explicitly annotated as unsafe. Ensure that its safety requirements are upheld. + #[derive(Clone, Copy, ::ctor::MoveAndAssignViaCopy)] + #[cfi_encoding = "N6crubit4test10UnsafeViewE"] + #[repr(C, align(8))] + #[cfg_attr(not(doc), doc = "CRUBIT_ANNOTATE: cpp_type=crubit :: test :: UnsafeView")] + #[cfg_attr(not(doc), doc = "CRUBIT_ANNOTATE: cpp_move_constructible=")] + pub struct UnsafeView { + __non_field_data: [::core::mem::MaybeUninit<u8>; 0], + /// Reason for representing this field as a blob of bytes: + /// Types of non-public C++ fields can be elided away + pub(crate) data_: [::core::mem::MaybeUninit<u8>; 8], + } + /// Cannot infer `Send` because: + /// * field `data_` is represented as opaque bytes + impl !Send for UnsafeView {} + /// Cannot infer `Sync` because: + /// * field `data_` is represented as opaque bytes + impl !Sync for UnsafeView {} + unsafe impl ::cxx::ExternType for UnsafeView { + type Id = ::cxx::type_id!("crubit :: test :: UnsafeView"); + type Kind = ::cxx::kind::Trivial; + } + forward_declare::unsafe_define!( + forward_declare::symbol!("crubit :: test :: UnsafeView"), + crate::crubit::test::UnsafeView + ); + impl UnsafeView { + /// # Safety + /// + /// The caller must ensure that the following unsafe arguments are not misused by the function: + /// * `__this`: raw pointer + #[inline(always)] + pub unsafe fn Get(__this: *const Self) -> ::ffi_11::c_int { + unsafe { self::unsafe_view::Get(__this) } + } + } + + impl Default for UnsafeView { + #[inline(always)] + fn default() -> Self { + let mut tmp = ::core::mem::MaybeUninit::<Self>::zeroed(); + unsafe { + crate::detail::__rust_thunk___ZN6crubit4test10UnsafeViewC1Ev( + &raw mut tmp as *mut _, + ); + tmp.assume_init() + } + } + } + + pub mod unsafe_view { + /// # Safety + /// + /// The caller must ensure that the following unsafe arguments are not misused by the function: + /// * `__this`: raw pointer + #[cfg_attr(not(doc), doc = "CRUBIT_ANNOTATE: do_not_bind=")] + #[inline(always)] + pub unsafe fn Get(__this: *const crate::crubit::test::UnsafeView) -> ::ffi_11::c_int { + unsafe { crate::detail::__rust_thunk___ZNK6crubit4test10UnsafeView3GetEv(__this) } + } + } + + /// Not Rust-movable: type is neither copyable nor movable + #[::ctor::recursively_pinned(PinnedDrop)] + #[cfi_encoding = "N6crubit4test9SafeOwnerE"] + #[repr(C, align(8))] + #[cfg_attr(not(doc), doc = "CRUBIT_ANNOTATE: cpp_type=crubit :: test :: SafeOwner")] + pub struct SafeOwner { + __non_field_data: [::core::cell::Cell<::core::mem::MaybeUninit<u8>>; 0], + /// Reason for representing this field as a blob of bytes: + /// Types of non-public C++ fields can be elided away + pub(crate) data_: [::core::cell::Cell<::core::mem::MaybeUninit<u8>>; 8], + } + /// Cannot infer `Send` because: + /// * field `data_` is represented as opaque bytes + impl !Send for SafeOwner {} + /// Cannot infer `Sync` because: + /// * field `data_` is represented as opaque bytes + impl !Sync for SafeOwner {} + unsafe impl ::cxx::ExternType for SafeOwner { + type Id = ::cxx::type_id!("crubit :: test :: SafeOwner"); + type Kind = ::cxx::kind::Opaque; + } + forward_declare::unsafe_define!( + forward_declare::symbol!("crubit :: test :: SafeOwner"), + crate::crubit::test::SafeOwner + ); + + impl ::ctor::CtorNew<()> for SafeOwner { + type CtorType = ::ctor::Ctor![Self]; + type Error = ::ctor::Infallible; + #[inline(always)] + fn ctor_new(args: ()) -> Self::CtorType { + unsafe { + ::ctor::FnCtor::new(move |__crubit_dest: *mut Self| { + let () = args; + crate::detail::__rust_thunk___ZN6crubit4test9SafeOwnerC1Ev( + __crubit_dest as *mut ::core::ffi::c_void, + ); + }) + } + } + } + + impl ::ctor::PinnedDrop for SafeOwner { + #[inline(always)] + unsafe fn pinned_drop<'a>(self: ::core::pin::Pin<&'a mut Self>) { + unsafe { crate::detail::__rust_thunk___ZN6crubit4test9SafeOwnerD1Ev(self) } + } + } + + /// # Safety + /// + /// The caller must ensure that the following unsafe arguments are not misused by the function: + /// * `v`: unsafe struct or union + #[inline(always)] + pub unsafe fn ReadUnsafeView(v: crate::crubit::test::UnsafeView) -> ::ffi_11::c_int { + let mut v = ::core::mem::MaybeUninit::new(v); + unsafe { + crate::detail::__rust_thunk___ZN6crubit4test14ReadUnsafeViewENS0_10UnsafeViewE( + v.as_mut_ptr(), + ) + } + } + } +} + +// namespace crubit::test + +mod detail { + #[allow(unused_imports)] + use super::*; + unsafe extern "C" { + pub(crate) unsafe fn __rust_thunk___ZN6crubit4test10UnsafeViewC1Ev( + __this: *mut ::core::ffi::c_void, + ); + pub(crate) unsafe fn __rust_thunk___ZNK6crubit4test10UnsafeView3GetEv( + __this: *const crate::crubit::test::UnsafeView, + ) -> ::ffi_11::c_int; + pub(crate) unsafe fn __rust_thunk___ZN6crubit4test9SafeOwnerC1Ev( + __this: *mut ::core::ffi::c_void, + ); + pub(crate) unsafe fn __rust_thunk___ZN6crubit4test9SafeOwnerD1Ev<'a>( + __this: ::core::pin::Pin<&'a mut crate::crubit::test::SafeOwner>, + ); + pub(crate) unsafe fn __rust_thunk___ZN6crubit4test14ReadUnsafeViewENS0_10UnsafeViewE( + v: *mut crate::crubit::test::UnsafeView, + ) -> ::ffi_11::c_int; + } +} + +const _: () = { + assert!(::core::mem::size_of::<crate::crubit::test::UnsafeView>() == 8); + assert!(::core::mem::align_of::<crate::crubit::test::UnsafeView>() == 8); + static_assertions::assert_impl_all!(crate::crubit::test::UnsafeView: Copy,Clone); + static_assertions::assert_not_impl_any!(crate::crubit::test::UnsafeView: Drop); + assert!(::core::mem::offset_of!(crate::crubit::test::UnsafeView, data_) == 0); + assert!(::core::mem::size_of::<crate::crubit::test::SafeOwner>() == 8); + assert!(::core::mem::align_of::<crate::crubit::test::SafeOwner>() == 8); + static_assertions::assert_impl_all!(crate::crubit::test::SafeOwner: Drop); + static_assertions::assert_not_impl_any!(crate::crubit::test::SafeOwner: Copy); + assert!(::core::mem::offset_of!(crate::crubit::test::SafeOwner, data_) == 0); +};
diff --git a/support/annotations.h b/support/annotations.h index 4d9b2d9..3160354 100644 --- a/support/annotations.h +++ b/support/annotations.h
@@ -297,6 +297,42 @@ CRUBIT_INTERNAL_ANNOTATE("crubit_unsafe_ignore_attr" __VA_OPT__(, ) \ __VA_ARGS__) +// CRUBIT_VIEW and CRUBIT_OWNER mark a record as a view (it borrows what its +// pointer-like members point to) or an owner (it owns what they point to, and +// its destructor releases it). +// +// ```c++ +// class CRUBIT_VIEW StrRef { const char* data_; size_t size_; }; +// class CRUBIT_OWNER Buffer { char* data_; public: ~Buffer(); }; +// ``` +// +// Under clang they expand to `[[gsl::Pointer]]` and `[[gsl::Owner]]`, and to +// nothing otherwise, the same as `ABSL_ATTRIBUTE_VIEW` and +// `ABSL_ATTRIBUTE_OWNER`. They are defined here so that Crubit's support +// headers do not depend on absl. +// +// Effect on Crubit's bindings today: in targets with the `unsafe_view` feature, +// a CRUBIT_VIEW record is an unsafe type (its Rust struct documents a safety +// requirement, and functions that take it by value are `unsafe fn`). That +// check also fires on the inferred `[[gsl::Pointer]]` of standard library +// types. CRUBIT_OWNER has no effect on the bindings. Using the annotations to +// decide which lifetimes a record carries is planned but not implemented; that +// classification will count only attributes written in the source, not the +// ones clang infers for some standard library types (for example +// `std::optional` and `std::string_view`). +// +// Effect on clang today: `-Wdangling`, `-Wdangling-gsl` and `-Wlifetime-safety` +// key several rules on these attributes. For example, a view constructed from a +// temporary owner is diagnosed as dangling. Where those diagnostics are errors, +// adding either annotation can break the build of code that misuses the +// record, or, if the annotation is wrong, of correct code. Clang rejects both +// annotations on one record. Specializations of an annotated class template +// inherit the attribute. +// +// This is not CRUBIT_OWNED_POINTER (below), which makes Crubit generate an +// owning Rust wrapper type for one pointer. CRUBIT_OWNER classifies a whole +// record. + // The CRUBIT_OWNED_POINTER AND CRUBIT_OWNED_POINTEE annotations work together // to map conventionally "owned" C++ pointer usages to a Rust type that provides // Rust-style ownership wrapping a raw pointer.
diff --git a/support/annotations.swig b/support/annotations.swig index 0efac67..502980e 100644 --- a/support/annotations.swig +++ b/support/annotations.swig
@@ -32,6 +32,8 @@ %enddef %define CRUBIT_OWNED_POINTER %enddef +#define CRUBIT_VIEW +#define CRUBIT_OWNER %define CRUBIT_OVERRIDE_DISPLAY(should_bind) %enddef %define CRUBIT_THREAD_SAFE
diff --git a/support/annotations_internal.h b/support/annotations_internal.h index d59b174..5ee1baf 100644 --- a/support/annotations_internal.h +++ b/support/annotations_internal.h
@@ -10,6 +10,7 @@ #if defined(__clang__) #define CRUBIT_TRIVIAL_ABI [[clang::trivial_abi]] #define CRUBIT_VIEW [[gsl::Pointer]] +#define CRUBIT_OWNER [[gsl::Owner]] #if __has_cpp_attribute(clang::lifetimebound) #define CRUBIT_LIFETIME_BOUND [[clang::lifetimebound]] #elif __has_attribute(lifetimebound) @@ -29,6 +30,7 @@ #else #define CRUBIT_TRIVIAL_ABI #define CRUBIT_VIEW +#define CRUBIT_OWNER #define CRUBIT_LIFETIME_BOUND #define crubit_nonnull #define crubit_nullable