a handful of DIP1000 fixes (#185)

a handful of DIP1000 fixes
merged-on-behalf-of: BBasile <BBasile@users.noreply.github.com>
This commit is contained in:
Cameron Ross 2018-07-18 04:14:42 -03:00 committed by The Dlang Bot
parent 46db7d3ba2
commit 6e12cf9d64
3 changed files with 139 additions and 140 deletions

View file

@ -332,7 +332,7 @@ struct Emitter(Range, CharType) if (isOutputRange!(Range, CharType))
} }
///Expect nothing, throwing if we still have something. ///Expect nothing, throwing if we still have something.
void expectNothing() const @safe void expectNothing() @safe
{ {
throw new EmitterException("Expected nothing, but got " ~ event_.idString); throw new EmitterException("Expected nothing, but got " ~ event_.idString);
} }

View file

@ -1216,70 +1216,51 @@ struct Node
* Throws: NodeException if the node is not a sequence or an * Throws: NodeException if the node is not a sequence or an
* element could not be converted to specified type. * element could not be converted to specified type.
*/ */
template opApply(T) int opApply(D)(D dg) if (isDelegate!D && (Parameters!D.length == 1))
{ {
int opApplyImpl(DG)(DG dg) enforce(isSequence,
{ new NodeException("Trying to sequence-foreach over a " ~ nodeTypeString ~ " node",
enforce(isSequence, startMark_));
new NodeException("Trying to sequence-foreach over a " ~ nodeTypeString ~ " node",
startMark_));
int result; int result;
foreach(ref node; get!(Node[])) foreach(ref node; get!(Node[]))
{
static if(is(Unqual!T == Node))
{
result = dg(node);
}
else
{
T temp = node.as!T;
result = dg(temp);
}
if(result){break;}
}
return result;
}
// Ugly workaround due to issues with inout and delegate params
int opApplyImpl(DG)(DG dg) const
{ {
enforce(isSequence, static if(is(Unqual!(Parameters!D[0]) == Node))
new NodeException("Trying to sequence-foreach over a " ~ nodeTypeString ~ " node", {
startMark_)); result = dg(node);
}
else
{
Parameters!D[0] temp = node.as!(Parameters!D[0]);
result = dg(temp);
}
if(result){break;}
}
return result;
}
/// ditto
int opApply(D)(D dg) const if (isDelegate!D && (Parameters!D.length == 1))
{
enforce(isSequence,
new NodeException("Trying to sequence-foreach over a " ~ nodeTypeString ~ " node",
startMark_));
int result; int result;
foreach(ref node; get!(Node[])) foreach(ref node; get!(Node[]))
{
static if(is(Unqual!(Parameters!D[0]) == Node))
{ {
static if(is(Unqual!T == Node)) result = dg(node);
{
result = dg(node);
}
else
{
T temp = node.as!T;
result = dg(temp);
}
if(result){break;}
} }
return result; else
{
Parameters!D[0] temp = node.as!(Parameters!D[0]);
result = dg(temp);
}
if(result){break;}
} }
int opApply(scope int delegate(ref T) @system dg) return result;
{ }
return opApplyImpl(dg);
}
int opApply(scope int delegate(ref T) @safe dg)
{
return opApplyImpl(dg);
}
int opApply(scope int delegate(ref const T) @system dg) const
{
return opApplyImpl(dg);
}
int opApply(scope int delegate(ref const T) @safe dg) const
{
return opApplyImpl(dg);
}
}
@safe unittest @safe unittest
{ {
Node n1 = Node(11); Node n1 = Node(11);
@ -1287,8 +1268,9 @@ struct Node
Node n3 = Node(13); Node n3 = Node(13);
Node n4 = Node(14); Node n4 = Node(14);
Node narray = Node([n1, n2, n3, n4]); Node narray = Node([n1, n2, n3, n4]);
const cNArray = narray;
int[] array, array2; int[] array, array2, array3;
foreach(int value; narray) foreach(int value; narray)
{ {
array ~= value; array ~= value;
@ -1297,8 +1279,13 @@ struct Node
{ {
array2 ~= node.as!int; array2 ~= node.as!int;
} }
foreach (const Node node; cNArray)
{
array3 ~= node.as!int;
}
assert(array == [11, 12, 13, 14]); assert(array == [11, 12, 13, 14]);
assert(array2 == [11, 12, 13, 14]); assert(array2 == [11, 12, 13, 14]);
assert(array3 == [11, 12, 13, 14]);
} }
@safe unittest @safe unittest
{ {
@ -1325,6 +1312,13 @@ struct Node
i++; i++;
} }
} }
@safe unittest
{
auto node = Node(["a":1, "b":2, "c":3]);
const cNode = node;
assertThrown({foreach (Node n; node) {}}());
assertThrown({foreach (const Node n; cNode) {}}());
}
/** Foreach over a mapping, getting each key/value as K/V. /** Foreach over a mapping, getting each key/value as K/V.
* *
@ -1334,94 +1328,79 @@ struct Node
* Throws: NodeException if the node is not a mapping or an * Throws: NodeException if the node is not a mapping or an
* element could not be converted to specified type. * element could not be converted to specified type.
*/ */
template opApply(K, V) int opApply(DG)(DG dg) if (isDelegate!DG && (Parameters!DG.length == 2))
{ {
int opApplyImpl(DG)(DG dg) alias K = Parameters!DG[0];
{ alias V = Parameters!DG[1];
enforce(isMapping, enforce(isMapping,
new NodeException("Trying to mapping-foreach over a " ~ nodeTypeString ~ " node", new NodeException("Trying to mapping-foreach over a " ~ nodeTypeString ~ " node",
startMark_)); startMark_));
int result; int result;
foreach(ref pair; get!(Node.Pair[])) foreach(ref pair; get!(Node.Pair[]))
{
static if(is(Unqual!K == Node) && is(Unqual!V == Node))
{ {
static if(is(Unqual!K == Node) && is(Unqual!V == Node)) result = dg(pair.key, pair.value);
{
result = dg(pair.key, pair.value);
}
else static if(is(Unqual!K == Node))
{
V tempValue = pair.value.as!V;
result = dg(pair.key, tempValue);
}
else static if(is(Unqual!V == Node))
{
K tempKey = pair.key.as!K;
result = dg(tempKey, pair.value);
}
else
{
K tempKey = pair.key.as!K;
V tempValue = pair.value.as!V;
result = dg(tempKey, tempValue);
}
if(result){break;}
} }
return result; else static if(is(Unqual!K == Node))
}
// Ugly workaround due to issues with inout and delegate params
int opApplyImpl(DG)(DG dg) const
{
enforce(isMapping,
new NodeException("Trying to mapping-foreach over a " ~ nodeTypeString ~ " node",
startMark_));
int result;
foreach(ref pair; get!(Node.Pair[]))
{ {
static if(is(Unqual!K == Node) && is(Unqual!V == Node)) V tempValue = pair.value.as!V;
{ result = dg(pair.key, tempValue);
result = dg(pair.key, pair.value);
}
else static if(is(Unqual!K == Node))
{
V tempValue = pair.value.as!V;
result = dg(pair.key, tempValue);
}
else static if(is(Unqual!V == Node))
{
K tempKey = pair.key.as!K;
result = dg(tempKey, pair.value);
}
else
{
K tempKey = pair.key.as!K;
V tempValue = pair.value.as!V;
result = dg(tempKey, tempValue);
}
if(result){break;}
} }
return result; else static if(is(Unqual!V == Node))
{
K tempKey = pair.key.as!K;
result = dg(tempKey, pair.value);
}
else
{
K tempKey = pair.key.as!K;
V tempValue = pair.value.as!V;
result = dg(tempKey, tempValue);
}
if(result){break;}
} }
int opApply(scope int delegate(ref K, ref V) @system dg) return result;
}
/// ditto
int opApply(DG)(DG dg) const if (isDelegate!DG && (Parameters!DG.length == 2))
{
alias K = Parameters!DG[0];
alias V = Parameters!DG[1];
enforce(isMapping,
new NodeException("Trying to mapping-foreach over a " ~ nodeTypeString ~ " node",
startMark_));
int result;
foreach(ref pair; get!(Node.Pair[]))
{ {
return opApplyImpl(dg); static if(is(Unqual!K == Node) && is(Unqual!V == Node))
{
result = dg(pair.key, pair.value);
}
else static if(is(Unqual!K == Node))
{
V tempValue = pair.value.as!V;
result = dg(pair.key, tempValue);
}
else static if(is(Unqual!V == Node))
{
K tempKey = pair.key.as!K;
result = dg(tempKey, pair.value);
}
else
{
K tempKey = pair.key.as!K;
V tempValue = pair.value.as!V;
result = dg(tempKey, tempValue);
}
if(result){break;}
} }
int opApply(scope int delegate(ref K, ref V) @safe dg) return result;
{ }
return opApplyImpl(dg);
}
int opApply(scope int delegate(ref const K, ref const V) @system dg) const
{
return opApplyImpl(dg);
}
int opApply(scope int delegate(ref const K, ref const V) @safe dg) const
{
return opApplyImpl(dg);
}
}
@safe unittest @safe unittest
{ {
Node n1 = Node(cast(long)11); Node n1 = Node(cast(long)11);
@ -1466,6 +1445,19 @@ struct Node
default: assert(false); default: assert(false);
} }
} }
const nmap3 = nmap2;
foreach(const Node key, const Node value; nmap3)
{
switch(key.as!string)
{
case "11": assert(value.as!int == 5 ); break;
case "12": assert(value.as!bool == true ); break;
case "13": assert(value.as!float == 1.0 ); break;
case "14": assert(value.as!string == "yarly"); break;
default: assert(false);
}
}
} }
@safe unittest @safe unittest
{ {
@ -1486,6 +1478,13 @@ struct Node
assert(elem == testStrs[i]); assert(elem == testStrs[i]);
} }
} }
@safe unittest
{
auto node = Node(["a", "b", "c"]);
const cNode = node;
assertThrown({foreach (Node a, Node b; node) {}}());
assertThrown({foreach (const Node a, const Node b; cNode) {}}());
}
/** Add an element to a sequence. /** Add an element to a sequence.
* *

View file

@ -526,7 +526,7 @@ public:
/// end of the slice being built, the slice is extended (trivial operation). /// end of the slice being built, the slice is extended (trivial operation).
/// ///
/// See_Also: begin /// See_Also: begin
void write(char[] str) @safe pure nothrow @nogc void write(scope char[] str) @safe pure nothrow @nogc
{ {
assert(inProgress, "write called without begin"); assert(inProgress, "write called without begin");
assert(end_ <= reader_.bufferOffset_, assert(end_ <= reader_.bufferOffset_,