Repository navigation
SONARJAVA-7287 Prepare the integration of PSI into the Java frontend - #6455
aurelien-coet-sonarsource wants to merge 3 commits into
Conversation
❌ ErrorsYour PR has failed checks. Please review the issues below and take necessary action before merging. 🚦 3 Pipeline jobs failed
Useful? React with 👍 / 👎 This comment will be updated automatically if new data arrives.🔗 Commit SHA: b3bebe7 | Docs | View more details | Give us feedback! |
| var type = variable.getTypeElement(); | ||
| if (type != null && type.getText().equals("var")) { | ||
| add(variable instanceof PsiParameter ? Feature.LAMBDA_VAR : Feature.LOCAL_VAR, type); | ||
| } |
There was a problem hiding this comment.
⚠️ Bug: Java 10 for (var x : xs) is wrongly rejected as fatal 'var' misuse
PsiLanguageFeatures.visitVariable tags every var-typed PsiParameter as Feature.LAMBDA_VAR (PsiLanguageFeatures.java:82). In IntelliJ PSI, the iteration variable of an enhanced for (PsiForeachStatement.getIterationParameter()) is also a PsiParameter, not a PsiLocalVariable, so it gets the same tag.
At source level 10, PsiLanguageValidator turns every LAMBDA_VAR into a Kind.FATAL "'var' is not allowed here" (PsiLanguageValidator.java:91-92). PsiDiagnosticProvider.language then throws a RecognitionException for the first FATAL diagnostic. JLS 10 (JEP 286) allows var in enhanced-for loops, so a valid Java 10 file such as for (var s : list) {} fails to parse. Only Java 11 added var for lambda parameters, which is the case this branch is meant to reject. The tests only cover the lambda case (PsiLanguageValidatorTest.java:72-73).
Fix: give LAMBDA_VAR only to parameters whose declaration scope is a PsiLambdaExpression, and treat foreach iteration parameters (and other non-lambda parameters) as LOCAL_VAR.
Only classify parameters declared by a lambda as LAMBDA_VAR.:
var type = variable.getTypeElement();
if (type != null && type.getText().equals("var")) {
boolean lambdaParameter = variable instanceof PsiParameter parameter
&& parameter.getDeclarationScope() instanceof com.intellij.psi.PsiLambdaExpression;
add(lambdaParameter ? Feature.LAMBDA_VAR : Feature.LOCAL_VAR, type);
}
Was this helpful? React with 👍 / 👎
3eec715 to
b3bebe7
Compare
CI failed: Two of the three CI jobs fail for reasons unclear to this PR: the license-maven-plugin flags PSI/JetBrains dependencies as forbidden, and java-frontend has three PSI classpath/runtime resolution test failures. The third template is not analyzed in the supplied findings.OverviewThree unique error templates were analyzed from 5 logs for commit b3bebe7. Two distinct failure patterns are identified: a license policy failure and PSI classpath test failures. Both are marked related_to_pr: unclear, so neither is confirmed as caused by this change. FailuresLicense check fails on forbidden licenses in PSI/JetBrains dependencies (confidence: high)
PSI classpath and Android runtime resolution tests fail (confidence: medium)
Summary
Code Review
|
| Auto-apply | Compact | Unblock |
|
|
|
Was this helpful? React with 👍 / 👎 | Gitar
Summary by Gitar
JavaPsiEnvironmentto open a standalone PSI project from explicit source snapshots, classpath, and JDK imageJavaPsiApplication,JrtVirtualFileSystem, andExplicitJavaFileManagerto load JDK modules and resolve sourcesPsiLanguageValidatorandPsiLanguageFeaturesto report source-level and fatal syntax errorsPsiDiagnosticProviderwith unused import, redundant cast, assignment, and masked catch checksPsiTokenIndexto map PSI tokens to original UTF-16 source positionsThis will update automatically on new commits.