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

Formatter panic with empty attribute inner comment #6181

Closed
Tracked by #6203
konstin opened this issue Jul 30, 2023 · 0 comments · Fixed by #6332
Closed
Tracked by #6203

Formatter panic with empty attribute inner comment #6181

konstin opened this issue Jul 30, 2023 · 0 comments · Fixed by #6332
Labels
bug Something isn't working formatter Related to the formatter help wanted Contributions especially welcome

Comments

@konstin
Copy link
Member

konstin commented Jul 30, 2023

The following snippet causes the formatter to panic:

(#
()).a

There is not preceding node nor is the comment after the value range, since the comment is ahead of the value range, while the code was writing in the assumption that comments are either leading comments of the entire attribute expression or are after the value, not considering this odd parentheses case.

thread 'main' panicked at 'The enclosing node an attribute expression, expected to be at least after the name', crates/ruff_python_formatter/src/comments/placement.rs:934:5
stack backtrace:
   0: rust_begin_unwind
             at /rustc/90c541806f23a127002de5b4038be731ba1458ca/library/std/src/panicking.rs:578:5
   1: core::panicking::panic_fmt
             at /rustc/90c541806f23a127002de5b4038be731ba1458ca/library/core/src/panicking.rs:67:14
   2: ruff_python_formatter::comments::placement::handle_attribute_comment
             at ./crates/ruff_python_formatter/src/comments/placement.rs:934:5
   3: ruff_python_formatter::comments::placement::place_comment
             at ./crates/ruff_python_formatter/src/comments/placement.rs:53:49
   4: ruff_python_formatter::comments::visitor::CommentsVisitor::start_node_impl
             at ./crates/ruff_python_formatter/src/comments/visitor.rs:80:38
   5: ruff_python_formatter::comments::visitor::CommentsVisitor::start_node
             at ./crates/ruff_python_formatter/src/comments/visitor.rs:54:9
   6: <ruff_python_formatter::comments::visitor::CommentsVisitor as ruff_python_ast::visitor::preorder::PreorderVisitor>::visit_expr
             at ./crates/ruff_python_formatter/src/comments/visitor.rs:205:12
   7: ruff_python_ast::visitor::preorder::walk_expr
             at ./crates/ruff_python_ast/src/visitor/preorder.rs:665:13
   8: <ruff_python_formatter::comments::visitor::CommentsVisitor as ruff_python_ast::visitor::preorder::PreorderVisitor>::visit_expr
             at ./crates/ruff_python_formatter/src/comments/visitor.rs:206:13
   9: ruff_python_ast::visitor::preorder::walk_stmt
             at ./crates/ruff_python_ast/src/visitor/preorder.rs:156:15
  10: <ruff_python_formatter::comments::visitor::CommentsVisitor as ruff_python_ast::visitor::preorder::PreorderVisitor>::visit_stmt
             at ./crates/ruff_python_formatter/src/comments/visitor.rs:185:13
  11: <ruff_python_formatter::comments::visitor::CommentsVisitor as ruff_python_ast::visitor::preorder::PreorderVisitor>::visit_body
             at ./crates/ruff_python_formatter/src/comments/visitor.rs:168:23
  12: ruff_python_ast::visitor::preorder::walk_module
             at ./crates/ruff_python_ast/src/visitor/preorder.rs:118:13
  13: <ruff_python_formatter::comments::visitor::CommentsVisitor as ruff_python_ast::visitor::preorder::PreorderVisitor>::visit_mod
             at ./crates/ruff_python_formatter/src/comments/visitor.rs:158:13
  14: ruff_python_formatter::comments::visitor::CommentsVisitor::visit
             at ./crates/ruff_python_formatter/src/comments/visitor.rs:45:9
  15: ruff_python_formatter::comments::Comments::from_ast
             at ./crates/ruff_python_formatter/src/comments/mod.rs:265:13
  16: ruff_python_formatter::format_node
             at ./crates/ruff_python_formatter/src/lib.rs:148:20
  17: ruff_python_formatter::cli::format_and_debug_print
             at ./crates/ruff_python_formatter/src/cli.rs:60:21
  18: ruff_python_formatter::main
             at ./crates/ruff_python_formatter/src/main.rs:40:29
  19: core::ops::function::FnOnce::call_once
             at /rustc/90c541806f23a127002de5b4038be731ba1458ca/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
@konstin konstin added bug Something isn't working formatter Related to the formatter labels Jul 30, 2023
@MichaReiser MichaReiser added the help wanted Contributions especially welcome label Jul 31, 2023
@MichaReiser MichaReiser added this to the Formatter: Alpha milestone Jul 31, 2023
davidszotten added a commit to davidszotten/ruff that referenced this issue Aug 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working formatter Related to the formatter help wanted Contributions especially welcome
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants