Skip to content

Commit 82656a3

Browse files
committed
Fix stale expr kind
1 parent 0518e90 commit 82656a3

5 files changed

Lines changed: 23 additions & 14 deletions

File tree

‎cpp2rust/converter/converter.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3008,7 +3008,6 @@ bool Converter::ConvertLValueConditional(clang::ConditionalOperator *expr) {
30083008
}
30093009
// `&(c ? a : b)` -> `c ? &a : &b`
30103010
if (isAddrOf()) {
3011-
PushExprKind push(*this, ExprKind::RValue);
30123011
Convert(MakeConditionalAddrOf(ctx_, expr));
30133012
return true;
30143013
}
@@ -4860,6 +4859,7 @@ void Converter::ConvertAddrOf(clang::Expr *expr, clang::QualType pointer_type) {
48604859
StrCat("&raw", pointer_type->getPointeeType().isConstQualified()
48614860
? keyword::kConst
48624861
: keyword_mut_);
4862+
PushExprKind push(*this, ExprKind::LValue);
48634863
Convert(expr);
48644864
ConvertCast(pointer_type);
48654865
computed_expr_type_ = ComputedExprType::FreshPointer;
@@ -4868,6 +4868,7 @@ void Converter::ConvertAddrOf(clang::Expr *expr, clang::QualType pointer_type) {
48684868
if (!pointer_type->getPointeeType().isConstQualified()) {
48694869
StrCat(keyword_mut_);
48704870
}
4871+
PushExprKind push(*this, ExprKind::LValue);
48714872
Convert(expr);
48724873
ConvertCast(pointer_type);
48734874
computed_expr_type_ = ComputedExprType::FreshPointer;

‎cpp2rust/converter/models/converter_refcount.cpp‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -918,7 +918,7 @@ bool ConverterRefCount::ConvertIncAndDec(clang::UnaryOperator *expr) {
918918

919919
bool ConverterRefCount::VisitConditionalOperator(
920920
clang::ConditionalOperator *expr) {
921-
if (!isAddrOf() && ConvertLValueConditional(expr)) {
921+
if (ConvertLValueConditional(expr)) {
922922
return false;
923923
}
924924
StrCat(keyword::kIf);
@@ -3033,6 +3033,14 @@ pub fn main() {{
30333033

30343034
void ConverterRefCount::ConvertAddrOf(clang::Expr *expr,
30353035
clang::QualType pointer_type) {
3036+
auto pointee_type = pointer_type->getPointeeType();
3037+
if (isObject() && WantsElementPtr() &&
3038+
(IsBoxedType(ctx_, pointee_type) || pointee_type->isArrayType())) {
3039+
StrCat(std::format("Ptr::<{}>::decay(&({}))", ToString(pointee_type),
3040+
ConvertPointer(expr)));
3041+
computed_expr_type_ = ComputedExprType::FreshPointer;
3042+
return;
3043+
}
30363044
StrCat(ConvertPointer(expr));
30373045
}
30383046

‎tests/unit/out/refcount/09_references.rs‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,24 @@ fn main_0() -> i32 {
3030
let a: Value<i32> = Rc::new(RefCell::new(1));
3131
let b: Value<i32> = Rc::new(RefCell::new(2));
3232
let r: Ptr<i32> = if ((*a.borrow()) < (*b.borrow())) {
33-
a.as_pointer()
33+
(a.as_pointer())
3434
} else {
35-
b.as_pointer()
35+
(b.as_pointer())
3636
};
3737
r.write(10);
3838
assert!(((*a.borrow()) == 10));
3939
let cr: Ptr<i32> = if ((*a.borrow()) > (*b.borrow())) {
40-
a.as_pointer()
40+
(a.as_pointer())
4141
} else {
42-
b.as_pointer()
42+
(b.as_pointer())
4343
};
4444
assert!(((cr.read()) == 10));
4545
let x: Value<i32> = Rc::new(RefCell::new(1));
4646
let y: Value<i32> = Rc::new(RefCell::new(2));
4747
let cx: Ptr<i32> = if ((*x.borrow()) < (*y.borrow())) {
48-
x.as_pointer()
48+
(x.as_pointer())
4949
} else {
50-
y.as_pointer()
50+
(y.as_pointer())
5151
};
5252
assert!(((cx.read()) == 1));
5353
let mut cp: Ptr<i32> = if ((*a.borrow()) > (*b.borrow())) {
@@ -81,9 +81,9 @@ fn main_0() -> i32 {
8181
};
8282
assert!(((*b.borrow()) == 25));
8383
let mut ap: Ptr<i32> = (if ((*a.borrow()) > (*b.borrow())) {
84-
a.as_pointer()
84+
(a.as_pointer())
8585
} else {
86-
b.as_pointer()
86+
(b.as_pointer())
8787
});
8888
ap.write(40);
8989
assert!(((*a.borrow()) == 40));

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ pub fn fn_0(v: Ptr<Vec<i32>>, v3: Vec<i32>) {
3131
(*v2.borrow_mut())[0_usize] = 1;
3232
elem!(
3333
((if true {
34-
v3.as_pointer()
34+
(Ptr::<Vec<i32>>::decay(&(v3.as_pointer())))
3535
} else {
36-
Ptr::<Vec<i32>>::decay(&(v))
36+
(Ptr::<Vec<i32>>::decay(&(v)))
3737
}) as Ptr<i32>),
3838
0_usize
3939
)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ pub fn fn_1(v: Ptr<Vec<i32>>, v3: Vec<i32>) {
2626
v2[0_usize] = 1;
2727
elem!(
2828
((if true {
29-
v3.as_pointer()
29+
(Ptr::<Vec<i32>>::decay(&(v3.as_pointer())))
3030
} else {
31-
Ptr::<Vec<i32>>::decay(&(v))
31+
(Ptr::<Vec<i32>>::decay(&(v)))
3232
}) as Ptr<i32>),
3333
0_usize
3434
)

0 commit comments

Comments
 (0)