Bug 9588 – format prints context pointer for struct
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-02-25T15:07:36Z
Last change time
2019-12-07T08:48:32Z
Keywords
pull
Assigned to
No Owner
Creator
Andrej Mitrovic
Comments
Comment #0 by andrej.mitrovich — 2013-02-25T15:07:36Z
import std.string;
import std.stdio;
void main()
{
struct S { int x; bool empty() { return false; } }
writeln( format("%s", S()) );
}
> S(0, null)
It shouldn't try to print the function. I think this should also apply to property functions since e.g. "empty" is typically a property function.
Comment #1 by andrej.mitrovich — 2013-02-25T18:11:48Z
(In reply to comment #0)
> import std.string;
> import std.stdio;
>
> void main()
> {
> struct S { int x; bool empty() { return false; } }
> writeln( format("%s", S()) );
> }
>
> > S(0, null)
>
> It shouldn't try to print the function. I think this should also apply to
> property functions since e.g. "empty" is typically a property function.
Oh wait a minute, I know what this is. It's the hidden context pointer. If you change the struct to 'static' the null disappears. But I don't think format() should print that.
Comment #2 by hsteoh — 2013-02-26T16:23:47Z
Is there a way to tell whether a field is the hidden context pointer? Does it have a specific name (that isn't compiler-dependent)?
Comment #3 by andrej.mitrovich — 2013-02-26T19:01:50Z
(In reply to comment #2)
> Is there a way to tell whether a field is the hidden context pointer? Does it
> have a specific name (that isn't compiler-dependent)?
None that I know of, but it appears it's always the last member (at least in DMD).
When we get the isNested[1] trait pulled we could simply do a test on the aggregate and just ignore the last field when writing its contents.
[1]: https://github.com/D-Programming-Language/dmd/pull/1362
Comment #4 by hsteoh — 2013-02-26T20:19:22Z
Hmm. Using __traits(allMembers, S) seems to indicate that the field name is 'this'. Since 'this' is a reserved keyword, that may be safer to check for than assuming that the compiler will always put it last.
Comment #5 by andrej.mitrovich — 2013-02-26T20:22:21Z
(In reply to comment #4)
> Hmm. Using __traits(allMembers, S) seems to indicate that the field name is
> 'this'. Since 'this' is a reserved keyword, that may be safer to check for than
> assuming that the compiler will always put it last.
Nice catch.
Comment #6 by andrej.mitrovich — 2013-03-07T19:18:48Z
(In reply to comment #4)
> Hmm. Using __traits(allMembers, S) seems to indicate that the field name is
> 'this'. Since 'this' is a reserved keyword, that may be safer to check for than
> assuming that the compiler will always put it last.
This won't work out that easy. The code in format uses .tupleof to get to the values, but .tupleof and allMembers return different results, for example:
void main()
{
struct S { int x; this(int) { } }
}
allMembers:
tuple("x", "__ctor", "this")
.tupleof:
tuple(0, S(0).this)
So the indexes won't match. Since my new isNested trait was pulled, and assuming the context pointer is last, I could do:
immutable tupleLen = val.tupleof.length;
foreach (i, e; val.tupleof)
{
// skip printing context pointer
static if (__traits(isNested, T) && i+i == tupleLen) { }
}
It's hard to tell whether this will be safe.
Another idea is to change how .tupleof works internally (to remove exposing the context pointer), but this might be a bad idea, serialization might probably require it as well as other code.
Comment #7 by simen.kjaras — 2018-07-18T11:56:47Z
Another option: __traits(identifier, S.tupleof[1]) returns "this".
Comment #8 by dlang-bot — 2019-12-07T06:35:32Z
@berni44 created dlang/phobos pull request #7307 "Fix Issue 9588 - format prints context pointer for struct" fixing this issue:
- Fix Issue 9588 - format prints context pointer for struct
https://github.com/dlang/phobos/pull/7307
Comment #9 by dlang-bot — 2019-12-07T08:48:32Z
dlang/phobos pull request #7307 "Fix Issue 9588 - format prints context pointer for struct" was merged into master:
- 209dfd391589dd2e8e26c2999570bf38c368e0d5 by Bernhard Seckinger:
Fix Issue 9588 - format prints context pointer for struct
https://github.com/dlang/phobos/pull/7307