Skip to content

@compileLog formats arrays of identical bytes as integers instead of text #22679

@rohlem

Description

@rohlem

Zig Version

0.14.0-dev.2987+183bb8b08

Steps to Reproduce and Observed Output

I've hit this several times when debugging, but never realized when exactly it happened.
Most byte arrays (and pointers to them) passed to compileLog are formatted as text strings.
Yet for some reason, if they specifically hold more than one byte, and all bytes hold the same value, they are instead output as u8 integers.

Repro:

comptime {
    @compileLog("\xFF"); //output as "\xff"
    @compileLog("\xFF\xFE"); //output as "\xff\xfe"
    @compileLog("\xFF\xFF"); //output as &.{ 255, 255 }
}

Output:

.zig:2:5: error: found compile log statement
    @compileLog("\xFF"); //output as "\xff"
    ^~~~~~~~~~~~~~~~~~~

Compile Log Output:
@as(*const [1:0]u8, "\xff")
@as(*const [2:0]u8, "\xff\xfe")
@as(*const [2:0]u8, &.{ 255, 255 })

This behavior is so specific that it seems intentional, yet I don't understand the intention behind it.
When I sometimes randomly end up with both @compileLog("??"); and @compileLog("!!"); in my code, it's very unhelpful of the compiler to output these symbols as their ASCII values.

Expected Output

I would expect the text formatting that is used for most byte arrays to also apply to byte arrays of a single repeated value.
It's not a huge deal, but I think this behavior should eventually be changed to be more uniform.

Metadata

Metadata

Assignees

No one assigned

    Labels

    error messageThis issue points out an error message that is unhelpful and should be improved.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions