瀏覽代碼

[typer] use top-down inference for comparison operators

closes #7394
Simon Krajewski 7 年之前
父節點
當前提交
27a7a4927b
共有 2 個文件被更改,包括 23 次插入1 次删除
  1. 5 1
      src/typing/typer.ml
  2. 18 0
      tests/unit/src/unit/issues/Issue7394.hx

+ 5 - 1
src/typing/typer.ml

@@ -687,7 +687,11 @@ let rec type_binop ctx op e1 e2 is_assign_op with_type p =
 		type_binop2 ctx op e1 e2 is_assign_op wt p
 
 and type_binop2 ctx op (e1 : texpr) (e2 : Ast.expr) is_assign_op wt p =
-	let e2 = type_expr ctx e2 (if op == OpEq || op == OpNotEq then WithType e1.etype else wt) in
+	let with_type = match op with
+		| OpEq | OpNotEq | OpLt | OpLte | OpGt | OpGte -> WithType e1.etype
+		| _ -> wt
+	in
+	let e2 = type_expr ctx e2 with_type in
 	let tint = ctx.t.tint in
 	let tfloat = ctx.t.tfloat in
 	let tstring = ctx.t.tstring in

+ 18 - 0
tests/unit/src/unit/issues/Issue7394.hx

@@ -0,0 +1,18 @@
+package unit.issues;
+
+private typedef Foo = {
+	final i:Int;
+}
+
+@:forward
+private abstract Bar(Foo) from Foo {
+	@:op(A > B) function compare(other:Bar)
+		return this.i > other.i;
+}
+
+class Issue7394 extends unit.Test {
+	function test() {
+		var b:Bar = {i: 0};
+		f(b > {i: 1});
+	}
+}