Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MFromMacroInMacro constraint and check it when iterating/matching #11406

Open
wants to merge 2 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/context/typecore.ml
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,17 @@ let spawn_monomorph' ctx p =
let spawn_monomorph ctx p =
TMono (spawn_monomorph' ctx p)

let extract_macro_in_macro_constraint m =
let rec loop l = match l with
| MFromMacroInMacro p :: _ ->
Some p
| _ :: l ->
loop l
| [] ->
None
in
loop m.tm_down_constraints

let make_static_field_access c cf t p =
let ethis = Texpr.Builder.make_static_this c p in
mk (TField (ethis,(FStatic (c,cf)))) t p
Expand Down
1 change: 1 addition & 0 deletions src/core/tPrinting.ml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ and s_constraint = function
| MType(t,_) -> Printf.sprintf "MType %s" (s_type_kind t)
| MOpenStructure -> "MOpenStructure"
| MEmptyStructure -> "MEmptyStructure"
| MFromMacroInMacro _ -> "MFromMacroInMacro"

let s_type_param s_type ttp =
let s = match (get_constraints ttp) with
Expand Down
1 change: 1 addition & 0 deletions src/core/tType.ml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ and tmono_constraint =
| MType of t * string option
| MOpenStructure
| MEmptyStructure
| MFromMacroInMacro of pos

and tmono_constraint_kind =
| CUnknown
Expand Down
2 changes: 2 additions & 0 deletions src/core/tUnification.ml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ module Monomorph = struct
| MOpenStructure
| MEmptyStructure ->
is_open := true
| MFromMacroInMacro _ ->
()
in
List.iter check m.tm_down_constraints;
let kind =
Expand Down
19 changes: 17 additions & 2 deletions src/typing/callUnification.ml
Original file line number Diff line number Diff line change
Expand Up @@ -471,8 +471,23 @@ object(self)
let ethis_f = ref (fun () -> ()) in
let macro_in_macro () =
(fun () ->
let e = (EThrow((EConst(String("macro-in-macro",SDoubleQuotes))),p),p) in
type_expr ~mode ctx e with_type
let e_msg = Texpr.type_constant ctx.com.basic (String("macro-in-macro",SDoubleQuotes)) p in
let type_as t = mk (TThrow e_msg) t p in
match with_type with
| WithType.NoValue ->
type_as t_dynamic
| WithType.Value _ ->
let m = spawn_monomorph' ctx.e p in
Monomorph.add_down_constraint m (MFromMacroInMacro p);
type_as (TMono m)
| WithType(t,_) ->
begin match follow t with
| TMono m ->
Monomorph.add_down_constraint m (MFromMacroInMacro p);
| _ ->
()
end;
type_as t
)
in
let f = (match ethis.eexpr with
Expand Down
24 changes: 21 additions & 3 deletions src/typing/forLoop.ml
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,18 @@ module IterationKind = struct
display_error ctx.com "You can't iterate on a Dynamic value, please specify Iterator or Iterable" e.epos;
IteratorDynamic,e,t_dynamic
in
let mono_iterator m e =
begin match extract_macro_in_macro_constraint m with
| Some p ->
let sub = if p <> e.epos then [Error.make_error (Custom "Call was here") p] else [] in
display_error_ext ctx.com (Error.make_error
~sub
(Custom "Cannot iterate on expression from macro-in-macro call") e.epos);
| None ->
display_error ctx.com "Cannot iterate on unknown value" e.epos;
end;
IteratorDynamic,e,t_dynamic
in
let check_iterator () =
let array_access_result = ref None in
let last_resort () =
Expand All @@ -185,8 +197,12 @@ module IterationKind = struct
| Some result -> result
| None ->
match Abstract.follow_with_abstracts e1.etype with
| (TMono _ | TDynamic _) -> dynamic_iterator e1;
| _ -> (IteratorIterator,e1,pt)
| TMono m ->
mono_iterator m e
| TDynamic _ ->
dynamic_iterator e1;
| _ ->
(IteratorIterator,e1,pt)
in
let try_forward_array_iterator () =
match follow e.etype with
Expand Down Expand Up @@ -251,7 +267,9 @@ module IterationKind = struct
with Not_found -> check_iterator ())
| _,TInst ({ cl_kind = KGenericInstance ({ cl_path = ["haxe";"ds"],"GenericStack" },[pt]) } as c,[]) ->
IteratorGenericStack c,e,pt
| _,(TMono _ | TDynamic _) ->
| _,TMono m ->
mono_iterator m e;
| _,TDynamic _ ->
dynamic_iterator e
| _ ->
check_iterator ()
Expand Down
24 changes: 22 additions & 2 deletions src/typing/matcher.ml
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,35 @@ module Match = struct

let match_expr ctx e cases def with_type postfix_match p =
let match_debug = Meta.has (Meta.Custom ":matchDebug") ctx.f.curfield.cf_meta in
let check_mono e =
match follow e.etype with
| TMono m ->
begin match extract_macro_in_macro_constraint m with
| Some p ->
let sub = if p <> e.epos then [Error.make_error (Custom "Call was here") p] else [] in
Error.raise_typing_error_ext (Error.make_error
~sub
(Custom "Cannot match on expression from macro-in-macro call") e.epos);
| None ->
()
end
| _ ->
()
in
let type_expr e =
let e = type_expr ctx e WithType.value in
check_mono e;
e
in
let rec loop e = match fst e with
| EArrayDecl el when (match el with [(EFor _ | EWhile _),_] -> false | _ -> true) ->
let el = List.map (fun e -> type_expr ctx e WithType.value) el in
let el = List.map type_expr el in
let t = ExprToPattern.tuple_type (List.map (fun e -> e.etype) el) in
t,el
| EParenthesis e1 ->
loop e1
| _ ->
let e = type_expr ctx e WithType.value in
let e = type_expr e in
e.etype,[e]
in
let t,subjects = loop e in
Expand Down
Loading