Bug 10201 – "= void" initialization should not be allowed in @safe
Status
RESOLVED
Resolution
WONTFIX
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-05-29T08:23:00Z
Last change time
2016-06-07T17:18:56Z
Keywords
safe
Assigned to
nobody
Creator
public
Comments
Comment #0 by public — 2013-05-29T08:23:23Z
Compiles in 2.063:
------------
@safe void foo()
{
int x = void;
}
------------
Reporting because Andrei has clearly stated this is not intentional: http://forum.dlang.org/post/[email protected]
Comment #1 by public — 2013-05-29T08:42:06Z
Additional explanation - it does report an error if initialized value is a pointer or contains a pointer. It does not report an error if value has disabled constructor though.
So it is not that dangerous in that sense, but can be used to workaround some guarantees provided by disabled constructor. Prohibiting all void initalizers in @safe may be both more simple and consistent.
Comment #2 by monarchdodra — 2013-07-26T13:05:21Z
(In reply to comment #1)
> So it is not that dangerous in that sense, but can be used to workaround some
> guarantees provided by disabled constructor. Prohibiting all void initalizers
> in @safe may be both more simple and consistent.
It is dangerous in the sense that for a struct that is not POD, you are violating its internal integrity, at which point, it has no way to guarantee the safety of its own internal operations.
I mentioned this in https://github.com/D-Programming-Language/phobos/pull/1434#issuecomment-21644766 with the example:
//----
struct S
{
private size_t index;
private ubyte[1] arr;
ref ubyte get() @trusted
{
return arr.ptr[index];
}
}
void main() @safe
{
S s = void;
ubyte a = s.get();
}
//----
Comment #3 by yebblies — 2013-11-21T07:27:23Z
As noted in the github discussion, this is working as designed. Unless you can break memory safety without using @system features, it's an enhancement.
http://forum.dlang.org/post/[email protected]
I'm going to mark this as "wontfix" because it is not the charter of @safe to guarantee ints have a predictable value. The charter is memory safety. Trying to heap other things into it is a never-ending thing because there's no definition for it.
Comment #6 by public — 2016-06-07T10:21:13Z
(In reply to Walter Bright from comment #4)
> (In reply to monarchdodra from comment #2)
> > I mentioned this in
> > https://github.com/D-Programming-Language/phobos/pull/1434#issuecomment-
> > 21644766 with the example:
>
> The example is still @safe because the array bounds checking will not allow
> an out of bounds index.
There is no array bounds checking in that example as `arr.ptr` syntax is used.
Comment #7 by public — 2016-06-07T10:24:17Z
I do agree with closing it for now though, as the issue is unlikely to cause any trouble in real code and thus is not worth major attention.
Comment #8 by bugzilla — 2016-06-07T17:18:56Z
(In reply to Dicebot from comment #6)
> There is no array bounds checking in that example as `arr.ptr` syntax is
> used.
You're right.
But it's marked @trusted anyway, which defeats @safe checking, because pointer indexing is not allowed in @safe code.