Browse Source

Add CfNoLookup (#11825)

* add CfNoLookup

* invalidate
Simon Krajewski 8 months ago
parent
commit
004d7dffa9

+ 2 - 1
src/core/tType.ml

@@ -433,10 +433,11 @@ type flag_tclass_field =
 	| CfGeneric
 	| CfGeneric
 	| CfDefault (* Interface field with default implementation (only valid on Java) *)
 	| CfDefault (* Interface field with default implementation (only valid on Java) *)
 	| CfPostProcessed (* Marker to indicate the field has been post-processed *)
 	| CfPostProcessed (* Marker to indicate the field has been post-processed *)
+	| CfNoLookup (* Field cannot be accessed by-name. *)
 
 
 (* Order has to match declaration for printing*)
 (* Order has to match declaration for printing*)
 let flag_tclass_field_names = [
 let flag_tclass_field_names = [
-	"CfPublic";"CfStatic";"CfExtern";"CfFinal";"CfModifiesThis";"CfOverride";"CfAbstract";"CfOverload";"CfImpl";"CfEnum";"CfGeneric";"CfDefault"
+	"CfPublic";"CfStatic";"CfExtern";"CfFinal";"CfModifiesThis";"CfOverride";"CfAbstract";"CfOverload";"CfImpl";"CfEnum";"CfGeneric";"CfDefault";"CfNoLookup"
 ]
 ]
 
 
 type flag_tvar =
 type flag_tvar =

+ 2 - 1
src/filters/filters.ml

@@ -75,7 +75,8 @@ module LocalStatic = struct
 			typing_error ~depth:1 "Conflicting field was found here" cf.cf_name_pos;
 			typing_error ~depth:1 "Conflicting field was found here" cf.cf_name_pos;
 		with Not_found ->
 		with Not_found ->
 			let cf = mk_field name ~static:true v.v_type v.v_pos v.v_pos in
 			let cf = mk_field name ~static:true v.v_type v.v_pos v.v_pos in
-			cf.cf_meta <- v.v_meta;
+			cf.cf_meta <- (Meta.NoCompletion,[],Globals.null_pos) :: v.v_meta;
+			add_class_field_flag cf CfNoLookup;
 			begin match eo with
 			begin match eo with
 			| None ->
 			| None ->
 				()
 				()

+ 5 - 0
src/typing/fields.ml

@@ -316,6 +316,9 @@ let type_field cfg ctx e i p mode (with_type : WithType.t) =
 			acc
 			acc
 		) c.cl_implements
 		) c.cl_implements
 	in
 	in
+	let no_no_lookup cf =
+		if has_class_field_flag cf CfNoLookup then display_error ctx.com "This field cannot be accessed explicitly" pfield
+	in
 	let rec type_field_by_type e t =
 	let rec type_field_by_type e t =
 		let field_access = field_access e in
 		let field_access = field_access e in
 		match t with
 		match t with
@@ -345,6 +348,7 @@ let type_field cfg ctx e i p mode (with_type : WithType.t) =
 					let t = enum_field_type ctx en c p in
 					let t = enum_field_type ctx en c p in
 					AKExpr (mk (TField (e,fmode)) t p)
 					AKExpr (mk (TField (e,fmode)) t p)
 				| Statics c ->
 				| Statics c ->
+					no_no_lookup f;
 					field_access f (FHStatic c)
 					field_access f (FHStatic c)
 				| _ ->
 				| _ ->
 					field_access f FHAnon
 					field_access f FHAnon
@@ -403,6 +407,7 @@ let type_field cfg ctx e i p mode (with_type : WithType.t) =
 				let c = find_some a.a_impl in
 				let c = find_some a.a_impl in
 				let f = PMap.find i c.cl_statics in
 				let f = PMap.find i c.cl_statics in
 				if not (has_class_field_flag f CfImpl) then raise Not_found;
 				if not (has_class_field_flag f CfImpl) then raise Not_found;
+				no_no_lookup f;
 				field_access f (FHAbstract (a,tl,c))
 				field_access f (FHAbstract (a,tl,c))
 			with Not_found ->
 			with Not_found ->
 				type_field_by_forward_member type_field_by_type e a tl
 				type_field_by_forward_member type_field_by_type e a tl

+ 24 - 0
tests/server/src/cases/display/issues/Issue11817.hx

@@ -0,0 +1,24 @@
+package cases.display.issues;
+
+class Issue11817 extends DisplayTestCase {
+	function test(_) {
+		vfs.putContent("Main.hx", getTemplate("issues/Issue11817/MainBefore.hx"));
+		vfs.putContent("Utils.hx", getTemplate("issues/Issue11817/Utils.hx"));
+		runHaxe(["--main", "Main"]);
+		var mainHx = Marker.extractMarkers(getTemplate("issues/Issue11817/MainAfter.hx"));
+		vfs.putContent("Main.hx", mainHx.source);
+		runHaxeJson([], ServerMethods.Invalidate, {file: file});
+		runHaxeJson([], DisplayMethods.Completion, {
+			file: file,
+			offset: mainHx.markers[1],
+			wasAutoTriggered: true
+		});
+		var result = parseCompletion().result;
+		Assert.equals(1, result.items.length);
+		Assert.equals('foo', result.items[0].args.field.name);
+
+		vfs.putContent("Main.hx", getTemplate("issues/Issue11817/MainError.hx"));
+		runHaxe(["--main", "Main"]);
+		assertErrorMessage("This field cannot be accessed explicitly");
+	}
+}

+ 6 - 0
tests/server/test/templates/issues/Issue11817/MainAfter.hx

@@ -0,0 +1,6 @@
+class Main {
+	static function main() {
+		Utils.foo;
+		Utils.{-1-}
+	}
+}

+ 5 - 0
tests/server/test/templates/issues/Issue11817/MainBefore.hx

@@ -0,0 +1,5 @@
+class Main {
+	static function main() {
+		Utils.foo;
+	}
+}

+ 6 - 0
tests/server/test/templates/issues/Issue11817/MainError.hx

@@ -0,0 +1,6 @@
+class Main {
+	static function main() {
+		Utils.foo;
+		Utils.foo_leak;
+	}
+}

+ 6 - 0
tests/server/test/templates/issues/Issue11817/Utils.hx

@@ -0,0 +1,6 @@
+class Utils {
+	public static function foo():Int {
+		static var leak = 0;
+		return leak;
+	}
+}