-
Notifications
You must be signed in to change notification settings - Fork 137
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
Syntax error on token "RestrictedIdentifierWhen" #456
Comments
This looks like a duplicate of #441. Are you in the position to re-test with a recent integration build? |
I had the same error described in #441 (a method named Didn't try to run anything yet, but compilation works at least. |
Thanks, @nlisker , for reporting success. @JanMosigItemis could you confirm that current integration builds fix the issue also for you? FYI, you'll find those builds of Eclipse SDK on https://download.eclipse.org/eclipse/downloads/index.html |
Thx for working on this @nlisker @stephan-herrmann, I'll try to confirm things on friday if you don't mind me being a bit late. |
@stephan-herrmann I can only confirm a partial win here. When playing around with my example project, I found:
Tested with 2022-12 | 4.26 | I20221027-1800 |
Hi @stephan-herrmann, I confirm this is still an issue part of the previous release To reproduce the problem, you can add the following unit tests in:
public void testDisambiguatedRestrictedIdentifierWhenAsFirstMethodInvokation() {
runConformTest(
new String[] {
"X.java",
"public class X {\n"
+ " public static void main(String argv[]) {\n"
+ " when(\"Pass\");\n"
+ " }\n"
+ " static void when(String arg) {\n"
+ " System.out.println(arg);\n"
+ " }\n"
+ "}"
},
"Pass");
}
public void testDisambiguatedRestrictedIdentifierWhenAsFirstVariableDeclaration() {
runConformTest(
new String[] {
"when.java",
"public class when {\n"
+ " public static void main(String argv[]) {\n"
+ " when x = new when();\n"
+ " System.out.println(x);\n"
+ " }\n"
+ " public String toString() {\n"
+ " return \"Pass\";\n"
+ " }\n"
+ "}"
},
"Pass");
}
public void testDisambiguatedRestrictedIdentifierWhenAsTypeInACase() {
runConformTest(
new String[] {
"when.java",
"public class when {\n"
+ " public String toString() {\n"
+ " return switch((Object) this) {\n"
+ " case when x -> \"Pass\";\n"
+ " default -> \"Fail\";\n"
+ " };\n"
+ " }\n"
+ " public static void main(String argv[]) {\n"
+ " System.out.println(new when());\n"
+ " }\n"
+ "}"
},
"Pass");
}
public void testDisambiguatedRestrictedIdentifierWhenAfterAParenthesis() {
runConformTest(
new String[] {
"X.java",
"public class X {\n"
+ " public static void main(String argv[]) {\n"
+ " System.out.println( (Boolean) when(true) );\n"
+ " }\n"
+ " static Object when(Object arg) {\n"
+ " return arg;\n"
+ " }\n"
+ "}"
},
"true");
}
public void testValidCodeWithVeryAmbiguousUsageOfWhen() {
runConformTest(
new String[] {
"when.java",
"class when {\n"
+ " boolean when = true;\n"
+ " static boolean when(when arg) {\n"
+ " return switch(arg) {\n"
+ " case when when when when.when && when.when(null) -> when.when;\n"
+ " case null -> true;\n"
+ " default -> false;\n"
+ " };\n"
+ " }\n"
+ " public static void main(String[] args) {\n"
+ " System.out.println(when(new when()));\n"
+ " }\n"
+ "}"
},
"true");
}
I did some investigations and my understanding is:
I don't know if removing the |
I will have a look at it again as well, I have to admit I had a bad feeling about the not a token token but not enough experience / insight to back it up. |
Hi @MaisiKoleni, I am speaking on behalf of Sonar's Java analyzer team.
|
I am not a regular contributor (made about 2 PRs so far), so I cannot say anything about the timeline or releases. I will open a PR thought that fixes the not a token token problem. |
I guess this is important because the pattern matching will exit preview with 21, right? So our fix should be geared towards Java 21 and not Java 20. Sadly, I couldn't find a preview build of the JLS for 21, but the JEP should contain the most important changes as well. In particular, JEP 441 mentions:
However, record patterns exit preview with 21 as well, and those require parentheses, too. So in that regard, the logic could stay mostly the same (apart from removing the The important bits for us:
The local variable declaration is restricted as follows:
Which results in the following grammar: LocalVariableDeclaration:
As a consequence, looking back from when permits ...
We have two look back steps available, so the question now is, if it solves the ambiguity problems:
It does not look much better in the other direction because So either: we remember that we started a switch |
I noticed that we often create a fork of the Scanner for the What I will definitely do first now is removing the not-a-token token and enhance the look back usage. This will still have problems with the cast and similar constructs. I think it is most important that the common case with Mockito when at the start of a method body gets solved first. And a problematic cast before that is very unlikely, as the method and the downstream chain return the generic type Still, we should find a proper solution to the problem that aligns with the JLS. |
I think I also found and solved a more general bug related to the look back. There might have been many different edge cases where it didn't work properly (or we just had a lot of luck so far). |
@srikanth-sankaran are you following this issue? Mentions of |
@MaisiKoleni Where are you with this ? I can take over if you wish. I was the original author of VanguardParser and VanguardScanner although I will state I don't understand why they are getting involved here :) |
Open heart surgery in progress here: #1428 - It will take a while for the dust to settle. Too late for M1 |
@stephan-herrmann - heads up, I will ask for a review after the proposed implementation gets cleaned up. |
A lot less ambitious effort here: #1436 @alban-auzeill Many thanks for those test snippets - I have included them in the PR, |
Consult the parser's state to disambiguate identifier "when" from restricted key word "when" * Fixes #609 * Fixes #456 * Fixes eclipse-jdt/eclipse.jdt#59
I too encountered this exact problem with static importing I am trying to figure out how I can get this fix in my Eclipse installation, which currently is latest stable 2023-09 (4.29) (Build id: I20230903-1000), and with the Java 21 patch 1.2.300.v20230920-0334_BETA_JAVA21. Must I upgrade to an integration build of JDT, or will it eventually be part of the Java 21 support patch? |
Same problem as @runeflobakk here. I can confirm that the integration build seems to work, but is cumbersome to install. Would really appreciate an update to the stable version, 2023-09. |
@jarthana @mpalat What is your recommendation for @runeflobakk and @post-svejk - TIA |
I updated Eclipse Java Development Tools using the update site for 4.30 integration builds I am currently at version 3.19.300.v20231024-0830, which fixes this error, and everything else seems to work as it should 👍 |
…ipse-jdt#1436) Consult the parser's state to disambiguate identifier "when" from restricted key word "when" * Fixes eclipse-jdt#609 * Fixes eclipse-jdt#456 * Fixes eclipse-jdt/eclipse.jdt#59
Hi there,
I am working on projects that use the new java.lang.foreign Preview API. However the other day I encountered an issue wrt. Mockito's when method. One of my tests stopped compiling, when I migrated the project from JDK17 to JDK19.
Although static imports are there and other code in the same java file works, one call is marked red in the UI and the error tool tip says "Syntax error on token "RestrictedIdentifierWhen", e. g.:
The project compiles just fine with maven on the command line.
To make reproducing the problem easier, I provided a repo with a demo project: https://github.com/JanMosigItemis/eclipse.jdk19.when.bug
Not sure if this is a JDT Core issue but I figured, I should give it a try. Seems like some collision on reserved symbols maybe in conjunction with
--enable-native-access=ALL-UNNAMED --enable-preview
.Any hints would be appreciated.
System:
The text was updated successfully, but these errors were encountered: