Skip to content

Commit 868574a

Browse files
ntreldlang-bot
authored andcommitted
[spec/function] Improve safe cast rules
Add links. Casting pointer to pointer has the same rules as dynamic arrays. Fix wrong item about casting dynamic arrays - it's not the same as casting element types. Add detail instead. Document dlang/dmd#16622.
1 parent c4cc92c commit 868574a

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

spec/function.dd

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3795,14 +3795,24 @@ $(H3 $(LNAME2 safe-functions, Safe Functions))
37953795
$(UL
37963796
$(LI No casting from a pointer type `T` to any type `U` with pointers, except when:)
37973797
* `T` implicitly converts to `U`
3798-
* `U` implements class or interface `T`
3798+
* `U` $(DDSUBLINK spec/expression, cast_class, implements) class or interface
3799+
`T`, and both types are `extern(D)`
37993800
* `T.opCast!U` is `@safe`
3800-
* Both types $(DDSUBLINK spec/expression, cast_array, are dynamic arrays) and:
3801-
* Casting a source element to a target element type is `@safe`
3801+
* Both types $(DDSUBLINK spec/expression, cast_array, are dynamic arrays), or
3802+
both types $(DDSUBLINK spec/expression, cast_pointers, are raw pointers), and:
3803+
* The target element type is not a pointer type
3804+
* The target element type is not mutable when the source element type is a pointer type
3805+
* Either the target element type is a dynamic array, or the target element
3806+
type is no larger than the source element type
3807+
* Any source element type modifiers implicitly convert to the target element type modifiers
3808+
* Neither element type is a function type
38023809
* The target element type is not mutable when the source type is `void[]`
3803-
* The target type is not `bool[]` unless the operand is a literal
3810+
* The target element type is not $(DDSUBLINK spec/type, bool, `bool`)
3811+
* The source element type is not `bool` when the target element type is mutable
3812+
* Neither element type is $(DDSUBLINK spec/struct, opaque_struct_unions, opaque).
38043813
$(LI No casting from any non-pointer type to a pointer type.)
3805-
$(LI No pointer arithmetic (including pointer indexing & slicing).)
3814+
$(LI No $(DDSUBLINK spec/expression, pointer_arithmetic, pointer arithmetic)
3815+
(including pointer indexing & slicing).)
38063816
$(LI Cannot access union fields that:)
38073817
* Have pointers or references overlapping with other types
38083818
* Have invariants overlapping with other types

0 commit comments

Comments
 (0)