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
Change enum Desugarings #6154
Change enum Desugarings #6154
Conversation
Change the condition when type parameters of an enum are added to a class case. Quoting my comment on scala#6095: We have the following possibilities: 1. type parameters are added only of there is no extends clause, 2. (all) type parameters are added if one of them is referenced in the case, 3. type parameters are always added if the case has value parameters (i.e. is not translated to an object). (1) is what the current rules say. (2) is what Adriaan proposed. (3) is what is currently implemented. In addition we have to clarify what should happen if a type parameter that is not added is nevertheless referred to in the case. Since enum expansion is done during desugaring, this could silently rebind to some other type. We should come up with a solution that avoids this, if possible. This PR changes the rules to implement (2).
3bdc4a5
to
2d8db01
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.
👌
As discussed off-line: IIUC, the purpose of this new rule is to avoid the surprising scoping effect that referring to I think we should at least complement the rule by a similar one that reports an error if we do refer to such a That's still not completely enough, as I wonder whether that "not completely enough" is still "sufficiently complete" to avoid most confusing scenarios--and therefore worth having the new rule at all--or whether the non-completeness suggests that the new rule should not be added at all. |
OK, I added checks for detecting illegal parameter references, both if a type parameter list is given and also for value cases where no parameter list is given at all. |
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.
LGTM, although I'd like a test case highlighting what will happen in the following cases:
class T
enum Foo[T](val foo: Any) {
case Case1(x: Int) extends Foo(new T)
case Case2[U](x: U) extends Foo(new T)
case Case3 extends Foo(new T)
}
enum Bar[T] {
case Case1(x: Int) {
def foobar: T = new T
}
case Case2[U](x: U) {
def foobar: T = new T
}
case Case3 {
def foobar: T = new T
}
}
Perhaps something similar should be done for value references to parameters and fields? val test = 0
enum A(p: Int) {
val test = 1
case B extends A(test) // confusingly, refers to the outer 'test'
} Also, does the new test also check for type members? type Out = String
enum A[T] {
type Out = Int
case B extends A[Out] // confusingly, refers to the outer 'Out'
} |
@sjrd I added a (pos) test for the |
@LPTK Both of these generalizations would require a completely different - and much more complicated - compiler machinery than we have now. I don't think it's worth it, or at least not now. It could come later if someone wants to invest the effort and the result is not too complex. But I'm skeptical. |
@odersky wouldn't the check amount to making sure the following two sets do not intersect?
|
In the case where an enum case contains an extends clause whose type is not fully specified, we cannot generate a function type parent for the companion object. In fact, this business of generating function types as parents of case class companions is getting more and more fragile. We should maybe get out of it altogether.
The previous condition when we can and cannot generate a parent is quite obscure, so we should drop it altogether.
5bb73b1
to
d1edad6
Compare
@LPTK Yes, but how do you compute these sets? Given imports, inheritance, etc... |
Change the condition when type parameters of an enum are added to a class case.
Quoting my comment on #6095:
This PR changes the rules to implement (2).