Skip to content

Commit

Permalink
Revert "remove Const flag (closes HaxeFoundation#3192)"
Browse files Browse the repository at this point in the history
This reverts commit 90c821d.
  • Loading branch information
ousado committed May 7, 2016
1 parent a8ca67c commit 557c1eb
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/macro/interp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4628,7 +4628,7 @@ and encode_anon_status s =
let tag, pl = (match s with
| Closed -> 0, []
| Opened -> 1, []
(* | Type.Const -> 2, [] *)
| Type.Const -> 2, []
| Extend tl -> 3, [encode_ref tl (fun tl -> enc_array (List.map encode_type tl)) (fun() -> "<extended types>")]
| Statics cl -> 4, [encode_clref cl]
| EnumStatics en -> 5, [encode_enref en]
Expand Down
8 changes: 8 additions & 0 deletions src/optimization/optimizer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,14 @@ let rec type_inline ctx cf f ethis params tret config p ?(self_calling_closure=f
l.i_write <- true;
let e2 = map false e2 in
{e with eexpr = TBinop(op,{e1 with eexpr = TLocal l.i_subst},e2)}
| TObjectDecl fl ->
let fl = List.map (fun (s,e) -> s,map false e) fl in
begin match follow e.etype with
| TAnon an when (match !(an.a_status) with Const -> true | _ -> false) ->
{e with eexpr = TObjectDecl fl; etype = TAnon { an with a_status = ref Closed}}
| _ ->
{e with eexpr = TObjectDecl fl}
end
| TFunction f ->
(match f.tf_args with [] -> () | _ -> has_vars := true);
let old = save_locals ctx and old_fun = !in_local_fun in
Expand Down
12 changes: 8 additions & 4 deletions src/typing/type.ml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ and tfunc = {
and anon_status =
| Closed
| Opened
| Const
| Extend of t list
| Statics of tclass
| EnumStatics of tenum
Expand Down Expand Up @@ -1809,7 +1810,7 @@ let rec unify a b =
(match !(an.a_status) with
| Opened -> an.a_status := Closed;
| Statics _ | EnumStatics _ | AbstractStatics _ -> error []
| Closed | Extend _ -> ())
| Closed | Extend _ | Const -> ())
with
Unify_error l -> error (cannot_unify a b :: l))
| TAnon a1, TAnon a2 ->
Expand Down Expand Up @@ -1933,11 +1934,14 @@ and unify_anons a b a1 a2 =
| Opened ->
if not (link (ref None) a f2.cf_type) then error [];
a1.a_fields <- PMap.add n f2 a1.a_fields
| Const when Meta.has Meta.Optional f2.cf_meta ->
()
| _ ->
if not (Meta.has Meta.Optional f2.cf_meta) then
error [has_no_field a n];
error [has_no_field a n];
) a2.a_fields;
(match !(a1.a_status) with
| Const when not (PMap.is_empty a2.a_fields) ->
PMap.iter (fun n _ -> if not (PMap.mem n a2.a_fields) then error [has_extra_field a n]) a1.a_fields;
| Opened ->
a1.a_status := Closed
| _ -> ());
Expand All @@ -1946,7 +1950,7 @@ and unify_anons a b a1 a2 =
| EnumStatics e -> (match !(a1.a_status) with EnumStatics e2 when e == e2 -> () | _ -> error [])
| AbstractStatics a -> (match !(a1.a_status) with AbstractStatics a2 when a == a2 -> () | _ -> error [])
| Opened -> a2.a_status := Closed
| Extend _ | Closed -> ())
| Const | Extend _ | Closed -> ())
with
Unify_error l -> error (cannot_unify a b :: l))

Expand Down
42 changes: 38 additions & 4 deletions src/typing/typer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,34 @@ let get_abstract_froms a pl =
acc
) l a.a_from_field

