Skip to content

Commit

Permalink
Fix Issue 17269 - Use toString if available instead of implicitly con…
Browse files Browse the repository at this point in the history
…verting to string
  • Loading branch information
CromFr committed Jan 31, 2021
1 parent 7558583 commit f1ff009
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions std/format.d
Original file line number Diff line number Diff line change
Expand Up @@ -3638,7 +3638,7 @@ private void formatChar(Writer)(ref Writer w, in dchar c, in char quote)
// undocumented because of deprecation
// string elements are formatted like UTF-8 string literals.
void formatElement(Writer, T, Char)(auto ref Writer w, T val, scope const ref FormatSpec!Char f)
if (is(StringTypeOf!T) && !is(T == enum))
if (is(StringTypeOf!T) && !hasToString!(T, Char) && !is(T == enum))
{
import std.array : appender;
import std.utf : decode, UTFException;
Expand Down Expand Up @@ -3749,7 +3749,7 @@ if (is(CharTypeOf!T) && !is(T == enum))
// undocumented
// Maybe T is noncopyable struct, so receive it by 'auto ref'.
void formatElement(Writer, T, Char)(auto ref Writer w, auto ref T val, scope const ref FormatSpec!Char f)
if (!is(StringTypeOf!T) && !is(CharTypeOf!T) || is(T == enum))
if ((!is(StringTypeOf!T) || hasToString!(T, Char)) && !is(CharTypeOf!T) || is(T == enum))
{
formatValue(w, val, f);
}
Expand Down Expand Up @@ -3807,6 +3807,29 @@ if (is(AssocArrayTypeOf!T) && !is(T == enum) && !hasToString!(T, Char))
put(w, f.seqAfter);
}

@safe unittest
{
// Bug #17269. Behavior similar to `struct A { Nullable!string B; }`
struct StringAliasThis
{
@property string value() const { assert(0); }
alias value this;
string toString() { return "helloworld"; }
private string _value;
}
struct TestContainer
{
StringAliasThis testVar;
}

import std.array : appender;
auto w = appender!string();
auto spec = singleSpec("%s");
formatElement(w, TestContainer(), spec);

assert(w.data == "TestContainer(helloworld)", w.data);
}

@safe unittest
{
assert(collectExceptionMsg!FormatException(format("%d", [0:1])).back == 'd');
Expand Down

0 comments on commit f1ff009

Please sign in to comment.