Skip to content

Commit

Permalink
copy var flags when duplicating
Browse files Browse the repository at this point in the history
Also don't unroll loops that have static vars
closes #11800
  • Loading branch information
Simn committed Oct 24, 2024
1 parent 912550d commit 991e227
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/core/texpr.ml
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ let duplicate_tvars f_this e =
let v2 = alloc_var v.v_kind v.v_name v.v_type v.v_pos in
v2.v_meta <- v.v_meta;
v2.v_extra <- v.v_extra;
v2.v_flags <- v.v_flags;
Hashtbl.add vars v.v_id v2;
v2;
in
Expand Down
2 changes: 2 additions & 0 deletions src/typing/forLoop.ml
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,8 @@ let is_cheap_enough ctx e2 i =
let rec loop e = match fst e with
| EContinue | EBreak ->
raise Exit
| EVars vl when (List.exists (fun ev -> ev.ev_static) vl) ->
raise Exit
| _ ->
incr num_expr;
Ast.map_expr loop e
Expand Down
21 changes: 21 additions & 0 deletions tests/unit/src/unit/issues/Issue11800.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package unit.issues;

class Issue11800 extends unit.Test {
public function test() {
static var a = 0; // Works.
var buf = new StringBuf();
function append(v:Int) {
buf.add(Std.string(v));
}
for (i in 0...3) {
switch i {
case n if (n < 2):
append(++a);
static var b = 0; // Not static.
append(++b); // Always `1`.
case _:
}
}
eq("1122", buf.toString());
}
}

0 comments on commit 991e227

Please sign in to comment.