Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 11 additions & 17 deletions std/json.d
Original file line number Diff line number Diff line change
Expand Up @@ -1537,19 +1537,15 @@ if (isOutputRange!(Out,char))
toStringImpl!char(str);
}

// recursive @safe inference is broken here
// workaround: if json.put is @safe, we should be too,
// so annotate the recursion as @safe manually
static if (isSafe!({ json.put(""); }))
{
void delegate(ref const JSONValue, ulong) @safe toValue;
}
else
{
void delegate(ref const JSONValue, ulong) @system toValue;
}
/* make the function infer @system when json.put() is @system
*/
if (0)
json.put(' ');

void toValueImpl(ref const JSONValue value, ulong indentLevel)
/* Mark as @trusted because json.put() may be @system. This has difficulty
* inferring @safe because it is recursive.
*/
void toValueImpl(ref const JSONValue value, ulong indentLevel) @trusted
{
void putTabs(ulong additionalIndent = 0)
{
Expand Down Expand Up @@ -1594,7 +1590,7 @@ if (isOutputRange!(Out,char))
json.put(':');
if (pretty)
json.put(' ');
toValue(member, indentLevel + 1);
toValueImpl(member, indentLevel + 1);
}
}

Expand Down Expand Up @@ -1631,7 +1627,7 @@ if (isOutputRange!(Out,char))
if (i)
putCharAndEOL(',');
putTabs(1);
toValue(el, indentLevel + 1);
toValueImpl(el, indentLevel + 1);
}
putEOL();
putTabs();
Expand Down Expand Up @@ -1710,9 +1706,7 @@ if (isOutputRange!(Out,char))
}
}

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.

toValueImpl(root, 0);
}

// https://issues.dlang.org/show_bug.cgi?id=12897
Expand Down