Skip to content

Commit 1cbc95b

Browse files
Keep stable nonlocal pointer selectors on compact assignment lowering
Signed-off-by: Perfloop Agent <agent@perfloop.ai>
1 parent 6035b1c commit 1cbc95b

3 files changed

Lines changed: 155 additions & 153 deletions

File tree

‎cpp2rust/converter/converter.cpp‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5345,11 +5345,21 @@ bool Converter::NeedsAssignmentBinding(
53455345
}
53465346
// A pointer-valued selector cannot overlap a non-character arithmetic
53475347
// target in a well-defined program. Still check its base and indices.
5348-
if (!clang::isa<clang::DeclRefExpr>(place) &&
5349-
place->getType()->isPointerType() &&
5348+
if (place->getType()->isPointerType() &&
53505349
lhs->getType()->isArithmeticType() &&
53515350
!lhs->getType()->isAnyCharacterType()) {
5352-
return self(self, place);
5351+
for (const auto *part = place;;) {
5352+
if (const auto *element =
5353+
clang::dyn_cast<clang::ArraySubscriptExpr>(part);
5354+
element && !self(self, element->getIdx())) {
5355+
return false;
5356+
}
5357+
const auto *object = GetEnclosingObject(part);
5358+
if (!object) {
5359+
return clang::isa<clang::DeclRefExpr>(part) || self(self, part);
5360+
}
5361+
part = object->IgnoreParenImpCasts();
5362+
}
53535363
}
53545364
while (
53555365
clang::isa<clang::MemberExpr, clang::ArraySubscriptExpr>(place)) {

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

Lines changed: 88 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1868,58 +1868,64 @@ pub fn test_scoped_pointer_selectors_51() {
18681868
let mut i: i32 = 0;
18691869
let mut j: i32 = 1;
18701870
let mut a: u32 = ({
1871-
match ({ 3_u32 }, {
1872-
let __tmp_135 = ({
1871+
elem!(
1872+
({
18731873
let __idx0 = (i) as usize;
18741874
scoped_pointers_49.with(|rc| rc.borrow()[__idx0].clone())
1875-
})
1876-
.offset((j) as isize);
1877-
__tmp_135
1878-
}) {
1879-
(__tmp_134, __tmp_135) => __tmp_135.with_mut(|__tmp_135| {
1880-
(*__tmp_135) = __tmp_134;
1881-
(*__tmp_135).clone()
18821875
}),
1883-
}
1876+
j
1877+
)
1878+
.write(3_u32);
1879+
(elem!(
1880+
({
1881+
let __idx0 = (i) as usize;
1882+
scoped_pointers_49.with(|rc| rc.borrow()[__idx0].clone())
1883+
}),
1884+
j
1885+
)
1886+
.read())
18841887
});
18851888
let mut b: u32 = ({
1886-
match ({ 4_u32 }, {
1887-
let __tmp_137 = ({
1889+
elem!(
1890+
({
18881891
let __idx0 = (i) as usize;
18891892
saved_52.with(|rc| rc.borrow()[__idx0].clone())
1890-
})
1891-
.offset((j) as isize);
1892-
__tmp_137
1893-
}) {
1894-
(__tmp_136, __tmp_137) => __tmp_137.with_mut(|__tmp_137| {
1895-
(*__tmp_137) = __tmp_136;
1896-
(*__tmp_137).clone()
18971893
}),
1898-
}
1894+
j
1895+
)
1896+
.write(4_u32);
1897+
(elem!(
1898+
({
1899+
let __idx0 = (i) as usize;
1900+
saved_52.with(|rc| rc.borrow()[__idx0].clone())
1901+
}),
1902+
j
1903+
)
1904+
.read())
18991905
});
19001906
let mut c: u32 = ({
1901-
match ({ 5_u32 }, {
1902-
let __tmp_139 = { (*scoped_record_50.with(Value::clone).borrow()).ptr.clone() }
1903-
.offset((j) as isize);
1904-
__tmp_139
1905-
}) {
1906-
(__tmp_138, __tmp_139) => __tmp_139.with_mut(|__tmp_139| {
1907-
(*__tmp_139) = __tmp_138;
1908-
(*__tmp_139).clone()
1909-
}),
1910-
}
1907+
elem!(
1908+
{ (*scoped_record_50.with(Value::clone).borrow()).ptr.clone() },
1909+
j
1910+
)
1911+
.write(5_u32);
1912+
(elem!(
1913+
{ (*scoped_record_50.with(Value::clone).borrow()).ptr.clone() },
1914+
j
1915+
)
1916+
.read())
19111917
});
19121918
let mut d: u32 = ({
1913-
match ({ 6_u32 }, {
1914-
let __tmp_141 =
1915-
{ (*saved_record_53.with(Value::clone).borrow()).ptr.clone() }.offset((j) as isize);
1916-
__tmp_141
1917-
}) {
1918-
(__tmp_140, __tmp_141) => __tmp_141.with_mut(|__tmp_141| {
1919-
(*__tmp_141) = __tmp_140;
1920-
(*__tmp_141).clone()
1921-
}),
1922-
}
1919+
elem!(
1920+
{ (*saved_record_53.with(Value::clone).borrow()).ptr.clone() },
1921+
j
1922+
)
1923+
.write(6_u32);
1924+
(elem!(
1925+
{ (*saved_record_53.with(Value::clone).borrow()).ptr.clone() },
1926+
j
1927+
)
1928+
.read())
19231929
});
19241930
printf!(
19251931
b"scoped pointers %u %u %u %u %u\n",
@@ -2036,9 +2042,9 @@ pub fn test_stable_containers_54() {
20362042
let it: Value<RandomAccessIterator<i32>> = Rc::new(RefCell::new(v.decay().begin()));
20372043
let mut iterator_value: u32 = (({
20382044
match ({ 1_u32 }, &({ (*it.borrow()).iter_deref() })) {
2039-
(__tmp_142, __tmp_143) => __tmp_143.with_mut(|__tmp_143| {
2040-
(*__tmp_143) = (((*__tmp_143) as u32).wrapping_add((__tmp_142 as u32))) as i32;
2041-
(*__tmp_143).clone()
2045+
(__tmp_134, __tmp_135) => __tmp_135.with_mut(|__tmp_135| {
2046+
(*__tmp_135) = (((*__tmp_135) as u32).wrapping_add((__tmp_134 as u32))) as i32;
2047+
(*__tmp_135).clone()
20422048
}),
20432049
}
20442050
}) as u32);
@@ -2049,9 +2055,9 @@ pub fn test_stable_containers_54() {
20492055
{ 1_u32 },
20502056
&({ (*m.borrow_mut()).subscript(k.as_pointer(), || 0_u32) }),
20512057
) {
2052-
(__tmp_144, __tmp_145) => __tmp_145.with_mut(|__tmp_145| {
2053-
(*__tmp_145) = (((*__tmp_145) as u32).wrapping_add((__tmp_144 as u32))) as u32;
2054-
(*__tmp_145).clone()
2058+
(__tmp_136, __tmp_137) => __tmp_137.with_mut(|__tmp_137| {
2059+
(*__tmp_137) = (((*__tmp_137) as u32).wrapping_add((__tmp_136 as u32))) as u32;
2060+
(*__tmp_137).clone()
20552061
}),
20562062
}
20572063
});
@@ -2130,12 +2136,12 @@ fn main_0() -> i32 {
21302136
alias.write(0);
21312137
let __rhs = ({
21322138
match ({ 1 }, {
2133-
let __tmp_147 = a.decay().offset((alias.read()));
2134-
__tmp_147
2139+
let __tmp_139 = a.decay().offset((alias.read()));
2140+
__tmp_139
21352141
}) {
2136-
(__tmp_146, __tmp_147) => __tmp_147.with_mut(|__tmp_147| {
2137-
(*__tmp_147) = __tmp_146;
2138-
(*__tmp_147).clone()
2142+
(__tmp_138, __tmp_139) => __tmp_139.with_mut(|__tmp_139| {
2143+
(*__tmp_139) = __tmp_138;
2144+
(*__tmp_139).clone()
21392145
}),
21402146
}
21412147
});
@@ -2200,9 +2206,9 @@ fn main_0() -> i32 {
22002206
{ 1 },
22012207
&mut (owner.values[(owner.values[(0) as usize]) as usize]),
22022208
) {
2203-
(__tmp_148, __tmp_149) => {
2204-
(*__tmp_149) = __tmp_148;
2205-
(*__tmp_149).clone()
2209+
(__tmp_140, __tmp_141) => {
2210+
(*__tmp_141) = __tmp_140;
2211+
(*__tmp_141).clone()
22062212
}
22072213
}
22082214
});
@@ -2221,12 +2227,12 @@ fn main_0() -> i32 {
22212227
let mut member_alias: Ptr<i32> = (field_ptr!(alias_selector.as_pointer(), value));
22222228
let __rhs = ({
22232229
match ({ 1 }, {
2224-
let __tmp_151 = member_alias.offset(({ (*alias_selector.borrow()).value }) as isize);
2225-
__tmp_151
2230+
let __tmp_143 = member_alias.offset(({ (*alias_selector.borrow()).value }) as isize);
2231+
__tmp_143
22262232
}) {
2227-
(__tmp_150, __tmp_151) => __tmp_151.with_mut(|__tmp_151| {
2228-
(*__tmp_151) = __tmp_150;
2229-
(*__tmp_151).clone()
2233+
(__tmp_142, __tmp_143) => __tmp_143.with_mut(|__tmp_143| {
2234+
(*__tmp_143) = __tmp_142;
2235+
(*__tmp_143).clone()
22302236
}),
22312237
}
22322238
});
@@ -2238,12 +2244,12 @@ fn main_0() -> i32 {
22382244
let mut self_: Ptr<i32> = (self_index.as_pointer());
22392245
let __rhs = ({
22402246
match ({ 1 }, {
2241-
let __tmp_153 = self_.offset((*self_index.borrow()) as isize);
2242-
__tmp_153
2247+
let __tmp_145 = self_.offset((*self_index.borrow()) as isize);
2248+
__tmp_145
22432249
}) {
2244-
(__tmp_152, __tmp_153) => __tmp_153.with_mut(|__tmp_153| {
2245-
(*__tmp_153) = __tmp_152;
2246-
(*__tmp_153).clone()
2250+
(__tmp_144, __tmp_145) => __tmp_145.with_mut(|__tmp_145| {
2251+
(*__tmp_145) = __tmp_144;
2252+
(*__tmp_145).clone()
22472253
}),
22482254
}
22492255
});
@@ -2259,13 +2265,13 @@ fn main_0() -> i32 {
22592265
|| -> i32 {
22602266
return ({
22612267
match ({ 10000 }, {
2262-
let __tmp_155 =
2268+
let __tmp_147 =
22632269
(captured_pointer.read()).offset((captured_index.read()) as isize);
2264-
__tmp_155
2270+
__tmp_147
22652271
}) {
2266-
(__tmp_154, __tmp_155) => __tmp_155.with_mut(|__tmp_155| {
2267-
(*__tmp_155) = __tmp_154;
2268-
(*__tmp_155).clone()
2272+
(__tmp_146, __tmp_147) => __tmp_147.with_mut(|__tmp_147| {
2273+
(*__tmp_147) = __tmp_146;
2274+
(*__tmp_147).clone()
22692275
}),
22702276
}
22712277
});
@@ -2305,9 +2311,9 @@ fn main_0() -> i32 {
23052311
0
23062312
)),
23072313
) {
2308-
(__tmp_156, __tmp_157) => __tmp_157.with_mut(|__tmp_157| {
2309-
(*__tmp_157) = (((*__tmp_157) as u32).wrapping_add((__tmp_156 as u32))) as u32;
2310-
(*__tmp_157).clone()
2314+
(__tmp_148, __tmp_149) => __tmp_149.with_mut(|__tmp_149| {
2315+
(*__tmp_149) = (((*__tmp_149) as u32).wrapping_add((__tmp_148 as u32))) as u32;
2316+
(*__tmp_149).clone()
23112317
}),
23122318
}
23132319
});
@@ -2324,17 +2330,17 @@ fn main_0() -> i32 {
23242330
({ test_boxed_array_39() });
23252331
let mut global_result: u32 = ({
23262332
match ({ 1_u32 }, {
2327-
let __tmp_159 = global_array_41.with(|v| v.decay()).offset(
2333+
let __tmp_151 = global_array_41.with(|v| v.decay()).offset(
23282334
({
23292335
let __idx0 = (0) as usize;
23302336
global_array_41.with(|rc| rc.borrow()[__idx0])
23312337
}),
23322338
);
2333-
__tmp_159
2339+
__tmp_151
23342340
}) {
2335-
(__tmp_158, __tmp_159) => __tmp_159.with_mut(|__tmp_159| {
2336-
(*__tmp_159) = (((*__tmp_159) as u32).wrapping_add((__tmp_158 as u32))) as u32;
2337-
(*__tmp_159).clone()
2341+
(__tmp_150, __tmp_151) => __tmp_151.with_mut(|__tmp_151| {
2342+
(*__tmp_151) = (((*__tmp_151) as u32).wrapping_add((__tmp_150 as u32))) as u32;
2343+
(*__tmp_151).clone()
23382344
}),
23392345
}
23402346
});
@@ -2349,7 +2355,7 @@ fn main_0() -> i32 {
23492355
i = 0;
23502356
result = ({
23512357
match ({ 1_u32 }, {
2352-
let __tmp_161 = global_rows_42
2358+
let __tmp_153 = global_rows_42
23532359
.with(|v| v.decay())
23542360
.offset(
23552361
({
@@ -2360,11 +2366,11 @@ fn main_0() -> i32 {
23602366
)
23612367
.decay()
23622368
.offset(0);
2363-
__tmp_161
2369+
__tmp_153
23642370
}) {
2365-
(__tmp_160, __tmp_161) => __tmp_161.with_mut(|__tmp_161| {
2366-
(*__tmp_161) = (((*__tmp_161) as u32).wrapping_add((__tmp_160 as u32))) as u32;
2367-
(*__tmp_161).clone()
2371+
(__tmp_152, __tmp_153) => __tmp_153.with_mut(|__tmp_153| {
2372+
(*__tmp_153) = (((*__tmp_153) as u32).wrapping_add((__tmp_152 as u32))) as u32;
2373+
(*__tmp_153).clone()
23682374
}),
23692375
}
23702376
});

0 commit comments

Comments
 (0)