Skip to content

String literal shorter than n is quietly coerced into *const [n]u8 #22373

@boomlinde

Description

@boomlinde

Zig Version

0.14.0-dev.2577+271452d22

Steps to Reproduce and Observed Behavior

test "String literal shorter than n is quietly coerced into *const [n]u8" {
    const alphabet: *const [64]u8 = "A";
    @import("std").debug.print("{s}\n", .{alphabet});
}

This will produce output that contains the string literal followed by random string data presumably present after the literal in memory.

Expected Behavior

I expected and would prefer that this fails at compile time. It makes some sense to me that a longer string literal would coerce into a pointer to a shorter string (which is also the case), but not the other way around, especially not in a way that places some undefined data in the array.

Happy Gregorian new year!

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behaviorfrontendTokenization, parsing, AstGen, Sema, and Liveness.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions