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 some yield expressions not recognized #3714

Merged
merged 2 commits into from
Oct 6, 2022

Conversation

PPazderski
Copy link

Existing parser fails to recognize some yield expressions. Especially yielding the result of a method call or the result of a conditional expression.
E.g. the following snippet currently fails:

int x = switch (5) {
    default -> {
        yield getX();
    }
};

The problem I found is that yield is not a keyword but just a restricted identifier or since Java 17 called contextual keyword. Javaparser currently tries to parse the statement as VariableDeclarationExpression.

This pull request is a minimal change to fix the problem of yield statements not recognized. For a better solution I think javaparser would need to better different between Identifier and TypeIdentifier according to https://docs.oracle.com/javase/specs/jls/se17/html/jls-3.html#jls-3.8
Then the VariableDeclarationExpression would start with a type which cannot be the name "yield".
If a class could be named "yield" a statement like yield x = null; is ambiguous and could be a local variable declaration plus initialization or a yield statement whose expression is an assignment.
But I expect this to be a notably larger and riskier change.

PS: if someone wants to try the SwitchExprTest.yieldYield example, the following code compiles:

public class Main {

    public enum X {
        yield,
    }

    public static void main(String[] args) {
        X yield = X.yield;
        yield = switch (yield) {
        default -> {
            yield yield = yield;
        }
        case yield -> {
            yield yield == yield ? yield : yield;
        }
        };
    }
}

Paul Pazderski and others added 2 commits September 29, 2022 21:11
Existing parser failed to recognize some yield expressions.
Especially yielding the result of a method call or the result
of a conditional expression.
@jlerbsc jlerbsc merged commit b9a7d0f into javaparser:master Oct 6, 2022
@jlerbsc
Copy link
Collaborator

jlerbsc commented Oct 6, 2022

Thank you for your contribution.

@jlerbsc jlerbsc added this to the next release milestone Oct 6, 2022
@jlerbsc jlerbsc added the PR: Fixed A PR that offers a fix or correction label Oct 6, 2022
@PPazderski
Copy link
Author

Thank you for merging my pull requests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR: Fixed A PR that offers a fix or correction
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants