Ver código fonte

Generalize state restoration of module types (#11001)

* [server] add restore functions to other module types

* [server] save path of all module types

see #10986

* [tests] add tests for 10986

---------

Co-authored-by: Rudy Ges <[email protected]>
Simon Krajewski 2 anos atrás
pai
commit
85816bdcef

+ 1 - 14
src/compiler/server.ml

@@ -421,20 +421,7 @@ let add_modules sctx ctx m p =
 				m.m_extra.m_added <- ctx.com.compilation_step;
 				ServerMessage.reusing com tabs m;
 				List.iter (fun t ->
-					match t with
-					| TClassDecl c -> c.cl_restore()
-					| TEnumDecl e ->
-						let rec loop acc = function
-							| [] -> ()
-							| (Meta.RealPath,[Ast.EConst (Ast.String(path,_)),_],_) :: l ->
-								e.e_path <- Ast.parse_path path;
-								e.e_meta <- (List.rev acc) @ l;
-							| x :: l -> loop (x::acc) l
-						in
-						loop [] e.e_meta
-					| TAbstractDecl a ->
-						a.a_meta <- List.filter (fun (m,_,_) -> m <> Meta.ValueUsed) a.a_meta
-					| _ -> ()
+					(t_infos t).mt_restore()
 				) m.m_types;
 				TypeloadModule.ModuleLevel.add_module ctx m p;
 				PMap.iter (Hashtbl.replace com.resources) m.m_extra.m_binded_res;

+ 2 - 0
src/core/tFunctions.ml

@@ -147,6 +147,7 @@ let mk_typedef m path pos name_pos t =
 		t_params = [];
 		t_using = [];
 		t_type = t;
+		t_restore = (fun () -> ());
 	}
 
 let module_extra file sign time kind policy =
