Skip to content

Commit c72ef36

Browse files
zrlkcopybara-github
authored andcommitted
assume_lifetimes: ascribe a lifetime to this regardless of
constness, bringing assume_lifetimes in line with assume_this_lifetimes. PiperOrigin-RevId: 879636739
1 parent 0103613 commit c72ef36

30 files changed

+61
-547
lines changed

rs_bindings_from_cc/generate_bindings/lifetime_defaults_transform.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,7 @@ impl<'a> LifetimeDefaults<'a> {
230230
new_ty.explicit_lifetimes = self.get_lifetime_for_state(&state);
231231
LifetimeResult { ty: new_ty, state, this_state: LifetimeState::Unseen }
232232
}
233-
CcTypeVariant::Pointer(pty)
234-
if (is_this && pty.pointee_type.is_const)
235-
|| pty.kind == PointerTypeKind::LValueRef =>
236-
{
233+
CcTypeVariant::Pointer(pty) if is_this || pty.kind == PointerTypeKind::LValueRef => {
237234
let LifetimeResult { ty: pointee_type, .. } = self.add_lifetime_to_input_type(
238235
false,
239236
false,
@@ -454,12 +451,16 @@ impl<'a> LifetimeDefaults<'a> {
454451
.for_each(|name| new_func.lifetime_inputs.push(self.bindings.push_new_binding(name)));
455452
self.lower_clang_annotations(&mut new_func)?;
456453
new_func.params.iter_mut().enumerate().for_each(|(ix, param)| {
457-
let is_this = ix == 0 && &*param.identifier.identifier == "__this";
454+
let is_constructor = func.cc_name == ir::UnqualifiedIdentifier::Constructor;
455+
// `this` in a constructor is strange. The !is_constructor restriction fixes some
456+
// situations where we would bind a `'__this` in a constructor and then not use it
457+
// (because the actual `__this` is a void*).
458+
let is_this = ix == 0 && &*param.identifier.identifier == "__this" && !is_constructor;
458459
had_this |= is_this;
459460
let LifetimeResult { ty: new_type, state: new_state, this_state: new_this_state } =
460461
self.add_lifetime_to_input_type(
461462
is_this,
462-
func.cc_name == ir::UnqualifiedIdentifier::Constructor,
463+
is_constructor,
463464
Some(&param.identifier.identifier),
464465
&mut new_func.lifetime_inputs,
465466
&param.type_,

rs_bindings_from_cc/generate_bindings/lifetime_defaults_transform_test.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -899,7 +899,7 @@ fn test_struct_binds_lifetime_param() -> Result<()> {
899899
return_type: CcType { ... explicit_lifetimes: ["a"] ... }, ...
900900
lifetime_params: [],
901901
...
902-
lifetime_inputs: [],
902+
lifetime_inputs: ["__this"],
903903
...
904904
}
905905
}
@@ -937,7 +937,7 @@ fn test_struct_shadows_unknown_lifetime_param() -> Result<()> {
937937
return_type: CcType { ... explicit_lifetimes: ["unknown_0"] ... }, ...
938938
lifetime_params: [],
939939
...
940-
lifetime_inputs: [],
940+
lifetime_inputs: ["__this"],
941941
...
942942
}
943943
}
@@ -975,7 +975,7 @@ fn test_struct_does_not_shadow_unrelated_lifetime_param() -> Result<()> {
975975
return_type: CcType { ... explicit_lifetimes: ["a"] ... }, ...
976976
lifetime_params: [],
977977
...
978-
lifetime_inputs: ["a"],
978+
lifetime_inputs: ["__this", "a"],
979979
...
980980
}
981981
}
@@ -1013,7 +1013,7 @@ fn test_struct_renames_shadowed_lifetime_param_in_function() -> Result<()> {
10131013
return_type: CcType { ... explicit_lifetimes: ["a_0"] ... }, ...
10141014
lifetime_params: [],
10151015
...
1016-
lifetime_inputs: ["a_0"],
1016+
lifetime_inputs: ["a_0", "__this"],
10171017
...
10181018
}
10191019
}
@@ -1065,7 +1065,7 @@ fn test_struct_renames_multiple_shadowed_lifetime_param_in_function() -> Result<
10651065
return_type: CcType { ... explicit_lifetimes: ["a_2"] ... }, ...
10661066
lifetime_params: [],
10671067
...
1068-
lifetime_inputs: ["a_2"],
1068+
lifetime_inputs: ["a_2", "__this"],
10691069
...
10701070
}
10711071
}
@@ -1117,7 +1117,7 @@ fn test_function_uses_top_of_renamed_lifetime_stack() -> Result<()> {
11171117
return_type: CcType { ... explicit_lifetimes: ["a_1"] ... }, ...
11181118
lifetime_params: [],
11191119
...
1120-
lifetime_inputs: [],
1120+
lifetime_inputs: ["__this"],
11211121
...
11221122
}
11231123
}

