Skip to content

Commit 288386e

Browse files
committed
check for missing return statement
1 parent 5a3505e commit 288386e

7 files changed

Lines changed: 39 additions & 4 deletions

File tree

parser.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,11 @@ const Statement *Parser::parseFunctionDefinition(Scope *scope)
913913
function->statements.push_back(s);
914914
}
915915
}
916+
if (returntype != TYPE_NOTHING) {
917+
if (function->statements.empty() || dynamic_cast<const ReturnStatement *>(function->statements.back()) == nullptr) {
918+
error(2146, tokens[i], "missing RETURN statement");
919+
}
920+
}
916921
++i;
917922
if (tokens[i].type != FUNCTION) {
918923
error(2102, tokens[i], "'FUNCTION' expected");

t/errors/S2146.simple

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
FUNCTION foo(): Number
2+
END FUNCTION

t/recursion.simple

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
% SKIP (infinite loop, RETURN jump to end of function)
12
FUNCTION factorial(x: Number): Number
23
IF x = 0 THEN
34
RETURN 1
4-
ELSE
5-
RETURN x * factorial(x - 1)
65
END IF
6+
RETURN x * factorial(x - 1)
77
END FUNCTION
88

99
print(str(factorial(5)))

t/return-case.simple

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
% TODO
2+
3+
FUNCTION foo(x: Number): Number
4+
CASE x
5+
WHEN < 5 DO
6+
RETURN -1
7+
WHEN >= 5 DO
8+
RETURN 1
9+
END CASE
10+
END FUNCTION
11+
12+
print(str(foo(5))
13+
14+
%= 1

t/return2.simple renamed to t/return-if.simple

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
% TODO
2+
13
FUNCTION foo(x: Number): String
24
IF x = 0 THEN
35
RETURN "zero"

t/return-loop.simple

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
% TODO
2+
3+
FUNCTION foo(x: Number): Number
4+
LOOP
5+
IF x = 3 THEN
6+
RETURN x
7+
END IF
8+
x := x - 1
9+
END LOOP
10+
END FUNCTION
11+
12+
print(foo(5))
13+
14+
%= 3

t/return.simple

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
% TODO
2-
31
FUNCTION foo(x: Number): Number
42
print(str(x))
53
END FUNCTION

0 commit comments

Comments
 (0)