Skip to content

Commit 8cc028c

Browse files
authored
Fix pointers passed to rules that expect references (#636)
Pointers were dereferenced before being passed to reference arguments, that's wrong. This PR passes the pointers without dereferencing them.
1 parent b6fe8e5 commit 8cc028c

15 files changed

Lines changed: 238 additions & 15 deletions

File tree

‎cpp2rust/converter/converter.cpp‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5609,7 +5609,8 @@ std::string Converter::GetMappedAsString(clang::Expr *expr, clang::Expr **args,
56095609
// The rule takes the value of a transparent union.
56105610
auto *arg = IgnoreTransparentUnion(all_args[i]);
56115611
auto type = arg->getType();
5612-
bool by_value = arg->isPRValue() && !tgt_ir->takesAddress(ctx_, i, arg);
5612+
bool by_value =
5613+
arg->isPRValue() && !tgt_ir->takesAddress(ctx_, expr, i, arg);
56135614
if (!by_value && arg->isPRValue()) {
56145615
arg = new (ctx_) clang::MaterializeTemporaryExpr(type, arg, false);
56155616
}
@@ -5704,7 +5705,9 @@ std::string Converter::ConvertIRFragment(
57045705
assert(arg_idx < all_args.size());
57055706
auto *arg = all_args[arg_idx];
57065707
bool is_receiver = HasReceiver(expr) && arg_idx == 0;
5707-
bool is_cpp_ptr = arg->getType()->isPointerType();
5708+
bool cpp_ptr = arg->getType()->isPointerType();
5709+
bool is_cpp_ptr =
5710+
cpp_ptr && RuleRegistry::ParamTakesPointerValue(expr, arg_idx);
57085711

57095712
PlaceholderCtx ph_ctx{
57105713
.arg_idx = arg_idx,
@@ -5717,9 +5720,8 @@ std::string Converter::ConvertIRFragment(
57175720
.is_cpp_ptr = is_cpp_ptr,
57185721
// Whatever its spelling, a pointer in C++ is converted as one.
57195722
.maps_to_rust_ptr =
5720-
!is_cpp_ptr &&
5721-
RuleRegistry::MapsToPointer(
5722-
ctx_, arg->IgnoreParenNoopCasts(ctx_)->getType()),
5723+
!cpp_ptr && RuleRegistry::MapsToPointer(
5724+
ctx_, arg->IgnoreParenNoopCasts(ctx_)->getType()),
57235725
.declared_in_rule_as_rust_ptr =
57245726
RuleRegistry::ParamIsPointer(ctx_, expr, arg_idx),
57255727
.is_index_base = ph->is_index_base,

‎cpp2rust/converter/models/boxed_vars.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ class AddressTakenVisitor
345345
// borrowed, unless the rule takes a pointer to them, other than to a
346346
// temporary copy of them, or they are bound to references. A pointer
347347
// parameter receives the value of an argument that is itself translated to a
348-
// pointer.
348+
// pointer, unless the rule binds a reference to that argument.
349349
void AddRuleArgs(clang::Expr *expr, clang::Expr **args, unsigned num_args) {
350350
auto *rule = RuleRegistry::GetExprRule(ctx_, expr);
351351
if (!rule) {
@@ -382,7 +382,7 @@ class AddressTakenVisitor
382382
})) {
383383
continue;
384384
}
385-
if (!rule->takesAddress(ctx_, i, arg) ||
385+
if (!rule->takesAddress(ctx_, expr, i, arg) ||
386386
rule->takesTemporaryAddress(ctx_, expr, i, arg)) {
387387
AddValueUse(arg);
388388
}

‎cpp2rust/converter/rules/registry.cpp‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,24 @@ bool ParamIsPointer(clang::ASTContext &ctx, const clang::Expr *expr,
562562
return rule->params.at(index).is_pointer();
563563
}
564564

565+
bool ParamTakesPointerValue(const clang::Expr *expr, unsigned index) {
566+
if (HasReceiver(const_cast<clang::Expr *>(expr)) && index == 0) {
567+
return true;
568+
}
569+
auto match = findExpr(expr);
570+
assert(match.rule_decl && "expression must have a translation rule");
571+
auto decl = match.rule_decl->decl;
572+
auto function = decl->getAsFunction();
573+
auto type =
574+
function ? function->getType()
575+
: llvm::cast<clang::VarDecl>(decl)->getType()->getPointeeType();
576+
auto proto = type->getAs<clang::FunctionProtoType>();
577+
assert(proto && "expression rule is neither a function nor a function "
578+
"pointer");
579+
return index >= proto->getNumParams() ||
580+
!proto->getParamType(index)->isReferenceType();
581+
}
582+
565583
clang::Expr *BuildParamConversion(clang::ASTContext &ctx,
566584
const clang::Expr *expr, unsigned index,
567585
clang::Expr *arg) {

‎cpp2rust/converter/rules/registry.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ bool ReturnsReference(const clang::Expr *expr);
5757
bool ParamIsPointer(clang::ASTContext &ctx, const clang::Expr *expr,
5858
unsigned index);
5959

60+
bool ParamTakesPointerValue(const clang::Expr *expr, unsigned index);
61+
6062
clang::Expr *BuildParamConversion(clang::ASTContext &ctx,
6163
const clang::Expr *expr, unsigned index,
6264
clang::Expr *arg);

‎cpp2rust/converter/translation_rule.cpp‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -332,22 +332,27 @@ ExprRule::argsToBind(clang::ASTContext &ctx, const clang::Expr *expr,
332332
return bind;
333333
}
334334

335-
bool ExprRule::takesAddress(clang::ASTContext &ctx, unsigned i,
336-
const clang::Expr *arg) const {
335+
bool ExprRule::takesAddress(clang::ASTContext &ctx, const clang::Expr *expr,
336+
unsigned i, const clang::Expr *arg) const {
337337
// An init pack forwards references to its arguments, not their values.
338338
if (i >= params.size()) {
339339
return usesInit();
340340
}
341-
return params[i].is_pointer() && !arg->getType()->isPointerType() &&
342-
!RuleRegistry::MapsToPointer(ctx, arg->getType());
341+
if (!params[i].is_pointer()) {
342+
return false;
343+
}
344+
if (arg->getType()->isPointerType()) {
345+
return !RuleRegistry::ParamTakesPointerValue(expr, i);
346+
}
347+
return !RuleRegistry::MapsToPointer(ctx, arg->getType());
343348
}
344349

345350
bool ExprRule::takesTemporaryAddress(clang::ASTContext &ctx,
346351
const clang::Expr *expr, unsigned i,
347352
const clang::Expr *arg) const {
348353
auto *call = clang::dyn_cast<clang::CallExpr>(expr);
349354
unsigned first_arg = clang::isa<clang::CXXMemberCallExpr>(expr);
350-
return call && i >= first_arg && takesAddress(ctx, i, arg) &&
355+
return call && i >= first_arg && takesAddress(ctx, expr, i, arg) &&
351356
GetRefBindingTempType(ctx, call, i - first_arg).has_value();
352357
}
353358

‎cpp2rust/converter/translation_rule.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,9 @@ struct ExprRule {
138138
std::vector<bool> argsToBind(clang::ASTContext &ctx, const clang::Expr *expr,
139139
llvm::ArrayRef<clang::Expr *> args) const;
140140

141-
// Whether the body takes a pointer to arg, its argument i, which is not a
142-
// pointer.
143-
bool takesAddress(clang::ASTContext &ctx, unsigned i,
141+
// Whether the body takes a pointer to arg, argument i of expr, rather than
142+
// its value.
143+
bool takesAddress(clang::ASTContext &ctx, const clang::Expr *expr, unsigned i,
144144
const clang::Expr *arg) const;
145145

146146
// Whether the body takes a pointer to a temporary copy of arg, argument i

‎tests/unit/map.cpp‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,5 +152,20 @@ int main() {
152152
k = 0;
153153
const auto &value_0 = m.at(k);
154154
assert(m.size() + x1 + x2 + x3 + x4 + x5 + value_0 == 21);
155+
156+
int pk1 = 0, pk2 = 0;
157+
std::map<int *, int> ptr_counts;
158+
int *ptr_key = &pk1;
159+
ptr_counts[ptr_key] = 1;
160+
ptr_key = &pk2;
161+
ptr_counts[ptr_key] = 2;
162+
ptr_counts[ptr_key] += 3;
163+
assert(ptr_counts.size() == 2);
164+
assert(ptr_counts[ptr_key] == 5);
165+
ptr_key = &pk1;
166+
assert(ptr_counts[ptr_key] == 1);
167+
ptr_counts[&pk2] += 1;
168+
assert(ptr_counts[&pk2] == 6);
169+
assert(pk1 == 0 && pk2 == 0);
155170
return 0;
156171
}

‎tests/unit/min_max_ptr2ptr.cpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,13 @@ int main() {
99
int r1 = std::max(**ppa, **ppb);
1010
int r2 = std::min(**ppa, **ppb);
1111
assert(r1 + r2 == 30);
12+
13+
int values[2] = {2, 1};
14+
int *low = &values[0];
15+
int *high = &values[1];
16+
assert(std::min(low, high) == low);
17+
assert(std::min(high, low) == low);
18+
assert(std::max(low, high) == high);
19+
assert(std::max(high, low) == high);
1220
return 0;
1321
}

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,40 @@ fn main_0() -> i32 {
788788
== 21_usize)
789789
})
790790
);
791+
let pk1: Value<i32> = Rc::new(RefCell::new(0));
792+
let pk2: Value<i32> = Rc::new(RefCell::new(0));
793+
let ptr_counts: Value<Map<Ptr<i32>, (Value<Ptr<i32>>, Value<i32>)>> =
794+
Rc::new(RefCell::new(Map::new()));
795+
let ptr_key: Value<Ptr<i32>> = Rc::new(RefCell::new((pk1.as_pointer())));
796+
Map::subscript(ptr_counts.as_pointer(), ptr_key.as_pointer()).write(1);
797+
(*ptr_key.borrow_mut()) = (pk2.as_pointer());
798+
Map::subscript(ptr_counts.as_pointer(), ptr_key.as_pointer()).write(2);
799+
{
800+
Map::subscript(ptr_counts.as_pointer(), ptr_key.as_pointer())
801+
.with_mut(|__v| *__v = *__v + 3)
802+
};
803+
assert!(({ ((*ptr_counts.borrow()).len() == 2_usize) }));
804+
assert!(((Map::subscript(ptr_counts.as_pointer(), ptr_key.as_pointer()).read()) == 5));
805+
(*ptr_key.borrow_mut()) = (pk1.as_pointer());
806+
assert!(((Map::subscript(ptr_counts.as_pointer(), ptr_key.as_pointer()).read()) == 1));
807+
{
808+
Map::subscript_move(
809+
ptr_counts.as_pointer(),
810+
Rc::new(RefCell::new((pk2.as_pointer()))).as_pointer(),
811+
|__k| cpp_move!(__k),
812+
)
813+
.with_mut(|__v| *__v = *__v + 1)
814+
};
815+
assert!(
816+
((Map::subscript_move(
817+
ptr_counts.as_pointer(),
818+
Rc::new(RefCell::new((pk2.as_pointer()))).as_pointer(),
819+
|__k| cpp_move!(__k)
820+
)
821+
.read())
822+
== 6)
823+
);
824+
assert!(({ ((*pk1.borrow()) == 0) && ((*pk2.borrow()) == 0) }));
791825
return 0;
792826
}
793827
pub fn __cpp2rust_init_globals() {}

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,57 @@ fn main_0() -> i32 {
3030
}
3131
.read());
3232
assert!(((r1 + r2) == 30));
33+
let values: Value<Box<[i32]>> = Rc::new(RefCell::new(Box::new([2, 1])));
34+
let low: Value<Ptr<i32>> = Rc::new(RefCell::new((values.decay().offset(0))));
35+
let high: Value<Ptr<i32>> = Rc::new(RefCell::new((values.decay().offset(1))));
36+
assert!(
37+
({
38+
({
39+
(if CppLess::less(&high.as_pointer(), &low.as_pointer()) {
40+
high.as_pointer()
41+
} else {
42+
low.as_pointer()
43+
}
44+
.read())
45+
} == { (*low.borrow()).clone() })
46+
})
47+
);
48+
assert!(
49+
({
50+
({
51+
(if CppLess::less(&low.as_pointer(), &high.as_pointer()) {
52+
low.as_pointer()
53+
} else {
54+
high.as_pointer()
55+
}
56+
.read())
57+
} == { (*low.borrow()).clone() })
58+
})
59+
);
60+
assert!(
61+
({
62+
({
63+
(if CppLess::less(&low.as_pointer(), &high.as_pointer()) {
64+
high.as_pointer()
65+
} else {
66+
low.as_pointer()
67+
}
68+
.read())
69+
} == { (*high.borrow()).clone() })
70+
})
71+
);
72+
assert!(
73+
({
74+
({
75+
(if CppLess::less(&high.as_pointer(), &low.as_pointer()) {
76+
low.as_pointer()
77+
} else {
78+
high.as_pointer()
79+
}
80+
.read())
81+
} == { (*high.borrow()).clone() })
82+
})
83+
);
3384
return 0;
3485
}
3586
pub fn __cpp2rust_init_globals() {}

0 commit comments

Comments
 (0)