Skip to content

Commit 5931047

Browse files
authored
Fix mismatched usize/u64 array element type (#494)
Closes #320
1 parent 8108666 commit 5931047

4 files changed

Lines changed: 24 additions & 3 deletions

File tree

‎cpp2rust/converter/converter.cpp‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5019,9 +5019,9 @@ std::string Converter::ConvertPlaceholder(clang::Expr *expr, clang::Expr *arg,
50195019
}
50205020

50215021
if (ph_ctx.declared_in_rule_as_rust_ptr && arg->getType()->isArrayType()) {
5022-
return std::format(
5023-
"({} as {})", ConvertFreshPointer(arg),
5024-
Mapper::GetParamType(ctx_, GetCalleeOrExpr(expr), ph_ctx.arg_idx));
5022+
auto elem = ctx_.getAsArrayType(arg->getType())->getElementType();
5023+
return std::format("({} as {})", ConvertFreshPointer(arg),
5024+
ToStringBase(ctx_.getPointerType(elem)));
50255025
}
50265026

50275027
if (ph_ctx.needs_materialization()) {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,12 @@ fn main_0() -> i32 {
202202
Rc::new(RefCell::new(1.try_into().expect("failed conversion"))),
203203
)));
204204
assert!(((*(*pr.borrow()).0.borrow()) == 21_u64));
205+
let sizes: Value<Box<[usize]>> =
206+
Rc::new(RefCell::new(Box::new([1_usize, 2_usize, 3_usize, 4_usize])));
207+
let mut sizes_end: Ptr<usize> =
208+
((sizes.as_pointer() as Ptr<usize>).to_end()).reinterpret_cast::<usize>();
209+
assert!((((sizes_end).clone() - (sizes.as_pointer() as Ptr::<usize>)) as i64 == 4_i64));
210+
assert!((((sizes_end.offset(-((1) as isize))).read()) == 4_usize));
205211
return 0;
206212
}
207213
pub fn __cpp2rust_init_globals() {}

‎tests/unit/out/unsafe/size_t_ssize_t.rs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,15 @@ unsafe fn main_0() -> i32 {
188188
assert!(((mins[(2) as usize]) == (2_usize)));
189189
let mut pr: (u64, i32) = ((sz as u64).into(), 1.into());
190190
assert!(((pr.0) == (21_u64)));
191+
let sizes: [usize; 4] = [1_usize, 2_usize, 3_usize, 4_usize];
192+
let mut sizes_end: *const usize =
193+
(sizes.as_ptr().add(sizes.len()) as *const u64).cast::<usize>();
194+
assert!(
195+
((((sizes_end as usize - sizes.as_ptr() as usize) / ::std::mem::size_of::<usize>())
196+
as i64)
197+
== (4_i64))
198+
);
199+
assert!(((*(sizes_end.offset(-((1) as isize)))) == (4_usize)));
191200
return 0;
192201
}
193202
pub unsafe fn __cpp2rust_init_globals() {}

‎tests/unit/size_t_ssize_t.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <cassert>
33
#include <cstddef>
44
#include <cstdint>
5+
#include <iterator>
56
#include <sys/types.h>
67
#include <utility>
78

@@ -107,5 +108,10 @@ int main() {
107108

108109
std::pair<std::size_t, int> pr(sz, 1);
109110
assert(pr.first == 21);
111+
112+
const size_t sizes[] = {1, 2, 3, 4};
113+
const size_t *sizes_end = std::end(sizes);
114+
assert(sizes_end - sizes == 4);
115+
assert(*(sizes_end - 1) == 4);
110116
return 0;
111117
}

0 commit comments

Comments
 (0)