Bladeren bron

turn Int initialization constants to Float constants if expected type is Float (closes #3039)

Simon Krajewski 11 jaren geleden
bovenliggende
commit
abdc39debb
2 gewijzigde bestanden met toevoegingen van 18 en 2 verwijderingen
  1. 11 0
      tests/unit/issues/Issue3039.hx
  2. 7 2
      typeload.ml

+ 11 - 0
tests/unit/issues/Issue3039.hx

@@ -0,0 +1,11 @@
+package unit.issues;
+
+class Issue3039 extends Test {
+	static inline var a:Int = 1;
+	static inline var b:Float = 2;
+	static inline var c = Std.int(a + b);
+
+	function test() {
+		eq(3, c);
+	}
+}

+ 7 - 2
typeload.ml

@@ -1715,8 +1715,13 @@ let init_class ctx c p context_init herits fields =
 			let check_cast e =
 				(* insert cast to keep explicit field type (issue #1901) *)
 				if not (type_iseq e.etype cf.cf_type)
-				then mk (TCast(e,None)) cf.cf_type e.epos
-				else e
+				then begin match e.eexpr,follow cf.cf_type with
+					| TConst (TInt i),TAbstract({a_path=[],"Float"},_) ->
+						(* turn int constant to float constant if expected type is float *)
+						{e with eexpr = TConst (TFloat (Int32.to_string i))}
+					| _ ->
+						mk (TCast(e,None)) cf.cf_type e.epos
+				end else e
 			in
 			let r = exc_protect ctx (fun r ->
 				(* type constant init fields (issue #1956) *)