Skip to content

Commit 3014d36

Browse files
committed
declaration.dd: clarify void initializations, @safe, and UB
1 parent 64ee618 commit 3014d36

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

spec/declaration.dd

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -608,14 +608,31 @@ $(GNAME VoidInitializer):
608608
used before it is set, undefined program behavior will result.
609609
)
610610

611-
$(UNDEFINED_BEHAVIOR If a void initialized variable's value is
612-
used before it is set, the behavior is undefined.
611+
$(IMPLEMENTATION_DEFINED If a void initialized variable's value is
612+
used before it is set, the value is implementation defined.
613613

614614
---
615+
import std.stdio;
615616
void foo()
616617
{
617618
int x = void;
618-
writeln(x); // will print garbage
619+
writeln(x); // will likely print garbage
620+
}
621+
---
622+
)
623+
624+
$(UNDEFINED_BEHAVIOR If a void initialized variable is a reference type
625+
and its value is dereferenced before it is set, the behavior is undefined.
626+
Such initializations are not allowed in `@safe` code.
627+
628+
---
629+
void foo()
630+
{
631+
int* p = void;
632+
*p = 3; // undefined behavior
633+
int x;
634+
p = &x;
635+
*p = 3; // ok
619636
}
620637
---
621638
)

0 commit comments

Comments
 (0)