Skip to content

Commit 1546739

Browse files
authored
Fix translation of conversion operators (#644)
Fix non-const conversion operators (was called with const receiver) and template conversion constructors (clashed with the constructor in the generated code).
1 parent cd37b02 commit 1546739

8 files changed

Lines changed: 229 additions & 3 deletions

File tree

‎cpp2rust/converter/converter.cpp‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,11 @@ void Converter::EmitConversionAttrs(clang::CXXRecordDecl *decl) {
10091009
ForEachConvertingConstructor(decl, [&](clang::CXXConstructorDecl *ctor) {
10101010
auto *param = ctor->getParamDecl(0);
10111011
auto type = param->getType();
1012+
if (ctx_.hasSameUnqualifiedType(type.getNonReferenceType(),
1013+
ctx_.getCanonicalTagType(decl)) &&
1014+
RecordImplementsClone(decl)) {
1015+
return;
1016+
}
10121017
auto source =
10131018
GetUnboxedTypeAsString(type.getNonReferenceType().getUnqualifiedType());
10141019
bool is_move = type->isRValueReferenceType();
@@ -1034,8 +1039,9 @@ void Converter::EmitConversionAttrs(clang::CXXRecordDecl *decl) {
10341039
if (!into_types.insert(target).second) {
10351040
return;
10361041
}
1037-
StrCat(
1038-
std::format("#[cpp_into({}, {})]", target, GetMethodName(conversion)));
1042+
StrCat(std::format("#[cpp_into({}, {}{})]", target,
1043+
GetMethodName(conversion),
1044+
conversion->isConst() ? "" : ", mut_ptr"));
10391045
});
10401046
}
10411047

‎libcc2rs-macros/src/cpp_from.rs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use syn::{DeriveInput, Ident, Token, Type, parse_macro_input};
1414
// #[cpp_from(T, new_3, mut_ptr)] // S(T &)
1515
// #[cpp_from_move(T, new_4, ptr)] // S(T &&)
1616
// #[cpp_into(i32, to_i32)] // operator int() const
17+
// #[cpp_into(i64, to_i64, mut_ptr)] // operator long()
1718
// pub struct S { ... }
1819
//
1920
// Implements libcc2rs::CppFrom<Source> for S with each converting constructor
@@ -199,7 +200,9 @@ pub fn expand(input: TokenStream, is_unsafe: bool) -> TokenStream {
199200
let op = &conversion.method;
200201
let self_ty = quote!(#name #ty_generics);
201202
let src = pointer(&syn::parse_quote!(#self_ty), false);
202-
let body = if is_unsafe {
203+
let body = if is_unsafe && matches!(conversion.kind, Kind::MutPtr) {
204+
quote!((*src.cast_mut()).#op())
205+
} else if is_unsafe {
203206
quote!((*src).#op())
204207
} else {
205208
quote!(#impl_trait::#op(&src))
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <cassert>
2+
3+
// N4950 [class.conv.ctor]/1: A constructor that is not explicit (9.2.3)
4+
// specifies a conversion from the types of its parameters (if any) to the type
5+
// of its class.
6+
namespace p1_template_constructor_from_own_type {
7+
struct Probe {
8+
int value;
9+
Probe(int v) : value(v) {}
10+
template <class U> Probe(U &u) : value(u.value + 1) {}
11+
};
12+
13+
void test() {
14+
Probe probe(22);
15+
Probe converted(probe);
16+
assert(converted.value == 23);
17+
}
18+
} // namespace p1_template_constructor_from_own_type
19+
20+
int main() {
21+
p1_template_constructor_from_own_type::test();
22+
return 0;
23+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <cassert>
2+
3+
// N4950 [class.conv.fct]/2: A conversion function shall have no non-object
4+
// parameters and shall be a non-static member function of a class or class
5+
// template X; it specifies a conversion from X to the type specified by the
6+
// conversion-type-id
7+
namespace p2_non_const_conversion_function {
8+
struct Gauge {
9+
long value;
10+
operator long() { return ++value; }
11+
};
12+
13+
void test() {
14+
Gauge gauge{20};
15+
long converted = gauge;
16+
assert(converted == 21);
17+
assert(gauge.value == 21);
18+
}
19+
} // namespace p2_non_const_conversion_function
20+
21+
int main() {
22+
p2_non_const_conversion_function::test();
23+
return 0;
24+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
extern crate libcc2rs;
2+
use libcc2rs::*;
3+
use std::cell::RefCell;
4+
use std::collections::BTreeMap;
5+
use std::io::prelude::*;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::AsFd;
8+
use std::rc::{Rc, Weak};
9+
impl p1_template_constructor_from_own_type_Probe {
10+
pub fn new_1(u: Ptr<p1_template_constructor_from_own_type_Probe>) -> Self {
11+
Self {
12+
value: (u.with(|__s| __s.value) + 1),
13+
}
14+
}
15+
}
16+
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, CppFrom, Default)]
17+
#[cpp_from(i32, new_2, value)]
18+
#[byte_size(4)]
19+
pub struct p1_template_constructor_from_own_type_Probe {
20+
#[offset(0)]
21+
pub value: i32,
22+
}
23+
impl p1_template_constructor_from_own_type_Probe {
24+
pub fn new_2(mut v: i32) -> Self {
25+
Self { value: v }
26+
}
27+
}
28+
pub fn test_0() {
29+
let probe: Value<p1_template_constructor_from_own_type_Probe> = Rc::new(RefCell::new(
30+
p1_template_constructor_from_own_type_Probe::new_2({ 22 }),
31+
));
32+
let mut converted: p1_template_constructor_from_own_type_Probe =
33+
p1_template_constructor_from_own_type_Probe::new_1({ probe.as_pointer() });
34+
assert!((converted.value == 23));
35+
}
36+
pub fn main() {
37+
__cpp2rust_init_globals();
38+
std::process::exit(main_0());
39+
}
40+
fn main_0() -> i32 {
41+
({ test_0() });
42+
return 0;
43+
}
44+
pub fn __cpp2rust_init_globals() {}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
extern crate libcc2rs;
2+
use libcc2rs::*;
3+
use std::cell::RefCell;
4+
use std::collections::BTreeMap;
5+
use std::io::prelude::*;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::AsFd;
8+
use std::rc::{Rc, Weak};
9+
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, CppFrom, Default)]
10+
#[cpp_into(i64, to_i64, mut_ptr)]
11+
#[byte_size(8)]
12+
pub struct p2_non_const_conversion_function_Gauge {
13+
#[offset(0)]
14+
pub value: i64,
15+
}
16+
pub fn test_0() {
17+
let gauge: Value<p2_non_const_conversion_function_Gauge> =
18+
Rc::new(RefCell::new(p2_non_const_conversion_function_Gauge {
19+
value: 20_i64,
20+
}));
21+
let mut converted: i64 =
22+
({ p2_non_const_conversion_function_GaugeImpl::to_i64(&gauge.as_pointer()) });
23+
assert!((converted == 21_i64));
24+
assert!(({ ({ (*gauge.borrow()).value } == 21_i64) }));
25+
}
26+
pub fn main() {
27+
__cpp2rust_init_globals();
28+
std::process::exit(main_0());
29+
}
30+
fn main_0() -> i32 {
31+
({ test_0() });
32+
return 0;
33+
}
34+
pub trait p2_non_const_conversion_function_GaugeImpl {
35+
fn to_i64(&self) -> i64;
36+
}
37+
impl p2_non_const_conversion_function_GaugeImpl for Ptr<p2_non_const_conversion_function_Gauge> {
38+
fn to_i64(&self) -> i64 {
39+
return field!((*self), value).with_mut(|__v| __v.prefix_inc().clone());
40+
}
41+
}
42+
pub fn __cpp2rust_init_globals() {}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
extern crate libc;
2+
use libc::*;
3+
extern crate libcc2rs;
4+
use libcc2rs::*;
5+
use std::collections::BTreeMap;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
8+
use std::rc::Rc;
9+
impl p1_template_constructor_from_own_type_Probe {
10+
pub unsafe fn new_1(u: *mut p1_template_constructor_from_own_type_Probe) -> Self {
11+
let mut this = Self {
12+
value: (((*u).value) + (1)),
13+
};
14+
this
15+
}
16+
}
17+
#[repr(C)]
18+
#[derive(Copy, Clone, VaArg, FnPtrArg, CppFromUnsafe, Default)]
19+
#[cpp_from(i32, new_2, value)]
20+
pub struct p1_template_constructor_from_own_type_Probe {
21+
pub value: i32,
22+
}
23+
impl p1_template_constructor_from_own_type_Probe {
24+
pub unsafe fn new_2(mut v: i32) -> Self {
25+
let mut this = Self { value: v };
26+
this
27+
}
28+
}
29+
pub unsafe fn test_0() {
30+
let mut probe: p1_template_constructor_from_own_type_Probe =
31+
p1_template_constructor_from_own_type_Probe::new_2({ 22 });
32+
let mut converted: p1_template_constructor_from_own_type_Probe =
33+
p1_template_constructor_from_own_type_Probe::new_1({ &mut probe });
34+
assert!(((converted.value) == (23)));
35+
}
36+
pub fn main() {
37+
unsafe {
38+
__cpp2rust_init_globals();
39+
std::process::exit(main_0() as i32);
40+
}
41+
}
42+
unsafe fn main_0() -> i32 {
43+
(unsafe { test_0() });
44+
return 0;
45+
}
46+
pub unsafe fn __cpp2rust_init_globals() {}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
extern crate libc;
2+
use libc::*;
3+
extern crate libcc2rs;
4+
use libcc2rs::*;
5+
use std::collections::BTreeMap;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
8+
use std::rc::Rc;
9+
#[repr(C)]
10+
#[derive(Copy, Clone, VaArg, FnPtrArg, CppFromUnsafe, Default)]
11+
#[cpp_into(i64, to_i64, mut_ptr)]
12+
pub struct p2_non_const_conversion_function_Gauge {
13+
pub value: i64,
14+
}
15+
impl p2_non_const_conversion_function_Gauge {
16+
pub unsafe fn to_i64(&mut self) -> i64 {
17+
return (*self.value.prefix_inc());
18+
}
19+
}
20+
pub unsafe fn test_0() {
21+
let mut gauge: p2_non_const_conversion_function_Gauge =
22+
p2_non_const_conversion_function_Gauge { value: 20_i64 };
23+
let mut converted: i64 =
24+
(unsafe { p2_non_const_conversion_function_Gauge::to_i64(&mut gauge) });
25+
assert!(((converted) == (21_i64)));
26+
assert!(((gauge.value) == (21_i64)));
27+
}
28+
pub fn main() {
29+
unsafe {
30+
__cpp2rust_init_globals();
31+
std::process::exit(main_0() as i32);
32+
}
33+
}
34+
unsafe fn main_0() -> i32 {
35+
(unsafe { test_0() });
36+
return 0;
37+
}
38+
pub unsafe fn __cpp2rust_init_globals() {}

0 commit comments

Comments
 (0)