Skip to content

Unclear error message when passing wrong type to allocator.free #14492

Description

@Jarred-Sumner

Zig Version

0.11.0-dev.1393+38eebf3c4

Steps to Reproduce and Observed Output

Pass a struct to std.mem.Allocator.free(allocator, my_struct);

You get:

  • an error message that doesn't clearly say what went wrong
  • no stack trace with a line number saying where it happened
/Users/jarred/zig/0.11.0-dev.1393+38eebf3c4/files/lib/std/mem/Allocator.zig:296:45: error: access of union field 'Pointer' while field 'Struct' is active
    const Slice = @typeInfo(@TypeOf(memory)).Pointer;
                  ~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
/Users/jarred/zig/0.11.0-dev.1393+38eebf3c4/files/lib/std/builtin.zig:201:18: note: union declared here
pub const Type = union(enum) {
                 ^~~~~
/Users/jarred/zig/0.11.0-dev.1393+38eebf3c4/files/lib/std/mem/Allocator.zig:296:45: error: access of union field 'Pointer' while field 'Struct' is active
    const Slice = @typeInfo(@TypeOf(memory)).Pointer;
                  ~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
/Users/jarred/zig/0.11.0-dev.1393+38eebf3c4/files/lib/std/builtin.zig:201:18: note: union declared here
pub const Type = union(enum) {
                 ^~~~~
/Users/jarred/zig/0.11.0-dev.1393+38eebf3c4/files/lib/std/mem/Allocator.zig:296:45: error: access of union field 'Pointer' while field 'Struct' is active
    const Slice = @typeInfo(@TypeOf(memory)).Pointer;
                  ~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
/Users/jarred/zig/0.11.0-dev.1393+38eebf3c4/files/lib/std/builtin.zig:201:18: note: union declared here
pub const Type = union(enum) {
                 ^~~~~

Expected Output

Something like this would be better:

/Users/jarred/zig/0.11.0-dev.1393+38eebf3c4/files/lib/std/mem/Allocator.zig:296:45: error: free() expects a slice but received struct `my.type.name`
@compilerError("error: free() expects a slice but received struct `" ++ @typeName(@TypeOf(value)) ++ "`");
^ 

allocator.free(my_struct);
^
./my-app.zig:201:18: note: passed here

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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