Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Issue 20502: Converting std.typecons.RefCounted!T to a string gives T's storage location instead of T's fields when T is a struct without an explicit toString #7352

Merged
merged 1 commit into from
Mar 2, 2021

Conversation

n8sh
Copy link
Member

@n8sh n8sh commented Jan 13, 2020

Example of what this PR fixes:

void main()
{
    import std.typecons : refCounted;
    import std.stdio : writeln;

    writeln(123); // "123"
    writeln(refCounted(123)); // "123"
    
    struct A { string toString() { return "a"; } }
    writeln(A.init); // "a"
    writeln(refCounted(A.init)); // "a"

    // This one doesn't match:
    struct B { int x; }
    writeln(B(123)); // "B(123)"
    writeln(refCounted(B(123))); // "RefCounted!(B, cast(RefCountedAutoInitialize)0)(RefCountedStore(558C7FFBC660))"
}

@n8sh n8sh requested a review from MetaLang as a code owner January 13, 2020 18:58
@dlang-bot
Copy link
Contributor

dlang-bot commented Jan 13, 2020

Thanks for your pull request, @n8sh!

Bugzilla references

Auto-close Bugzilla Severity Description
20502 minor Converting std.typecons.RefCounted!T to a string gives T's storage location instead of T's fields when T is a struct without an explicit toString

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "master + phobos#7352"

@MoonlightSentinel
Copy link
Contributor

MoonlightSentinel commented Jan 13, 2020

Compiling with DMD master prints:

123
123
a
a
B(123)
B(123)

Is there a mistake in your example?

EDIT: This was tested on Win10, run.dlang.io yields the RefCounted!(B, cast(RefCountedAutoInitialize)0)(RefCountedStore(558C7FFBC660))

@n8sh
Copy link
Member Author

n8sh commented Jan 13, 2020

@MoonlightSentinel I've verified this is still happening with freshly-compiled DMD master with that example verbatim. Pasting the example into https://run.dlang.io/ and running it also gives that result.

@MoonlightSentinel
Copy link
Contributor

Strange, are you on Mac/Posix?

std/typecons.d Outdated Show resolved Hide resolved
std/typecons.d Outdated
@@ -6544,6 +6559,23 @@ pure @system unittest
RefCounted!(int*) c;
}

@system unittest // Issue 20502
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason for this to be @system other than the cast?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RefCounted in general is @system.

std/typecons.d Outdated Show resolved Hide resolved
@n8sh
Copy link
Member Author

n8sh commented Jan 14, 2020

@MoonlightSentinel I've tested this on Windows 10 using dmd v2.090.0. Maybe you had this branch checked out and dmd used it when you ran the example so the fix was already applied.

@MoonlightSentinel
Copy link
Contributor

@MoonlightSentinel I've tested this on Windows 10 using dmd v2.090.0. Maybe you had this branch checked out and dmd used it when you ran the example so the fix was already applied.

I somehow missed that the above behaviour was fixed and thought it was still present. Sorry for the noise

// structs that have alias this.
struct B { int b; alias b this; }
struct C { B b; alias b this; }
assert(to!string(refCounted(C(B(123)))) == to!string(C(B(123))));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add tests for const/immutable RefCounted (and maybe other storage classes as well)

@berni44
Copy link
Contributor

berni44 commented Mar 1, 2021

@RazvanN7 Browsing through old issues I came across this PR. Seems to be one of the forgotten PRs, that could be merged (after fixing the trivial merge conflict). In my opinion the request of @MoonlightSentinel for const/immutable RefCounted is an enhancement, not a must have (don't let the best be the enemy of the better).

Copy link
Collaborator

@RazvanN7 RazvanN7 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Please fix the merge conflict and we're good to go.

…es T's storage location instead of T's fields when T is a struct without an explicit toString
std/typecons.d Outdated Show resolved Hide resolved
@dlang-bot dlang-bot merged commit 90e9ce2 into dlang:master Mar 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants