From 31a8f97423715fa5b4d0977a76e98a8eb83707d0 Mon Sep 17 00:00:00 2001 From: k-hara Date: Sat, 7 Jan 2012 03:22:30 +0900 Subject: [PATCH] Issue 7230 - Crash during printing anonymous union with writeln family functions. --- std/format.d | 44 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/std/format.d b/std/format.d index 9f36f6333fd..f48e5e8537b 100644 --- a/std/format.d +++ b/std/format.d @@ -1885,9 +1885,24 @@ if ((is(T == struct) || is(T == union)) && !isInputRange!T) put(w, left); foreach (i, e; val.tupleof) { - static if (i > 0) - put(w, separator); - formatElement(w, e, f); + static if (0 < i && val.tupleof[i-1].offsetof == val.tupleof[i].offsetof) + { + static if (i == val.tupleof.length - 1 || val.tupleof[i].offsetof != val.tupleof[i+1].offsetof) + put(w, separator~val.tupleof[i].stringof[4..$]~"}"); + else + put(w, separator~val.tupleof[i].stringof[4..$]); + } + else + { + static if (i+1 < val.tupleof.length && val.tupleof[i].offsetof == val.tupleof[i+1].offsetof) + put(w, (i > 0 ? separator : "")~"#{overlap "~val.tupleof[i].stringof[4..$]); + else + { + static if (i > 0) + put(w, separator); + formatElement(w, e, f); + } + } } put(w, right); } @@ -1954,6 +1969,29 @@ unittest assert(a.data == "hello"); } +unittest +{ + // 7230 + static struct Bug7230 + { + string s = "hello"; + union { + string a; + int b; + double c; + } + long x = 10; + } + + Bug7230 bug; + bug.b = 123; + + FormatSpec!char f; + auto w = appender!(char[])(); + formatValue(w, bug, f); + assert(w.data == `Bug7230("hello", #{overlap a, b, c}, 10)`); +} + /** Static-size arrays are formatted just like arrays. */