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

Jep 445 grammar #1521

Closed
wants to merge 12 commits into from
Closed

Conversation

mickaelistria
Copy link
Contributor

What it does

How to test

Author checklist

Copy link
Contributor

@mpalat mpalat left a comment

Choose a reason for hiding this comment

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

Thanks @mickaelistria - please find the code review comments attached.

} else if (astNode instanceof TypeDeclaration type) {
if (!Arrays.equals(this.compilationUnit.getMainTypeName(), type.name)) {
types.addFirst(type);
} else {
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks good as per spec

org.eclipse.jdt.core.compiler.batch/grammar/java.g Outdated Show resolved Hide resolved
public UnnamedClass(CompilationResult result) {
super(result);
this.modifiers = ClassFileConstants.AccDefault | ClassFileConstants.AccFinal;
this.name = "<unnamed class>".toCharArray(); //$NON-NLS-1$
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of embedding the string in the code, it may be better(?) to put it in a static variable.

Copy link
Contributor

Choose a reason for hiding this comment

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

With this change, it seems running ecj as a jar on an unnamed class in a source file called MySourceFile.java will produce a file called <unnamed class>.class. However, javac will produce MySourceFile.class. According to the specification, it's okay that the class file is not named the same thing as the source file. However, you are also allowed to have multiple unnamed classes, kind of like how you can have multiple classes with public static void main(String... args). For instance, I can build a maven project from the command line with multiple unnamed classes. Currently, the output file for these classes would be the same file, which likely will cause issues when doing a full project build.

The easiest way to resolve this (IMO) would be to mimic javac's behaviour. Maybe this should be handled as a part of the code generation change, though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

At the moment, the codegen is not really supported in this PR. It was part of #1487 but Manoj asked for those bits to be removed. In #1487, there are tests checking that the right .class is generated

fields.addFirst(field);
}
}
if ((!methods.isEmpty() || !fields.isEmpty()) && problemReporter().validateJavaFeatureSupport(JavaFeature.UNNAMMED_CLASSES_AND_INSTANCE_MAIN_METHODS, 0, 0)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this should be !problemReporter().validateJavaFeatureSupport(..., since it seems like validateJavaFeatureSupport returns true when a diagnostic is reported and false otherwise.

Copy link
Contributor

Choose a reason for hiding this comment

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

mpalat requested a review from srikanth-sankaran now

@srikanth-sankaran - adding you as a reviewer once you are done with String templates. thanks

1. ERROR in X.java (at line 1)
void ___eval() {
^
The preview feature Unnamed Classes and Instance Main Methods is only available with source level 21 and above
Copy link
Contributor

Choose a reason for hiding this comment

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

It may not make sense to throw this error when compiling at a level below 21. Please handle this to throw an appropriate error

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It can make sense for the scenario of a user who's using Unnamed Classes and has not configured the project to Java 21 + early-access features. This problem is a way to remind them that maybe what they want is newer Java.

Copy link
Contributor

@mpalat mpalat Nov 13, 2023

Choose a reason for hiding this comment

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

@mickaelistria let's say I am compiling at 17 and if I get this error then does it make sense to throw an error with a java language feature much later? Please check

Copy link
Contributor

Choose a reason for hiding this comment

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

Only one error is reported if you try to create an unnamed class when the project is set to Java 17 (or any Java less than 21). A quick fix is presented which can update the Java version of the project and enable preview features.

}
}
""";

Copy link
Contributor

Choose a reason for hiding this comment

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

Are these changes just changing to text block or anything more?

Copy link
Contributor

Choose a reason for hiding this comment

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

The source code that's being tested has not changed, but the expected output has changed. Since the file contains methods that are not in any class body, the parser parses the code as an unnamed class, and the printed parsed class reflects this change

import org.eclipse.jdt.internal.compiler.problem.ProblemReporter;
import org.junit.Test;

public class JEP445UnnamedClassTest extends AbstractCompilerTest {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Please rename it withput JEP445 - you can mention it in the comments. You might have seen JEPxx earlier, but nowadays we follow this feature based naming since the JEPS change across different preview versions

Copy link
Contributor

Choose a reason for hiding this comment

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

I've addressed this

public static boolean optimizeStringLiterals = false;
public static long sourceLevel = ClassFileConstants.JDK21; //$NON-NLS-1$

public JEP445UnnamedClassTest(String testName){
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add
public static Class<?> testClass() {
return UnnammedClassTest.class;
}
public static Test suite() {
return buildMinimalComplianceTestSuite(testClass(), F_21);
}

refer other junit tests in 21 - say SwitchPatternTes/RecPatTes

Copy link
Contributor

Choose a reason for hiding this comment

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

I've addressed this

Copy link
Contributor

@mpalat mpalat left a comment

Choose a reason for hiding this comment

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

Please have a look at the comments inline

@srikanth-sankaran
Copy link
Contributor

Thanks for your patience. I am starting my review of this change set now. I will come back with comments and questions as I chew on this.

@akurtakov
Copy link
Contributor

@srikanth-sankaran As per #1517 (comment) - they guys considered the other PR in better shape than this one so it makes sense to review it first.

@srikanth-sankaran
Copy link
Contributor

@srikanth-sankaran As per #1517 (comment) - they guys considered the other PR in better shape than this one so it makes sense to review it first.

I'll make a high level pass over both and make a deeper dive into the one considered more ready. Just this morning I was having a conversation with other committers that early and more frequent agile style feedback may work better to avoid rewrites and expensive backtracks.

@datho7561
Copy link
Contributor

FYI, there is a new version of the preview feature for Java 22. Mickael told me about this. It changes the name "unnamed class" to "implicitly declared class", and later down in the proposal it says that you still can't reference these implicitly declared classes from other classes. It seems like implicitly declared classes might support some things that aren't allowed in the current unnamed classes preview feature, such as having static initializer blocks in the implicitly declared class, but I think it's best to see the final spec changes to understand what the difference is.

This PR will aim to support the Java 21 version of the preview feature.

@datho7561 datho7561 force-pushed the jep-445-grammar branch 3 times, most recently from 6cf0445 to 9942a54 Compare December 8, 2023 22:15
@datho7561 datho7561 force-pushed the jep-445-grammar branch 3 times, most recently from 8d6fdac to 1debc42 Compare December 13, 2023 18:25
@datho7561
Copy link
Contributor

@srikanth-sankaran @mpalat got the tests passing

@datho7561
Copy link
Contributor

@jarthana when you have time, could you please review this PR?

@datho7561
Copy link
Contributor

@srikanth-sankaran @mpalat @jarthana could you please take a look at this PR when you have a chance? I'll take a look into rebasing it today

@srikanth-sankaran
Copy link
Contributor

@srikanth-sankaran @mpalat @jarthana could you please take a look at this PR when you have a chance? I'll take a look into rebasing it today

Sorry, I have been too busy with some rewrite of pattern matching implementation, That is mostly done and I will look into this, I am trying to determine the best order of reviews given there are multiple PRs open

@srikanth-sankaran
Copy link
Contributor

With #1517 approved for merge by @jarthana and myself, we can start on this.
@mpalat, next week I can review the grammar changes - for other areas, I would request other reviewers. I have somewhat limited cycles

mickaelistria and others added 11 commits January 29, 2024 16:52
Updated some tests accordingly:
* Some DietRecoveryTests could be entirely removed as the case are now
parse-able; some other were totally changes.
Signed-off-by: David Thompson <davthomp@redhat.com>
Signed-off-by: David Thompson <davthomp@redhat.com>
Signed-off-by: David Thompson <davthomp@redhat.com>
Signed-off-by: David Thompson <davthomp@redhat.com>
- sometimes valid Java < 21 code needs to go through these rules
- we can manually generate the error if we detect an unnamed class in
  Java < 21

Signed-off-by: David Thompson <davthomp@redhat.com>
Signed-off-by: David Thompson <davthomp@redhat.com>
This should resolve some issues related to formatting

Signed-off-by: David Thompson <davthomp@redhat.com>
Signed-off-by: David Thompson <davthomp@redhat.com>
Signed-off-by: David Thompson <davthomp@redhat.com>
Signed-off-by: David Thompson <davthomp@redhat.com>
Signed-off-by: David Thompson <davthomp@redhat.com>
@mpalat
Copy link
Contributor

mpalat commented Feb 1, 2024

With #1517 approved for merge by @jarthana and myself, we can start on this. @mpalat, next week I can review the grammar changes - for other areas, I would request other reviewers. I have somewhat limited cycles

@srikanth-sankaran @datho7561 would you want me to review some changes here? I see a merge - so checking is there any other PR that addresses the rest of the phases (post parsing)?

@srikanth-sankaran
Copy link
Contributor

With #1517 approved for merge by @jarthana and myself, we can start on this. @mpalat, next week I can review the grammar changes - for other areas, I would request other reviewers. I have somewhat limited cycles

@srikanth-sankaran @datho7561 would you want me to review some changes here? I see a merge - so checking is there any other PR that addresses the rest of the phases (post parsing)?

I am asking someone (you?) to take the lead on driving this review to merge. I can review the grammar/parser changes. Reviews have to be scheduled in other areas.

@mpalat
Copy link
Contributor

mpalat commented Feb 2, 2024

@datho7561 - am assuming that you would not be continuing on this but on the Java 22 part - #1640 (comment). Can you please create a PR against that issue (or a subissue created under that -say for grammar changes) - thanks.

@datho7561
Copy link
Contributor

@datho7561 - am assuming that you would not be continuing on this but on the Java 22 part

This PR is for Java 21 preview support of unnamed classes, not Java 22 support

@mpalat
Copy link
Contributor

mpalat commented Feb 6, 2024

@datho7561 - am assuming that you would not be continuing on this but on the Java 22 part

This PR is for Java 21 preview support of unnamed classes, not Java 22 support

I understand that. Let me explain the query more.. Refer the discussions of #1106 - So you are planning to support the Java 21 version (here) and then the Java 22 version (1640) as well rather than choosing the Java 22 over Java 21. Is this understanding correct?

@datho7561
Copy link
Contributor

Is this understanding correct?

Yes, this is correct

@srikanth-sankaran
Copy link
Contributor

My recommendation is to leverage this work and bridge to the preview in 22. That would be a better path than releasing outdated preview mode.

@datho7561
Copy link
Contributor

I have to switch to other tasks now, feel free to continue work on this PR and add whatever is needed for Java 22. We've released this as a Java 21 preview feature in JDT-LS as part of our incubator.

Regardless, I still think this is worth adding as a Java 21 preview feature instead of a Java 22 preview feature.

The reason is we cannot claim to support Java 22 until lists of patterns in case statements is merged, since unnamed variables and patterns is a full feature in Java 22. However, lists of patterns in case statements is not yet merged, since Srikanth is working on refactoring the code generation for patterns. I will have to rebase my "lists of patterns in case statements" PR on top of those changes before it's ready for merge, since the changes I made for code generation will conflict with Srikanth's changes.

This PR (unnamed classes) is orthogonal to pattern code generation and unnamed variables and patterns, so I think we can merge this before the pattern code generation refactoring is complete. If this PR is merged first, we should list this as a Java 21 preview feature instead of a Java 22 preview feature, since we won't support Java 22 until the "lists of patterns in case statements" feature is in.

@akurtakov
Copy link
Contributor

@mpalat This one should be closed, right?

@mpalat
Copy link
Contributor

mpalat commented May 16, 2024

@mpalat This one should be closed, right?

@akurtakov : yes. Thanks @mickaelistria for closing

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.

None yet

5 participants