Skip to content

Commit 4b21a89

Browse files
ntreldlang-bot
authored andcommitted
Fix Bugzilla 24876 - Undocumented cast from slice to static array
1 parent 560132e commit 4b21a89

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
@@ -1409,7 +1409,27 @@ $(H4 $(LNAME2 cast_array, Arrays))
14091409
$(UNDEFINED_BEHAVIOR Casting a non-literal array to `bool[]` when any
14101410
element has a byte representation $(DDSUBLINK spec/type, bool, other than 0 or 1).)
14111411

1412-
$(DDOC_SEE_ALSO $(RELATIVE_LINK2 cast_array_literal, Casting array literals).)
1412+
$(P $(B See also:) $(RELATIVE_LINK2 cast_array_literal, Casting array literals).)
1413+
1414+
$(P A slice of statically known length can be cast to a static array type when the
1415+
byte counts of their respective data match.)
1416+
1417+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
1418+
---
1419+
void f(int[] b)
1420+
{
1421+
char[4] a;
1422+
static assert(!__traits(compiles, a = cast(char[4]) b)); // unknown length
1423+
static assert(!__traits(compiles, a = cast(char[4]) b[0..2])); // too many bytes
1424+
1425+
a = cast(char[4]) b[0..1]; // OK
1426+
const i = 1;
1427+
a = cast(char[4]) b[i..2]; // OK
1428+
}
1429+
---
1430+
)
1431+
1432+
$(P $(B See also:) $(RELATIVE_LINK2 slice_to_static_array, Slice conversion to static array).)
14131433

14141434
$(H4 $(LNAME2 cast_static_array, Static Arrays))
14151435

0 commit comments

Comments
 (0)