Skip to content

Commit 0686487

Browse files
committed
Add FnPtrArg for unsafe
1 parent 7ef95ed commit 0686487

179 files changed

Lines changed: 424 additions & 362 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎cpp2rust/converter/converter.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4098,7 +4098,7 @@ std::string Converter::GetRecordName(const clang::NamedDecl *decl) const {
40984098
std::vector<const char *>
40994099
Converter::GetStructAttributes(const clang::RecordDecl *decl) {
41004100
if (decl->isUnion()) {
4101-
return {"Copy", "Clone"};
4101+
return {"Copy", "Clone", "FnPtrArg"};
41024102
}
41034103

41044104
std::vector<const char *> struct_attrs;
@@ -4113,6 +4113,7 @@ Converter::GetStructAttributes(const clang::RecordDecl *decl) {
41134113

41144114
if (RecordImplementsClone(decl)) {
41154115
struct_attrs.emplace_back("VaArg");
4116+
struct_attrs.emplace_back("FnPtrArg");
41164117
}
41174118

41184119
if (RecordDerivesDefault(decl)) {

‎libcc2rs/src/fn_ptr_arg.rs‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,49 @@ impl FnPtrArg for AnyPtr {
130130
}
131131
}
132132

133+
impl<T: ?Sized + 'static> FnPtrArg for *mut T {
134+
#[inline]
135+
fn to_repr(&self) -> ArgRepr<'_> {
136+
ArgRepr::Record(self)
137+
}
138+
fn from_repr(r: &ArgRepr) -> Self {
139+
record_from_repr(r)
140+
}
141+
}
142+
143+
impl<T: ?Sized + 'static> FnPtrArg for *const T {
144+
#[inline]
145+
fn to_repr(&self) -> ArgRepr<'_> {
146+
ArgRepr::Record(self)
147+
}
148+
fn from_repr(r: &ArgRepr) -> Self {
149+
record_from_repr(r)
150+
}
151+
}
152+
153+
macro_rules! impl_fn_ptr_arg_unsafe_fn {
154+
() => {
155+
impl_fn_ptr_arg_unsafe_fn!(@gen A B C D E F G H I J K L M N O P);
156+
};
157+
(@gen $($a:ident)*) => {
158+
impl<R: 'static $(, $a: 'static)*> FnPtrArg for Option<unsafe fn($($a,)*) -> R> {
159+
#[inline]
160+
fn to_repr(&self) -> ArgRepr<'_> {
161+
ArgRepr::Record(self)
162+
}
163+
fn from_repr(r: &ArgRepr) -> Self {
164+
record_from_repr(r)
165+
}
166+
}
167+
impl_fn_ptr_arg_unsafe_fn!(@peel $($a)*);
168+
};
169+
(@peel) => {};
170+
(@peel $head:ident $($tail:ident)*) => {
171+
impl_fn_ptr_arg_unsafe_fn!(@gen $($tail)*);
172+
};
173+
}
174+
impl_fn_ptr_arg_unsafe_fn!();
175+
133176
pub fn record_from_repr<T: Any + Clone>(r: &ArgRepr) -> T {
134177
match r {
135178
ArgRepr::Record(v) => v

‎tests/multi-file/cross_tu_tag_collision/out/unsafe/cross_tu_tag_collision.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::io::{Read, Seek, Write};
77
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
88
use std::rc::Rc;
99
#[repr(C)]
10-
#[derive(Copy, Clone, VaArg, Default)]
10+
#[derive(Copy, Clone, VaArg, FnPtrArg, Default)]
1111
pub struct widget {
1212
pub id: i32,
1313
}

‎tests/multi-file/friend_operator/out/unsafe/friend_operator.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::io::{Read, Seek, Write};
77
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
88
use std::rc::Rc;
99
#[repr(C)]
10-
#[derive(Copy, Clone, VaArg, Default)]
10+
#[derive(Copy, Clone, VaArg, FnPtrArg, Default)]
1111
pub struct S {
1212
pub a: i32,
1313
pub b: i32,

‎tests/multi-file/opaque_forward_decl/out/unsafe/opaque_forward_decl.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::io::{Read, Seek, Write};
77
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
88
use std::rc::Rc;
99
#[repr(C)]
10-
#[derive(Copy, Clone, VaArg, Default)]
10+
#[derive(Copy, Clone, VaArg, FnPtrArg, Default)]
1111
pub struct container {
1212
pub p: *mut opaque,
1313
pub x: i32,

‎tests/multi-file/out_of_line_methods_cross_tu/out/unsafe/out_of_line_methods_cross_tu.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::io::{Read, Seek, Write};
77
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
88
use std::rc::Rc;
99
#[repr(C)]
10-
#[derive(Copy, Clone, VaArg, Default)]
10+
#[derive(Copy, Clone, VaArg, FnPtrArg, Default)]
1111
pub struct S {
1212
pub v: i32,
1313
}
@@ -20,7 +20,7 @@ pub unsafe trait Base {
2020
unsafe fn apply(&mut self, x: i32) -> i32;
2121
}
2222
#[repr(C)]
23-
#[derive(Copy, Clone, VaArg, Default)]
23+
#[derive(Copy, Clone, VaArg, FnPtrArg, Default)]
2424
pub struct Derived {
2525
pub factor: i32,
2626
}

‎tests/multi-file/template_method_late_instantiation/out/unsafe/template_method_late_instantiation.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::io::{Read, Seek, Write};
77
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
88
use std::rc::Rc;
99
#[repr(C)]
10-
#[derive(Copy, Clone, VaArg)]
10+
#[derive(Copy, Clone, VaArg, FnPtrArg)]
1111
pub struct S_int_ {
1212
pub x: i32,
1313
}

‎tests/ub/out/unsafe/ctor_ref_member.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::io::{Read, Seek, Write};
77
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
88
use std::rc::Rc;
99
#[repr(C)]
10-
#[derive(Copy, Clone, VaArg, Default)]
10+
#[derive(Copy, Clone, VaArg, FnPtrArg, Default)]
1111
pub struct S {
1212
pub r: *const i32,
1313
}

‎tests/ub/out/unsafe/ub6.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::io::{Read, Seek, Write};
77
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
88
use std::rc::Rc;
99
#[repr(C)]
10-
#[derive(Copy, Clone, VaArg, Default)]
10+
#[derive(Copy, Clone, VaArg, FnPtrArg, Default)]
1111
pub struct Pair {
1212
pub x1: *mut i32,
1313
pub x2: *mut i32,

‎tests/unit/out/refcount/lambda_basic.rs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ fn main_0() -> i32 {
3333
}),
3434
));
3535
assert!((({ (*three.borrow_mut())(1, 2, 3,) }) == 123));
36+
let k: Value<i32> = Rc::new(RefCell::new(3));
37+
let m: Value<i32> = Rc::new(RefCell::new(4));
38+
let constants: Value<_> = Rc::new(RefCell::new(
39+
(|x: i32| {
40+
let x: Value<i32> = Rc::new(RefCell::new(x));
41+
return (((*x.borrow()) + (*k.borrow())) + (*m.borrow()));
42+
}),
43+
));
44+
assert!((({ (*constants.borrow_mut())(1,) }) == 8));
3645
return 0;
3746
}
3847
pub fn __cpp2rust_init_globals() {}

0 commit comments

Comments
 (0)