Merge pull request #1329 from 9rnsr/fix10220

[REG2.064a] Issue 10220 - `array` doesn't work with disabled default construction
This commit is contained in:
Walter Bright 2013-06-04 22:30:00 -07:00
commit 7b0a1219fd
8 changed files with 89 additions and 58 deletions

View file

@ -169,6 +169,27 @@ unittest
}
}
// Bugzilla 10220
unittest
{
import std.algorithm : equal;
import std.range : repeat;
static struct S
{
int val;
@disable this();
this(int v) { val = v; }
}
assertCTFEable!(
{
auto r = S(1).repeat(2).array();
assert(equal(r, [S(1), S(1)]));
});
}
/**
Returns a newly allocated associative array out of elements of the input range,
which must be a range of tuples (Key, Value).
@ -187,8 +208,8 @@ assert(b == ["foo":"bar", "baz":"quux"]);
*/
auto assocArray(Range)(Range r)
if (isInputRange!Range && isTuple!(ElementType!Range)
&& ElementType!Range.length == 2)
if (isInputRange!Range && isTuple!(ElementType!Range) &&
ElementType!Range.length == 2)
{
alias ElementType!Range.Types[0] KeyType;
alias ElementType!Range.Types[1] ValueType;
@ -202,7 +223,7 @@ unittest
{
static assert(!__traits(compiles, [ tuple("foo", "bar", "baz") ].assocArray()));
static assert(!__traits(compiles, [ tuple("foo") ].assocArray()));
static assert(__traits(compiles, [ tuple("foo", "bar") ].assocArray()));
static assert( __traits(compiles, [ tuple("foo", "bar") ].assocArray()));
auto aa1 = [ tuple("foo", "bar"), tuple("baz", "quux") ].assocArray();
assert(is(typeof(aa1) == string[string]));
@ -224,7 +245,8 @@ private template blockAttribute(T)
enum blockAttribute = GC.BlkAttr.NO_SCAN;
}
}
unittest {
unittest
{
static assert(!(blockAttribute!void & GC.BlkAttr.NO_SCAN));
}
@ -241,7 +263,8 @@ private template nDimensions(T)
}
}
unittest {
unittest
{
static assert(nDimensions!(uint[]) == 1);
static assert(nDimensions!(float[][]) == 2);
}
@ -319,7 +342,12 @@ if(allSatisfy!(isIntegral, I))
alias typeof(T.init[0]) E;
auto ptr = (__ctfe) ?
(new E[](sizes[0])).ptr :
{
E[] arr;
foreach (i; 0 .. sizes[0])
arr ~= E.init;
return arr.ptr;
}() :
cast(E*) GC.malloc(sizes[0] * E.sizeof, blockAttribute!(E));
auto ret = ptr[0..sizes[0]];
@ -1160,14 +1188,13 @@ unittest
unittest
{
static int[] testCTFE()
assertCTFEable!(
{
int[] a = [1, 2];
a.insertInPlace(2, 3);
a.insertInPlace(0, -1, 0);
return a;
}
static assert(testCTFE() == [-1, 0, 1, 2, 3]);
return a == [-1, 0, 1, 2, 3];
});
}
unittest // bugzilla 6874
@ -2586,23 +2613,19 @@ unittest
}
}
static struct S10122
{
static struct S10122
{
int val;
int val;
@disable this();
this(int v) { val = v; }
}
@disable this();
this(int v) { val = v; }
}
assertCTFEable!(
{
auto w = appender!(S10122[])();
w.put(S10122(1));
static assert({
auto w = appender!(S10122[])();
w.put(S10122(1));
return w.data.length == 1 && w.data[0].val == 1;
}());
}
assert(w.data.length == 1 && w.data[0].val == 1);
});
}
/++

View file

