-
Notifications
You must be signed in to change notification settings - Fork 117
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
Jep 445 grammar #1521
Conversation
8e5e4ac
to
5f093e9
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.
Thanks @mickaelistria - please find the code review comments attached.
org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/parser/Parser.java
Outdated
Show resolved
Hide resolved
} else if (astNode instanceof TypeDeclaration type) { | ||
if (!Arrays.equals(this.compilationUnit.getMainTypeName(), type.name)) { | ||
types.addFirst(type); | ||
} else { |
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 as per spec
public UnnamedClass(CompilationResult result) { | ||
super(result); | ||
this.modifiers = ClassFileConstants.AccDefault | ClassFileConstants.AccFinal; | ||
this.name = "<unnamed class>".toCharArray(); //$NON-NLS-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.
Instead of embedding the string in the code, it may be better(?) to put it in a static variable.
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.
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.
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.
org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/parser/Parser.java
Show resolved
Hide resolved
0cdeed9
to
cf67147
Compare
cf67147
to
20d43ed
Compare
fields.addFirst(field); | ||
} | ||
} | ||
if ((!methods.isEmpty() || !fields.isEmpty()) && problemReporter().validateJavaFeatureSupport(JavaFeature.UNNAMMED_CLASSES_AND_INSTANCE_MAIN_METHODS, 0, 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.
I think this should be !problemReporter().validateJavaFeatureSupport(...
, since it seems like validateJavaFeatureSupport
returns true
when a diagnostic is reported and false
otherwise.
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.
mpalat requested a review from srikanth-sankaran now
@srikanth-sankaran - adding you as a reviewer once you are done with String templates. thanks
20d43ed
to
db1446d
Compare
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 |
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 may not make sense to throw this error when compiling at a level below 21. Please handle this to throw an appropriate error
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 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.
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.
@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
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.
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.
} | ||
} | ||
"""; | ||
|
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.
Are these changes just changing to text block or anything more?
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.
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 { |
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.
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
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've addressed this
public static boolean optimizeStringLiterals = false; | ||
public static long sourceLevel = ClassFileConstants.JDK21; //$NON-NLS-1$ | ||
|
||
public JEP445UnnamedClassTest(String testName){ |
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.
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
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've addressed this
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.
Please have a look at the comments inline
db1446d
to
8643f8d
Compare
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. |
@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. |
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. |
6cf0445
to
9942a54
Compare
8d6fdac
to
1debc42
Compare
@srikanth-sankaran @mpalat got the tests passing |
@jarthana when you have time, could you please review this PR? |
@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 |
f2840be
to
dfda508
Compare
dfda508
to
b918e38
Compare
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>
b918e38
to
5265b65
Compare
Signed-off-by: David Thompson <davthomp@redhat.com>
@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. |
@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. |
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? |
Yes, this is correct |
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. |
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. |
@mpalat This one should be closed, right? |
@akurtakov : yes. Thanks @mickaelistria for closing |
What it does
How to test
Author checklist