Skip to content

Commit 4decab6

Browse files
authored
[spec/expression] Reword runtime array cast (#3846)
The original wasn't clear how the runtime check is done.
1 parent 94ee3dd commit 4decab6

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

spec/expression.dd

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,10 +1332,18 @@ $(H4 $(LNAME2 cast_pointers, Pointers))
13321332

13331333
$(H4 $(LNAME2 cast_array, Arrays))
13341334

1335-
$(P Casting a dynamic array to another dynamic array is done only if the
1336-
array lengths multiplied by the element sizes match. The cast is done
1337-
as a type paint, with the array length adjusted to match any change in
1338-
element size. If there's not a match, a runtime error is generated.)
1335+
---
1336+
T[] a;
1337+
...
1338+
cast(U[]) a
1339+
---
1340+
$(P Casting a non-literal dynamic array `a` to another dynamic array type `U[]`
1341+
is allowed only when the result will contain every byte of data that was
1342+
referenced by `a`.
1343+
This is enforced with a runtime check that the byte length of `a`'s elements
1344+
is divisible by `U.sizeof`. If there is a remainder, a runtime error is generated.
1345+
The cast is done as a type paint, and the resulting array's length is set to
1346+
`(a.length * T.sizeof) / U.sizeof`.)
13391347

13401348
$(SPEC_RUNNABLE_EXAMPLE_RUN
13411349
---

0 commit comments

Comments
 (0)