Skip to content

Commit ca62737

Browse files
committed
remove more explicit temporaries from rules
1 parent a495c8d commit ca62737

20 files changed

Lines changed: 154 additions & 165 deletions

File tree

‎cpp2rust/converter/converter.cpp‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5738,11 +5738,13 @@ std::string Converter::GetMappedAsString(clang::Expr *expr, clang::Expr **args,
57385738
}
57395739
auto first = ArgsToEvaluateFirst(expr, *tgt_ir, all_args, bind);
57405740
// The object that init constructs is constructed first, rather than its
5741-
// arguments evaluated first one by one.
5741+
// arguments evaluated first one by one, and once if the body uses it more
5742+
// than once.
57425743
auto num_params = std::min(tgt_ir->params.size(), all_args.size());
57435744
bool init_first =
57445745
tgt_ir->alwaysConstructsInit() &&
5745-
std::find(first.begin() + num_params, first.end(), true) != first.end();
5746+
(tgt_ir->initCopies() > 1 ||
5747+
std::find(first.begin() + num_params, first.end(), true) != first.end());
57465748
std::vector<std::string> values, names;
57475749
// Bindings in a global initializer are local to the expansion too.
57485750
clang::DeclContext *binding_context = curr_function_;

‎cpp2rust/converter/translation_rule.cpp‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,19 @@ void ExprRule::dump() const {
258258
}
259259

260260
bool ExprRule::alwaysConstructsInit() const {
261-
return usesInit() && !AnyFragment(body, [](const BodyFragment &frag) {
262-
auto *init = std::get_if<InitFragment>(&frag);
263-
return init && init->conditional;
264-
});
261+
return AnyFragment(body, [](const BodyFragment &frag) {
262+
auto *init = std::get_if<InitFragment>(&frag);
263+
return init && !init->conditional;
264+
});
265+
}
266+
267+
unsigned ExprRule::initCopies() const {
268+
unsigned copies = 0;
269+
AnyFragment(body, [&](const BodyFragment &frag) {
270+
copies += std::holds_alternative<InitFragment>(frag);
271+
return false;
272+
});
273+
return copies;
265274
}
266275

