[Issue 24355] Slice copy with static arrays incorrect bounds checking

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Jan 26 12:44:18 UTC 2024


https://issues.dlang.org/show_bug.cgi?id=24355

Nick Treleaven <nick at geany.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |nick at geany.org

--- Comment #3 from Nick Treleaven <nick at geany.org> ---
    char[32] id = 0;
    const(char)* str = "hello";
    id = str[0 .. 6];

The error is correct by the spec:
> A static array can be assigned from a dynamic array - the data is copied. The lengths must match
https://dlang.org/spec/arrays.html#assignment

    char[4] id;
    id = "hello asdad";

This causes a runtime error. You're right it could be caught at compile-time.
Assigning an array literal with excess elements is a compile error.

    char[32] id = "hello";

An array *initializer* is allowed to have fewer elements. If there are excess
elements, for an array literal it's a compile error, for a string literal, it's
a runtime error as above.

--


More information about the Digitalmars-d-bugs mailing list