Skip to content

Commit

Permalink
[typer] map this when restoring typed expressions
Browse files Browse the repository at this point in the history
see #11212
  • Loading branch information
Simn committed Aug 31, 2023
1 parent a0ac5ad commit 6de366c
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 12 deletions.
4 changes: 0 additions & 4 deletions src/context/typecore.ml
Original file line number Diff line number Diff line change
Expand Up @@ -739,10 +739,6 @@ let get_next_stored_typed_expr_id =
let uid = ref 0 in
(fun() -> incr uid; !uid)

let get_stored_typed_expr com id =
let e = com.stored_typed_exprs#find id in
Texpr.duplicate_tvars e

let store_typed_expr com te p =
let id = get_next_stored_typed_expr_id() in
com.stored_typed_exprs#add id te;
Expand Down
6 changes: 5 additions & 1 deletion src/core/texpr.ml
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,9 @@ let rec equal e1 e2 = match e1.eexpr,e2.eexpr with
| TEnumParameter(e1,ef1,i1),TEnumParameter(e2,ef2,i2) -> equal e1 e2 && ef1 == ef2 && i1 = i2
| _ -> false

let duplicate_tvars e =
let e_identity e = e

let duplicate_tvars f_this e =
let vars = Hashtbl.create 0 in
let copy_var v =
let v2 = alloc_var v.v_kind v.v_name v.v_type v.v_pos in
Expand Down Expand Up @@ -344,6 +346,8 @@ let duplicate_tvars e =
{e with eexpr = TLocal v2}
with _ ->
e)
| TConst TThis ->
f_this e
| _ ->
map_expr build_expr e
in
Expand Down
2 changes: 1 addition & 1 deletion src/optimization/analyzerTexpr.ml
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ module TexprFilter = struct
let e_if eo = mk (TIf(e_not,e_break,eo)) com.basic.tvoid p in
let rec map_continue e = match e.eexpr with
| TContinue ->
Texpr.duplicate_tvars (e_if (Some e))
Texpr.duplicate_tvars e_identity (e_if (Some e))
| TWhile _ | TFor _ ->
e
| _ ->
Expand Down
4 changes: 2 additions & 2 deletions src/optimization/inline.ml
Original file line number Diff line number Diff line change
Expand Up @@ -506,14 +506,14 @@ class inline_state ctx ethis params cf f p = object(self)
| VIInline ->
begin match e'.eexpr with
(* If we inline a function expression, we have to duplicate its locals. *)
| TFunction _ -> Texpr.duplicate_tvars e'
| TFunction _ -> Texpr.duplicate_tvars e_identity e'
| TCast(e1,None) when in_assignment -> e1
| _ -> e'
end
| VIInlineIfCalled when in_call ->
(* We allow inlining function expressions into call-places. However, we have to substitute
their locals to avoid duplicate declarations. *)
Texpr.duplicate_tvars e'
Texpr.duplicate_tvars e_identity e'
| _ -> e
end
with Not_found ->
Expand Down
4 changes: 2 additions & 2 deletions src/typing/forLoop.ml
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ module IterationKind = struct
| _ -> map_expr loop e
in
let e2 = loop e2 in
Texpr.duplicate_tvars e2
Texpr.duplicate_tvars e_identity e2
) in
mk (TBlock el) t_void p
| IteratorIntConst(a,b,ascending) ->
Expand Down Expand Up @@ -367,7 +367,7 @@ module IterationKind = struct
let el = List.map (fun e ->
let ev = mk (TVar(v,Some e)) t_void e.epos in
let e = concat ev e2 in
Texpr.duplicate_tvars e
Texpr.duplicate_tvars e_identity e
) el in
mk (TBlock el) t_void p
| IteratorArray | IteratorArrayAccess ->
Expand Down
2 changes: 1 addition & 1 deletion src/typing/macroContext.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1058,4 +1058,4 @@ let setup() =

let type_stored_expr ctx e1 =
let id = match e1 with (EConst (Int (s, _)),_) -> int_of_string s | _ -> die "" __LOC__ in
get_stored_typed_expr ctx.com id
TyperBase.get_stored_typed_expr ctx id
2 changes: 1 addition & 1 deletion src/typing/matcher/texprConverter.ml
Original file line number Diff line number Diff line change
Expand Up @@ -393,4 +393,4 @@ let to_texpr ctx t_switch with_type dt =
| None ->
raise_typing_error "Unmatched patterns: _" p;
| Some e ->
Texpr.duplicate_tvars e
Texpr.duplicate_tvars e_identity e
4 changes: 4 additions & 0 deletions src/typing/typerBase.ml
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ let get_this ctx p =
| FunConstructor | FunMember ->
mk (TConst TThis) ctx.tthis p

let get_stored_typed_expr ctx id =
let e = ctx.com.stored_typed_exprs#find id in
Texpr.duplicate_tvars (fun e -> get_this ctx e.epos) e

let assign_to_this_is_allowed ctx =
match ctx.curclass.cl_kind with
| KAbstractImpl _ ->
Expand Down
19 changes: 19 additions & 0 deletions tests/unit/src/unit/issues/Issue11212.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package unit.issues;

class Issue11212 extends Test {
var value = 123;

function test() {
#if !macro
var foo:Foo = value; // on JS this generates `let foo = function() { return this.value; };` on JS
eq(123, foo());
#end
}
}

@:callable
private abstract Foo(() -> Int) from () -> Int {
@:from macro static function ofExpr(e) {
return macro @:pos(e.pos) (function():Int return $e : Foo);
}
}

0 comments on commit 6de366c

Please sign in to comment.