Skip to content

Commit 5036b77

Browse files
forbid return outside functions (closes #9659) (#9665)
1 parent 8a67dc6 commit 5036b77

File tree

8 files changed

+34
-3
lines changed

8 files changed

+34
-3
lines changed

extra/CHANGES.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
2020-XX-XX 4.1.3
22

3+
General improvements:
4+
5+
all : added an error on `return` outside of a function (#9659)
6+
php : support @:native("") for extern classes (#6448)
7+
python : support @:native("") for extern classes (#6448)
8+
39
Bugfixes:
410

511
all : fixed compilation server bug caused by removing `inline` from a method (#9690)
@@ -8,9 +14,7 @@
814
flash : fixed var shadowing issue for variables captured in a closure inside of a loop (#9624)
915
flash : fixed `VerifyError` exception when `Void` end up as an argument type after inlining (#9678)
1016
php : fixed runtime error "cannot use temporary expression in write context" for call arguments passed by reference
11-
php : support @:native("") for extern classes (#6448)
1217
cs : fixed `cs.Lib.rethrow` (#9738)
13-
python : support @:native("") for extern classes (#6448)
1418
nullsafety: fixed `@:nullSafety(Off)` in closures inside of constructors (#9643)
1519
nullsafety: fixed "Type not found NullSafetyMode_Impl_" (#9483)
1620

src/context/typecore.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ and typer = {
122122
(* per-function *)
123123
mutable curfield : tclass_field;
124124
mutable untyped : bool;
125+
mutable in_function : bool;
125126
mutable in_loop : bool;
126127
mutable in_display : bool;
127128
mutable in_macro : bool;

src/typing/typeloadFunction.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,14 @@ let save_field_state ctx =
4545
let old_ret = ctx.ret in
4646
let old_fun = ctx.curfun in
4747
let old_opened = ctx.opened in
48+
let old_in_function = ctx.in_function in
4849
let locals = ctx.locals in
4950
(fun () ->
5051
ctx.locals <- locals;
5152
ctx.ret <- old_ret;
5253
ctx.curfun <- old_fun;
5354
ctx.opened <- old_opened;
55+
ctx.in_function <- old_in_function;
5456
)
5557

5658
let type_var_field ctx t e stat do_display p =
@@ -105,6 +107,7 @@ let type_function ctx args ret fmode f do_display p =
105107
if n = "this" then v.v_meta <- (Meta.This,[],null_pos) :: v.v_meta;
106108
v,c
107109
) args f.f_args in
110+
ctx.in_function <- true;
108111
ctx.curfun <- fmode;
109112
ctx.ret <- ret;
110113
ctx.opened <- [];

src/typing/typeloadModule.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,7 @@ let type_types_into_module ctx m tdecls p =
879879
untyped = false;
880880
in_macro = ctx.in_macro;
881881
in_display = false;
882+
in_function = false;
882883
in_loop = false;
883884
opened = [];
884885
in_call_args = false;

src/typing/typer.ml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2562,7 +2562,18 @@ and type_expr ?(mode=MGet) ctx (e,p) (with_type:WithType.t) =
25622562
let e = Matcher.Match.match_expr ctx e1 cases def with_type false p in
25632563
wrap e
25642564
| EReturn e ->
2565-
type_return ctx e with_type p
2565+
if not ctx.in_function then begin
2566+
display_error ctx "Return outside function" p;
2567+
match e with
2568+
| None ->
2569+
Texpr.Builder.make_null t_dynamic p
2570+
| Some e ->
2571+
(* type the return expression to see if there are more errors
2572+
as well as use its type as if there was no `return`, since
2573+
that is most likely what was meant *)
2574+
type_expr ctx e WithType.value
2575+
end else
2576+
type_return ctx e with_type p
25662577
| EBreak ->
25672578
if not ctx.in_loop then display_error ctx "Break outside loop" p;
25682579
mk TBreak t_dynamic p
@@ -2666,6 +2677,7 @@ let rec create com =
26662677
macro_depth = 0;
26672678
untyped = false;
26682679
curfun = FunStatic;
2680+
in_function = false;
26692681
in_loop = false;
26702682
in_display = false;
26712683
get_build_infos = (fun() -> None);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Main {
2+
static var field = return 3;
3+
4+
static function main() {
5+
var x:String = field;
6+
}
7+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--main Main
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Main.hx:2: characters 21-29 : Return outside function
2+
Main.hx:5: characters 3-24 : Int should be String

0 commit comments

Comments
 (0)