-
-
Notifications
You must be signed in to change notification settings - Fork 353
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: minimise bracket printing in case of string operands of binary operator #4809
Conversation
I think I have figured out why it is happening. To understand it, let's consider a working case first - "1 + 2 + 3 + 4". 1 + 2 + 3 + 4The above expression is modelled as:
So everytime nestedOperatorRequiresRoundBrackets is called for each nested operator, the "1" + "2" + "3" + "4"This is the failing test case. We expect
For the first nested operator, besides The resolution would be to see why there is a difference in AST modelling of the two expressions and probably fix it if it turns out to be a bug. @slarse do you also agree that the difference in behaviour arises from how Spoon is modelling nested binary operators? |
assertEquals("\"\" + (\"\"// comment multi line string" + newLine | ||
+ " + \"\")", ctLocalVariableString.getDefaultExpression().toString()); | ||
assertEquals(createFakeComment(f, "comment multi line string"), (((CtBinaryOperator) ctLocalVariableString.getDefaultExpression()).getLeftHandOperand()).getComments().get(0)); | ||
assertEquals("(\"\" + \"\")// comment multi line string" + newLine |
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 minimizeRoundBrackets
was false
for this test case. Hence, I rearranged the brackets.
op.setLeftHandOperand(operator.getRightHandOperand()); | ||
op.setRightHandOperand((CtExpression<?>) child); | ||
op.setLeftHandOperand(operator.getLeftHandOperand()); | ||
op.setRightHandOperand(operator.getRightHandOperand()); | ||
op.setType((CtTypeReference) operator.getFactory().Type().STRING.clone()); | ||
operator.setRightHandOperand(op); | ||
operator.setLeftHandOperand(op); | ||
operator.setRightHandOperand(((CtExpression<?>) child)); |
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 have done exactly what I have explained here. I ensured that the left operand takes the nested operator whenever possible.
I have not put the exact test case as listed in the issue, but I feel the one I added in the PR captures the essence and should fix it. @slarse @MartinWitt @I-Al-Istannen @SirYwell please review it when you get time. P.S. Is there a team for maintainers so that I can collectively tag them? |
I don't see any justification for the reordering in the introducing commit, which apparently was meant for switch case statements: 25156e1. Based on your example the current behaviour seems questionable. It should be semantically correct, as |
No, there isn't. There isn't really a strong use case for it. We go over PRs for review if they are prefixed with
Well, if it builds a non-equivalent AST (even if semantically equivalent), I'd say it's a bug. It makes string concatenation look right associative, which it technically isn't. Although it also technically doesn't matter because there's only one operator involved in concatenation of literal strings. |
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.
@algomaster99 Thanks for the PR. Could you add a minimal test case demonstrating the problem on the AST level? That is to say, given a literal string concatenation "a" + "b" + "c"
, we expect the AST binop(binop("a", "b", +), "c", +)
.
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 @algomaster99 , lgtm!
I'm adjusting the commit message from the PR title as this isn't really about pretty-printing (the printer was doing the right thing by the AST all along).
Fixes #4807
I have tried to reproduce #4807 with a related test case.
I have noticed that the test pass when I change each operand in the above expression to an integer, thanks to #3823.