Require `return` in all match branches

Before the parser only checked if the catch-all branch has a return in
order to determine if the entire match block has a return.

This code block was assumed to always return.

    match value:
        "test":
            print("test")
        _:
            return

Now as soon as one of the branches has no return, the entire match block
is marked to not have a return.

(cherry picked from commit 79176decd5)
This commit is contained in:
Daniel Spaniol 2019-02-20 20:49:48 +01:00 committed by Rémi Verschelde
parent 8b1b8181b3
commit 044ec35f62
1 changed files with 4 additions and 2 deletions

View File

@ -2225,6 +2225,8 @@ GDScriptParser::PatternNode *GDScriptParser::_parse_pattern(bool p_static) {
void GDScriptParser::_parse_pattern_block(BlockNode *p_block, Vector<PatternBranchNode *> &p_branches, bool p_static) { void GDScriptParser::_parse_pattern_block(BlockNode *p_block, Vector<PatternBranchNode *> &p_branches, bool p_static) {
int indent_level = tab_level.back()->get(); int indent_level = tab_level.back()->get();
p_block->has_return = true;
while (true) { while (true) {
while (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE && _parse_newline()) while (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE && _parse_newline())
@ -2282,8 +2284,8 @@ void GDScriptParser::_parse_pattern_block(BlockNode *p_block, Vector<PatternBran
current_block = p_block; current_block = p_block;
if (catch_all && branch->body->has_return) { if (!branch->body->has_return) {
p_block->has_return = true; p_block->has_return = false;
} }
p_branches.push_back(branch); p_branches.push_back(branch);