Bug 14387 – Disallow string literals as assert conditions

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P4
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2015-04-01T12:34:44Z
Last change time
2024-01-26T16:25:42Z
Keywords
pull
Assigned to
No Owner
Creator
Marc Schütz

Comments

Comment #0 by schuetzm — 2015-04-01T12:34:44Z
void foo(int x) { assert("foo"); static assert("bar"); if(!isXValid(x)) assert("x has the invalid value of " ~ x.to!string); } These should be disallowed, because it's too easy to forget the `0` as first argument. Other expression that are concatenations of string literals should be kept, of course.
Comment #1 by clugdbug — 2015-04-02T08:40:13Z
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.
Comment #2 by dlang-bugzilla — 2015-04-05T03:49:31Z
(In reply to Don from comment #1) > 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. For non-static assert, D1 and D2 pre 2.023 say: test.d(4): Error: expression "test" of type invariant(char[4u]) does not have a boolean value From 2.023 changelog: "the type of a string literal is now invariant(char)[] rather than invariant(char)[length_of_string]. It is still implicitly convertible to the latter. This is intended to reduce template instantiation bloat." This is a case of dynamic arrays implicitly converting to bool whether they're null or non-null. So, this will automatically be "fixed" when implicit array-to-bool conversion is deprecated ( https://github.com/D-Programming-Language/dmd/pull/2885 ).
Comment #3 by nick — 2022-12-04T18:36:48Z
> this will automatically be "fixed" when implicit array-to-bool conversion is deprecated ( https://github.com/D-Programming-Language/dmd/pull/2885 ). Sadly that was reverted and Issue 4733 was marked as WONTFIX.
Comment #4 by dlang-bot — 2023-11-20T12:12:52Z
@ntrel created dlang/dmd pull request #15837 "Deprecate boolean evaluation of array/string literals" fixing this issue: - Deprecate boolean evaluation of array/string literals Fix Issue 14387 - Disallow string literals as assert conditions. https://github.com/dlang/dmd/pull/15837
Comment #5 by dlang-bot — 2023-11-24T12:13:18Z
@ntrel created dlang/dmd pull request #15860 "Deprecate string literals as (static) assert conditions" fixing this issue: - Deprecate string literals as assert conditions Fix Issue 14387 - Disallow string literals as assert conditions https://github.com/dlang/dmd/pull/15860
Comment #6 by dlang-bot — 2023-12-29T17:11:12Z
dlang/dmd pull request #15860 "Deprecate string literals as (static) assert conditions" was merged into master: - 55d0483a5d5ceef59327cde7cb12124d98547459 by Nick Treleaven: Deprecate string literals as assert conditions Fix Issue 14387 - Disallow string literals as assert conditions https://github.com/dlang/dmd/pull/15860
Comment #7 by nick — 2024-01-26T16:25:42Z
*** Issue 11080 has been marked as a duplicate of this issue. ***