-
Notifications
You must be signed in to change notification settings - Fork 333
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enforce function returns at compile time (#2788)
* Enforce function returns at compile time * Raise cost of dynamic functions * Revert previous change Forgot const optimization isn't currently being used. Needs an analyzer step. Should really not have included that code in the rewrite but it's fine being there for now. * Add test cases * Simplify No need for `returned` field, can just check if scope is dead. * Add switch case return logic Adds logic for switch case to be the last statement in a function and be detected for return values.
- Loading branch information
Showing
5 changed files
with
88 additions
and
27 deletions.
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
data/expression2/tests/compiler/compiler/restrictions/fn_return.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
## SHOULD_FAIL:COMPILE | ||
|
||
function string nothing() {} |
22 changes: 22 additions & 0 deletions
22
data/expression2/tests/compiler/compiler/restrictions/fn_return_pass.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
## SHOULD_PASS:COMPILE | ||
|
||
function string nothing() { | ||
return "something" | ||
} | ||
|
||
function number deadcase() { | ||
if (1) { | ||
return 2158129 | ||
} else { | ||
return 2321515 | ||
} | ||
} | ||
|
||
function number switchcase() { | ||
switch (5) { | ||
case 5, | ||
return 2 | ||
default, | ||
return 5 | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
data/expression2/tests/compiler/compiler/restrictions/fn_return_switch.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
## SHOULD_FAIL:COMPILE | ||
|
||
function string failure() { | ||
switch (5) { | ||
case 2, | ||
break | ||
default, | ||
break | ||
|
||
# 'break' does not return a value or cause a runtime error, just early returns switch. | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
data/expression2/tests/compiler/compiler/restrictions/fn_return_switch2.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
## SHOULD_FAIL:COMPILE | ||
|
||
function string failure() { | ||
switch (5) { | ||
case 2, | ||
return "boowomp" | ||
# no default case, compiler can't guarantee that this always runs, fails to compile. | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters