Ver Fonte

[typer] create `@:structInit` ctor before checking for uninitialized finals

closes #7182
Simon Krajewski há 6 anos atrás
pai
commit
b944685827
2 ficheiros alterados com 21 adições e 3 exclusões
  1. 4 3
      src/typing/typeloadFields.ml
  2. 17 0
      tests/unit/src/unit/issues/Issue7182.hx

+ 4 - 3
src/typing/typeloadFields.ml

@@ -1421,6 +1421,9 @@ let init_class ctx c p context_init herits fields =
 	(*
 		make sure a default contructor with same access as super one will be added to the class structure at some point.
 	*)
+	let has_struct_init = Meta.has Meta.StructInit c.cl_meta in
+	if has_struct_init then
+		ensure_struct_init_constructor ctx c fields p;
 	begin match cctx.uninitialized_final with
 		| Some pf when c.cl_constructor = None ->
 			display_error ctx "This class has uninitialized final vars, which requires a constructor" p;
@@ -1428,9 +1431,7 @@ let init_class ctx c p context_init herits fields =
 		| _ ->
 			()
 	end;
-	if Meta.has Meta.StructInit c.cl_meta then
-		ensure_struct_init_constructor ctx c fields p
-	else
+	if not has_struct_init then
 		(* add_constructor does not deal with overloads correctly *)
 		if not ctx.com.config.pf_overload then TypeloadFunction.add_constructor ctx c cctx.force_constructor p;
 	(* check overloaded constructors *)

+ 17 - 0
tests/unit/src/unit/issues/Issue7182.hx

@@ -0,0 +1,17 @@
+package unit.issues;
+
+@:structInit
+private class Person {
+    public final name:String;
+	public final hobbies = "compiler errors";
+}
+
+class Issue7182 extends unit.Test {
+	function test() {
+		var person:Person = {
+			name: "nadako"
+		}
+		eq("nadako", person.name);
+		eq("compiler errors", person.hobbies);
+	}
+}