Bug 22873 – Wrong std.format output for `inout`

Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2022-03-11T17:03:44Z
Last change time
2022-03-22T13:32:09Z
Keywords
industry, pull
Assigned to
No Owner
Creator
johanengelen

Comments

Comment #0 by johanengelen — 2022-03-11T17:03:44Z
For us, this is a regression since dlang2.099, but the testcase shows the bigger problem that has apparently always existed. Testcase: ``` import std.stdio; import std.format; struct U8 { string toString() const { return "blah"; } } struct ContainsU8 { U8 text; auto makeInout() inout // with/without `inout` gives different output { foo(text); } } void foo(T)(T obj) { pragma(msg, T); writeln(format("%s", obj)); } void main() { ContainsU8 a; a.makeInout(); } ``` This prints "inout(U8)()". If you remove `inout` from line 8, the program outputs "blah", as it should. The problem is that `std.format.internal.write.hasToString` does not give correct output for `inout(T)`, i.e. `hasToString(inout(U8), char)` will return `HasToStringResult.none`. Another viewpoint could be that `inout` should not have been applied to the type of `text` on line 10...
Comment #1 by johanengelen — 2022-03-11T18:03:45Z
I've tried to simplify the issue with `inout`: ``` struct U8 { } enum canCreateVariable(T) = __traits(compiles, { T val; }); struct ContainsU8 { U8 text; auto withInout() inout { foo(text); } auto normal() { foo(text); } } void foo(T)(T obj) { pragma(msg, T); pragma(msg, canCreateVariable!T); } void main() { ContainsU8 a; } ``` Outputs: inout(U8) false U8 true
Comment #2 by johanengelen — 2022-03-11T18:32:42Z
Paul Backus wrote on Slack: Yes, this is an easy pit to fall into. In order to work correctly with inout, dummy variables like the one in hasToString need to be declared as parameters, not locals. See e.g. std.range.isInputRange, which gets this right: https://github.com/dlang/phobos/blob/v2.099.0/std/range/primitives.d#L174
Comment #3 by dlang-bot — 2022-03-11T20:18:40Z
@pbackus created dlang/phobos pull request #8409 "Fix issue 22873 - Wrong std.format output for `inout`" fixing this issue: - Fix issue 22873 - Wrong std.format output for `inout` https://github.com/dlang/phobos/pull/8409
Comment #4 by dlang-bot — 2022-03-22T13:32:09Z
dlang/phobos pull request #8409 "Fix issue 22873 - Wrong std.format output for `inout`" was merged into master: - 2c91f907145935bb6eb7c2b4dc124da6bdca8263 by Paul Backus: Fix issue 22873 - Wrong std.format output for `inout` https://github.com/dlang/phobos/pull/8409