Skip to content

Commit f794ea0

Browse files
ntrelBolpat
authored andcommitted
Fix Bugzilla 24876 - Undocumented cast from slice to static array
1 parent ae4345e commit f794ea0

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

spec/expression.dd

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1366,7 +1366,27 @@ $(H4 $(LNAME2 cast_array, Arrays))
13661366
$(UNDEFINED_BEHAVIOR Casting a non-literal array to `bool[]` when any
13671367
element has a byte representation $(DDSUBLINK spec/type, bool, other than 0 or 1).)
13681368

1369-
$(DDOC_SEE_ALSO $(RELATIVE_LINK2 cast_array_literal, Casting array literals).)
1369+
$(P $(B See also:) $(RELATIVE_LINK2 cast_array_literal, Casting array literals).)
1370+
1371+
$(P A slice of statically known length can be cast to a static array type when the
1372+
byte counts of their respective data match.)
1373+
1374+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
1375+
---
1376+
void f(int[] b)
1377+
{
1378+
char[4] a;
1379+
static assert(!__traits(compiles, a = cast(char[4]) b)); // unknown length
1380+
static assert(!__traits(compiles, a = cast(char[4]) b[0..2])); // too many bytes
1381+
1382+
a = cast(char[4]) b[0..1]; // OK
1383+
const i = 1;
1384+
a = cast(char[4]) b[i..2]; // OK
1385+
}
1386+
---
1387+
)
1388+
1389+
$(P $(B See also:) $(RELATIVE_LINK2 slice_to_static_array, Slice conversion to static array).)
13701390

13711391
$(H4 $(LNAME2 cast_static_array, Static Arrays))
13721392

0 commit comments

Comments
 (0)