Skip to content

Commit aef69a5

Browse files
committed
fix(sema): extend implicit else branch to containing statement
1 parent 927077b commit aef69a5

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

regression-tests/pure2-last-use.cpp2

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,15 @@ issue_857_3: @struct type = {
7979
f: (move this) = _ = f_inout(i);
8080
}
8181

82+
issue_884: () = {
83+
x := new<int>(0);
84+
if true { }
85+
{
86+
{ f_inout(x); }
87+
f_copy(x);
88+
}
89+
}
90+
8291
issue_888: (copy r: std::string, copy size: int) = {
8392
// ...
8493
_ = r.size();

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

Lines changed: 17 additions & 5 deletions
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 94 "pure2-last-use.cpp2"
23+
#line 103 "pure2-last-use.cpp2"
2424
class my_string;
2525

2626

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

91+
auto issue_884() -> void;
92+
93+
#line 91 "pure2-last-use.cpp2"
9194
auto issue_888(std::string r, int size) -> void;
9295

93-
#line 88 "pure2-last-use.cpp2"
96+
#line 97 "pure2-last-use.cpp2"
9497
auto draw() -> void;
9598

96-
#line 94 "pure2-last-use.cpp2"
99+
#line 103 "pure2-last-use.cpp2"
97100
class my_string {
98101
public: std::string string;
99102
public: std::size_t size {CPP2_UFCS(size)(string)};
@@ -188,6 +191,15 @@ int gi {0};
188191
auto issue_857_3::f() && -> void { static_cast<void>(f_inout(std::move(*this).i)); }
189192

190193
#line 82 "pure2-last-use.cpp2"
194+
auto issue_884() -> void{
195+
auto x {cpp2_new<int>(0)};
196+
if (true) {}
197+
{
198+
{f_inout(x); }
199+
f_copy(std::move(x));
200+
}
201+
}
202+
191203
auto issue_888(std::string r, int size) -> void{
192204
// ...
193205
static_cast<void>(CPP2_UFCS_MOVE(size)(std::move(r)));
@@ -200,10 +212,10 @@ auto draw() -> void{
200212
static_cast<void>(CPP2_UFCS_MOVE(vertex)((std::move(pos))));
201213
}
202214

203-
#line 99 "pure2-last-use.cpp2"
215+
#line 108 "pure2-last-use.cpp2"
204216
auto main(int const argc_, char** argv_) -> int{
205217
auto const args = cpp2::make_args(argc_, argv_);
206-
#line 100 "pure2-last-use.cpp2"
218+
#line 109 "pure2-last-use.cpp2"
207219
issue_683(args);
208220
issue_847_2(std::vector<std::unique_ptr<int>>());
209221
}

source/sema.h

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1882,11 +1882,15 @@ class sema
18821882
++scope_depth;
18831883
}
18841884

1885-
auto end(selection_statement_node const&, int) -> void
1885+
auto end(selection_statement_node const& s, int) -> void
18861886
{
18871887
symbols.emplace_back( scope_depth, selection_sym{ false, active_selections.back() } );
18881888
active_selections.pop_back();
1889-
--scope_depth;
1889+
// Extend the scope of an implicit else branch
1890+
// to the closing brace that contains it.
1891+
if (s.false_branch->position() != source_position(0, 0)) {
1892+
--scope_depth;
1893+
}
18901894
}
18911895

18921896
auto kind_of(compound_statement_node const& n)
@@ -1922,6 +1926,18 @@ class sema
19221926

19231927
auto end(compound_statement_node const& n, int) -> void
19241928
{
1929+
// Pop an implicit 'else' branch.
1930+
if (auto s = std::find_if(
1931+
symbols.rbegin(),
1932+
symbols.rend(),
1933+
[=](symbol s) {
1934+
return s.depth == scope_depth - 1;
1935+
});
1936+
s == symbols.rend()
1937+
|| std::get_if<symbol::selection>(&s->sym)
1938+
) {
1939+
--scope_depth;
1940+
}
19251941
symbols.emplace_back(
19261942
scope_depth,
19271943
compound_sym{ false, &n, kind_of(n) }

0 commit comments

Comments
 (0)