Skip to content

Commit c0a8338

Browse files
committed
fix(sema): pop consecutive implicit else branches
1 parent f16d2aa commit c0a8338

File tree

3 files changed

+51
-21
lines changed

3 files changed

+51
-21
lines changed

regression-tests/pure2-last-use.cpp2

+11
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,17 @@ issue_857_3: @struct type = {
7979
f: (move this) = _ = f_inout(i);
8080
}
8181

82+
83+
issue_884_3: () = {
84+
x := new<int>(0);
85+
if true { }
86+
if true { }
87+
{
88+
{ f_inout(x); }
89+
f_copy(x);
90+
}
91+
}
92+
8293
issue_884: () = {
8394
x := new<int>(0);
8495
if true { }

regression-tests/test-results/pure2-last-use.cpp

+22-8
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class issue_857_2;
2020
class issue_857_3;
2121

2222

23-
#line 112 "pure2-last-use.cpp2"
23+
#line 123 "pure2-last-use.cpp2"
2424
class my_string;
2525

2626

@@ -88,18 +88,22 @@ class issue_857_3 {
8888
public: auto f() && -> void;
8989
};
9090

91+
#line 83 "pure2-last-use.cpp2"
92+
auto issue_884_3() -> void;
93+
94+
#line 93 "pure2-last-use.cpp2"
9195
auto issue_884() -> void;
9296

93-
#line 91 "pure2-last-use.cpp2"
97+
#line 102 "pure2-last-use.cpp2"
9498
auto issue_884_2() -> void;
9599

96-
#line 100 "pure2-last-use.cpp2"
100+
#line 111 "pure2-last-use.cpp2"
97101
auto issue_888(std::string r, int size) -> void;
98102

99-
#line 106 "pure2-last-use.cpp2"
103+
#line 117 "pure2-last-use.cpp2"
100104
auto draw() -> void;
101105

102-
#line 112 "pure2-last-use.cpp2"
106+
#line 123 "pure2-last-use.cpp2"
103107
class my_string {
104108
public: std::string string;
105109
public: std::size_t size {CPP2_UFCS(size)(string)};
@@ -193,7 +197,17 @@ int gi {0};
193197
#line 79 "pure2-last-use.cpp2"
194198
auto issue_857_3::f() && -> void { static_cast<void>(f_inout(std::move(*this).i)); }
195199

196-
#line 82 "pure2-last-use.cpp2"
200+
#line 83 "pure2-last-use.cpp2"
201+
auto issue_884_3() -> void{
202+
auto x {cpp2_new<int>(0)};
203+
if (true) {}
204+
if (true) {}
205+
{
206+
{f_inout(x); }
207+
f_copy(std::move(x));
208+
}
209+
}
210+
197211
auto issue_884() -> void{
198212
auto x {cpp2_new<int>(0)};
199213
if (true) {}
@@ -224,10 +238,10 @@ auto draw() -> void{
224238
static_cast<void>(CPP2_UFCS_MOVE(vertex)((std::move(pos))));
225239
}
226240

227-
#line 117 "pure2-last-use.cpp2"
241+
#line 128 "pure2-last-use.cpp2"
228242
auto main(int const argc_, char** argv_) -> int{
229243
auto const args = cpp2::make_args(argc_, argv_);
230-
#line 118 "pure2-last-use.cpp2"
244+
#line 129 "pure2-last-use.cpp2"
231245
issue_683(args);
232246
issue_847_2(std::vector<std::unique_ptr<int>>());
233247
}

source/sema.h

+18-13
Original file line numberDiff line numberDiff line change
@@ -1935,19 +1935,24 @@ class sema
19351935
);
19361936
--scope_depth;
19371937

1938-
// Pop an implicit 'else' branch.
1939-
if (auto s = std::find_if(
1940-
symbols.rbegin(),
1941-
symbols.rend(),
1942-
[=](symbol s) {
1943-
return s.depth == scope_depth;
1944-
});
1945-
s != symbols.rend()
1946-
&& std::get_if<symbol::selection>(&s->sym)
1947-
)
1948-
{
1949-
--scope_depth;
1950-
}
1938+
auto pop_implicit_else_branch = [&]() -> bool {
1939+
if (auto s = std::find_if(
1940+
symbols.rbegin(),
1941+
symbols.rend(),
1942+
[=](symbol s) {
1943+
return s.depth <= scope_depth;
1944+
});
1945+
s != symbols.rend()
1946+
&& std::get_if<symbol::selection>(&s->sym)
1947+
&& s->depth == scope_depth
1948+
)
1949+
{
1950+
--scope_depth;
1951+
return true;
1952+
}
1953+
return false;
1954+
};
1955+
while (pop_implicit_else_branch()) { }
19511956
}
19521957

19531958
auto start(assignment_expression_node const& n, int)

0 commit comments

Comments
 (0)