diff --git a/std/array.d b/std/array.d index c1c2e3b2a..de53aa848 100644 --- a/std/array.d +++ b/std/array.d @@ -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); + }); } /++ diff --git a/std/conv.d b/std/conv.d index 2d50ce1f9..fda09a42f 100644 --- a/std/conv.d +++ b/std/conv.d @@ -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 diff --git a/std/exception.d b/std/exception.d index 7cdf88dc2..6e96355c9 100644 --- a/std/exception.d +++ b/std/exception.d @@ -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(); +} diff --git a/std/format.d b/std/format.d index 977eccf46..bb7ddc203 100644 --- a/std/format.d +++ b/std/format.d @@ -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 diff --git a/std/numeric.d b/std/numeric.d index 5aca3f4b3..63bef31d1 100644 --- a/std/numeric.d +++ b/std/numeric.d @@ -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); }); } /** diff --git a/std/path.d b/std/path.d index a9bd74f32..113dffd8a 100644 --- a/std/path.d +++ b/std/path.d @@ -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); } diff --git a/std/string.d b/std/string.d index c1c7b1b95..e2cc0772c 100644 --- a/std/string.d +++ b/std/string.d @@ -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"); + }); } diff --git a/std/typecons.d b/std/typecons.d index 7b79d5f32..470a6df67 100644 --- a/std/typecons.d +++ b/std/typecons.d @@ -829,12 +829,12 @@ unittest } unittest { - static assert({ + // Bugzilla 10218 + assertCTFEable!( + { auto t = tuple(1); - t = tuple(2); - - return true; - }()); + t = tuple(2); // assignment + }); } /**