[Issue 18002] New: assert subverts the type system with the messages that it accepts
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Tue Nov 21 19:19:41 UTC 2017
https://issues.dlang.org/show_bug.cgi?id=18002
Issue ID: 18002
Summary: assert subverts the type system with the messages that
it accepts
Product: D
Version: D2
Hardware: All
OS: All
Status: NEW
Severity: normal
Priority: P1
Component: dmd
Assignee: nobody at puremagic.com
Reporter: issues.dlang at jmdavisProg.com
This code should not compile but does
=====================
void main()
{
char[] message = "foo".dup;
assert(0, message);
}
=====================
When assert fails, it gets converted to an AssertError, which - like all
Throwables - takes a string for its message. So, the fact that assert accepts a
char[] rather than a string means that either it's iduping what it's given or
that it's implicitly converting char[] to immutable(char)[], which violates the
type system. And as this D.Learn post demonstrates:
http://forum.dlang.org/post/oiyngoqbjmmyscvlmpyj@forum.dlang.org
if you go and pass it a slice of a static array, it does _not_ copy the
contents. It's clearly slicing them, which means that it is implicitly
converting the char[] to immutable(char)[], thus violating the type system, and
in the case where you pass it a slice of a static array, you're then dealing
with an invalid pointer.
I don't know why assert accepts anything other than string for its message
(particularly given that AssertError requires a string, and the message needs
to be on the stack), but either it needs to be fixed so that it requires
string, and passing it a char[] is an error, or the implementation needs to
idup the message. I'm inclined to think that restricting it to string and
forcing the user to idup the char[] in the rare case that that's what's wanted
would be the better option, but either way, it needs to be fixed so that assert
doesn't subvert the type system.
--
More information about the Digitalmars-d-bugs
mailing list