Explorar o código

check final field is not re-assigned in a wrong constructor (fixes #9378)

Aleksandr Kuzmenko %!s(int64=5) %!d(string=hai) anos
pai
achega
bec8e9e16d

+ 4 - 1
src/typing/fields.ml

@@ -282,7 +282,10 @@ let field_access ctx mode f fmode t e p =
 		| AccInline ->
 		| AccInline ->
 			AKInline (e,f,fmode,t)
 			AKInline (e,f,fmode,t)
 		| AccCtor ->
 		| AccCtor ->
-			if ctx.curfun = FunConstructor then normal() else AKNo f.cf_name
+			(match ctx.curfun, fmode with
+				| FunConstructor, FInstance(c,_,_) when c == ctx.curclass -> normal()
+				| _ -> AKNo f.cf_name
+			)
 		| AccRequire (r,msg) ->
 		| AccRequire (r,msg) ->
 			match msg with
 			match msg with
 			| None -> error_require r p
 			| None -> error_require r p

+ 15 - 0
tests/misc/projects/Issue9378/Main.hx

@@ -0,0 +1,15 @@
+class C {
+	public final x:Int;
+	public function new() {
+		x = 10;
+	}
+}
+
+class Main {
+	function new() {
+		new C().x = 10;
+	}
+
+	static function main() {
+	}
+}

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

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

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

@@ -0,0 +1 @@
+Main.hx:10: characters 3-17 : Cannot access field or identifier x for writing