Browse Source

fixed default values for @:structInit ctor (#9177, closes #9258)

Aleksandr Kuzmenko 5 years ago
parent
commit
42fe9a72b8
2 changed files with 5 additions and 2 deletions
  1. 1 1
      src/typing/typer.ml
  2. 4 1
      tests/unit/src/unit/issues/Issue9177.hx

+ 1 - 1
src/typing/typer.ml

@@ -1666,7 +1666,7 @@ and type_object_decl ctx fl with_type p =
 				try
 				try
 					match ctor.cf_expr with
 					match ctor.cf_expr with
 					| Some { eexpr = TFunction fn } ->
 					| Some { eexpr = TFunction fn } ->
-						Option.get (snd (List.find (fun (v,e) -> Option.is_some e) fn.tf_args))
+						Option.get (snd (List.find (fun (v,e) -> n = v.v_name && Option.is_some e) fn.tf_args))
 					| _ ->
 					| _ ->
 						raise Not_found
 						raise Not_found
 				with Not_found | Option.No_value ->
 				with Not_found | Option.No_value ->

+ 4 - 1
tests/unit/src/unit/issues/Issue9177.hx

@@ -5,13 +5,16 @@ import unit.Test;
 class Issue9177 extends Test {
 class Issue9177 extends Test {
 	public function test() {
 	public function test() {
 		eq(123, ({}:C).x);
 		eq(123, ({}:C).x);
+		eq("hi", ({}:C).y);
 	}
 	}
 }
 }
 
 
 @:structInit
 @:structInit
 private class C {
 private class C {
 	public var x:Int;
 	public var x:Int;
-	public function new(x:Int = 123) {
+	public var y:String;
+	public function new(x:Int = 123, y:String = "hi") {
 		this.x = x;
 		this.x = x;
+		this.y = y;
 	}
 	}
 }
 }