Skip to content

Commit 72eadca

Browse files
dkorpeldlang-bot
authored andcommitted
Specify safe values for bool
1 parent 1a7db7b commit 72eadca

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

spec/declaration.dd

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -652,8 +652,9 @@ $(GNAME VoidInitializer):
652652
$(GLINK Initializer) or are set to the default value for the
653653
type of the variable. If the $(I Initializer) is $(D void),
654654
however, the variable is not initialized.
655-
Void initializers for objects that contain indirections is not
656-
allowed in `@safe` code.
655+
Void initializers for variables with a type that may contain
656+
$(DDSUBLINK spec/function, safe-values, unsafe values) (such as types with pointers)
657+
are not allowed in `@safe` code.
657658
)
658659

659660
$(IMPLEMENTATION_DEFINED If a void initialized variable's value is

spec/function.dd

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3966,7 +3966,9 @@ $(H3 $(LNAME2 safe-interfaces, Safe Interfaces))
39663966

39673967
$(H3 $(LNAME2 safe-values, Safe Values))
39683968

3969-
$(P For $(DDSUBLINK spec/type, basic-data-types, basic data types), all
3969+
$(P For a `bool`, only 0 and 1 are safe values.)
3970+
3971+
$(P For all other $(DDSUBLINK spec/type, basic-data-types, basic data types), all
39703972
possible bit patterns are safe.)
39713973

39723974
$(P A pointer is a safe value when it is one of:)
@@ -4006,6 +4008,8 @@ $(H3 $(LNAME2 safe-values, Safe Values))
40064008
---
40074009
int[] f() @system
40084010
{
4011+
bool b = true; /* b is initialized safe */
4012+
*(cast(ubyte*) &b) = 0xAA; /* b is now unsafe because it's not 0 or 1 */
40094013
int[3] a;
40104014
int[] d1 = a[0 .. 2]; /* d1 is safe. */
40114015
int[] d2 = a.ptr[0 .. 3]; /* d2 is unsafe because it goes beyond a's

0 commit comments

Comments
 (0)