@@ -215,6 +216,7 @@ let null_abstract = {
 	a_meta = [];
 	a_params = [];
 	a_using = [];
+	a_restore = (fun () -> ());
 	a_ops = [];
 	a_unops = [];
 	a_impl = None;

+ 5 - 1
src/core/tType.ml

@@ -240,6 +240,7 @@ and tinfos = {
 	mutable mt_meta : metadata;
 	mt_params : type_params;
 	mutable mt_using : (tclass * pos) list;
+	mutable mt_restore : unit -> unit;
 }
 
 and tclass = {
@@ -252,6 +253,7 @@ and tclass = {
 	mutable cl_meta : metadata;
 	mutable cl_params : type_params;
 	mutable cl_using : (tclass * pos) list;
+	mutable cl_restore : unit -> unit;
 	(* do not insert any fields above *)
 	mutable cl_kind : tclass_kind;
 	mutable cl_flags : int;
@@ -267,7 +269,6 @@ and tclass = {
 	mutable cl_init : texpr option;
 
 	mutable cl_build : unit -> build_state;
-	mutable cl_restore : unit -> unit;
 	(*
 		These are classes which directly extend or directly implement this class.
 		Populated automatically in post-processing step (Filters.run)
@@ -296,6 +297,7 @@ and tenum = {
 	mutable e_meta : metadata;
 	mutable e_params : type_params;
 	mutable e_using : (tclass * pos) list;
+	mutable e_restore : unit -> unit;
 	(* do not insert any fields above *)
 	e_type : tdef;
 	mutable e_extern : bool;
@@ -313,6 +315,7 @@ and tdef = {
 	mutable t_meta : metadata;
 	mutable t_params : type_params;
 	mutable t_using : (tclass * pos) list;
+	mutable t_restore : unit -> unit;
 	(* do not insert any fields above *)
 	mutable t_type : t;
 }
@@ -327,6 +330,7 @@ and tabstract = {
 	mutable a_meta : metadata;
 	mutable a_params : type_params;
 	mutable a_using : (tclass * pos) list;
+	mutable a_restore : unit -> unit;
 	(* do not insert any fields above *)
 	mutable a_ops : (Ast.binop * tclass_field) list;
 	mutable a_unops : (Ast.unop * unop_flag * tclass_field) list;

+ 24 - 2
src/filters/filters.ml

@@ -883,8 +883,30 @@ let save_class_state ctx t =
 			c.cl_descendants <- [];
 			List.iter (fun (v, t) -> v.v_type <- t) !vars;
 		)
-	| _ ->
-		()
+	| TEnumDecl en ->
+		let path = en.e_path in
+		en.e_restore <- (fun () ->
+			let rec loop acc = function
+				| [] ->
+					en.e_path <- path;
+				| (Meta.RealPath,[Ast.EConst (Ast.String(path,_)),_],_) :: l ->
+					en.e_path <- Ast.parse_path path;
+					en.e_meta <- (List.rev acc) @ l;
+				| x :: l -> loop (x::acc) l
+			in
+			loop [] en.e_meta
+		)
+	| TTypeDecl td ->
+		let path = td.t_path in
+		td.t_restore <- (fun () ->
+			td.t_path <- path
+		);
+	| TAbstractDecl a ->
+		let path = a.a_path in
+		a.a_restore <- (fun () ->
+			a.a_path <- path;
+			a.a_meta <- List.filter (fun (m,_,_) -> m <> Meta.ValueUsed) a.a_meta
+		)
 
 let run com tctx main =
 	let detail_times = Common.defined com DefineList.FilterTimes in

+ 2 - 0
src/typing/typeloadModule.ml

@@ -141,6 +141,7 @@ module ModuleLevel = struct
 					e_meta = d.d_meta;
 					e_params = [];
 					e_using = [];
+					e_restore = (fun () -> ());
 					e_private = priv;
 					e_extern = List.mem EExtern d.d_flags;
 					e_constrs = PMap.empty;
@@ -183,6 +184,7 @@ module ModuleLevel = struct
 					a_doc = d.d_doc;
 					a_params = [];
 					a_using = [];
+					a_restore = (fun () -> ());
 					a_meta = d.d_meta;
 					a_from = [];
 					a_to = [];

+ 11 - 0
tests/server/src/cases/ServerTests.hx

@@ -235,6 +235,17 @@ class ServerTests extends TestCase {
 		utest.Assert.equals("function() {_Vector.Vector_Impl_.toIntVector(null);}", moreHack(type.args.statics[0].expr.testHack)); // lmao
 	}
 
+	function test10986() {
+		vfs.putContent("Main.hx", getTemplate("issues/Issue10986/Main.hx"));
+		vfs.putContent("haxe/ds/Vector.hx", getTemplate("issues/Issue10986/Vector.hx"));
+		var args = ["-main", "Main", "--jvm", "Main.jar"];
+		runHaxe(args);
+		vfs.touchFile("haxe/ds/Vector.hx");
+		runHaxe(args);
+		assertSuccess();
+
+	}
+
 	// See https://github.com/HaxeFoundation/haxe/issues/8368#issuecomment-525379060
 	#if false
 	function testXRedefinedFromX() {

+ 7 - 0
tests/server/test/templates/issues/Issue10986/Main.hx

@@ -0,0 +1,7 @@
+import haxe.ds.Vector;
+
+class Main {
+	static public function main() {
+		final v = new Vector(2, 5);
+	}
+}

+ 29 - 0
tests/server/test/templates/issues/Issue10986/Vector.hx

@@ -0,0 +1,29 @@
+package haxe.ds;
+
+private typedef VectorData<T> = java.NativeArray<T>
+
+abstract Vector<T>(VectorData<T>) {
+	extern overload public inline function new(length:Int) {
+		this = new java.NativeArray(length);
+	}
+
+	extern overload public inline function new(length:Int, defaultValue:T) {
+		this = new java.NativeArray(length);
+		// uncomment and rebuild to break compilation server
+		// server restart will fix errors until the next change in this file
+
+		// trace("uncomment me");
+	}
+
+	public static function blit<T>(src:Vector<T>, srcPos:Int, dest:Vector<T>, destPos:Int, len:Int):Void {}
+
+	static public function fromData<T>(data:VectorData<T>):Vector<T>
+		return cast data;
+
+	public function copy<T>():Vector<T> {
+		return cast this;
+	}
+
+	public function toData():VectorData<T>
+		return cast this;
+}

+ 3 - 0
tests/server/test/templates/issues/Issue10986/build.hxml

@@ -0,0 +1,3 @@
+--main Main
+--jvm Main.jar
+# --cmd java -jar bin/Main.jar