Skip to content

Commit

Permalink
remove context flushing from encoder
Browse files Browse the repository at this point in the history
This should no longer be necessary. I'm adding the relevant tests to make sure.
  • Loading branch information
Simn committed Feb 1, 2024
1 parent 40eefab commit a092d9d
Show file tree
Hide file tree
Showing 18 changed files with 97 additions and 16 deletions.
4 changes: 0 additions & 4 deletions src/macro/eval/evalContext.ml
Original file line number Diff line number Diff line change
Expand Up @@ -403,10 +403,6 @@ let exc_string_p str p = throw (vstring (EvalString.create_ascii str)) p

let error_message = exc_string

let flush_core_context f =
let ctx = get_ctx() in
ctx.curapi.MacroApi.flush_context f

(* Environment handling *)

let no_timer = fun () -> ()
Expand Down
8 changes: 2 additions & 6 deletions src/macro/macroApi.ml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ type 'value compiler_api = {
encode_expr : Ast.expr -> 'value;
encode_ctype : Ast.type_hint -> 'value;
decode_type : 'value -> t;
flush_context : (unit -> t) -> t;
info : ?depth:int -> string -> pos -> unit;
warning : ?depth:int -> Warning.warning -> string -> pos -> unit;
display_error : ?depth:int -> (string -> pos -> unit);
Expand Down Expand Up @@ -172,8 +171,6 @@ module type InterpApi = sig

val value_string : value -> string

val flush_core_context : (unit -> t) -> t

val handle_decoding_error : (string -> unit) -> value -> Type.t -> (string * int) list

val get_api_call_pos : unit -> pos
Expand Down Expand Up @@ -1099,7 +1096,7 @@ and encode_cfield f =
"params", encode_type_params f.cf_params;
"meta", encode_meta f.cf_meta (fun m -> f.cf_meta <- m);
"expr", vfun0 (fun() ->
ignore (flush_core_context (fun() -> follow f.cf_type));
ignore (follow f.cf_type);
(match f.cf_expr with None -> vnull | Some e -> encode_texpr e)
);
"kind", encode_field_kind f.cf_kind;
Expand Down Expand Up @@ -1266,8 +1263,7 @@ and encode_lazy_type t =
| LAvailable t ->
encode_type t
| LWait _ ->
(* we are doing some typing here, let's flush our context if it's not already *)
encode_type (flush_core_context (fun() -> lazy_type f))
encode_type (lazy_type f)
| LProcessing _ ->
(* our type in on the processing stack, error instead of returning most likely an unbound mono *)
error_message "Accessing a type while it's being typed");
Expand Down
6 changes: 0 additions & 6 deletions src/typing/macroContext.ml
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,6 @@ let make_macro_com_api com mcom p =
type_expr = (fun e ->
Interp.exc_string "unsupported"
);
flush_context = (fun f ->
Interp.exc_string "unsupported"
);
store_typed_expr = (fun te ->
let p = te.epos in
snd (Typecore.store_typed_expr com te p)
Expand Down Expand Up @@ -431,9 +428,6 @@ let make_macro_api ctx mctx p =
MacroApi.type_expr = (fun e ->
typing_timer ctx true (fun() -> type_expr ctx e WithType.value)
);
MacroApi.flush_context = (fun f ->
typing_timer ctx true f
);
MacroApi.type_patch = (fun t f s v ->
typing_timer ctx false (fun() ->
let v = (match v with None -> None | Some s ->
Expand Down
3 changes: 3 additions & 0 deletions tests/misc/projects/Issue5456/Main.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function main() {
trace(new Test().c);
}
16 changes: 16 additions & 0 deletions tests/misc/projects/Issue5456/Test.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package;

import test.C;

@:build(TestBuilder.build())
class Test extends B {
public function new() {
super();
}
}

class B {
public var c:Int = C.func();

public function new() {}
}
20 changes: 20 additions & 0 deletions tests/misc/projects/Issue5456/TestBuilder.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package;

import haxe.macro.Context;
import haxe.macro.Expr;
import haxe.macro.Type;

class TestBuilder {
public static function build():Array<Field> {
var fields:Array<Field> = Context.getBuildFields();
var classType:ClassType;
switch (Context.getLocalType()) {
case TInst(r, _):
classType = r.get();
case _:
}
classType.superClass.t.get().fields.get();

return fields;
}
}
2 changes: 2 additions & 0 deletions tests/misc/projects/Issue5456/compile.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--main Main
--interp
1 change: 1 addition & 0 deletions tests/misc/projects/Issue5456/compile.hxml.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Main.hx:2: 0
7 changes: 7 additions & 0 deletions tests/misc/projects/Issue5456/test/C.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package test;

class C {
public static function func():Int {
return 0;
}
}
3 changes: 3 additions & 0 deletions tests/misc/projects/Issue6321/A.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class A {
public var a:Int;
}
4 changes: 4 additions & 0 deletions tests/misc/projects/Issue6321/B.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class B {
var a:A;
function b() a.a;
}
14 changes: 14 additions & 0 deletions tests/misc/projects/Issue6321/Macro.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Macro {
static function doStuff() {
haxe.macro.Context.onAfterInitMacros(() -> {
switch (haxe.macro.Context.getType("B")) {
case TInst(_.get() => cl, _):
for (field in cl.fields.get()) {
trace(field.name, field.expr()?.pos);
}
default:
throw false;
}
});
}
}
12 changes: 12 additions & 0 deletions tests/misc/projects/Issue6321/MacroFail.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class MacroFail {
static function doStuff() {
switch (haxe.macro.Context.getType("B")) {
case TInst(_.get() => cl, _):
for (field in cl.fields.get()) {
trace(field.name, field.expr()?.pos);
}
default:
throw false;
}
}
}
1 change: 1 addition & 0 deletions tests/misc/projects/Issue6321/compile-fail.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--macro "MacroFail.doStuff()"
2 changes: 2 additions & 0 deletions tests/misc/projects/Issue6321/compile-fail.hxml.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
MacroFail.hx:3: characters 11-42 : Cannot use this API from initialization macros.
MacroFail.hx:3: characters 11-42 : ... Use `Context.onAfterInitMacros` to register a callback to run when context is ready.
1 change: 1 addition & 0 deletions tests/misc/projects/Issue6321/compile.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--macro "Macro.doStuff()"
2 changes: 2 additions & 0 deletions tests/misc/projects/Issue6321/compile.hxml.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Macro.hx:7: a,null
Macro.hx:7: b,#pos(B.hx:3: characters 18-21)
7 changes: 7 additions & 0 deletions tests/misc/projects/Issue6321/test/C.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package test;

class C {
public static function func():Int {
return 0;
}
}

0 comments on commit a092d9d

Please sign in to comment.