Skip to content

Unintuitive error message when mis-calling builtin functions #23108

@kiedtl

Description

@kiedtl

Zig Version

0.14.0-dev.3452+0367d684f

Steps to Reproduce and Observed Output

When passing an enum literal to a builtin function that takes a []const u8, the resulting error message assumes the enum literal is a decl literal.

const foo = struct {
    bar: []const u8,
};

pub fn baz(_: @FieldType(foo, .bar)) void {}

pub fn main() void {
    baz("hi");
}

yields:

test.zig:5:32: error: type '[]const u8' has no members
pub fn baz(_: @FieldType(foo, .bar)) void {}
                              ~^~~
test.zig:5:32: note: slice values have 'len' and 'ptr' members

Here, the problem is that @FieldType takes a comptime string, not an enum literal as std.meta.fieldInfo did.

Another example that yields the same error message:

const foo = struct { bar: usize };

pub fn main() void {
    _ = @field(foo, .bar);
}

Expected Output

The error can be simplified to some variation of Expected a comptime []const u8; found an enum literal.

If this is too difficult, a note can be added instead: @<builtin> takes a comptime []const u8, not an enum literal.

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

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions