Skip to content

Commit

Permalink
Merge pull request #5425 from JackStouffer/issue16108
Browse files Browse the repository at this point in the history
Fix Issue 16108: to!string fails on struct with disabled postblit
merged-on-behalf-of: Sebastian Wilzbach <sebi.wilzbach@gmail.com>
  • Loading branch information
dlang-bot committed Jun 11, 2017
2 parents 455daf2 + 3d80936 commit 4f2d89b
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions std/conv.d
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,13 @@ template to(T)
{
return toImpl!T(arg);
}

// Fix issue 16108
T to(S)(ref S arg)
if (isAggregateType!S && !isCopyable!S)
{
return toImpl!T(arg);
}
}

/**
Expand Down Expand Up @@ -1004,6 +1011,51 @@ if (!(isImplicitlyConvertible!(S, T) &&
}
}

/*
To string conversion for non copy-able structs
*/
private T toImpl(T, S)(ref S value)
if (!(isImplicitlyConvertible!(S, T) &&
!isEnumStrToStr!(S, T) && !isNullToStr!(S, T)) &&
!isInfinite!S && isExactSomeString!T && !isCopyable!S)
{
import std.format : FormatSpec, formatValue;
import std.array : appender;

auto w = appender!T();
FormatSpec!(ElementEncodingType!T) f;
formatValue(w, value, f);
return w.data;
}

// Bugzilla 16108
@system unittest
{
static struct A
{
int val;
bool flag;

string toString() { return text(val, ":", flag); }

@disable this(this);
}

auto a = A();
assert(to!string(a) == "0:false");

static struct B
{
int val;
bool flag;

@disable this(this);
}

auto b = B();
assert(to!string(b) == "B(0, false)");
}

/*
Check whether type $(D T) can be used in a switch statement.
This is useful for compile-time generation of switch case statements.
Expand Down

0 comments on commit 4f2d89b

Please sign in to comment.