Bladeren bron

[display] transform fields in displayTexpr

closes #9115
Simon Krajewski 5 jaren geleden
bovenliggende
commit
e39bb29bea

+ 1 - 0
src/context/display/displayTexpr.ml

@@ -54,6 +54,7 @@ let check_display_field ctx sc c cf =
 	let cff = find_field_by_position sc cf.cf_name_pos in
 	let context_init = new TypeloadFields.context_init in
 	let ctx,cctx = TypeloadFields.create_class_context ctx c context_init cf.cf_pos in
+	let cff = TypeloadFields.transform_field (ctx,cctx) c cff (ref []) (pos cff.cff_name) in
 	let ctx,fctx = TypeloadFields.create_field_context (ctx,cctx) c cff in
 	let cf = TypeloadFields.init_field (ctx,cctx,fctx) cff in
 	flush_pass ctx PTypeField "check_display_field";

+ 19 - 18
src/typing/typeloadFields.ml

@@ -630,6 +630,24 @@ let type_opt (ctx,cctx) p t =
 	| _ ->
 		load_type_hint ctx p t
 
+let transform_field (ctx,cctx) c f fields p =
+	let f = match cctx.abstract with
+		| Some a ->
+			let a_t = TExprToExpr.convert_type' (TAbstract(a,List.map snd a.a_params)) in
+			let this_t = TExprToExpr.convert_type' a.a_this in (* TODO: better pos? *)
+			transform_abstract_field ctx.com this_t a_t a f
+		| None ->
+			f
+	in
+	if List.mem_assoc AMacro f.cff_access then
+		(match ctx.g.macros with
+		| Some (_,mctx) when Hashtbl.mem mctx.g.types_module c.cl_path ->
+			(* assume that if we had already a macro with the same name, it has not been changed during the @:build operation *)
+			if not (List.exists (fun f2 -> f2.cff_name = f.cff_name && List.mem_assoc AMacro f2.cff_access) (!fields)) then
+				error "Class build macro cannot return a macro function when the class has already been compiled into the macro context" p
+		| _ -> ());
+	f
+
 let build_fields (ctx,cctx) c fields =
 	let fields = ref fields in
 	let get_fields() = !fields in
@@ -638,24 +656,7 @@ let build_fields (ctx,cctx) c fields =
 	build_module_def ctx (TClassDecl c) c.cl_meta get_fields cctx.context_init (fun (e,p) ->
 		match e with
 		| EVars [_,_,Some (CTAnonymous f,p),None] ->
-			let f = List.map (fun f ->
-				let f = match cctx.abstract with
-					| Some a ->
-						let a_t = TExprToExpr.convert_type' (TAbstract(a,List.map snd a.a_params)) in
-						let this_t = TExprToExpr.convert_type' a.a_this in (* TODO: better pos? *)
-						transform_abstract_field ctx.com this_t a_t a f
-					| None ->
-						f
-				in
-				if List.mem_assoc AMacro f.cff_access then
-					(match ctx.g.macros with
-					| Some (_,mctx) when Hashtbl.mem mctx.g.types_module c.cl_path ->
-						(* assume that if we had already a macro with the same name, it has not been changed during the @:build operation *)
-						if not (List.exists (fun f2 -> f2.cff_name = f.cff_name && List.mem_assoc AMacro f2.cff_access) (!fields)) then
-							error "Class build macro cannot return a macro function when the class has already been compiled into the macro context" p
-					| _ -> ());
-				f
-			) f in
+			let f = List.map (fun f -> transform_field (ctx,cctx) c f fields p) f in
 			fields := f
 		| _ -> error "Class build macro must return a single variable with anonymous fields" p
 	);

+ 16 - 2
tests/server/src/DisplayTests.hx

@@ -1,3 +1,4 @@
+import haxe.display.JsonModuleTypes;
 import haxe.display.Protocol;
 import haxe.PosInfos;
 import haxe.display.Server;
@@ -251,10 +252,10 @@ typedef Foo = {
 		var args = ["-main", "Main"];
 		runHaxeJson(args, DisplayMethods.GotoDefinition, {file: new FsPath("Main.hx"), offset: 56});
 		var result = parseGotoDefinition();
-		if(result.result.length == 0) {
+		if (result.result.length == 0) {
 			Assert.fail('display/definition failed');
 		} else {
-			Assert.same({"start":{"line":7, "character":12}, "end":{"line":7, "character":15}}, result.result[0].range);
+			Assert.same({"start": {"line": 7, "character": 12}, "end": {"line": 7, "character": 15}}, result.result[0].range);
 		}
 	}
 
@@ -394,4 +395,17 @@ typedef Foo = {
 		// 	}
 		// ], result);
 	}
+
+	function testIssue9115() {
+		var content = getTemplate("issues/Issue9115/A.hx");
+		var transform = Marker.extractMarkers(content);
+		vfs.putContent("A.hx", transform.source);
+		runHaxe(["--no-output", "A"]);
+		runHaxeJson([], DisplayMethods.Hover, {
+			file: new FsPath("A.hx"),
+			offset: transform.markers[1]
+		});
+		var result = parseHover();
+		Assert.equals("A", result.result.item.type.args.path.typeName /* lol */);
+	}
 }

+ 11 - 0
tests/server/test/templates/issues/Issue9115/A.hx

@@ -0,0 +1,11 @@
+enum abstract A(String) {
+	var A1;
+	var A2;
+
+	function f() {
+		var {-1-}a:A = cast this; // hovering `a` will print Dynamic
+		switch (a) {
+			// no exhaustiveness check
+		}
+	}
+}