Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bad error message for method call on double pointer #19963

Open
digitcrusher opened this issue May 13, 2024 · 5 comments
Open

Bad error message for method call on double pointer #19963

digitcrusher opened this issue May 13, 2024 · 5 comments
Labels
error message This issue points out an error message that is unhelpful and should be improved.
Milestone

Comments

@digitcrusher
Copy link

Zig Version

0.13.0-dev.75+5c9eb4081

Steps to Reproduce and Observed Behavior

const std = @import("std");

const Foo = struct {
  fn bar(self: *Foo) void {
    std.debug.print("{*}\n", .{self});
  }
};

pub fn main() !void {
  var gpa = std.heap.GeneralPurposeAllocator(.{}){};
  var map = std.AutoHashMap(*Foo, void).init(gpa.allocator());
  var key = Foo{};
  try map.put(&key, {});

  map.getKey(&key).?.*.bar(); // Does work
  map.getKey(&key).?.bar(); // Does work
  var it = map.keyIterator();
  while(it.next()) |foo| foo.*.bar(); // Does work
  while(it.next()) |foo| foo.bar(); // Doesn't work
}

…produces the following error:

code.zig:19:29: error: no field or member function named 'bar' in '*code.Foo'
  while(it.next()) |foo| foo.bar(); // Doesn't work
                         ~~~^~~~

Expected Behavior

The code already says it all.

@digitcrusher digitcrusher added the bug Observed behavior contradicts documented or intended behavior label May 13, 2024
@xdBronch
Copy link
Contributor

this isnt a bug, the iterator returns pointers to the keys and your keys are *Foo so each foo is a double pointer, **Foo. field access and the like only go through 1 layer of pointer

@digitcrusher
Copy link
Author

Oh, I see. In that case, the compiler error message has misled me…

@xdBronch
Copy link
Contributor

yea that message could definitely be improved

@digitcrusher
Copy link
Author

Then I suppose this issue should be assigned the "error message" label or closed if duplicate?

@Vexu Vexu added error message This issue points out an error message that is unhelpful and should be improved. and removed bug Observed behavior contradicts documented or intended behavior labels May 19, 2024
@Vexu Vexu changed the title Cannot directly access method of pointer to struct from HashMap's iterator Bad error message for method call on double pointer May 19, 2024
@Vexu
Copy link
Member

Vexu commented May 19, 2024

Reduced example:

const S = struct { a: u32 };
pub export fn entry(a: **S) void {
    // _ = a.b(); // error: no field or member function named 'b' in '*a.S'
    _ = a.b; // error: type '**a.S' does not support field access
}

@Vexu Vexu added this to the 0.13.0 milestone May 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
error message This issue points out an error message that is unhelpful and should be improved.
Projects
None yet
Development

No branches or pull requests

3 participants