Skip to content

Commit 45e85af

Browse files
zrlkcopybara-github
authored andcommitted
rs_bindings_from_cc: graduate [[deprecated]] and [[nodiscard]] to supported.
Note that we do not currently do anything with [[deprecated]] as applied to bitfields, we still do not support binding class templates, and we do nothing special with existing Rust types. Rust does not allow us to mark particular trait methods in impl blocks as #[deprecated] or #[must_use]; we will still create bindings for these, but they will not be marked. For example, a [[deprecated]] copy constructor may be projected into Rust without a deprecation tag. We will remove the _experimental variants of the golden tests for deprecated/nodiscard in a followup change. PiperOrigin-RevId: 901020946
1 parent dd983af commit 45e85af

File tree

12 files changed

+350
-163
lines changed

12 files changed

+350
-163
lines changed

bazel/llvm.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def _llvm_loader_repository(repository_ctx):
5353
executable = False,
5454
)
5555

56-
LLVM_COMMIT_SHA = "815edc3ff646392bfee2b381d37dd35e4b04f9c5"
56+
LLVM_COMMIT_SHA = "2cf353b5e8560723409f3f9164bddec76f499963"
5757

5858
def llvm_loader_repository_dependencies():
5959
# This *declares* the dependency, but it won't actually be *downloaded* unless it's used.

rs_bindings_from_cc/generate_bindings/database/code_snippet.rs

Lines changed: 3 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -216,24 +216,8 @@ pub fn required_crubit_features(
216216
);
217217
}
218218
match item {
219-
Item::Constant(c) => {
220-
if c.deprecated.is_some() {
221-
require_any_feature(
222-
&mut missing_features,
223-
crubit_feature::CrubitFeature::Experimental.into(),
224-
&|| "[[deprecated]] attribute".into(),
225-
);
226-
}
227-
}
228-
Item::GlobalVar(g) => {
229-
if g.deprecated.is_some() {
230-
require_any_feature(
231-
&mut missing_features,
232-
crubit_feature::CrubitFeature::Experimental.into(),
233-
&|| "[[deprecated]] attribute".into(),
234-
);
235-
}
236-
}
219+
Item::Constant(_) => {}
220+
Item::GlobalVar(_) => {}
237221
Item::Comment { .. } | Item::UnsupportedItem(..) | Item::UseMod { .. } => {}
238222

239223
Item::Func(func) => {
@@ -278,20 +262,6 @@ pub fn required_crubit_features(
278262
&|| "[[noreturn]] attribute".into(),
279263
);
280264
}
281-
if func.nodiscard.is_some() {
282-
require_any_feature(
283-
&mut missing_features,
284-
crubit_feature::CrubitFeature::Experimental.into(),
285-
&|| "[[nodiscard]] attribute".into(),
286-
);
287-
}
288-
if func.deprecated.is_some() {
289-
require_any_feature(
290-
&mut missing_features,
291-
crubit_feature::CrubitFeature::Experimental.into(),
292-
&|| "[[deprecated]] attribute".into(),
293-
);
294-
}
295265
for param in &func.params {
296266
if let Some(unknown_attr) = &param.unknown_attr {
297267
require_any_feature(
@@ -324,39 +294,8 @@ pub fn required_crubit_features(
324294
)?,
325295
&|| "".into(),
326296
);
327-
let deprecated = match item {
328-
Item::Record(r) => r.deprecated.clone(),
329-
Item::TypeAlias(t) => t.deprecated.clone(),
330-
Item::Enum(e) => {
331-
if e.nodiscard.is_some() {
332-
require_any_feature(
333-
&mut missing_features,
334-
crubit_feature::CrubitFeature::Experimental.into(),
335-
&|| "[[nodiscard]] attribute".into(),
336-
);
337-
};
338-
e.deprecated.clone()
339-
}
340-
Item::ExistingRustType(_) => None,
341-
_ => unreachable!(),
342-
};
343-
if deprecated.is_some() {
344-
require_any_feature(
345-
&mut missing_features,
346-
crubit_feature::CrubitFeature::Experimental.into(),
347-
&|| "[[deprecated]] attribute".into(),
348-
);
349-
}
350-
}
351-
Item::Namespace(n) => {
352-
if n.deprecated.is_some() {
353-
require_any_feature(
354-
&mut missing_features,
355-
crubit_feature::CrubitFeature::Experimental.into(),
356-
&|| "[[deprecated]] attribute".into(),
357-
);
358-
}
359297
}
298+
Item::Namespace(_) => {}
360299
Item::IncompleteRecord(_) => {
361300
require_any_feature(
362301
&mut missing_features,

rs_bindings_from_cc/generate_bindings/generate_enum.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,6 @@ pub fn generate_enum(db: &BindingsGenerator, enum_: Rc<Enum>) -> Result<ApiSnipp
4242
__COMMENT__ #comment
4343
}
4444
};
45-
if enumerator.deprecated.is_some()
46-
&& !db
47-
.ir()
48-
.target_crubit_features(&enum_.owning_target)
49-
.contains(CrubitFeature::Experimental)
50-
{
51-
return omitting_bindings_comment(format!(
52-
"marked as deprecated; requires experimental"
53-
));
54-
}
5545
if let Some(unknown_attr) = &enumerator.unknown_attr {
5646
return omitting_bindings_comment(format!("unknown attribute(s): {unknown_attr}"));
5747
}

rs_bindings_from_cc/generate_bindings/generate_struct_and_union.rs

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -128,17 +128,6 @@ fn get_field_rs_type_kind_for_layout(
128128
}
129129
}
130130
}
131-
if field.deprecated.is_some() {
132-
for target in
133-
db.defining_target(record.id()).as_ref().into_iter().chain([&record.owning_target])
134-
{
135-
let enabled_features = ir.target_crubit_features(target);
136-
ensure!(
137-
enabled_features.contains(crubit_feature::CrubitFeature::Experimental),
138-
"field is marked as deprecated; requires experimental features on {target}"
139-
);
140-
}
141-
}
142131
let type_kind = db.rs_type_kind(field.type_.clone())?;
143132

