zig icon indicating copy to clipboard operation
zig copied to clipboard

compiler error improvement: assist if `foo.x` access doesn't exist, but a field/method/var `x` does exist on `foo`

Open emidoots opened this issue 3 years ago • 2 comments

Zig Version

0.10.0-dev.4476+0f0076666

Steps to Reproduce and Observed Behavior

    var arena = std.heap.ArenaAllocator.init(testing.allocator);
    defer arena.deinit();
    const allocator = arena.allocator;

This code produces an error:

/Users/slimsag/Desktop/hexops/mach-examples/libs/mach/libs/earcut/src/main.zig:740:29: error: no field named 'allocator' in struct 'heap.arena_allocator.ArenaAllocator'
    const allocator = arena.allocator;
                            ^~~~~~~~~

Expected Behavior

The Zig compiler should be smart enough to see the error I made, and suggest (maybe you meant to call .allocator(...) ?) since the fixed code is:

    var arena = std.heap.ArenaAllocator.init(testing.allocator);
    defer arena.deinit();
-   const allocator = arena.allocator;
+   const allocator = arena.allocator();

emidoots avatar Oct 30 '22 12:10 emidoots

while this could be helpful, i think the error should stay as is. an argument could be made that the suggestion could potentially be useful as a note, but its better to have errors avoid guessing what the user meant to do. particularly since fields and declarations occupy separate namespaces, the context this note provides starts to become context dependent and less helpful.

imo this note would be better suited as an observation made by 3rd party tooling, such as a dev's editor

nektro avatar Oct 31 '22 02:10 nektro

Re-opening because people are hitting into this in other cases: https://www.reddit.com/r/Zig/comments/146avyi/error_no_field_named_x_in_struct_y_despite_field/

Maybe it's not the compiler's job to tell users about this - but there should probably be some answer to this.

emidoots avatar Jun 11 '23 02:06 emidoots