Bug 17623 – Unexpected failure of an assertion on empty strings

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dlang.org
Product
D
Version
D2
Platform
All
OS
All
Creation time
2017-07-08T17:10:56Z
Last change time
2022-07-20T10:38:30Z
Keywords
pull, spec
Assigned to
No Owner
Creator
kdevel

Comments

Comment #0 by kdevel — 2017-07-08T17:10:56Z
According to the documentation [1]: "For static and dynamic arrays, identity is defined as referring to the same array elements and the same number of elements." this code $ cat stringidentity.d void main () { string s = null; string t = ""; assert (s is t); } shall pass but the assertion fails $ ./stringidentity [email protected](5): Assertion failure [1] https://dlang.org/spec/expression.html#identity_expressions
Comment #1 by dlang-bugzilla — 2017-07-08T19:04:47Z
This is a documentation bug; the code is correct. A dynamic array in D is represented as a pointer (.ptr) and a length (.length). > string s = null; s.length is 0 and s.ptr is null. > string t = ""; t.length is also 0, but t.ptr points towards a zero-length string literal. The distinction is mainly that D string literals are zero-terminated, so that they can be implicitly converted to a C string (const char*).
Comment #2 by kdevel — 2021-04-25T11:30:00Z
(In reply to Vladimir Panteleev from comment #1) > The distinction is mainly that D string literals are zero-terminated, so > that they can be implicitly converted to a C string (const char*). The nonidentity of null and "" arises from null not having a NUL "at the end"?
Comment #3 by razvan.nitu1305 — 2022-07-15T11:02:24Z
The documentation is not wrong. To me this looks like quirk involving dynamic arrays and string literals. Although it is not specifically spelled out, the spec [1] does document string literals as being 0 terminated. So I think that this issue is invalid. [1] https://dlang.org/spec/arrays.html#printf
Comment #4 by kdevel — 2022-07-15T20:57:24Z
(In reply to RazvanN from comment #3) > The documentation is not wrong. That is an untrue statement. The documentation /is/ wrong [1]. Both s and t in string s = null; string t = ""; refer to the same elements and have the same number of elements. The documentation must be corrected. A possible replacement for the original statement For static and dynamic arrays, identity is defined as referring to the same array elements and the same number of elements. is For static and dynamic arrays, identity of two arrays is given when both arrays refer to the same memory location and contain the same number of elements. [1] http://forum.dlang.org/post/[email protected]
Comment #5 by dlang-bot — 2022-07-20T09:55:49Z
@RazvanN7 created dlang/dlang.org pull request #3345 "Fix Issue 17623 - Unexpected failure of an assertion on empty strings" fixing this issue: - Fix Issue 17623 - Unexpected failure of an assertion on empty strings https://github.com/dlang/dlang.org/pull/3345
Comment #6 by dlang-bot — 2022-07-20T10:38:30Z
dlang/dlang.org pull request #3345 "Fix Issue 17623 - Unexpected failure of an assertion on empty strings" was merged into master: - 7c45b3aae88e23c10ed2a292e70d85fd3db0221a by Razvan Nitu: Fix Issue 17623 - Unexpected failure of an assertion on empty strings https://github.com/dlang/dlang.org/pull/3345