-
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
How should unqualified lookup work for impls? #2377
Comments
Actually, I should note, https://github.com/carbon-language/carbon-lang/blob/trunk/docs/design/generics/details.md doesn't seem to define rules for out-of-line definitions of |
Note the example for case 0, copied out of #2287 is (updating class String {
impl as TotalOrder {
fn TotalLess[me: Self](right: Self) -> bool { ... }
}
fn LessHelper(v: i32, v: i32) -> bool
var v: i32;
}
fn (String as TotalOrder).TotalLess[me: Self](right: Self) -> bool {
// Does unqualified lookup into TotalOrder then String, finding
// String.LessHelper?
return LessHelper(me.v, right.v);
} |
I believe the current rule (in both the design and implemented in Explorer) is that in the scope of [ We currently limit to one forward declaration of an impl, if you ignore There are rules for forward declaring and then later defining Should we use the same rule for accessing private members as allowing unqualified lookup to find members of the class? It would expect that an external impl that is not mentioned in a class definition to not have access to any private members of the class, beyond friendship. I would expect that an impl (external or not) fully defined in the class' scope to have access to private members. I don't know how I feel about an impl declared in class scope but defined outside, but I would lean toward giving those access to private members. |
I think the rule that seems most appealing to me is that lookup for a name appearing in an out-of-line definition of a member of X (where X is a class, interface, impl, adapter, ...) always looks in the same places as for a name appearing in the definition of X. So:
but
Conversely:
|
@zygoloid I'm fine with your resolution, but I think I would prefer the 2nd example to work more like the 3rd. I'm worried that the distinction between those examples is too subtle and acknowledging |
Summary of issue:
Asked by @zygoloid at https://github.com/carbon-language/carbon-lang/pull/2287/files/47e08ca918ded47684369e64addf2c42f7e07095#r1015980990, filing on his behalf:
Details:
Internal impl defined in class (current example).
Internal impl declared in class and defined out of class.
If we want (0) to find
LessHelper
and (4) to not find it, where do we draw the line between them? (Another example that didn't fit on the above scale: an external impl that's nonetheless defined in the class.)A related example:
One possible general rule would be that a definition can see all the names that were in scope in any declaration (excluding those following the definition). Though that could be odd if there is more than one declaration of an entity:
Any other information that you want to share?
No response
The text was updated successfully, but these errors were encountered: