Browse Source

[dce] keep property accessors through structures

closes #7259
Simon Krajewski 7 years ago
parent
commit
3da30c3686
2 changed files with 44 additions and 1 deletions
  1. 16 1
      src/core/type.ml
  2. 28 0
      tests/unit/src/unit/TestDCE.hx

+ 16 - 1
src/core/type.ml

@@ -2063,7 +2063,22 @@ let rec unify a b =
 					then error [Missing_overload (f1, f2o.cf_type)]
 				) f2.cf_overloads;
 				(* we mark the field as :?used because it might be used through the structure *)
-				if not (Meta.has Meta.MaybeUsed f1.cf_meta) then f1.cf_meta <- (Meta.MaybeUsed,[],f1.cf_pos) :: f1.cf_meta;
+				if not (Meta.has Meta.MaybeUsed f1.cf_meta) then begin
+					f1.cf_meta <- (Meta.MaybeUsed,[],f1.cf_pos) :: f1.cf_meta;
+					match f2.cf_kind with
+					| Var vk ->
+						let check name =
+							try
+								let _,_,cf = raw_class_field make_type c tl name in
+								if not (Meta.has Meta.MaybeUsed cf.cf_meta) then
+									cf.cf_meta <- (Meta.MaybeUsed,[],f1.cf_pos) :: cf.cf_meta
+							with Not_found ->
+								()
+						in
+						(match vk.v_read with AccCall -> check ("get_" ^ f1.cf_name) | _ -> ());
+						(match vk.v_write with AccCall -> check ("set_" ^ f1.cf_name) | _ -> ());
+					| _ -> ()
+				end;
 				(match f1.cf_kind with
 				| Method MethInline ->
 					if (c.cl_extern || f1.cf_extern) && not (Meta.has Meta.Runtime f1.cf_meta) then error [Has_no_runtime_field (a,n)];

+ 28 - 0
tests/unit/src/unit/TestDCE.hx

@@ -1,5 +1,26 @@
 package unit;
 
+private typedef Foo = {
+	var bar(get, null): Bar;
+}
+
+private typedef Bar = {
+	var data: Int;
+}
+
+private class AdrianV {
+	public var bar(get, null): Bar = {data: 100};
+	function get_bar() {
+		return bar;
+	}
+
+	public function new() {}
+
+	static public function testFoo(foo: Foo) {
+		return foo.bar.data;
+	}
+}
+
 @:analyzer(no_local_dce)
 class DCEClass {
 	// used statics
@@ -162,6 +183,13 @@ class TestDCE extends Test {
 		#end
 		hf(ThrownWithToString, "toString");
 	}
+
+	public function testIssue7259() {
+		var me = new AdrianV();
+		AdrianV.testFoo(me);
+		var c = Type.getClass(me);
+		hf(c, "get_bar");
+	}
 }
 
 class UsedConstructed {