Skip to content

Commit

Permalink
fix(sema): pop consecutive implicit else branches
Browse files Browse the repository at this point in the history
  • Loading branch information
JohelEGP committed Dec 14, 2023
1 parent f16d2aa commit c0a8338
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 21 deletions.
11 changes: 11 additions & 0 deletions regression-tests/pure2-last-use.cpp2
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ issue_857_3: @struct type = {
f: (move this) = _ = f_inout(i);
}


issue_884_3: () = {
x := new<int>(0);
if true { }
if true { }
{
{ f_inout(x); }
f_copy(x);
}
}

issue_884: () = {
x := new<int>(0);
if true { }
Expand Down
30 changes: 22 additions & 8 deletions regression-tests/test-results/pure2-last-use.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class issue_857_2;
class issue_857_3;


#line 112 "pure2-last-use.cpp2"
#line 123 "pure2-last-use.cpp2"
class my_string;


Expand Down Expand Up @@ -88,18 +88,22 @@ class issue_857_3 {
public: auto f() && -> void;
};

#line 83 "pure2-last-use.cpp2"
auto issue_884_3() -> void;

#line 93 "pure2-last-use.cpp2"
auto issue_884() -> void;

#line 91 "pure2-last-use.cpp2"
#line 102 "pure2-last-use.cpp2"
auto issue_884_2() -> void;

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

#line 106 "pure2-last-use.cpp2"
#line 117 "pure2-last-use.cpp2"
auto draw() -> void;

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

#line 82 "pure2-last-use.cpp2"
#line 83 "pure2-last-use.cpp2"
auto issue_884_3() -> void{
auto x {cpp2_new<int>(0)};
if (true) {}
if (true) {}
{
{f_inout(x); }
f_copy(std::move(x));
}
}

auto issue_884() -> void{
auto x {cpp2_new<int>(0)};
if (true) {}
Expand Down Expand Up @@ -224,10 +238,10 @@ auto draw() -> void{
static_cast<void>(CPP2_UFCS_MOVE(vertex)((std::move(pos))));
}

#line 117 "pure2-last-use.cpp2"
#line 128 "pure2-last-use.cpp2"
auto main(int const argc_, char** argv_) -> int{
auto const args = cpp2::make_args(argc_, argv_);
#line 118 "pure2-last-use.cpp2"
#line 129 "pure2-last-use.cpp2"
issue_683(args);
issue_847_2(std::vector<std::unique_ptr<int>>());
}
Expand Down
31 changes: 18 additions & 13 deletions source/sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -1935,19 +1935,24 @@ class sema
);
--scope_depth;

// Pop an implicit 'else' branch.
if (auto s = std::find_if(
symbols.rbegin(),
symbols.rend(),
[=](symbol s) {
return s.depth == scope_depth;
});
s != symbols.rend()
&& std::get_if<symbol::selection>(&s->sym)
)
{
--scope_depth;
}
auto pop_implicit_else_branch = [&]() -> bool {
if (auto s = std::find_if(
symbols.rbegin(),
symbols.rend(),
[=](symbol s) {
return s.depth <= scope_depth;
});
s != symbols.rend()
&& std::get_if<symbol::selection>(&s->sym)
&& s->depth == scope_depth
)
{
--scope_depth;
return true;
}
return false;
};
while (pop_implicit_else_branch()) { }
}

auto start(assignment_expression_node const& n, int)
Expand Down

0 comments on commit c0a8338

Please sign in to comment.