rs_bindings_from_cc/test/assume_lifetimes/member_function_rs_api.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,10 @@ impl S {
3333
pub fn int_accessor<'__this>(&'__this self) -> &'__this ::ffi_11::c_int {
3434
unsafe { crate::detail::__rust_thunk___ZNK1S12int_accessorEv(self) }
3535
}
36-
/// # Safety
37-
///
38-
/// The caller must ensure that the following unsafe arguments are not misused by the function:
39-
/// * `__this`: raw pointer
40-
///
4136
/// Generated from: rs_bindings_from_cc/test/assume_lifetimes/member_function.h;l=10
4237
#[inline(always)]
43-
pub unsafe fn me(__this: *mut Self) -> *mut crate::S {
44-
crate::detail::__rust_thunk___ZN1S2meEv(__this)
38+
pub fn me<'__this>(&'__this mut self) -> &'__this mut crate::S {
39+
unsafe { crate::detail::__rust_thunk___ZN1S2meEv(self) }
4540
}
4641
}
4742

@@ -65,7 +60,9 @@ mod detail {
6560
pub(crate) unsafe fn __rust_thunk___ZNK1S12int_accessorEv<'__this>(
6661
__this: &'__this crate::S,
6762
) -> &'__this ::ffi_11::c_int;
68-
pub(crate) unsafe fn __rust_thunk___ZN1S2meEv(__this: *mut crate::S) -> *mut crate::S;
63+
pub(crate) unsafe fn __rust_thunk___ZN1S2meEv<'__this>(
64+
__this: &'__this mut crate::S,
65+
) -> &'__this mut crate::S;
6966
}
7067
}
7168

rs_bindings_from_cc/test/assume_lifetimes/member_function_test.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ fn my_test() {
1414
#[gtest]
1515
fn self_reference_test() {
1616
let mut s = member_function::S::default();
17-
unsafe {
18-
let s_ptr: *mut member_function::S =
19-
member_function::S::me(&mut s as *mut member_function::S);
20-
let int_field = (*s_ptr).int_accessor();
21-
assert_eq!(*int_field, 42);
22-
}
17+
let s_ref: &mut member_function::S = member_function::S::me(&mut s);
18+
let int_field = s_ref.int_accessor();
19+
assert_eq!(*int_field, 42);
2320
}

rs_bindings_from_cc/test/golden/bitfields.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#ifndef CRUBIT_RS_BINDINGS_FROM_CC_TEST_GOLDEN_BITFIELDS_H_
66
#define CRUBIT_RS_BINDINGS_FROM_CC_TEST_GOLDEN_BITFIELDS_H_
77

8-
#pragma clang lifetime_elision
98

109
struct WithBitfields {
1110
int f1 : 2;

rs_bindings_from_cc/test/golden/bitfields_rs_api.rs

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -51,32 +51,14 @@ impl Default for WithBitfields {
5151
}
5252
}
5353

54-
// Error while generating bindings for constructor 'WithBitfields::WithBitfields':
55-
// Can't generate bindings for WithBitfields::WithBitfields, because of missing required features (crubit.rs-features):
56-
// //rs_bindings_from_cc/test/golden:bitfields_cc needs [//features:experimental] for WithBitfields::WithBitfields (the type of __param_0 (parameter #1): references are not supported)
57-
58-
// Error while generating bindings for constructor 'WithBitfields::WithBitfields':
59-
// Can't generate bindings for WithBitfields::WithBitfields, because of missing required features (crubit.rs-features):
60-
// //rs_bindings_from_cc/test/golden:bitfields_cc needs [//features:experimental] for WithBitfields::WithBitfields (the type of __param_0 (parameter #1): references are not supported)
61-
62-
// Error while generating bindings for function 'WithBitfields::operator=':
63-
// Can't generate bindings for WithBitfields::operator=, because of missing required features (crubit.rs-features):
64-
// //rs_bindings_from_cc/test/golden:bitfields_cc needs [//features:experimental] for WithBitfields::operator= (return type: references are not supported)
65-
// //rs_bindings_from_cc/test/golden:bitfields_cc needs [//features:experimental] for WithBitfields::operator= (the type of __param_0 (parameter #1): references are not supported)
66-
67-
// Error while generating bindings for function 'WithBitfields::operator=':
68-
// Can't generate bindings for WithBitfields::operator=, because of missing required features (crubit.rs-features):
69-
// //rs_bindings_from_cc/test/golden:bitfields_cc needs [//features:experimental] for WithBitfields::operator= (return type: references are not supported)
70-
// //rs_bindings_from_cc/test/golden:bitfields_cc needs [//features:experimental] for WithBitfields::operator= (the type of __param_0 (parameter #1): references are not supported)
71-
7254
/// This is a regression test for b/283835873 where the alignment of the
7355
/// generated struct was wrong/missing.
7456
///
7557
/// # Safety
7658
///
7759
/// To call a function that accepts this type, you must uphold these requirements:
7860
/// * Document why the following public unsafe fields of this type cannot be misused by callee:
79-
/// * `status`: Rust type is unknown; safety requirements cannot be automatically generated: Unsupported type 'enum AlignmentRegressionTest::(unnamed at ./rs_bindings_from_cc/test/golden/bitfields.h:26:3)': No generated bindings found for ''
61+
/// * `status`: Rust type is unknown; safety requirements cannot be automatically generated: Unsupported type 'enum AlignmentRegressionTest::(unnamed at ./rs_bindings_from_cc/test/golden/bitfields.h:25:3)': No generated bindings found for ''
8062
#[derive(Clone, Copy, ::ctor::MoveAndAssignViaCopy)]
8163
#[repr(C, align(4))]
8264
///CRUBIT_ANNOTATE: cpp_type=AlignmentRegressionTest
@@ -103,24 +85,6 @@ impl Default for AlignmentRegressionTest {
10385
}
10486
}
10587

