-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
stage2 x86_64 struct self pointer passed badly into function? #13244
Comments
try - pub fn items(self: Self) []const f32 {
+ pub fn items(self: *const Self) []const f32 { |
as whereas |
Thanks for the quick explanation! |
Language proposals are currently disabled, so I'm writing this here to point to in upcoming pull requests and bug reports: Any member function that accepts the struct by-val as the first argument is a foot-gun: pub fn foo(self: Self) bar {
// is self a const pointer or a copy?
return self.internal_stuff.func(); // sometimes works, other times silently fails due to copied self
} You want this instead: pub fn foo(self: *const Self) bar {
// self is pointer (with or without const as needed)
return self.internal_stuff.func(); // always works
} I've made this mistake myself, seen it in zig libs, and now even many places in the zig std lib. I'll send PRs for the ones I've found. When language proposals open back up, I'll suggest making it a compile error to call any function with the |
You are still allowed to make proposals, especially ones aimed at removing footguns. The issue template is just meant to discourage drive-by proposals. |
Haha - it fooled me! Thanks! |
This fixes some rendering problems I encountered using zig stage2. See ziglang/zig#13244 which was me trying to figure out the bug. Also ziglang/zig#13249 which has more explanation.
This fixes some rendering problems I encountered using zig stage2. See ziglang/zig#13244 which was me trying to figure out the bug. Also ziglang/zig#13249 which has more explanation.
Zig Version
0.10.0-dev.4448+687a7d38a
Steps to Reproduce
Expected Behavior
Actual Behavior
Uncommenting the other debug prints shows that the self pointer passed into items() goes wrong, leading to the returned slice pointing to the wrong memory.
I'm trying to dig into it but it's my first time looking at zig compiler stuff. Any pointers would be very helpful. Thanks!
The text was updated successfully, but these errors were encountered: