Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -305,17 +305,20 @@ impl<'a> LifetimeDefaults<'a> {
.collect();
return new_ty;
}
// If there is no viable inferred lifetime, there is nothing to do.
if lifetime_hint.is_empty() {
return new_ty;
}
// If there is no viable inferred lifetime, we need to downgrade this to a raw
// pointer. We can at least mark it non-null. (An argument could be made about
// doing this later on provided we have a fuller treatement of safe/unsafe types
// selected by the presence of lifetime inputs.)
let kind =
if lifetime_hint.is_empty() { PointerTypeKind::NonNull } else { pty.kind };
let pointee_type = self.add_lifetime_to_output_type(
lifetime_hint,
new_bindings,
&pty.pointee_type,
);
new_ty.variant = CcTypeVariant::Pointer(PointerType {
pointee_type: pointee_type.into(),
kind,
..pty.clone()
});
new_ty.explicit_lifetimes = lifetime_hint.clone();
Expand Down
3 changes: 2 additions & 1 deletion rs_bindings_from_cc/test/assume_lifetimes/member_function.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@

struct S {
const int& int_accessor() const { return int_field; }
S& me() { return *this; }

int int_field;
int int_field = 42;
};

#endif // THIRD_PARTY_CRUBIT_RS_BINDINGS_FROM_CC_TEST_ASSUME_LIFETIMES_FREE_FUNCTION_H_
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,10 @@ extern "C" int const* __rust_thunk___ZNK1S12int_accessorEv(

static_assert((int const& (S::*)() const) & ::S::int_accessor);

extern "C" struct S* __rust_thunk___ZN1S2meEv(struct S* __this) {
return std::addressof(__this->me());
}

static_assert((struct S & (S::*)()) & ::S::me);

#pragma clang diagnostic pop
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ impl S {
pub fn int_accessor<'__this>(&'__this self) -> &'__this ::ffi_11::c_int {
unsafe { crate::detail::__rust_thunk___ZNK1S12int_accessorEv(self) }
}
/// # Safety
///
/// The caller must ensure that the following unsafe arguments are not misused by the function:
/// * `__this`: raw pointer
///
/// Generated from: rs_bindings_from_cc/test/assume_lifetimes/member_function.h;l=10
#[inline(always)]
pub unsafe fn me(__this: *mut Self) -> *mut crate::S {
crate::detail::__rust_thunk___ZN1S2meEv(__this)
}
}

/// Generated from: rs_bindings_from_cc/test/assume_lifetimes/member_function.h;l=8
Expand All @@ -55,6 +65,7 @@ mod detail {
pub(crate) unsafe fn __rust_thunk___ZNK1S12int_accessorEv<'__this>(
__this: &'__this crate::S,
) -> &'__this ::ffi_11::c_int;
pub(crate) unsafe fn __rust_thunk___ZN1S2meEv(__this: *mut crate::S) -> *mut crate::S;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,16 @@ use googletest::prelude::*;
fn my_test() {
let s = member_function::S::default();
let int_field = s.int_accessor();
assert_eq!(*int_field, 0);
assert_eq!(*int_field, 42);
}

#[gtest]
fn self_reference_test() {
let mut s = member_function::S::default();
unsafe {
let s_ptr: *mut member_function::S =
member_function::S::me(&mut s as *mut member_function::S);
let int_field = (*s_ptr).int_accessor();
assert_eq!(*int_field, 42);
}
}