@ -958,10 +958,12 @@ unittest
assert(wtext(int.min) == "-2147483648"w);
assert(to!string(0L) == "0");
//Test CTFE-ability.
static assert(to!string(1uL << 62) == "4611686018427387904");
static assert(to!string(0x100000000) == "4294967296");
static assert(to!string(-138L) == "-138");
assertCTFEable!(
{
assert(to!string(1uL << 62) == "4611686018427387904");
assert(to!string(0x100000000) == "4294967296");
assert(to!string(-138L) == "-138");
});
}
unittest
@ -1995,10 +1997,9 @@ unittest
unittest
{
//Some CTFE-ability checks.
static assert((){string s = "1234abc"; return parse!int(s) == 1234 && s == "abc";}());
static assert((){string s = "-1234abc"; return parse!int(s) == -1234 && s == "abc";}());
static assert((){string s = "1234abc"; return parse!uint(s) == 1234 && s == "abc";}());
assertCTFEable!({ string s = "1234abc"; assert(parse! int(s) == 1234 && s == "abc"); });
assertCTFEable!({ string s = "-1234abc"; assert(parse! int(s) == -1234 && s == "abc"); });
assertCTFEable!({ string s = "1234abc"; assert(parse!uint(s) == 1234 && s == "abc"); });
}
/// ditto

View file

@ -1197,3 +1197,10 @@ unittest
static assert(!__traits(compiles, 1.ifThrown(e=>new Object())));
static assert(!__traits(compiles, (new Object()).ifThrown(e=>1)));
}
version(unittest) package
@property void assertCTFEable(alias dg)()
{
static assert({ dg(); return true; }());
dg();
}

View file

@ -3627,13 +3627,6 @@ void formatReflectTest(T)(ref T val, string fmt, string formatted, string fn = _
input, fn, ln);
}
version(unittest)
@property void checkCTFEable(alias dg)()
{
static assert({ dg(); return true; }());
dg();
}
unittest
{
void booleanTest()
@ -3712,7 +3705,9 @@ unittest
formatReflectTest(aa, "{%([%s=%(%c%)]%|; %)}", `{[1=hello]; [2=world]}`);
}
checkCTFEable!({
import std.exception;
assertCTFEable!(
{
booleanTest();
integerTest();
if (!__ctfe) floatingTest(); // snprintf

View file

@ -1318,11 +1318,7 @@ unittest
[1.0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18];
static const y =
[2.0, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19];
assert(dotProduct(x, y) == 2280);
// Test in CTFE
enum ctfeDot = dotProduct(x, y);
static assert(ctfeDot == 2280);
assertCTFEable!({ assert(dotProduct(x, y) == 2280); });
}
/**

View file

@ -1619,8 +1619,11 @@ unittest
assert (equal2(pathSplitter(`\\foo\bar\baz`), [`\\foo\bar`, "baz"]));
}
// CTFE
static assert (equal(pathSplitter("/foo/bar".dup), ["/", "foo", "bar"]));
import std.exception;
assertCTFEable!(
{
assert (equal(pathSplitter("/foo/bar".dup), ["/", "foo", "bar"]));
});
}
@ -1943,8 +1946,10 @@ unittest
assert (relativePath("/foo/bar/baz", "/foo/bar") == "baz");
assertThrown(relativePath("/foo", "bar"));
// CTFE
static assert (relativePath("/foo/bar", "/foo/baz") == "../bar");
assertCTFEable!(
{
assert (relativePath("/foo/bar", "/foo/baz") == "../bar");
});
}
else version (Windows)
{
@ -1958,8 +1963,10 @@ unittest
assert (relativePath(`\\foo\bar`, `c:\foo`) == `\\foo\bar`);
assertThrown(relativePath(`c:\foo`, "bar"));
// CTFE
static assert (relativePath(`c:\foo\bar`, `c:\foo\baz`) == `..\bar`);
assertCTFEable!(
{
assert (relativePath(`c:\foo\bar`, `c:\foo\baz`) == `..\bar`);
});
}
else static assert (0);
}

View file

@ -2365,9 +2365,11 @@ unittest
assertThrown!FormatException(format("foo %s"));
assertThrown!FormatException(format("foo %s", 123, 456));
//Test CTFE-ability of format.
static assert(format("hel%slo%s%s%s", "world", -138, 'c', true) ==
"helworldlo-138ctrue", "[" ~ s ~ "]");
assertCTFEable!(
{
assert(format("hel%slo%s%s%s", "world", -138, 'c', true) ==
"helworldlo-138ctrue");
});
}

View file

@ -829,12 +829,12 @@ unittest
}
unittest
{
static assert({
// Bugzilla 10218
assertCTFEable!(
{
auto t = tuple(1);
t = tuple(2);
return true;
}());
t = tuple(2); // assignment
});
}
/**