-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Enforce private
and protected
access modifiers for class member access
#4248
Enforce private
and protected
access modifiers for class member access
#4248
Conversation
I'm also not sure whether it's best to split the test file into multiple files for this feature. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm focusing for the moment on the way name lookup is done in DiagnoseInvalidMemberAccess
's get_member_info
versus Context::LookupQualifiedName
(called by LookupMemberNameInScope
).
Hash table lookup is typically going to be a little more expensive, because it's indirect. Sometimes it's unavoidable, but had you considered either implementing as part of LookupQualifiedName
, or making more direct use of its results in order to avoid extra lookup?
Note, Context::LookupQualifiedName
currently can't have multiple extended scopes, but it may end up having ambiguities which are non-ambiguous when accounting for access control.
That said, I think already the separate implementations create a risk of divergent name lookup results. In particular, how about a test such as:
base class A {
fn F();
}
base class B {
extend base: A;
private fn F() -> i32;
}
base class C {
extend base: B;
fn G[self: Self]() -> () { return self.F(); }
}
Here, F()
should resolve to A::F
and compile successfully. The difference in return types should help identify if DiagnoseInvalidMemberAccess
returns success while LookupQualifiedName
continues to return B::F
.
|
private/protected
access modifiers for member accessesprivate/protected
access modifiers for member access
So running through the tester, I get:
I assume it has something to do with |
To be sure, what I'm trying to say is that I think this kind of access control check probably belongs in
My suggestion, to offer a little detail would be:
There's a bit more to flesh out there, but to be sure, member_access.cpp may only need a change to indicate to LookupQualifiedName which kind of access enforcement it requires. Rather than doing separate enforcement, it would be integrated into the existing lookup code. Let me know if there's a little more I can clarify here, sorry it took some time to sort out my thoughts here. |
private/protected
access modifiers for member accessprivate/protected
access modifiers for member access
I think I have implemented all your suggestions. What do you think about printing a diagnostic for |
private/protected
access modifiers for member accessprivate/protected
access modifiers for class member access
private/protected
access modifiers for class member accessprivate/protected
access modifiers for class member access
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the changes! I have a lot of comments, but I want to be clear, I do think this is a big improvement. I just tend to add a lot of comments for style things (a chunk of these, for example, are just small style things in the test file that happen to stick out to me).
I want to highlight two that are more structural:
- Moving the diagnostic call to where
DiagnoseNameNotFound
is found.- comment here
- I think this will shift how you track skipped calls, and also may lead to more changes to DiagnoseInvalidAccess.
- Avoiding querying whether the current function is an instance function before determining what to do with it.
I'd suggest starting with these two comments, because they may still shift your approach a little.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NB, just a few comments since it looks like you're still addressing old comments, but I wanted to point out a couple small things.
private/protected
access modifiers for class member accessprivate
and protected
access modifiers for class member access
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Just a few last things.
Since this is almost ready to merge, can you also update against trunk? Looking at GitHub, there are a few files with merge conflicts.
2550479
to
b994331
Compare
b994331
to
6f4c362
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks basically ready to merge, but I see one string_view use got missed. Can you please fix the argument to ClassInvalidMemberAccess?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! This is great. I'll kick off a merge. :)
I just realized that #4248 should now correctly enforce compound member access. This change adds tests for this functionality.
Print diagnostics for invalid class member access. This doesn't take into account compound member access.