106-
// Error while generating bindings for constructor 'AlignmentRegressionTest::AlignmentRegressionTest':
107-
// Can't generate bindings for AlignmentRegressionTest::AlignmentRegressionTest, because of missing required features (crubit.rs-features):
108-
// //rs_bindings_from_cc/test/golden:bitfields_cc needs [//features:experimental] for AlignmentRegressionTest::AlignmentRegressionTest (the type of __param_0 (parameter #1): references are not supported)
109-
110-
// Error while generating bindings for constructor 'AlignmentRegressionTest::AlignmentRegressionTest':
111-
// Can't generate bindings for AlignmentRegressionTest::AlignmentRegressionTest, because of missing required features (crubit.rs-features):
112-
// //rs_bindings_from_cc/test/golden:bitfields_cc needs [//features:experimental] for AlignmentRegressionTest::AlignmentRegressionTest (the type of __param_0 (parameter #1): references are not supported)
113-
114-
// Error while generating bindings for function 'AlignmentRegressionTest::operator=':
115-
// Can't generate bindings for AlignmentRegressionTest::operator=, because of missing required features (crubit.rs-features):
116-
// //rs_bindings_from_cc/test/golden:bitfields_cc needs [//features:experimental] for AlignmentRegressionTest::operator= (return type: references are not supported)
117-
// //rs_bindings_from_cc/test/golden:bitfields_cc needs [//features:experimental] for AlignmentRegressionTest::operator= (the type of __param_0 (parameter #1): references are not supported)
118-
119-
// Error while generating bindings for function 'AlignmentRegressionTest::operator=':
120-
// Can't generate bindings for AlignmentRegressionTest::operator=, because of missing required features (crubit.rs-features):
121-
// //rs_bindings_from_cc/test/golden:bitfields_cc needs [//features:experimental] for AlignmentRegressionTest::operator= (return type: references are not supported)
122-
// //rs_bindings_from_cc/test/golden:bitfields_cc needs [//features:experimental] for AlignmentRegressionTest::operator= (the type of __param_0 (parameter #1): references are not supported)
123-
12488
pub mod alignment_regression_test {
12589
#[allow(unused_imports)]
12690
use super::*;

rs_bindings_from_cc/test/golden/clang_attrs.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#ifndef CRUBIT_RS_BINDINGS_FROM_CC_TEST_GOLDEN_CLANG_ATTRS_H_
66
#define CRUBIT_RS_BINDINGS_FROM_CC_TEST_GOLDEN_CLANG_ATTRS_H_
77

8-
#pragma clang lifetime_elision
98

109
struct alignas(64) HasCustomAlignment {};
1110

0 commit comments

Comments
 (0)