Skip to content

Commit 6483cc9

Browse files
authored
Fix u64/usize mismatch in array init and ctor call (#442)
1 parent 591b6e2 commit 6483cc9

5 files changed

Lines changed: 81 additions & 6 deletions

File tree

‎cpp2rust/converter/converter.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3398,7 +3398,7 @@ bool Converter::VisitInitListExpr(clang::InitListExpr *expr) {
33983398
}
33993399
PushBracket bracket(*this);
34003400
for (auto *init : expr->inits()) {
3401-
ConvertVarInit(init->getType(), init);
3401+
ConvertVarInit(ctx_.getAsArrayType(qual_type)->getElementType(), init);
34023402
StrCat(token::kComma);
34033403
}
34043404
if (expr->hasArrayFiller()) {

‎cpp2rust/converter/converter_lib.cpp‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,19 +1450,20 @@ bool HasReceiver(clang::Expr *expr) {
14501450

14511451
std::optional<clang::QualType> GetParamImplicitConvertTarget(clang::Expr *expr,
14521452
unsigned arg_idx) {
1453-
auto *call = clang::dyn_cast<clang::CallExpr>(expr);
1454-
if (!call) {
1455-
return std::nullopt;
1453+
const clang::FunctionDecl *fn = nullptr;
1454+
if (auto *call = clang::dyn_cast<clang::CallExpr>(expr)) {
1455+
fn = call->getDirectCallee();
1456+
} else if (auto *ctor = clang::dyn_cast<clang::CXXConstructExpr>(expr)) {
1457+
fn = ctor->getConstructor();
14561458
}
1457-
auto *fn = call->getDirectCallee();
14581459
if (!fn) {
14591460
return std::nullopt;
14601461
}
14611462
unsigned param_idx = arg_idx - HasReceiver(expr);
14621463
if (param_idx >= fn->getNumParams()) {
14631464
return std::nullopt;
14641465
}
1465-
return fn->getParamDecl(param_idx)->getType();
1466+
return fn->getParamDecl(param_idx)->getType().getNonReferenceType();
14661467
}
14671468

14681469
std::optional<IteratorCategory>

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,43 @@ fn main_0() -> i32 {
208208
(*a64.borrow_mut()) -= ((*c.borrow()) as i64);
209209
assert!(((*a64.borrow()) == 120_i64));
210210
assert!(((((*n.borrow()).wrapping_rem(7_usize)) as i32) == 1));
211+
let mx: Value<usize> = Rc::new(RefCell::new(5_usize));
212+
let mins: Value<Box<[usize]>> = Rc::new(RefCell::new(Box::new([
213+
0_usize,
214+
({
215+
let __tmp_0: Value<u64> = Rc::new(RefCell::new(1_u64));
216+
let __tmp_1: Value<u64> = Rc::new(RefCell::new(((*mx.borrow()) as u64)));
217+
(if __tmp_0.as_pointer().read() <= __tmp_1.as_pointer().read() {
218+
__tmp_0.as_pointer()
219+
} else {
220+
__tmp_1.as_pointer()
221+
}
222+
.read())
223+
} as usize),
224+
({
225+
let __tmp_0: Value<u64> = Rc::new(RefCell::new(((*mx.borrow()) as u64)));
226+
let __tmp_1: Value<u64> =
227+
Rc::new(RefCell::new(((*mx.borrow()).wrapping_sub(3_usize) as u64)));
228+
(if __tmp_0.as_pointer().read() <= __tmp_1.as_pointer().read() {
229+
__tmp_0.as_pointer()
230+
} else {
231+
__tmp_1.as_pointer()
232+
}
233+
.read())
234+
} as usize),
235+
(*mx.borrow()),
236+
])));
237+
assert!(((*mins.borrow())[(1) as usize] == 1_usize));
238+
assert!(((*mins.borrow())[(2) as usize] == 2_usize));
239+
let pr: Value<(Value<u64>, Value<i32>)> = Rc::new(RefCell::new((
240+
Rc::new(RefCell::new(
241+
((*sz.borrow()) as u64)
242+
.try_into()
243+
.expect("failed conversion"),
244+
)),
245+
Rc::new(RefCell::new(1.try_into().expect("failed conversion"))),
246+
)));
247+
assert!(((*(*pr.borrow()).0.borrow()) == 21_u64));
211248
return 0;
212249
}
213250
pub fn __cpp2rust_init_globals() {}

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,33 @@ unsafe fn main_0() -> i32 {
161161
a64 -= (c as i64);
162162
assert!(((a64) == (120_i64)));
163163
assert!(((((n).wrapping_rem(7_usize)) as i32) == (1)));
164+
let mx: usize = 5_usize;
165+
let mut mins: [usize; 4] = [
166+
0_usize,
167+
({
168+
let mut __tmp_0: u64 = 1_u64;
169+
let mut __tmp_1: u64 = (mx as u64);
170+
(*if *&mut __tmp_0 <= *&mut __tmp_1 {
171+
(&mut __tmp_0) as *const _
172+
} else {
173+
(&mut __tmp_1) as *const _
174+
})
175+
} as usize),
176+
({
177+
let mut __tmp_0: u64 = (mx as u64);
178+
let mut __tmp_1: u64 = ((mx).wrapping_sub(3_usize) as u64);
179+
(*if *&mut __tmp_0 <= *&mut __tmp_1 {
180+
(&mut __tmp_0) as *const _
181+
} else {
182+
(&mut __tmp_1) as *const _
183+
})
184+
} as usize),
185+
mx,
186+
];
187+
assert!(((mins[(1) as usize]) == (1_usize)));
188+
assert!(((mins[(2) as usize]) == (2_usize)));
189+
let mut pr: (u64, i32) = ((sz as u64).into(), 1.into());
190+
assert!(((pr.0) == (21_u64)));
164191
return 0;
165192
}
166193
pub unsafe fn __cpp2rust_init_globals() {}

‎tests/unit/size_t_ssize_t.cpp‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <cstddef>
44
#include <cstdint>
55
#include <sys/types.h>
6+
#include <utility>
67

78
static size_t add_sizes(size_t a, size_t b) { return a + b; }
89

@@ -97,5 +98,14 @@ int main() {
9798
assert(a64 == 120);
9899

99100
assert((int)(n % 7) == 1);
101+
102+
const std::size_t mx = 5;
103+
std::size_t mins[] = {0, std::min<std::size_t>(1, mx), std::min(mx, mx - 3),
104+
mx};
105+
assert(mins[1] == 1);
106+
assert(mins[2] == 2);
107+
108+
std::pair<std::size_t, int> pr(sz, 1);
109+
assert(pr.first == 21);
100110
return 0;
101111
}

0 commit comments

Comments
 (0)