-
-
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
design flaw: using a struct as an interface and @fieldParentPtr can lead to bad pointer dereference #591
Comments
One possible solution:
|
Another possible solution:
We could disallow With this change, plus #287 (well-defined copy elision semantics), zig's "no hidden control flow" guarantee would even include no hidden |
Idea:
This comes close to promoting fieldParentPtr to full language feature, though. |
This bitten me so many times this weekend when I started to experiment with Zig! Few specific examples:
Perhaps I just need to practice more (just started to write in Zig), but it tricks me again and again in different forms. I started to recognize it and understand how to fix (all mentioned cases are fixed now), but it requires a lot of attention to not make such mistake. |
Thanks for your input @ul. I agree that this is a big flaw in the language. It's a top priority to fix in 0.4.0. |
I think it’s best to eliminate parentPointer as the preferred way to do interfaces because it’s hard to read, hard to write and inefficient/ not optimal because of pointer indirections which can be eliminated. |
Another issue with current interfaces is that you lose error set information. However, the current alternatives to this pattern aren't very good either. You can use
|
This is an important issue, but it's blocking on #287, so postponing it to 0.5.0. |
@dbandstra Your |
With copy elision part one complete is this issue unblocked? |
I believe this is solved by #7769 |
#7769 is an open proposal, not an implemented language feature, isn't it? Why did you close this issue? |
From a language design perspective, the issue is solved because #7769 is an accepted (planned) proposal, so there is not a design flaw but rather an implementation TODO. That's why @SpexGuy closed the issue. However, from a user perspective, this problem is still a footgun to look out for, and won't be solved until #7769 is actually implemented. For cases like this, I do prefer to have issues represent things from the user perspective. That means we leave it open until it's actually implemented, then close both of them. Regardless, @SpexGuy I do appreciate the organization/cleanup efforts you have been making on the issue tracker. |
Thank you for the clarification! |
Reported here: scurest/zimodre@8df208d#commitcomment-25444745
Any struct that uses
@fieldParentPtr
on an "interface" field relies on the API user not making a copy of the interface field.The text was updated successfully, but these errors were encountered: