Skip to content

Commit 0ea2d6f

Browse files
committed
Set the type of locals based on the expression they are initialized with
As a byproduct, the environment types get refined as well.
1 parent c91453f commit 0ea2d6f

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
* Compiler: rewrote inlining pass (#1935, #2018, #2027)
4747
* Compiler/wasm: optimize integer operations (#2032)
4848
* Compiler/wasm: use type analysis to remove some unnecessary uses of JavasScript strict equality (#2040)
49+
* Compiler/wasm: use more precise environment types (#2041)
4950

5051
## Bug fixes
5152
* Compiler: fix stack overflow issues with double translation (#1869)

compiler/lib-wasm/code_generation.ml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,11 @@ let tee ?typ x e =
493493
let* () = register_constant x e in
494494
return e
495495
else
496+
let* typ =
497+
match typ with
498+
| Some _ -> return typ
499+
| None -> expression_type e
500+
in
496501
let* i = add_var ?typ x in
497502
return (W.LocalTee (i, e))
498503

@@ -563,7 +568,14 @@ let rec store ?(always = false) ?typ x e =
563568
let* typ =
564569
match typ with
565570
| Some typ -> return typ
566-
| None -> value_type
571+
| None -> (
572+
if always
573+
then value_type
574+
else
575+
let* typ = expression_type e in
576+
match typ with
577+
| None -> value_type
578+
| Some typ -> return typ)
567579
in
568580
let* default, typ', cast = default_value typ in
569581
let* () =
@@ -577,6 +589,11 @@ let rec store ?(always = false) ?typ x e =
577589
in
578590
instr (GlobalSet (x, e))
579591
else
592+
let* typ =
593+
match typ with
594+
| Some _ -> return typ
595+
| None -> if always then return None else expression_type e
596+
in
580597
let* i = add_var ?typ x in
581598
instr (LocalSet (i, e))
582599

0 commit comments

Comments
 (0)