-
Notifications
You must be signed in to change notification settings - Fork 28
#3315. Add tests for initializer list #3349
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
base: master
Are you sure you want to change the base?
Conversation
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.
Looks good, with a handful of comments.
|
||
class C1 extends A { | ||
int x; | ||
this: x = 1, super(-1); |
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.
It's an error to use this:
here unless there is a primary constructor. To make it an in-body declaring constructor it needs to have a formal parameter list, e.g., ()
.
enum E1 { | ||
e0(); | ||
final int x; | ||
this : x = 1; |
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.
Same issue as line 28. Also, const
must be specified explicitly because this is an in-body constructor (so it's treated similarly to other in-body constructors).
enum E4 { | ||
e0(); | ||
final int x; | ||
this() : x = 4; |
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.
const
enum E5 { | ||
e0(5); | ||
final int x; | ||
this(int x) : x = x; |
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.
const
|
||
class C1 extends A { | ||
int x; | ||
this: super(-1), x = 1; |
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()
.
/// `k2` has an initializer list with the same elements in the same order. | ||
/// | ||
/// @description Check that it is a compile-time error if a parameter of a | ||
/// declaring constructor is initialized both in the constructor and in the |
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 not sure I understand 'in the constructor'. We can't initialize the same instance variable using more than one of: an initializing formal, an initializer list element, an initializing expression in the instance variable declaration, or a declaring formal parameter. The latter can use names from the formal parameter list of a primary constructor, if any, but we're more interested in the multiple-initializations error, so it's fine to just have an initializing expression that doesn't use the special scoping of primary constructors (int x = 1;
will do just fine, both when there is a primary constructor and when there isn't any).
} | ||
|
||
class C4(super.z) extends A { | ||
this: z = 0; |
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 is different because z
is declared by A
and hence C4
does not initialize z
, that's done by the A
constructor which is executed as an implicit super(z)
.
So the error caused by z = 0
is that there is no such instance variable, not that we're initializing anything twice.
I'm trying to come up with a way to include this, but I suspect that C4
should just be omitted because it isn't testing anything about multiple initializations of instance variables.
} | ||
|
||
class C14 extends A { | ||
this(super.z) : z = 0; |
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 would be omitted, similarly to C4
.
In case of final variables there is an error that the variable is already initialized and this error may shadow the tested one. But I don't see another way how to test the initializer list behavior.