Skip to content

Conversation

@WalterBright
Copy link
Member

The code has a kludge in it where it is recursively attempting to infer @safe, but that doesn't work, as the compiler assumes it is not @safe if recursive. The workaround in the code is to use a delegate. The trouble with the delegate a dynamic closure is allocated for it. Dynamic closures can escape, so dip1000 complains about ref json escaping the scope.

The solution is to get rid of the delegates, and use a nested function directly. Mark it as @trusted, which is also a workaround, but a better workaround than the old method, and at least this will work with dlang/dmd#14364

For the future, it would be better to simply require the json parameter to be @safe.

@dlang-bot
Copy link
Contributor

Thanks for your pull request, @WalterBright!

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

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#8530"


toValue = &toValueImpl;

toValue(root, 0);
Copy link
Contributor

Choose a reason for hiding this comment

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

To prevent adding a safety hole in toJSON, I suggest adding something like this outside of toValueImpl:

// Make the function infer `@system` when `json.put` is `@system`
if (0) json.put(' ');

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure how this improves anything.

Copy link
Contributor

Choose a reason for hiding this comment

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

Surely this shouldn't compile:

struct Sink {
    void put(char c) @system
    {
        *(cast(int*) 0xDEADBEEF) = 0;
    }
}

void main() @safe
{
    Sink s;
    auto jv = JSONValue("x");
    toJson(s, jv);
}

Copy link
Contributor

Choose a reason for hiding this comment

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

A function that takes a template OutputRange cannot be invariably @trusted, that's in violation of safe interfaces.

Copy link
Member Author

Choose a reason for hiding this comment

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

All right, but I dislike doing kludges like this. At least it's not as bad as the delegate kludge being replaced.

Co-authored-by: Dennis <[email protected]>
@dlang-bot dlang-bot merged commit 7c19342 into dlang:master Aug 12, 2022
@WalterBright WalterBright deleted the jsonDelegate branch August 12, 2022 18:12
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.

3 participants