Bug 21722 – toString(sink, string format) does not work with non-"%s" strings

Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2021-03-16T06:51:12Z
Last change time
2021-04-19T06:53:18Z
Keywords
pull
Assigned to
No Owner
Creator
Mathias LANG

Comments

Comment #0 by pro.mathias.lang — 2021-03-16T06:51:12Z
``` import std.stdio; struct Bar { void toString (scope void delegate (scope const(char)[]) sink, string fmt) { sink("Hello"); } } void main () { Bar b; writefln("%b", b); } ``` This throws: ``` ~/dlang/dmd-2.089.1/osx/bin/dmd -run foo.d :( std.format.FormatException@/Users/geod24/dlang/dmd-2.089.1/osx/bin/../../src/phobos/std/format.d(3982): Expected '%s' format specifier for type 'Bar' ---------------- ??:? pure @safe void std.exception.bailOut!(std.format.FormatException).bailOut(immutable(char)[], ulong, scope const(char)[]) [0x105f7463e] ??:? pure @safe bool std.exception.enforce!(std.format.FormatException).enforce!(bool).enforce(bool, lazy const(char)[], immutable(char)[], ulong) [0x105f745b4] ??:? pure @safe void std.format.enforceValidFormatSpec!(foo.Bar, char).enforceValidFormatSpec(scope ref const(std.format.FormatSpec!(char).FormatSpec)) [0x105f77960] ??:? void std.format.formatValueImpl!(std.stdio.File.LockingTextWriter, foo.Bar, char).formatValueImpl(ref std.stdio.File.LockingTextWriter, ref foo.Bar, scope ref const(std.format.FormatSpec!(char).FormatSpec)) [0x105f77918] ??:? void std.format.formatValue!(std.stdio.File.LockingTextWriter, foo.Bar, char).formatValue(ref std.stdio.File.LockingTextWriter, ref foo.Bar, scope ref const(std.format.FormatSpec!(char).FormatSpec)) [0x105f778f8] ??:? uint std.format.formattedWrite!(std.stdio.File.LockingTextWriter, char, foo.Bar).formattedWrite(ref std.stdio.File.LockingTextWriter, scope const(char[]), foo.Bar) [0x105f736c4] ??:? void std.stdio.File.writefln!(char, foo.Bar).writefln(const(char[]), foo.Bar) [0x105f7333b] ??:? void std.stdio.writefln!(char, foo.Bar).writefln(const(char[]), foo.Bar) [0x105f732c3] ??:? _Dmain [0x105f73225] ``` Reproducible with v2.089 - v2.096 (haven't tested older).
Comment #1 by bugzilla — 2021-03-16T15:57:58Z
Please note, that according to the specs, it's recommended not to use this type of toString [1]. [1] https://dlang.org/phobos/std_format.html#.formatValue
Comment #2 by dlang-bot — 2021-03-16T16:00:30Z
@berni44 created dlang/phobos pull request #7870 "Fix Issue 21722 - toString(sink, string format) does not work with non-"%s" strings" fixing this issue: - Fix Issue 21722 - toString(sink, string format) does not work with non-"%s" strings https://github.com/dlang/phobos/pull/7870
Comment #3 by dlang-bot — 2021-03-17T00:02:20Z
dlang/phobos pull request #7870 "Fix Issue 21722 - toString(sink, string format) does not work with non-"%s" strings" was merged into master: - e6e1d242cbe69016eed76ae59b419c8765b2c6ef by berni44: Fix Issue 21722 - toString(sink, string format) does not work with non-"%s" strings https://github.com/dlang/phobos/pull/7870
Comment #4 by dlang-bot — 2021-04-19T06:53:18Z
dlang/phobos pull request #7966 "Fix second part of Issue 21722 - toString(sink, string format) does not work with non-"%s" strings" was merged into master: - 360b0e51c04cf9e7a9756422974cc301e9dca61f by berni44: Fix second part of Issue 21722 - toString(sink, string format) does not work with non-"%s" strings https://github.com/dlang/phobos/pull/7966