-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[ruff] Fix panic when comments appear between unary operators and operands
#20494
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
base: main
Are you sure you want to change the base?
Conversation
|
|
Thank you. I need to think about whether this is the intended comment placement as it means that a comment can now move "forward" (it moves from after |
|
@MichaReiser Ok. If it's not intentional, I can make |
|
Sorry for the late reply. I just took a look at which node the formatter associates the comment for: if '' and (not #
0):
pass{
Node {
kind: ExprUnaryOp,
range: 11..18,
source: `not #⏎`,
}: {
"leading": [
SourceComment {
text: "#",
position: EndOfLine,
formatted: false,
},
],
"dangling": [],
"trailing": [],
},
}To me, associating the comment as the leading comment of I'd either expect this to be a leading comment of An other alternative, and I think my preferred solution, is to make the comment a dangling comment of the unary expression instead. In fact, it seems we already have some handling for unary comments inside ruff/crates/ruff_python_formatter/src/comments/placement.rs Lines 1846 to 1872 in a1e3d93
|
Summary -- This is a second attempt at fixing #19226 based on the feedback in #20494 (comment). We currently mark the comment in an expression like this: ```py if '' and (not # 0): pass ``` as a leading comment on `not`, eventually causing a panic in `Operand::has_unparenthesized_leading_comments` because the end of the comment is greater than the start of the expression. https://github.com/astral-sh/ruff/blob/a1d9cb5830eca9b63e7fb529504fc536e99bca23/crates/ruff_python_formatter/src/expression/binary_like.rs#L843 This PR fixes the issue by instead making such a comment a dangling comment on the unary expression. In the third commit, I instead tried making the comment a leading comment on the operand, which also looks pretty reasonable to me. Making it a dangling comment seems more in line with the docs on `handle_unary_op_comments`, though. I also tried deleting the leading comment logic in favor of the new dangling logic in the fifth commit before reverting in the sixth. This looks okay to me too, but the current state of the PR seems like the least invasive fix. Test Plan -- A new, minimized test case based on the issue. I also checked that the original snippet from the report works now. Co-authored-by: Takayuki Maeda <takoyaki0316@gmail.com>
Summary
Fixes #19226
Fixes a panic in binary expression formatting caused by TextRange assertion failures when comments are positioned between unary operators (like
not) and their operands.The issue occurred because
expression.start()points to the unary operator whilecomment.end()can be positioned after it, creating invalid TextRange where start > end. The fix usesoperand.start()for unary operations to ensure proper ordering.Test Plan
I have added new test cases to
crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/unary.py.