144133
if let RsTypeKind::Error { error, .. } = type_kind {
@@ -311,18 +300,7 @@ fn field_definition(
311300
});
312301
};
313302

314-
let mut deprecated_attr: Option<DeprecatedAttr> = None;
315-
if let Some(deprecated) = &field.deprecated {
316-
for target in
317-
db.defining_target(record.id()).as_ref().into_iter().chain([&record.owning_target])
318-
{
319-
let enabled_features = db.ir().target_crubit_features(target);
320-
if enabled_features.contains(crubit_feature::CrubitFeature::Experimental) {
321-
deprecated_attr = Some(DeprecatedAttr(deprecated.clone()));
322-
break;
323-
}
324-
}
325-
};
303+
let deprecated_attr = field.deprecated.clone().map(DeprecatedAttr);
326304
let ident = make_rs_field_ident(field, field_index);
327305
let field_rs_type_kind = get_field_rs_type_kind_for_layout(db, record, field);
328306
let doc_comment = match &field_rs_type_kind {

rs_bindings_from_cc/test/golden/deprecated_rs_api.rs

Lines changed: 149 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,121 @@
1414
#![allow(deprecated)]
1515
#![deny(warnings)]
1616

17-
// error: function `deprecated_function` could not be bound
18-
// [[deprecated]] attribute
17+
#[deprecated]
18+
#[inline(always)]
19+
pub fn deprecated_function() {
20+
unsafe { crate::detail::__rust_thunk___Z19deprecated_functionv() }
21+
}
22+
23+
#[deprecated = "old"]
24+
#[inline(always)]
25+
pub fn deprecated_function_with_message() {
26+
unsafe { crate::detail::__rust_thunk___Z32deprecated_function_with_messagev() }
27+
}
28+
29+
#[derive(Clone, Copy, ::ctor::MoveAndAssignViaCopy)]
30+
#[deprecated]
31+
#[repr(C)]
32+
///CRUBIT_ANNOTATE: cpp_type=DeprecatedStruct
33+
pub struct DeprecatedStruct {
34+
__non_field_data: [::core::mem::MaybeUninit<u8>; 1],
35+
}
36+
impl !Send for DeprecatedStruct {}
37+
impl !Sync for DeprecatedStruct {}
38+
unsafe impl ::cxx::ExternType for DeprecatedStruct {
39+
type Id = ::cxx::type_id!("DeprecatedStruct");
40+
type Kind = ::cxx::kind::Trivial;
41+
}
1942

20-
// error: function `deprecated_function_with_message` could not be bound
21-
// [[deprecated]] attribute
43+
impl Default for DeprecatedStruct {
44+
#[inline(always)]
45+
fn default() -> Self {
46+
let mut tmp = ::core::mem::MaybeUninit::<Self>::zeroed();
47+
unsafe {
48+
crate::detail::__rust_thunk___ZN16DeprecatedStructC1Ev(&raw mut tmp as *mut _);
49+
tmp.assume_init()
50+
}
51+
}
52+
}
2253

23-
// error: struct `DeprecatedStruct` could not be bound
24-
// [[deprecated]] attribute
54+
#[derive(Clone, Copy, ::ctor::MoveAndAssignViaCopy)]
55+
#[deprecated = "old"]
56+
#[repr(C)]
57+
///CRUBIT_ANNOTATE: cpp_type=DeprecatedStructWithMessage
58+
pub struct DeprecatedStructWithMessage {
59+
__non_field_data: [::core::mem::MaybeUninit<u8>; 1],
60+
}
61+
impl !Send for DeprecatedStructWithMessage {}
62+
impl !Sync for DeprecatedStructWithMessage {}
63+
unsafe impl ::cxx::ExternType for DeprecatedStructWithMessage {
64+
type Id = ::cxx::type_id!("DeprecatedStructWithMessage");
65+
type Kind = ::cxx::kind::Trivial;
66+
}
2567

26-
// error: struct `DeprecatedStructWithMessage` could not be bound
27-
// [[deprecated]] attribute
68+
impl Default for DeprecatedStructWithMessage {
69+
#[inline(always)]
70+
fn default() -> Self {
71+
let mut tmp = ::core::mem::MaybeUninit::<Self>::zeroed();
72+
unsafe {
73+
crate::detail::__rust_thunk___ZN27DeprecatedStructWithMessageC1Ev(
74+
&raw mut tmp as *mut _,
75+
);
76+
tmp.assume_init()
77+
}
78+
}
79+
}
2880

29-
// error: enum `DeprecatedEnum` could not be bound
30-
// [[deprecated]] attribute
81+
#[repr(transparent)]
82+
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash, PartialOrd, Ord)]
83+
#[deprecated]
84+
///CRUBIT_ANNOTATE: cpp_type=DeprecatedEnum
85+
pub struct DeprecatedEnum(::ffi_11::c_uint);
86+
impl DeprecatedEnum {}
87+
impl From<::ffi_11::c_uint> for DeprecatedEnum {
88+
fn from(value: ::ffi_11::c_uint) -> DeprecatedEnum {
89+
DeprecatedEnum(value)
90+
}
91+
}
92+
impl From<DeprecatedEnum> for ::ffi_11::c_uint {
93+
fn from(value: DeprecatedEnum) -> ::ffi_11::c_uint {
94+
value.0
95+
}
96+
}
3197

