Skip to content

Commit 523e68d

Browse files
committed
Fix array reference
1 parent 257a06b commit 523e68d

2 files changed

Lines changed: 12 additions & 3 deletions

File tree

‎cpp2rust/converter/models/converter_refcount.cpp‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ std::string ConverterRefCount::ConvertFreshPointer(
408408
}
409409

410410
std::string ConverterRefCount::ConvertPointeeCast(std::string str,
411-
const clang::Expr *,
411+
const clang::Expr *from,
412412
clang::QualType to) {
413413
if (to->isFunctionPointerType()) {
414414
computed_expr_type_ = ComputedExprType::FreshPointer;
@@ -420,6 +420,11 @@ std::string ConverterRefCount::ConvertPointeeCast(std::string str,
420420
if (to->isReferenceType() && !isAddrOf()) {
421421
return str;
422422
}
423+
if (auto pointee = GetExprPointee(ctx_, from, to);
424+
to->isReferenceType() && pointee->isArrayType()) {
425+
str = std::format("{} as {}", str,
426+
ToString(ctx_.getLValueReferenceType(pointee)));
427+
}
423428
PushConversionKind push(*this, ConversionKind::Unboxed);
424429
auto element = ctx_.getBaseElementType(to->getPointeeType());
425430
computed_expr_type_ = ComputedExprType::FreshPointer;
@@ -2336,7 +2341,7 @@ std::string ConverterRefCount::ConvertVarInitValue(clang::QualType qual_type,
23362341
return std::format("Ptr::<{}>::from_string_literal({})", code_unit,
23372342
ToString(expr->IgnoreParens()->IgnoreImplicit()));
23382343
}
2339-
return std::format("({} as {})", ConvertFreshPointer(expr),
2344+
return std::format("({} as {})", ConvertFreshPointer(expr, qual_type),
23402345
ToString(qual_type));
23412346
}
23422347
return ConvertFreshPointer(expr, qual_type);

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ pub fn main() {
4747
}
4848
fn main_0() -> i32 {
4949
let a1: Value<Box<[usize]>> = Rc::new(RefCell::new(Box::new([1_usize, 2_usize, 3_usize])));
50-
assert!((({ array_ref_0((a1.as_pointer() as Ptr<u64>),) }) == 3_u64));
50+
assert!(
51+
(({
52+
array_ref_0(((a1.as_pointer() as Ptr<usize>).reinterpret_cast::<u64>() as Ptr<u64>))
53+
}) == 3_u64)
54+
);
5155
assert!(((*a1.borrow())[(0) as usize] == 2_usize));
5256
let a2: Value<Box<[usize]>> = Rc::new(RefCell::new(Box::new([4_usize, 5_usize])));
5357
let pc: Value<PtrCtor_unsigned_long_> = Rc::new(RefCell::new(PtrCtor_unsigned_long_::new({

0 commit comments

Comments
 (0)