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
7 changes: 5 additions & 2 deletions cpp2rust/converter/rules/string_matcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,11 @@ Match<TranslationRule::TypeRule> searchType(const std::string &txt) {
RuleRegistry::TypeCandidates(typeKey(txt)), txt);
}

std::string mapTypeString(const std::string &cpp_type) {
auto [rule, subs] = searchType(cpp_type);
std::string mapTypeString(std::string_view cpp_type) {
auto [rule, subs] = searchType(std::string(cpp_type));
if (!rule && cpp_type.starts_with("const ")) {
return mapTypeString(cpp_type.substr(std::string_view("const ").size()));
}
if (!rule) {
llvm::errs() << "cpp_type: " << cpp_type << '\n';
assert(0 && "Type is not present in the registry");
Expand Down
2 changes: 1 addition & 1 deletion rules/map/tgt_unsafe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,5 +146,5 @@ unsafe fn f27<T1: Ord + Clone, T2>(a0: UnsafeMapIterator<T1, T2>) -> *mut (T1, T
}

unsafe fn f28<T1: Ord + Clone, T2>(a0: UnsafeMapIterator<T1, T2>) -> *const (T1, T2) {
a0.deref() as *const (T1, T2)
a0.deref().cast_const()
}
2 changes: 0 additions & 2 deletions rules/pair/src.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

template <typename T1, typename T2> using t1 = std::pair<T1, T2>;

template <typename T1, typename T2> using t2 = std::pair<const T1, T2>;

template <typename T1, typename T2> T2 &f1(std::pair<T1, T2> &o) {
return o.second;
}
Expand Down
7 changes: 0 additions & 7 deletions rules/pair/tgt_refcount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,6 @@ fn t1<T1: Default, T2: Default>() -> (Value<T1>, Value<T2>) {
)
}

fn t2<T1: Default, T2: Default>() -> (Value<T1>, Value<T2>) {
(
Rc::new(RefCell::new(T1::default())),
Rc::new(RefCell::new(T2::default())),
)
}

fn f1<T1, T2>(a0: (Value<T1>, Value<T2>)) -> Value<T2> {
a0.1
}
Expand Down
4 changes: 0 additions & 4 deletions rules/pair/tgt_unsafe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ fn t1<T1: Default, T2: Default>() -> (T1, T2) {
<(T1, T2)>::default()
}

fn t2<T1: Default, T2: Default>() -> (T1, T2) {
<(T1, T2)>::default()
}

unsafe fn f1<T1, T2>(a0: (T1, T2)) -> T2 {
a0.1
}
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ int main() {
assert(it4->second == 6);
assert(*p == 6);
assert(x5 == 5);
std::map<short, unsigned>::const_iterator const_it4 = m.find(4);
assert((*const_it4).second == 6);
auto &r = m;
assert(r.size() == 4);
assert(m.find(4) != m.end());
Expand Down
11 changes: 11 additions & 0 deletions tests/unit/out/refcount/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,17 @@ fn main_0() -> i32 {
assert!(((*(*it4.borrow()).deref().with(|__p| __p.1.clone()).borrow()) == 6_u32));
assert!(((p.read()) == 6_u32));
assert!((x5 == 5_u32));
let const_it4: Value<RefcountMapIter<i16, u32>> =
Rc::new(RefCell::new(RefcountMapIter::find_key(
(m.as_pointer() as Ptr<BTreeMap<i16, Value<(Value<i16>, Value<u32>)>>>),
&4_i16,
)));
assert!(
((*(*(*const_it4.borrow()).deref().upgrade().deref())
.1
.borrow())
== 6_u32)
);
let r: Ptr<BTreeMap<i16, Value<(Value<i16>, Value<u32>)>>> = m.as_pointer();
assert!(((*r.upgrade().deref()).len() == 4_usize));
assert!(
Expand Down
20 changes: 20 additions & 0 deletions tests/unit/out/refcount/pair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,26 @@ fn main_0() -> i32 {
)));
assert!(((*(*p3.borrow()).0.borrow()) == 5_f64));
assert!(((*(*p3.borrow()).1.borrow()) == 6));
let arr: Value<Box<[i32]>> = Rc::new(RefCell::new(Box::new([1, 2])));
let mut n: u32 = 2_u32;
let const_ptr_pairs: Value<Vec<(Value<Ptr<i32>>, Value<u64>)>> = Rc::new(RefCell::new(
(0..(1_usize) as usize)
.map(|_| <(Value<Ptr<i32>>, Value<u64>)>::default())
.collect::<Vec<_>>(),
));
(*const_ptr_pairs.borrow_mut())[0_usize] = (
Rc::new(RefCell::new(
(arr.as_pointer() as Ptr<i32>)
.offset((1) as isize)
.try_into()
.expect("failed conversion"),
)),
Rc::new(RefCell::new(n.try_into().expect("failed conversion"))),
);
assert!(
(((*(*const_ptr_pairs.borrow())[0_usize].0.borrow()).read()) == 2)
&& ((*(*const_ptr_pairs.borrow())[0_usize].1.borrow()) == 2_u64)
);
return 0;
}
pub fn __cpp2rust_init_globals() {}
3 changes: 3 additions & 0 deletions tests/unit/out/unsafe/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ unsafe fn main_0() -> i32 {
assert!(((*&raw mut (*it4.deref()).1) == (6_u32)));
assert!(((*p) == (6_u32)));
assert!(((x5) == (5_u32)));
let mut const_it4: UnsafeMapIterator<i16, u32> =
UnsafeMapIterator::find_key(&m as *const BTreeMap<i16, Box<(i16, u32)>>, &4_i16).clone();
assert!((((*const_it4.deref().cast_const()).1) == (6_u32)));
let r: *mut BTreeMap<i16, Box<(i16, u32)>> = &mut m;
assert!((((*r).len()) == (4_usize)));
assert!(
Expand Down
9 changes: 9 additions & 0 deletions tests/unit/out/unsafe/pair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ unsafe fn main_0() -> i32 {
let mut p3: (f64, i32) = (5.into(), 6.into());
assert!(((p3.0) == (5_f64)));
assert!(((p3.1) == (6)));
let arr: [i32; 2] = [1, 2];
let mut n: u32 = 2_u32;
let mut const_ptr_pairs: Vec<(*const i32, u64)> = (0..(1_usize) as usize)
.map(|_| <(*const i32, u64)>::default())
.collect::<Vec<_>>();
const_ptr_pairs[(0_usize)] = (arr.as_ptr().offset((1) as isize).into(), n.into());
assert!(
((*const_ptr_pairs[(0_usize)].0) == (2)) && ((const_ptr_pairs[(0_usize)].1) == (2_u64))
);
return 0;
}
pub unsafe fn __cpp2rust_init_globals() {}
6 changes: 6 additions & 0 deletions tests/unit/pair.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <assert.h>
#include <utility>
#include <vector>

int main() {
std::pair<double, int> p1(1, 2);
Expand All @@ -11,5 +12,10 @@ int main() {
std::pair<double, int> p3 = std::make_pair(5, 6);
assert(p3.first == 5.0);
assert(p3.second == 6);
const int arr[2] = {1, 2};
unsigned n = 2;
std::vector<std::pair<const int *, size_t>> const_ptr_pairs(1);
const_ptr_pairs[0] = std::make_pair(arr + 1, n);
assert(*const_ptr_pairs[0].first == 2 && const_ptr_pairs[0].second == 2);
return 0;
}
Loading