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: separate binary expression from pattern #606

Conversation

jtkiesel
Copy link
Contributor

@jtkiesel jtkiesel commented Sep 17, 2023

What changed with this PR:

Rather than treating the optional binary expression following a pattern as part of the pattern itself, treat the entire expression as a binary expression, of which the pattern is a part, so that the usual binary expression formatting applies.

Example

Input

class Example {

  public boolean test(final Object obj) {
    return obj instanceof final Integer x && (x == 5 || x == 6 || x == 7 || x == 8 || x == 9 || x == 10 || x == 11);
  }
}

Output

class Example {

  public boolean test(final Object obj) {
    return (
      obj instanceof final Integer x &&
      (x == 5 || x == 6 || x == 7 || x == 8 || x == 9 || x == 10 || x == 11)
    );
  }
}

Relative issues or prs:

Closes #605

@jtkiesel jtkiesel force-pushed the fix/separate-binary-expression-from-pattern branch from ec0e028 to f9c8f41 Compare September 18, 2023 00:06
@jtkiesel jtkiesel marked this pull request as draft September 18, 2023 00:31
@jtkiesel jtkiesel force-pushed the fix/separate-binary-expression-from-pattern branch 2 times, most recently from 1625496 to df2dd83 Compare September 18, 2023 02:25
@jtkiesel jtkiesel marked this pull request as ready for review September 18, 2023 02:33
@jtkiesel jtkiesel force-pushed the fix/separate-binary-expression-from-pattern branch from df2dd83 to 3806135 Compare September 18, 2023 04:00
@@ -3498,8 +3498,6 @@ export interface PatternCstNode extends CstNode {

export type PatternCtx = {
primaryPattern: PrimaryPatternCstNode[];
AndAnd?: IToken[];
Copy link
Contributor

Choose a reason for hiding this comment

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

I am not a fan of modifying the CST in order to make it easier to print the code afterwards, as it makes the java parser maintenance much more difficult for each deviation from the spec

Copy link
Contributor Author

@jtkiesel jtkiesel Sep 19, 2023

Choose a reason for hiding this comment

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

@clementdessoude I looked into the spec while implementing this solution, because I had similar concerns. What I found was that a Pattern is a TypePattern, which is a LocalVariableDeclaration, meaning that according to the spec, a Pattern should not contain AndAnd or a binaryExpression within it. I made this change because it seemed to make the parser more aligned with the spec.

All that being said, this solution is occasionally failing tests because it seems to have slowed down parsing, so I'll need to find a fix for that. But I do think this change to the CST is appropriate, unless I'm missing something.

Copy link
Contributor Author

@jtkiesel jtkiesel Sep 19, 2023

Choose a reason for hiding this comment

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

It seems that the && style of the switch label "guarded" pattern is no more, and has been replaced with when in Java 21 (the first version that supports switch label pattern matching outside of a preview feature), which is scheduled to release tomorrow. So I will likely need to rethink this solution regardless. https://docs.oracle.com/javase/specs/jls/se21/html/jls-14.html#jls-SwitchLabel

@jtkiesel
Copy link
Contributor Author

jtkiesel commented Oct 27, 2023

#611 solves the issue described in #605 also, while simultaneously supporting the now-official "when" style of guards. Assuming that PR is merged, we can close this one.

@jtkiesel jtkiesel closed this Nov 11, 2023
@jtkiesel jtkiesel deleted the fix/separate-binary-expression-from-pattern branch November 26, 2023 19:01
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.

Line break between instanceof and final
2 participants