Pārlūkot izejas kodu

enforce the spec of 'override'ing 'abstract function's (closes #10024)

Aleksandr Kuzmenko 4 gadi atpakaļ
vecāks
revīzija
287a8d7a24

+ 7 - 1
src/typing/typeloadCheck.ml

@@ -180,10 +180,16 @@ let check_overriding ctx c f =
 			(match f2.cf_kind with
 			| Var { v_read = AccRequire _ } -> raise Not_found;
 			| _ -> ());
+			if has_class_field_flag f2 CfAbstract then begin
+				if has_class_field_flag f CfOverride
+					then display_error ctx ("Field " ^ i ^ " is declared 'override' but parent field " ^ i ^ " is 'abstract' and does not provide any implementation to override") p
+				else
+					add_class_field_flag f CfOverride (* our spec requires users to not "override" abstract functions, but our implementation depends on implementations to be declared with "override" ¯\_(ツ)_/¯ *)
+			end;
 			if (has_class_field_flag f2 CfOverload && not (has_class_field_flag f CfOverload)) then
 				display_error ctx ("Field " ^ i ^ " should be declared with overload since it was already declared as overload in superclass") p
 			else if not (has_class_field_flag f CfOverride) then begin
-				if has_class_flag c CExtern || has_class_field_flag f2 CfAbstract then add_class_field_flag f CfOverride
+				if has_class_flag c CExtern then add_class_field_flag f CfOverride
 				else display_error ctx ("Field " ^ i ^ " should be declared with 'override' since it is inherited from superclass " ^ s_type_path csup.cl_path) p
 			end else if not (has_class_field_flag f CfPublic) && (has_class_field_flag f2 CfPublic) then
 				display_error ctx ("Field " ^ i ^ " has less visibility (public/private) than superclass one") p

+ 21 - 0
tests/misc/projects/Issue10024/Main.hx

@@ -0,0 +1,21 @@
+class Main {
+	static function main() {
+		(null:B).f();
+	}
+}
+
+abstract class A {
+	abstract public function f():Void;
+}
+
+class B extends A {
+	function f() {}
+}
+
+class C extends A {
+	override function f() {}
+}
+
+class E extends A {
+	private function f() {}
+}

+ 1 - 0
tests/misc/projects/Issue10024/compile-fail.hxml

@@ -0,0 +1 @@
+--main Main

+ 2 - 0
tests/misc/projects/Issue10024/compile-fail.hxml.stderr

@@ -0,0 +1,2 @@
+Main.hx:16: characters 20-21 : Field f is declared 'override' but parent field f is 'abstract' and does not provide any implementation to override
+Main.hx:20: characters 19-20 : Field f has less visibility (public/private) than superclass one