Skip to content

Commit 7d8615f

Browse files
Fix Bugzilla Issue 24739 - to!string creates unnecessary allocation for structs with toString
1 parent 3f990a7 commit 7d8615f

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

std/conv.d

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1013,7 +1013,13 @@ if (!(is(S : T) &&
10131013
!isEnumStrToStr!(S, T) && !isNullToStr!(S, T)) &&
10141014
!isInfinite!S && isExactSomeString!T)
10151015
{
1016-
static if (isExactSomeString!S && value[0].sizeof == ElementEncodingType!T.sizeof)
1016+
static if (is(typeof(S.init.toString())) &&
1017+
is(typeof(S.init.toString()) == string) &&
1018+
!is(S == class))
1019+
{
1020+
return value.toString();
1021+
}
1022+
else static if (isExactSomeString!S && value[0].sizeof == ElementEncodingType!T.sizeof)
10171023
{
10181024
// string-to-string with incompatible qualifier conversion
10191025
static if (is(ElementEncodingType!T == immutable))
@@ -1121,6 +1127,19 @@ if (!(is(S : T) &&
11211127
return toStr!T(value);
11221128
}
11231129
}
1130+
// https://issues.dlang.org/show_bug.cgi?id=24739
1131+
@system unittest
1132+
{
1133+
import std.conv : to;
1134+
import std.exception : enforce;
1135+
1136+
struct S
1137+
{
1138+
string toString() { return "S"; }
1139+
}
1140+
1141+
enforce(S.init.toString().ptr == S.init.to!string.ptr);
1142+
}
11241143

11251144
// https://issues.dlang.org/show_bug.cgi?id=14042
11261145
@system unittest

0 commit comments

Comments
 (0)