32-
// error: enum `DeprecatedEnumWithMessage` could not be bound
33-
// [[deprecated]] attribute
98+
#[repr(transparent)]
99+
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash, PartialOrd, Ord)]
100+
#[deprecated = "old"]
101+
///CRUBIT_ANNOTATE: cpp_type=DeprecatedEnumWithMessage
102+
pub struct DeprecatedEnumWithMessage(::ffi_11::c_uint);
103+
impl DeprecatedEnumWithMessage {}
104+
impl From<::ffi_11::c_uint> for DeprecatedEnumWithMessage {
105+
fn from(value: ::ffi_11::c_uint) -> DeprecatedEnumWithMessage {
106+
DeprecatedEnumWithMessage(value)
107+
}
108+
}
109+
impl From<DeprecatedEnumWithMessage> for ::ffi_11::c_uint {
110+
fn from(value: DeprecatedEnumWithMessage) -> ::ffi_11::c_uint {
111+
value.0
112+
}
113+
}
34114

35-
// error: namespace `DeprecatedNamespace` could not be bound
36-
// [[deprecated]] attribute
115+
#[deprecated]
116+
pub mod DeprecatedNamespace {
117+
#[inline(always)]
118+
pub fn f() {
119+
unsafe { crate::detail::__rust_thunk___ZN19DeprecatedNamespace1fEv() }
120+
}
121+
}
37122

38123
// namespace DeprecatedNamespace
39124

40-
// error: namespace `DeprecatedNamespaceWithMessage` could not be bound
41-
// [[deprecated]] attribute
125+
#[deprecated = "old"]
126+
pub mod DeprecatedNamespaceWithMessage {
127+
#[inline(always)]
128+
pub fn f() {
129+
unsafe { crate::detail::__rust_thunk___ZN30DeprecatedNamespaceWithMessage1fEv() }
130+
}
131+
}
42132

43133
// namespace DeprecatedNamespaceWithMessage
44134

