Skip to content

Commit 59d51b2

Browse files
authored
[spec/function] Improve @safe restrictions (#3845)
Define when casting a runtime array is safe. Pointer slicing isn't safe. Union operations on fields with safe types can be safe. Bool union field is not safe (https://issues.dlang.org/show_bug.cgi?id=24477). Assembler is OK if marked `@trusted`. Casting immutable is OK for basic data types. Casting immutable/shared can be OK if opCast defined. Bool void initialization is not safe (https://issues.dlang.org/show_bug.cgi?id=20148).
1 parent ab49eba commit 59d51b2

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

spec/function.dd

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3795,27 +3795,33 @@ $(H3 $(LNAME2 safe-functions, Safe Functions))
37953795
$(LI No casting from a pointer type `T` to any type `U` with pointers, except when:)
37963796
* `T` implicitly converts to `U`
37973797
* `U` implements class or interface `T`
3798-
* Both types are dynamic arrays
37993798
* `T.opCast!U` is `@safe`
3799+
* Both types $(DDSUBLINK spec/expression, cast_array, are dynamic arrays) and:
3800+
* Casting a source element to a target element type is `@safe`
3801+
* The target element type is not mutable when the source type is `void[]`
3802+
* The target type is not `bool[]` unless the operand is a literal
38003803
$(LI No casting from any non-pointer type to a pointer type.)
3801-
$(LI No pointer arithmetic (including pointer indexing).)
3802-
$(LI Cannot access unions that:)
3804+
$(LI No pointer arithmetic (including pointer indexing & slicing).)
3805+
$(LI Cannot access union fields that:)
38033806
* Have pointers or references overlapping with other types
3804-
* Have fields with invariants overlapping with other types
3807+
* Have invariants overlapping with other types
3808+
* Contain a $(DDSUBLINK spec/type, bool, `bool`)
38053809
$(LI Calling any $(RELATIVE_LINK2 system-functions, System Functions).)
38063810
$(LI No catching of exceptions that are not derived from
38073811
$(LINK2 https://dlang.org/phobos/object.html#.Exception, $(D class Exception)).)
3808-
$(LI No inline assembler.)
3809-
$(LI No explicit casting of:)
3810-
* mutable objects to immutable
3811-
* immutable objects to mutable
3812+
$(LI $(DDSUBLINK spec/iasm, asmstatements, Inline assembler) must be marked as
3813+
`@trusted`.)
3814+
$(LI No explicit casting (except with a matching `@safe` `opCast`) of:)
3815+
* mutable objects to immutable (except basic data types)
3816+
* immutable objects to mutable (except basic data types)
38123817
* thread local objects to shared
38133818
* shared objects to thread local
38143819
$(LI Cannot access $(DDSUBLINK spec/attribute, system-variables, `@system`)
38153820
or $(D __gshared) variables.)
3816-
$(LI Cannot use $(D void) initializers for:)
3817-
* Pointers/reference types or any type containing them
3818-
* Types that have invariants
3821+
$(LI Cannot use $(D void) initializers for types containing:)
3822+
* Pointers/reference types
3823+
* Types with invariants
3824+
* `bool`
38193825
)
38203826

38213827
$(NOTE When indexing or slicing an array, an out of bounds access

0 commit comments

Comments
 (0)