Skip to content
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

fix: records can have static initializers #4962

Merged
merged 2 commits into from
Oct 17, 2022

Conversation

SirYwell
Copy link
Collaborator

Fixes #4961

While instance initializers are forbidden, static initializers are allowed.

@SirYwell SirYwell changed the title wip: fix: records can have static initializers review: fix: records can have static initializers Oct 16, 2022
Comment on lines +211 to +217
void testBuildRecordModelWithStaticInitializer() {
// contract: a record can have static initializers
String code = "src/test/resources/records/WithStaticInitializer.java";
CtModel model = assertDoesNotThrow(() -> createModelFromPath(code));
List<CtAnonymousExecutable> execs = model.getElements(new TypeFilter<>(CtAnonymousExecutable.class));
assertThat(execs.size(), equalTo(2));
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe use ModelTest here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As building the model failed, we can't use ModelTest here

if (member instanceof CtAnonymousExecutable) {
JLSViolation.throwIfSyntaxErrorsAreNotIgnored(this, "Anonymous executable is not allowed in a record");
if (member instanceof CtAnonymousExecutable && !member.isStatic()) {
JLSViolation.throwIfSyntaxErrorsAreNotIgnored(this, "Instance initializer is not allowed in a record");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An oversight from myself earlier, we should add the JLS paragraph here. Could you add it?

@MartinWitt
Copy link
Collaborator

Impressive high speed at fixing issues, thank @SirYwell. Will merge later today/tomorrow after the 24-hour period. If I forgot to merge it, feel free to ping me.

@MartinWitt MartinWitt changed the title review: fix: records can have static initializers fix: records can have static initializers Oct 17, 2022
@MartinWitt MartinWitt merged commit 3f8c1b4 into INRIA:master Oct 17, 2022
@SirYwell SirYwell deleted the fix/static-initializers-in-records branch October 18, 2022 08:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: static initializers are forbidden in a record and throw an JLSViolation
2 participants