www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 21722] New: toString(sink, string format) does not work with

https://issues.dlang.org/show_bug.cgi?id=21722

          Issue ID: 21722
           Summary: toString(sink, string format) does not work with
                    non-"%s" strings
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: major
          Priority: P1
         Component: phobos
          Assignee: nobody puremagic.com
          Reporter: pro.mathias.lang gmail.com

```
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).

--
Mar 15 2021