Skip to content

Commit

Permalink
Fix tmpvar redefinition when incrementing a abstract array access exp…
Browse files Browse the repository at this point in the history
…ression involving null coalescing.
  • Loading branch information
Apprentice-Alchemist committed Mar 1, 2025
1 parent 6508421 commit 6a03656
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/typing/operators.ml
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,9 @@ let type_unop ctx op flag e with_type p =
| AKAccess(a,tl,c,ebase,ekey) ->
begin try
(match op with Increment | Decrement -> () | _ -> raise Not_found);
let v_base = alloc_var VGenerated "tmp" ebase.etype ebase.epos in
let evar_base = mk (TVar(v_base, Some ebase)) ctx.com.basic.tvoid ebase.epos in
let ebase = mk (TLocal v_base) ekey.etype ekey.epos in
let v_key = alloc_var VGenerated "tmp" ekey.etype ekey.epos in
let evar_key = mk (TVar(v_key,Some ekey)) ctx.com.basic.tvoid ekey.epos in
let ekey = mk (TLocal v_key) ekey.etype ekey.epos in
Expand All @@ -997,7 +1000,7 @@ let type_unop ctx op flag e with_type p =
let e_op = mk (TBinop((if op = Increment then OpAdd else OpSub),ev_get,e_one)) ev_get.etype p in
(* set *)
let e_set = mk_array_set_call ctx (AbstractCast.find_array_write_access_raise ctx a tl ekey e_op p) c ebase p in
let el = evar_key :: evar_get :: e_set :: (if flag = Postfix then [ev_get] else []) in
let el = evar_base :: evar_key :: evar_get :: e_set :: (if flag = Postfix then [ev_get] else []) in
mk (TBlock el) e_set.etype p
with Not_found ->
let e = mk_array_get_call ctx (AbstractCast.find_array_read_access ctx a tl ekey p) c ebase p in
Expand Down
1 change: 1 addition & 0 deletions tests/misc/cpp/projects/Issue12027/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
out
21 changes: 21 additions & 0 deletions tests/misc/cpp/projects/Issue12027/Main.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function main() {
foo(new Foo());
}

function foo(foo:Null<Foo>) {
(foo ?? throw "hello")[0]++;
}

abstract Foo(Int) {
public function new() {
this = 0;
}

@:op([]) function get(i:Int) {
return this;
}

@:op([]) function set(i:Int, val:Int) {
return this;
}
}
2 changes: 2 additions & 0 deletions tests/misc/cpp/projects/Issue12027/compile.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-m Main
-cpp out

0 comments on commit 6a03656

Please sign in to comment.