Pārlūkot izejas kodu

[typer] check abstract casts against int on OpInterval

closes #10998
Simon Krajewski 2 gadi atpakaļ
vecāks
revīzija
ee623c3449

+ 2 - 2
src/typing/operators.ml

@@ -393,8 +393,8 @@ let make_binop ctx op e1 e2 is_assign_op with_type p =
 		mk_op e1 e2 b
 	| OpInterval ->
 		let t = Typeload.load_core_type ctx "IntIterator" in
-		unify ctx e1.etype tint e1.epos;
-		unify ctx e2.etype tint e2.epos;
+		let e1 = AbstractCast.cast_or_unify_raise ctx tint e1 e1.epos in
+		let e2 = AbstractCast.cast_or_unify_raise ctx tint e2 e2.epos in
 		BinopSpecial (mk (TNew ((match t with TInst (c,[]) -> c | _ -> die "" __LOC__),[],[e1;e2])) t p,false)
 	| OpArrow ->
 		typing_error "Unexpected =>" p

+ 42 - 0
tests/misc/cpp/projects/Issue10998/Main.hx

@@ -0,0 +1,42 @@
+@:headerClassCode('
+    static constexpr int64_t myInt64 = 10;
+')
+class Test {
+	public function new() {}
+
+	public function getInt64():cpp.Int64
+		return untyped __cpp__('myInt64');
+}
+
+class Main {
+	public static function main() {
+		var t = new Test();
+
+		#if (haxe > "4.2.5") // nightly rc
+
+		// 1.
+		var other:Int = t.getInt64(); //  Warning : (WDeprecated) Implicit cast from Int64 to Int (32 bits) is deprecated. Use .toInt() or explicitly cast instead.
+
+		// 2. iterate implicitly truncated
+		for (i in 0...t.getInt64()) // ERROR: cpp.Int64 should be Int
+			trace(i);
+
+		// 3. iterate explicitly truncated
+		for (i in 0...t.getInt64().toInt()) // OK
+			trace(i);
+
+		// 4. iterate on int64-range
+		var start:cpp.Int64 = 0;
+		for (i in start...t.getInt64()) // ERROR: cpp.Int64 should be Int
+			trace(i);
+		#else // tested with 4.2.5
+
+		// truncate implicitly
+		var other:Int = t.getInt64(); // OK
+
+		// iterate implicitly truncated
+		for (i in 0...t.getInt64()) // OK
+			trace(i);
+		#end
+	}
+}

+ 3 - 0
tests/misc/cpp/projects/Issue10998/compile.hxml

@@ -0,0 +1,3 @@
+--main Main
+--cpp bin
+-D no-compilation

+ 4 - 0
tests/misc/cpp/projects/Issue10998/compile.hxml.stderr

@@ -0,0 +1,4 @@
+Main.hx:18: characters 3-32 : Warning : (WDeprecated) Implicit cast from Int64 to Int (32 bits) is deprecated. Use .toInt() or explicitly cast instead.
+Main.hx:21: characters 17-29 : Warning : (WDeprecated) Implicit cast from Int64 to Int (32 bits) is deprecated. Use .toInt() or explicitly cast instead.
+Main.hx:30: characters 13-18 : Warning : (WDeprecated) Implicit cast from Int64 to Int (32 bits) is deprecated. Use .toInt() or explicitly cast instead.
+Main.hx:30: characters 21-33 : Warning : (WDeprecated) Implicit cast from Int64 to Int (32 bits) is deprecated. Use .toInt() or explicitly cast instead.