Bug 15055 – isArray!NonArray doesn't short-circuit an expression

Status
RESOLVED
Resolution
WONTFIX
Severity
normal
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
x86_64
OS
Mac OS X
Creation time
2015-09-13T12:55:00Z
Last change time
2015-10-15T16:43:18Z
Assigned to
nobody
Creator
foldenyi.tamas

Comments

Comment #0 by foldenyi.tamas — 2015-09-13T12:55:28Z
I expect the following code run without an error: template sameUnqualArrays(A, B) { enum bool sameUnqualArrays = isArray!A && isArray!B && is(Unqual!(ForeachType!A) == Unqual!(ForeachType!B)); } unittest { static assert(sameUnqualArrays!(ubyte[], immutable(ubyte[]))); static assert(!sameUnqualArrays!(ubyte[], immutable(byte[]))); static assert(!sameUnqualArrays!(ubyte[], ubyte)); } But the last assert gives an error: /usr/include/dmd/phobos/std/traits.d(6062,9): Error: invalid foreach aggregate cast(ubyte)0u I expect that isArray short circuits the expression before the second half is evaluated. I am using DMD64 D Compiler v2.068.0
Comment #1 by k.hara.pg — 2015-09-13T15:18:45Z
This is a duplication of enhancement issue 9073. With static if and static assert, short circuit works for the logical expressions, like A && B - If A is evaluated to false at compile time, B won't be evaluated *even if B is an invalid expression*. On the other hand, an initializer expression on enum variable declaration does not work as so. With A && B, B is evaluated *always* and will make error if it's not correct. In old days, I've opened a PR to fix 9073, but was rejected by Walter. https://github.com/D-Programming-Language/dmd/pull/2559 To do what you expect, the template should be rewritten to: template sameUnqualArrays(A, B) { //enum bool sameUnqualArrays = isArray!A && isArray!B && // is(Unqual!(ForeachType!A) == Unqual!(ForeachType!B)); static if (isArray!A && isArray!B && is(Unqual!(ForeachType!A) == Unqual!(ForeachType!B))) { enum bool sameUnqualArrays = true; } else enum bool sameUnqualArrays = false; }
Comment #2 by dlang-bugzilla — 2015-10-15T16:43:18Z
I'm guessing this is WONTFIX if the PR was rejected.