浏览代码

allow derived classes to widen method visibility (fixed issue #792)

Simon Krajewski 13 年之前
父节点
当前提交
5266d6ac4f
共有 3 个文件被更改,包括 15 次插入2 次删除
  1. 8 0
      tests/unit/MyClass.hx
  2. 5 0
      tests/unit/TestType.hx
  3. 2 2
      typeload.ml

+ 8 - 0
tests/unit/MyClass.hx

@@ -24,5 +24,13 @@ class MyClass {
 	public function add(x,y) {
 		return val + x + y;
 	}
+}
 
+class MyParent {
+	public function new() { }
+	function a() return 11
+}
+
+class MyChild extends MyParent {
+	public override function a() { return 12; }
 }

+ 5 - 0
tests/unit/TestType.hx

@@ -74,4 +74,9 @@ class TestType extends Test {
 		eq( Type.allEnums(MyEnum).join("#"), "A#B" );
 	}
 	
+	function testWiderVisibility()
+	{
+		var c = new MyClass.MyChild();
+		eq(12, c.a());
+	}
 }

+ 2 - 2
typeload.ml

@@ -370,8 +370,8 @@ let check_overriding ctx c p () =
 				let p = (match f.cf_expr with None -> p | Some e -> e.epos) in
 				if not (List.mem i c.cl_overrides) then
 					display_error ctx ("Field " ^ i ^ " should be declared with 'override' since it is inherited from superclass") p
-				else if f.cf_public <> f2.cf_public then
-					display_error ctx ("Field " ^ i ^ " has different visibility (public/private) than superclass one") p
+				else if not f.cf_public && f2.cf_public then
+					display_error ctx ("Field " ^ i ^ " has less visibility (public/private) than superclass one") p
 				else (match f.cf_kind, f2.cf_kind with
 				| _, Method MethInline ->
 					display_error ctx ("Field " ^ i ^ " is inlined and cannot be overridden") p