(*
temporally remove the constant flag from structures to allow larger unification
*)
let remove_constant_flag t callb =
let tmp = ref [] in
let rec loop t =
match follow t with
| TAnon a ->
if !(a.a_status) = Const then begin
a.a_status := Closed;
tmp := a :: !tmp;
end;
PMap.iter (fun _ f -> loop f.cf_type) a.a_fields;
| _ ->
()
in
let restore() =
List.iter (fun a -> a.a_status := Const) (!tmp)
in
try
loop t;
let ret = callb (!tmp <> []) in
restore();
ret
with e ->
restore();
raise e

let rec is_pos_infos = function
| TMono r ->
(match !r with
Expand Down Expand Up @@ -583,7 +611,7 @@ let rec unify_min_raise ctx (el:texpr list) : t =
let expr f = match f.cf_expr with None -> mk (TBlock []) f.cf_type f.cf_pos | Some e -> e in
let fields = List.fold_left (fun acc e ->
match follow e.etype with
| TAnon a ->
| TAnon a when !(a.a_status) = Const ->
if !fcount = -1 then begin
fcount := field_count a;
PMap.map (fun f -> [expr f]) a.a_fields
Expand Down Expand Up @@ -1245,6 +1273,7 @@ let rec using_field ctx mode e i p =
| TMono _ -> raise Not_found
| t -> t == t_dynamic
in
let check_constant_struct = ref false in
let rec loop = function
| [] ->
raise Not_found
Expand Down Expand Up @@ -1274,6 +1303,7 @@ let rec using_field ctx mode e i p =
with Not_found ->
loop l
| Unify_error el | Error (Unify el,_) ->
if List.exists (function Has_extra_field _ -> true | _ -> false) el then check_constant_struct := true;
loop l
in
try loop ctx.m.module_using with Not_found ->
Expand All @@ -1284,7 +1314,8 @@ let rec using_field ctx mode e i p =
| _ -> assert false);
acc
with Not_found ->
raise Not_found
if not !check_constant_struct then raise Not_found;
remove_constant_flag e.etype (fun ok -> if ok then using_field ctx mode e i p else raise Not_found)

let rec type_ident_raise ctx i p mode =
match i with
Expand Down Expand Up @@ -3040,7 +3071,7 @@ and type_object_decl ctx fl with_type p =
let e = if is_quoted then wrap_quoted_meta e else e in
(n,e)
) fl in
let t = (TAnon { a_fields = !fields; a_status = ref Closed }) in
let t = (TAnon { a_fields = !fields; a_status = ref Const }) in
if not ctx.untyped then begin
(match PMap.foldi (fun n cf acc -> if not (Meta.has Meta.Optional cf.cf_meta) && not (PMap.mem n !fields) then n :: acc else acc) field_map [] with
| [] -> ()
Expand All @@ -3067,9 +3098,12 @@ and type_object_decl ctx fl with_type p =
end else acc)
in
let fields , types = List.fold_left loop ([],PMap.empty) fl in
mk (TObjectDecl (List.rev fields)) (TAnon { a_fields = types; a_status = ref Closed }) p
let x = ref Const in
ctx.opened <- x :: ctx.opened;
mk (TObjectDecl (List.rev fields)) (TAnon { a_fields = types; a_status = x }) p
| ODKWithStructure a ->
let t, fl = type_fields a.a_fields in
if !(a.a_status) <> Const then a.a_status := Closed;
mk (TObjectDecl fl) t p
| ODKWithClass (c,tl) ->
let t,ctor = get_constructor ctx c tl p in
Expand Down
5 changes: 0 additions & 5 deletions tests/misc/projects/Issue3192/Main1.hx

This file was deleted.

2 changes: 0 additions & 2 deletions tests/misc/projects/Issue3192/compile1-fail.hxml

This file was deleted.

1 change: 0 additions & 1 deletion tests/misc/projects/Issue3192/compile1-fail.hxml.stderr

This file was deleted.

17 changes: 0 additions & 17 deletions tests/unit/src/unit/issues/Issue3192.hx

This file was deleted.

0 comments on commit 557c1eb

Please sign in to comment.