-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Proposal: Make ambiguous field/decl access in the presence of shadowing a compile error #7942
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
Comments
The plan here is to follow the example set by decl shadowing: fields and declarations are allowed to shadow; however access must always be unambiguous. So the example given above would be a compile error. |
As I explained above, this is already technically unambiguous if the (subtle) rules are understood. I assume your proposed change is to force field access to look unambiguous as well? |
So I brought this up in the stage2 meeting today and it looks like one option to make this appear less ambiguous would be to implement the following behavior: foo.bar(); // compile error
(foo.bar)(); // function pointer field call
Foo.bar(foo); // function decl call I still feel like this isn't perfect though as the function pointer field may be accessed without calling it, and requiring it to be wraped in parentheses in e.g. The only way I see to resolve this issue in a way I personally find satisfactory seems to be #6966 or similar. |
I personally believe that field/decl shadowing should eliminated from the language. I don't see shadowing here being either necessary or useful: it instead introduces ambiguity. |
I believe this is resolved as of #21231 |
Currently Zig allows decls and fields of the same struct to share names. This is confusing, especially if the field in question is a function pointer:
The current rule is that the field shadows the decl, but this is non-obvious at the call-site if the user intended to call
Foo.bar()
instead of the function pointer stored in the fieldbar
. It should be noted that this is technically not ambiguous as field always shadows the decl and the decl may be accessed withFoo.bar(&foo);
. Nonetheless, I propose to make this a compile error as I don't think the ability to use the same name for fields and decls is a beneficial feature that outweighs the confusion and potential for errors caused by this situation.See also: #705 #6966
The text was updated successfully, but these errors were encountered: