Răsfoiți Sursa

[typer] guard against null for Std.string on abstracts

closes #10709
Simon Krajewski 3 ani în urmă
părinte
comite
7dc28d74e3
2 a modificat fișierele cu 19 adăugiri și 2 ștergeri
  1. 8 2
      src/context/abstractCast.ml
  2. 11 0
      tests/unit/src/unit/issues/Issue10709.hx

+ 8 - 2
src/context/abstractCast.ml

@@ -230,10 +230,16 @@ let handle_abstract_casts ctx e =
 				| TAbstract({a_impl = Some c} as a,tl) ->
 					begin try
 						let cf = PMap.find "toString" c.cl_statics in
+						let call() = make_static_call ctx c cf a tl [e1] ctx.t.tstring e.epos in
 						if not ctx.allow_transform then
 							{ e1 with etype = ctx.t.tstring; epos = e.epos }
-						else
-							make_static_call ctx c cf a tl [e1] ctx.t.tstring e.epos
+						else if not (is_nullable e1.etype) then
+							call()
+						else begin
+							let p = e.epos in
+							let chk_null = mk (TBinop (Ast.OpEq, e1, mk (TConst TNull) e1.etype p)) ctx.com.basic.tbool p in
+							mk (TIf (chk_null, mk (TConst (TString "null")) ctx.com.basic.tstring p, Some (call()))) ctx.com.basic.tstring p
+						end
 					with Not_found ->
 						e
 					end

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

@@ -0,0 +1,11 @@
+package unit.issues;
+
+class Issue10709 extends unit.Test {
+	function test() {
+		eq("null", Std.string(foo()));
+	}
+
+	static function foo():Null<Map<String, String>> {
+		return null;
+	}
+}