[Issue 14387] Disallow string literals as assert conditions

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Thu Apr 2 01:40:13 PDT 2015


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

--- Comment #1 from Don <clugdbug at yahoo.com.au> ---
The spec says:

"As a debugging tool, the compiler may insert checks to verify that the
condition indeed holds by evaluating AssignExpression at runtime. If it
evaluates to a non-null class reference, the class invariant is run. Otherwise,
if it evaluates to a non-null pointer to a struct, the struct invariant is run.
Otherwise, if the result is false, an AssertError is thrown. If the result is
true, then no exception is thrown. In this way, if a bug in the code causes the
assertion to fail, execution is aborted, prompting the programmer to fix the
problem. "

This does not mention the case where the expression evaluates to a string
literal.

At the very least this is a bug in the spec. It seems that, if the expression
evaluates neither to a class reference, nor to a pointer to a struct, then the
expression is implictly converted to bool. But the spec doesn't say that.

IMHO it's also pretty weird that string literals are implicitly convertable to
bool. Note that they weren't convertable in D1:  assert("abc"); and if("abc")
both generated compile-time errors. I don't think that was an intentional
change, but I could be wrong.

--


More information about the Digitalmars-d-bugs mailing list