@@ -47,10 +137,12 @@
47137
///CRUBIT_ANNOTATE: cpp_type=DeprecatedEnumerators
48138
pub struct DeprecatedEnumerators(::ffi_11::c_uint);
49139
impl DeprecatedEnumerators {
50-
// Omitting bindings for kDeprecatedEnumerator
51-
// reason: marked as deprecated; requires experimental
52-
// Omitting bindings for kDeprecatedEnumeratorWithMessage
53-
// reason: marked as deprecated; requires experimental
140+
#[deprecated]
141+
pub const kDeprecatedEnumerator: DeprecatedEnumerators =
142+
DeprecatedEnumerators(::ffi_11::new_c_uint(0));
143+
#[deprecated = "old"]
144+
pub const kDeprecatedEnumeratorWithMessage: DeprecatedEnumerators =
145+
DeprecatedEnumerators(::ffi_11::new_c_uint(1));
54146
}
55147
impl From<::ffi_11::c_uint> for DeprecatedEnumerators {
56148
fn from(value: ::ffi_11::c_uint) -> DeprecatedEnumerators {
@@ -63,22 +155,20 @@ impl From<DeprecatedEnumerators> for ::ffi_11::c_uint {
63155
}
64156
}
65157

66-
// error: type alias `DeprecatedUsing` could not be bound
67-
// [[deprecated]] attribute
158+
#[deprecated]
159+
pub type DeprecatedUsing = ::ffi_11::c_int;
68160

69-
// error: type alias `DeprecatedUsingWithMessage` could not be bound
70-
// [[deprecated]] attribute
161+
#[deprecated = "old"]
162+
pub type DeprecatedUsingWithMessage = ::ffi_11::c_int;
71163

72164
#[derive(Clone, Copy, ::ctor::MoveAndAssignViaCopy)]
73-
#[repr(C, align(4))]
165+
#[repr(C)]
74166
///CRUBIT_ANNOTATE: cpp_type=DeprecatedFields
75167
pub struct DeprecatedFields {
76-
/// Reason for representing this field as a blob of bytes:
77-
/// field is marked as deprecated; requires experimental features on //rs_bindings_from_cc/test/golden:deprecated_cc
78-
pub(crate) no_message: [::core::mem::MaybeUninit<u8>; 4],
79-
/// Reason for representing this field as a blob of bytes:
80-
/// field is marked as deprecated; requires experimental features on //rs_bindings_from_cc/test/golden:deprecated_cc
81-
pub(crate) message: [::core::mem::MaybeUninit<u8>; 4],
168+
#[deprecated]
169+
pub no_message: ::ffi_11::c_int,
170+
#[deprecated = "old"]
171+
pub message: ::ffi_11::c_int,
82172
}
83173
impl !Send for DeprecatedFields {}
84174
impl !Sync for DeprecatedFields {}
@@ -98,11 +188,15 @@ impl Default for DeprecatedFields {
98188
}
99189
}
100190

101-
// error: global variable `global_var` could not be bound
102-
// [[deprecated]] attribute
191+
unsafe extern "C" {
192+
#[deprecated]
193+
pub static mut global_var: ::ffi_11::c_int;
194+
}
103195

104-
// error: global variable `global_var_with_message` could not be bound
105-
// [[deprecated]] attribute
196+
unsafe extern "C" {
197+
#[deprecated = "old"]
198+
pub static mut global_var_with_message: ::ffi_11::c_int;
199+
}
106200

107201
// error: class `SomeTotalSpecialization` could not be bound
108202
// Class templates are not yet supported
@@ -120,13 +214,33 @@ mod detail {
120214
#[allow(unused_imports)]
121215
use super::*;
122216
unsafe extern "C" {
217+
pub(crate) unsafe fn __rust_thunk___Z19deprecated_functionv();
218+
pub(crate) unsafe fn __rust_thunk___Z32deprecated_function_with_messagev();
219+
pub(crate) unsafe fn __rust_thunk___ZN16DeprecatedStructC1Ev(
220+
__this: *mut ::core::ffi::c_void,
221+
);
222+
pub(crate) unsafe fn __rust_thunk___ZN27DeprecatedStructWithMessageC1Ev(
223+
__this: *mut ::core::ffi::c_void,
224+
);
225+
pub(crate) unsafe fn __rust_thunk___ZN19DeprecatedNamespace1fEv();
226+
pub(crate) unsafe fn __rust_thunk___ZN30DeprecatedNamespaceWithMessage1fEv();
123227
pub(crate) unsafe fn __rust_thunk___ZN16DeprecatedFieldsC1Ev(
124228
__this: *mut ::core::ffi::c_void,
125229
);
126230
}
127231
}
128232

129233
const _: () = {
234+
assert!(::core::mem::size_of::<crate::DeprecatedStruct>() == 1);
235+
assert!(::core::mem::align_of::<crate::DeprecatedStruct>() == 1);
236+
static_assertions::assert_impl_all!(crate::DeprecatedStruct: Copy,Clone);
237+
static_assertions::assert_not_impl_any!(crate::DeprecatedStruct: Drop);
238+
239+
assert!(::core::mem::size_of::<crate::DeprecatedStructWithMessage>() == 1);
240+
assert!(::core::mem::align_of::<crate::DeprecatedStructWithMessage>() == 1);
241+
static_assertions::assert_impl_all!(crate::DeprecatedStructWithMessage: Copy,Clone);
242+
static_assertions::assert_not_impl_any!(crate::DeprecatedStructWithMessage: Drop);
243+
130244
assert!(::core::mem::size_of::<crate::DeprecatedFields>() == 8);
131245
assert!(::core::mem::align_of::<crate::DeprecatedFields>() == 4);
132246
static_assertions::assert_impl_all!(crate::DeprecatedFields: Copy,Clone);

0 commit comments

Comments
 (0)