Skip to content

Commit

Permalink
Fix conditional returns messing up nested branches.
Browse files Browse the repository at this point in the history
Bug: issue #890
Test: new test
  • Loading branch information
dvander committed Jul 30, 2023
1 parent 508a64c commit 36264e5
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 1 deletion.
11 changes: 11 additions & 0 deletions compiler/parse-node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ LogicalExpr::FlattenLogical(int token, std::vector<Expr*>* out)
}
}

bool Stmt::IsTerminal() const {
switch (flow_type()) {
case Flow_Break:
case Flow_Continue:
case Flow_Return:
return true;
default:
return false;
}
}

BlockStmt*
BlockStmt::WrapStmt(Stmt* stmt)
{
Expand Down
2 changes: 1 addition & 1 deletion compiler/parse-node.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class Stmt : public ParseNode
FlowType flow_type() const { return flow_type_; }
void set_flow_type(FlowType type) { flow_type_ = type; }

bool IsTerminal() const { return flow_type() != Flow_None; }
bool IsTerminal() const;

StmtKind kind() const { return kind_; }
bool is(StmtKind k) const { return kind() == k; }
Expand Down
1 change: 1 addition & 0 deletions tests/regressions/terminal-statements-in-conditions.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hi
18 changes: 18 additions & 0 deletions tests/regressions/terminal-statements-in-conditions.sp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <shell>

public void main()
{
if (1)
{
if (0)
{
return;
}

print("hi\n");
}
else
{
print("this should never happen\n");
}
}
1 change: 1 addition & 0 deletions tests/regressions/terminal-statements-in-switch.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hi
30 changes: 30 additions & 0 deletions tests/regressions/terminal-statements-in-switch.sp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <shell>

public void main()
{
switch (0)
{
case 0:
{
if (0)
{
return;
}

print("hi\n");
}
case 1:
{
if (0)
{
return;
}

print("this should never happen\n");
}
default:
{
print("this should never happen either\n");
}
}
}

0 comments on commit 36264e5

Please sign in to comment.