267276
bool ExprRule::usesInit() const {

‎cpp2rust/converter/translation_rule.h‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,11 @@ struct ExprRule {
132132

133133
void dump() const;
134134
bool usesInit() const;
135-
// Whether the body constructs init whenever it is evaluated.
135+
// Whether the body constructs init whenever it is evaluated, as it uses init
136+
// unconditionally.
136137
bool alwaysConstructsInit() const;
138+
// The number of uses of init in the body.
139+
unsigned initCopies() const;
137140
// The operand of the reference that the body ends with, if it returns one.
138141
const std::vector<BodyFragment> *refExprOperand() const;
139142
// The statements of the body that compute the reference it returns.

‎docs/src/rules/ir.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ The fragment kinds are:
8383
`&mut T`, is a `ref_expr` too. The fragments before it are the statements that
8484
compute it; its optional `setup` holds the trailing `let` statements whose
8585
variables only the lvalue uses, e.g., `let __n = a0.len() - 1;` for
86-
`&mut a0[__n]`, which a discarded reference doesn't need. Where the converter would dereference the rule's result it emits
87-
the lvalue alone, so `p.first = 1.5` is `p.0 = 1.5` rather than
88-
`(*&mut p.0) = 1.5`. See
86+
`&mut a0[__n]`, which a discarded reference doesn't need. Where the converter
87+
would dereference the rule's result it emits the lvalue alone, so
88+
`p.first = 1.5` is `p.0 = 1.5` rather than `(*&mut p.0) = 1.5`. See
8989
[Returning references](./writing-rules.md#returning-references) for how a
9090
reference to a parameter, or to an element of one, is translated.
9191
- `va_args`: the expansion point for a variadic tail.

‎docs/src/rules/writing-rules.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,8 +374,8 @@ asks Sema which constructor builds a `Point` from `(4, 5)` and substitutes the
374374
converted construction for `init`. The rule needn't bind `init` before touching
375375
`a0`: when its arguments, or what pointers among them point to, may be an object
376376
that the body borrows mutably, e.g., `v.emplace_back(v[0])` or
377-
`v.emplace_back(p, p + 3)`, the converter constructs `init` before the body, unless
378-
the body may not construct it (`std::map::try_emplace`).
377+
`v.emplace_back(p, p + 3)`, the converter constructs `init` before the body,
378+
unless the body may not construct it (`std::map::try_emplace`).
379379

380380
## Passthrough rules
381381

‎rules/cstddef/tgt_unsafe.rs‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,12 @@ fn f2(a0: &mut u8, a1: u32) -> u8 {
3838
}
3939

4040
fn f3(a0: &mut u8, a1: u32) -> u8 {
41-
let __n = ((*a0 as u32) << a1) as u8;
42-
*a0 = __n;
41+
*a0 = ((*a0 as u32) << a1) as u8;
4342
*a0
4443
}
4544

4645
fn f4(a0: &mut u8, a1: u32) -> u8 {
47-
let __n = ((*a0 as u32) >> a1) as u8;
48-
*a0 = __n;
46+
*a0 = ((*a0 as u32) >> a1) as u8;
4947
*a0
5048
}
5149

‎rules/map/tgt_refcount.rs‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,7 @@ fn f67<T1: CppLess + 'static, T2: 'static>(
103103
a0: Ptr<Map<T1, (Value<T1>, Value<T2>)>>,
104104
init: cpp_type!("typename std::map<T1, T2, T3, T4>::value_type", (Value<T1>, Value<T2>)),
105105
) -> (Value<BidirectionalIterator<(Value<T1>, Value<T2>)>>, Value<bool>) {
106-
let __init = init;
107-
let (__it, __inserted) = Map::emplace(a0, &__init.0.as_pointer(), || __init);
106+
let (__it, __inserted) = Map::emplace(a0, &init.0.as_pointer(), || init);
108107
(Rc::new(RefCell::new(__it)), Rc::new(RefCell::new(__inserted)))
109108
}
110109

@@ -114,8 +113,7 @@ fn f68<T1: CppLess + 'static, T2: 'static>(
114113
init: cpp_type!("typename std::map<T1, T2, T3, T4>::value_type", (Value<T1>, Value<T2>)),
115114
) -> BidirectionalIterator<(Value<T1>, Value<T2>)> {
116115
let _ = a1;
117-
let __init = init;
118-
Map::emplace(a0, &__init.0.as_pointer(), || __init).0
116+
Map::emplace(a0, &init.0.as_pointer(), || init).0
119117
}
120118

121119
fn f73<T1: CppLess + Clone + ByteRepr + 'static, T2: 'static>(

‎rules/pair/tgt_refcount.rs‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,9 @@ fn f10<T1, T2>(a0: T1, a1: T2) -> (Value<T1>, Value<T2>) {
3232
}
3333

3434
fn f12<T1, T2>(a0: &mut (Value<T1>, Value<T2>)) -> (Value<T1>, Value<T2>) {
35-
let __first: Ptr<T1> = a0.0.as_pointer();
36-
let __second: Ptr<T2> = a0.1.as_pointer();
3735
(
38-
Rc::new(RefCell::new(cpp_move!(__first))),
39-
Rc::new(RefCell::new(cpp_move!(__second))),
36+
Rc::new(RefCell::new(cpp_move!(a0.0.as_pointer()))),
37+
Rc::new(RefCell::new(cpp_move!(a0.1.as_pointer()))),
4038
)
4139
}
4240

@@ -55,10 +53,8 @@ fn f14<'a, T1: ByteRepr, T2: ByteRepr>(
5553
a0: &'a mut (Value<T1>, Value<T2>),
5654
a1: &mut (Value<T1>, Value<T2>),
5755
) -> &'a mut (Value<T1>, Value<T2>) {
58-
let __src_first: Ptr<T1> = a1.0.as_pointer();
59-
let __src_second: Ptr<T2> = a1.1.as_pointer();
60-
let __first = cpp_move!(__src_first);
61-
let __second = cpp_move!(__src_second);
56+
let __first = cpp_move!(a1.0.as_pointer());
57+
let __second = cpp_move!(a1.1.as_pointer());
6258
*a0.0.borrow_mut() = __first;
6359
*a0.1.borrow_mut() = __second;
6460
a0

‎rules/vector/tgt_refcount.rs‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ fn f47(a0: Ptr<bool>) -> bool {
3030
}
3131

3232
fn f60<T1: ByteRepr>(a0: Ptr<Vec<T1>>, a1: RandomAccessIterator<T1>) -> RandomAccessIterator<T1> {
33-
let __off = a1.index();
34-
a0.with_mut(|__v: &mut Vec<T1>| __v.remove(__off));
33+
a0.with_mut(|__v: &mut Vec<T1>| __v.remove(a1.index()));
3534
a1
3635
}
3736

@@ -64,14 +63,12 @@ fn f76<T1>(a0: Ptr<T1>) -> RandomAccessIterator<T1> {
6463
}
6564

6665
fn f77<T1: ByteRepr>(a0: &mut Vec<T1>, a1: RandomAccessIterator<T1>, a2: T1) -> RandomAccessIterator<T1> {
67-
let __off = a1.index();
68-
a0.insert(__off, a2);
66+
a0.insert(a1.index(), a2);
6967
a1
7068
}
7169

7270
fn f79<T1: ByteRepr>(a0: &mut Vec<T1>, a1: RandomAccessIterator<T1>, a2: T1) -> RandomAccessIterator<T1> {
73-
let __off = a1.index();
74-
a0.insert(__off, a2);
71+
a0.insert(a1.index(), a2);
7572
a1
7673
}
7774

‎tests/std/containers/container.requirements/out/refcount/sequence.reqmts-unsupported.rs‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ pub fn test_0() {
1818
},)
1919
{
2020
(mut __tmp_0,) => {
21-
let __off = __tmp_0.index();
22-
(*pairs.borrow_mut()).insert(__off, cpp_move!(source.as_pointer()));
21+
(*pairs.borrow_mut()).insert(__tmp_0.index(), cpp_move!(source.as_pointer()));
2322
(__tmp_0).clone()
2423
}
2524
};

0 commit comments

Comments
 (0)