Skip to content

Commit 856daab

Browse files
authored
Merge pull request #2990 from WalterBright/safeInvariant
add @safe restrictions on types with invariants
2 parents dbd2c0e + 20397b0 commit 856daab

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

spec/declaration.dd

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -467,14 +467,27 @@ $(GNAME VoidInitializer):
467467
used before it is set, undefined program behavior will result.
468468
)
469469

470-
$(UNDEFINED_BEHAVIOR If a void initialized variable's value is
471-
used before it is set, the behavior is undefined.
470+
$(IMPLEMENTATION_DEFINED If a void initialized variable's value is
471+
used before it is set, its value is implementation defined.
472472

473473
---
474-
void foo()
474+
void bad()
475475
{
476476
int x = void;
477-
writeln(x); // will print garbage
477+
writeln(x); // print implementation defined value
478+
}
479+
---
480+
)
481+
482+
$(UNDEFINED_BEHAVIOR If a void initialized variable's value is
483+
used before it is set, and the value is a reference, pointer or an instance
484+
of a struct with an invariant, the behavior is undefined.
485+
486+
---
487+
void muchWorse()
488+
{
489+
char[] p = void;
490+
writeln(p); // may result in apocalypse
478491
}
479492
---
480493
)

spec/function.dd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3233,6 +3233,8 @@ $(H3 $(LNAME2 safe-functions, Safe Functions))
32333233
$(LI No pointer arithmetic (including pointer indexing).)
32343234
$(LI Cannot access unions that have pointers or references overlapping
32353235
with other types.)
3236+
$(LI Cannot access unions that have fields with invariants overlapping
3237+
with other types.)
32363238
$(LI Calling any $(RELATIVE_LINK2 system-functions, System Functions).)
32373239
$(LI No catching of exceptions that are not derived from
32383240
$(LINK2 https://dlang.org/phobos/object.html#.Exception, $(D class Exception)).)
@@ -3244,6 +3246,7 @@ $(H3 $(LNAME2 safe-functions, Safe Functions))
32443246
$(LI Cannot access $(D __gshared) variables.)
32453247
$(LI Cannot use $(D void) initializers for pointers.)
32463248
$(LI Cannot use $(D void) initializers for class or interface references.)
3249+
$(LI Cannot use $(D void) initializers for types that have invariants.)
32473250
)
32483251

32493252
$(P When indexing or slicing an array, an out of bounds access

0 commit comments

Comments
 (0)