Browse Source

[typer] use lhs as expected type for rhs when typing null coal

closes #10684
Simon Krajewski 3 years ago
parent
commit
05219b8023
2 changed files with 12 additions and 1 deletions
  1. 1 1
      src/typing/typer.ml
  2. 11 0
      tests/unit/src/unit/issues/Issue10684.hx

+ 1 - 1
src/typing/typer.ml

@@ -1774,7 +1774,7 @@ and type_expr ?(mode=MGet) ctx (e,p) (with_type:WithType.t) =
 	| EBinop (OpNullCoal,e1,e2) ->
 	| EBinop (OpNullCoal,e1,e2) ->
 		let vr = new value_reference ctx in
 		let vr = new value_reference ctx in
 		let e1 = type_expr ctx (Expr.ensure_block e1) with_type in
 		let e1 = type_expr ctx (Expr.ensure_block e1) with_type in
-		let e2 = type_expr ctx (Expr.ensure_block e2) with_type in
+		let e2 = type_expr ctx (Expr.ensure_block e2) (WithType.with_type e1.etype) in
 		let e1 = vr#as_var "tmp" {e1 with etype = ctx.t.tnull e1.etype} in
 		let e1 = vr#as_var "tmp" {e1 with etype = ctx.t.tnull e1.etype} in
 		let e_null = Builder.make_null e1.etype e1.epos in
 		let e_null = Builder.make_null e1.etype e1.epos in
 		let e_cond = mk (TBinop(OpNotEq,e1,e_null)) ctx.t.tbool e1.epos in
 		let e_cond = mk (TBinop(OpNotEq,e1,e_null)) ctx.t.tbool e1.epos in

+ 11 - 0
tests/unit/src/unit/issues/Issue10684.hx

@@ -0,0 +1,11 @@
+package unit.issues;
+
+class Issue10684 extends unit.Test {
+	function test() {
+		eq("1", makeMapF()([1 => "1", 2 => "2"]));
+	}
+
+	function makeMapF(?f:Map<Int, String>->String) {
+		return f ?? v -> v[1];
+	}
+}