Skip to content

Formatter errors when comments are placed alongside multi-line arrays #4638

Description

@achowd32

Describe the bug
As the title says, this is a fairly simple bug where the formatter will error on a DSLX file which has comments placed alongside multi-line arrays. I encountered this bug when writing this PR and thought it might be good to file an issue for it.

To Reproduce
Steps to reproduce the behavior:

  1. Write a DSLX file with comments placed on lines with multi-line arrays. A simple example might look as follows:
// example.x
fn make_arr(a: u32, b: u32, c: u32, d: u32) -> u32[2][2] {
   [[a, b], // comment here
    [c, d]] // comment here
}
  1. Locate the path to your DSLX formatter binary. Let's say mine is simply ./dslx_fmt. Run the formatter on the example file above, or another like it: ./dslx_fmt example.x.
  2. You should get the following error:
Error: INTERNAL: Formatting was skipped because a comment at example.x:2:14-3:1 would be deleted by the *this: // comment here
This is probably due to a bug (which may not have been reported yet). To complete formatting, try moving the comment to a different line.

Expected behavior
I would expect the formatter not to error on this. I have no strong preference on how the above example would be formatted; I would probably expect it to stay as is, though longer comments may be moved to their own lines.

Environment (this can be helpful for troubleshooting):
I encountered the above bug on Ubuntu 24.04.3.

Additional context
I am an open-source newcomer looking to get involved with the XLS project, and would love to try and tackle this issue if maintainers agree it is indeed undesired behavior. I have spent some time looking at xls/dslx/fmt/ast_fmt.cc and have some understanding of the bug. I would probably attempt to change FormatArray to FormatArrayWithoutComments, and add a new function FormatArray which is able to handle comments between multi-line arrays. This mirrors the structure used by FormatTuple and FormatTupleWithoutComments.

Let me know what you think, I am open to any and all feedback.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions