[Issue 14883] static if does not work although an is expression produces 'true'

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Fri Aug 7 06:34:36 PDT 2015


https://issues.dlang.org/show_bug.cgi?id=14883

--- Comment #2 from Kenji Hara <k.hara.pg at gmail.com> ---
I agree the 2.068.0-rc1 behavior looks inconsistent. But I'm not sure the 2.067
behavior (reaching to the `static assert(false)` then fail to compile) is
really intentional/designed behavior.

---

Let's explain what's happened:

When the init property access `Vector.init` happens, it would try to resolve
all forward reference of field variable declarations inside the Vector struct.

During that, it would try to evaluate the static-if condition
`(is(typeof(foo(Vector.init))))`. Until the condition is actually resolved to
true, its then block is not reachable.

In 2.067, while the Vector.init analysis, compiler had gave up searching struct
fields before the static-if condition evaluation finished. By that, Vector is
fixed to no field struct, and the static-if condition
`is(typeof(foo(Vector.init)))` is evaluated to true. Finally, compiler had
reached to the static assert, then failed to compile.

In 2.068.0-rc1, the Vector.init analysis continues to evaluate the static-if
condition (because there's no special reason to give up the search), and it
fails by the "circular typeof definition" error - but the actual error message
is hidden by is(...). Then the condition is evaluated to false and compiler
won't reach to the static assert.

In both compiler versions, pragma(msg) is evaluated after the determination of
Vector struct fields and its instance size. At that time, the expression
Vector.init is always valid, then it will print 'true'.

---

To me the 2.068.0-rc1 behavior looks correct. In the test case code, the Vector
struct is defined depending on its field definition - it's circular definition.
Although it's sad that compiler reports nothing about the circle, the
suppression of error message is the intentional design of the `is(...)`
expression.

--


More information about the Digitalmars-d-bugs mailing list