Browse Source

[filters] set up ctx.m for filters

closes #10646
Simon Krajewski 3 years ago
parent
commit
64452a2c2a

+ 1 - 0
src/filters/filtersCommon.ml

@@ -66,6 +66,7 @@ let run_expression_filters ?(ignore_processed_status=false) time_details ctx fil
 	| TClassDecl c when is_removable_class c -> ()
 	| TClassDecl c ->
 		ctx.curclass <- c;
+		ctx.m <- TypeloadModule.make_curmod ctx c.cl_module;
 		let rec process_field f =
 			if ignore_processed_status || not (has_class_field_flag f CfPostProcessed) then begin
 				ctx.curfield <- f;

+ 21 - 15
src/typing/typeloadModule.ml

@@ -698,18 +698,20 @@ module TypeLevel = struct
 			()
 end
 
+let make_curmod ctx m = {
+	curmod = m;
+	module_imports = List.map (fun t -> t,null_pos) ctx.g.std.m_types;
+	module_using = [];
+	module_globals = PMap.empty;
+	wildcard_packages = [];
+	import_statements = [];
+}
+
 let create_typer_context_for_module ctx m = {
 		com = ctx.com;
 		g = ctx.g;
 		t = ctx.com.basic;
-		m = {
-			curmod = m;
-			module_imports = List.map (fun t -> t,null_pos) ctx.g.std.m_types;
-			module_using = [];
-			module_globals = PMap.empty;
-			wildcard_packages = [];
-			import_statements = [];
-		};
+		m = make_curmod ctx m;
 		is_display_file = (ctx.com.display.dms_kind <> DMNone && DisplayPosition.display_position#is_in_file (Path.UniqueKey.lazy_key m.m_extra.m_file));
 		bypass_accessor = 0;
 		meta = [];
@@ -785,16 +787,20 @@ let type_module_hook = ref (fun _ _ _ -> None)
 
 let load_module' ctx g m p =
 	try
+		(* Check current context *)
 		ctx.com.module_lut#find m
-	with
-		Not_found ->
-			match !type_module_hook ctx m p with
-			| Some m -> m
-			| None ->
+	with Not_found ->
+		(* Check cache *)
+		match !type_module_hook ctx m p with
+		| Some m ->
+			m
+		| None ->
 			let is_extern = ref false in
-			let file, decls = (try
+			let file, decls = try
+				(* Try parsing *)
 				TypeloadParse.parse_module ctx m p
 			with Not_found ->
+				(* Nothing to parse, try loading extern type *)
 				let rec loop = function
 					| [] ->
 						raise (Error (Module_not_found m,p))
@@ -805,7 +811,7 @@ let load_module' ctx g m p =
 				in
 				is_extern := true;
 				loop ctx.com.load_extern_type
-			) in
+			in
 			let is_extern = !is_extern in
 			try
 				type_module ctx m file ~is_extern decls p

+ 1 - 1
src/typing/typer.ml

@@ -1220,7 +1220,7 @@ and type_local_function ctx kind f with_type p =
 				begin match follow t with
 				| TFun(args,ret) when List.length args = arity ->
 					List.iteri (fun i (_,_,t) ->
-						(* We don't wand to bind monomorphs because we want the widest type *)
+						(* We don't want to bind monomorphs because we want the widest type *)
 						let t = dynamify_monos t in
 						m#join t (i + 1);
 					) args;

+ 1 - 1
tests/server/.vscode/settings.json

@@ -1,6 +1,6 @@
 {
 	"haxe.diagnosticsPathFilter": "${workspaceRoot}/src",
-	// "editor.formatOnSave": true,
+	"editor.formatOnSave": true,
 	"haxeTestExplorer.testCommand": [
 		"${haxe}",
 		"build.hxml",

+ 23 - 5
tests/server/src/TestCase.hx

@@ -18,7 +18,12 @@ using Lambda;
 
 @:autoBuild(utils.macro.BuildHub.build())
 class TestCase implements ITest {
-	static public var debugLastResult:{hasError:Bool, stdout:String, stderr:String, prints:Array<String>};
+	static public var debugLastResult:{
+		hasError:Bool,
+		stdout:String,
+		stderr:String,
+		prints:Array<String>
+	};
 
 	var server:HaxeServerAsync;
 	var vfs:Vfs;
@@ -51,7 +56,7 @@ class TestCase implements ITest {
 		server.stop();
 	}
 
-	function runHaxe(args:Array<String>, done:()->Void) {
+	function runHaxe(args:Array<String>, done:() -> Void) {
 		messages = [];
 		errorMessages = [];
 		server.rawRequest(args, null, function(result) {
@@ -74,12 +79,25 @@ class TestCase implements ITest {
 		}, sendErrorMessage);
 	}
 
-	function runHaxeJson<TParams, TResponse>(args:Array<String>, method:HaxeRequestMethod<TParams, TResponse>, methodArgs:TParams, done:()->Void) {
+	function runHaxeJson<TParams, TResponse>(args:Array<String>, method:HaxeRequestMethod<TParams, TResponse>, methodArgs:TParams, done:() -> Void) {
 		var methodArgs = {method: method, id: 1, params: methodArgs};
 		args = args.concat(['--display', Json.stringify(methodArgs)]);
 		runHaxe(args, done);
 	}
 
+	function runHaxeJsonCb<TParams, TResponse>(args:Array<String>, method:HaxeRequestMethod<TParams, Response<TResponse>>, methodArgs:TParams,
+			callback:TResponse->Void, done:() -> Void) {
+		var methodArgs = {method: method, id: 1, params: methodArgs};
+		args = args.concat(['--display', Json.stringify(methodArgs)]);
+		server.rawRequest(args, null, function(result) {
+			callback(Json.parse(result.stderr).result.result);
+			done();
+		}, function(msg) {
+			sendErrorMessage(msg);
+			done();
+		});
+	}
+
 	function sendErrorMessage(msg:String) {
 		var split = msg.split("\n");
 		for (message in split) {
@@ -204,10 +222,10 @@ class TestCase implements ITest {
 		}
 	}
 
-	function assertClassField(completion:CompletionResult, name:String, ?callback:(field:JsonClassField)->Void, ?pos:PosInfos) {
+	function assertClassField(completion:CompletionResult, name:String, ?callback:(field:JsonClassField) -> Void, ?pos:PosInfos) {
 		for (item in completion.result.items) {
 			switch item.kind {
-				case ClassField if(item.args.field.name == name):
+				case ClassField if (item.args.field.name == name):
 					switch callback {
 						case null: Assert.pass(pos);
 						case fn: fn(item.args.field);

+ 21 - 0
tests/server/src/cases/issues/Issue10646.hx

@@ -0,0 +1,21 @@
+package cases.issues;
+
+class Issue10646 extends TestCase {
+	function test(_) {
+		vfs.putContent("HelloWorld.hx", getTemplate("HelloWorld.hx"));
+		var args = ["-main", "HelloWorld", "--neko", "test.n"];
+		runHaxe(args);
+		var nekoCtx = null;
+		runHaxeJsonCb([], ServerMethods.Contexts, null, function(ctxs) {
+			for (ctx in ctxs) {
+				if (ctx.desc == "after_init_macros") {
+					nekoCtx = ctx;
+				}
+			}
+		});
+		Assert.notNull(nekoCtx);
+		runHaxeJsonCb([], ServerMethods.ContextMemory, {signature: nekoCtx.signature}, function(mem) {
+			Assert.isNull(mem.leaks);
+		});
+	}
+}

+ 13 - 8
tests/server/src/utils/macro/TestBuilder.macro.hx

@@ -22,7 +22,7 @@ class TestBuilder {
 							});
 							name;
 						case [arg]:
-							if(arg.name == "_") {
+							if (arg.name == "_") {
 								arg.name = "async";
 								arg.type = macro:utest.Async;
 							}
@@ -63,6 +63,10 @@ class TestBuilder {
 					var e = transformHaxeCalls(asyncName, el);
 					args.push(macro() -> ${failOnException(asyncName, e)});
 					macro runHaxeJson($a{args});
+				case macro runHaxeJsonCb($a{args}):
+					var e = transformHaxeCalls(asyncName, el);
+					args.push(macro() -> ${failOnException(asyncName, e)});
+					macro runHaxeJsonCb($a{args});
 				case macro complete($a{args}):
 					var e = transformHaxeCalls(asyncName, el);
 					args.push(macro function(response, markers) ${failOnException(asyncName, e)});
@@ -76,12 +80,13 @@ class TestBuilder {
 	}
 
 	static function failOnException(asyncName:String, e:Expr):Expr {
-		return macro @:pos(e.pos) try {
-			$e;
-		} catch(e) {
-			Assert.fail(e.details());
-			$i{asyncName}.done();
-			return;
-		}
+		return macro
+			@:pos(e.pos) try {
+				$e;
+			} catch (e) {
+				Assert.fail(e.details());
+				$i{asyncName}.done();
+				return;
+			}
 	}
 }