Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run toString(null) for explicit nullable abstract #12019

Open
wants to merge 2 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/typing/calls.ml
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ let rec needs_temp_var e =
| TField (e, _) | TParenthesis e -> needs_temp_var e
| _ -> true

let call_to_string ctx ?(resume=false) e =
let call_to_string ctx ?(resume=false) ?(no_null_check=false) e =
if not ctx.allow_transform then
{ e with etype = ctx.t.tstring }
else
Expand All @@ -333,7 +333,7 @@ let call_to_string ctx ?(resume=false) e =
ctx.f.meta <- List.tl ctx.f.meta;
build_call ctx acc [] (WithType.with_type ctx.t.tstring) e.epos
in
if ctx.com.config.pf_static && not (is_nullable e.etype) then
if no_null_check || (ctx.com.config.pf_static && not (is_nullable e.etype)) then
gen_to_string e
else begin (* generate `if(e == null) 'null' else e.toString()` *)
let string_null = mk (TConst (TString "null")) ctx.t.tstring e.epos in
Expand Down
4 changes: 2 additions & 2 deletions src/typing/operators.ml
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ let make_binop ctx op e1 e2 is_assign_op p =
let tstring = ctx.t.tstring in
let to_string e =
let rec loop t = match classify t with
| KAbstract ({a_impl = Some c},_) when PMap.mem "toString" c.cl_statics ->
call_to_string ctx e
| KAbstract ({a_impl = Some c; a_this},_) when PMap.mem "toString" c.cl_statics ->
call_to_string ~no_null_check:(is_explicit_null a_this) ctx e
| KInt | KFloat | KString -> e
| KUnk | KDyn | KNumParam _ | KStrParam _ | KOther ->
Texpr.Builder.resolve_and_make_static_call ctx.com.std "string" [e] e.epos
Expand Down
7 changes: 4 additions & 3 deletions src/typing/typer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1603,7 +1603,8 @@ and type_meta ?(mode=MGet) ctx m e1 with_type p =
| (Meta.ToString,_,_) ->
let e = e() in
(match follow e.etype with
| TAbstract({a_impl = Some c},_) when PMap.mem "toString" c.cl_statics -> call_to_string ctx e
| TAbstract({a_impl = Some c; a_this},_) when PMap.mem "toString" c.cl_statics ->
call_to_string ~no_null_check:(is_explicit_null a_this) ctx e
| _ -> e)
| (Meta.Markup,_,_) ->
raise_typing_error "Markup literals must be processed by a macro" p
Expand Down Expand Up @@ -1725,8 +1726,8 @@ and type_call_builtin ctx e el mode with_type p =
let e = type_expr ctx e WithType.value in
let infos = type_expr ctx infos WithType.value in
let e = match follow e.etype with
| TAbstract({a_impl = Some c},_) when PMap.mem "toString" c.cl_statics ->
call_to_string ctx e
| TAbstract({a_impl = Some c; a_this},_) when PMap.mem "toString" c.cl_statics ->
call_to_string ~no_null_check:(is_explicit_null a_this) ctx e
| _ ->
e
in
Expand Down
48 changes: 48 additions & 0 deletions tests/unit/src/unit/issues/Issue12019.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package unit.issues;

private abstract MyStringA(Null<String>) from Null<String> {
function toString() {
if (this == null)
return "EMPTY";
return this;
}
}

private typedef NullableString = Null<String>;

private abstract MyStringB(NullableString) from NullableString {
function toString() {
if (this == null)
return "EMPTY";
return this;
}
}

class Issue12019 extends unit.Test {
final a:MyStringA = null;
final b:MyStringB = null;

var oldTrace:(Dynamic, ?Null<haxe.PosInfos>) -> Void;

function setup() {
oldTrace = haxe.Log.trace;
}

function teardown() {
haxe.Log.trace = oldTrace;
}

function testTrace() {
haxe.Log.trace = function(v, ?infos) {
eq("EMPTY", v);
};

trace(a);
trace(b);
}

function testConcatenate() {
eq("Concatenated: EMPTY", "Concatenated: " + a);
eq("Concatenated: EMPTY", "Concatenated: " + b);
}
}
Loading