Skip to content

A long string literal under a cast, a qualified .new or a method reference fails the whole file #252

Open

Description

@abashev

What happens

Reflowing a long string literal fails the whole file when the literal sits under something that binds tighter than +: a cast, the enclosing expression of a qualified .new, or a method reference. StringWrapper splits the literal into "a" + "b", which changes what the cast, the .new or the :: applies to:

  • (Object) "ab" becomes (Object) "a" + "b", that is ((Object) "a") + "b"
  • "ab".new Inner() becomes "a" + "b".new Inner(), that is "a" + ("b".new Inner())
  • "ab"::length becomes "a" + "b"::length

The AST check at the end of wrapOnce notices this and throws, so the formatter prints "Something has gone terribly wrong. Please file a bug" and exits with 2. Nothing in the file is formatted, the rest of it included. With the 21.0.2-29 jar:

class T {
    Object o = (Object) "one long incredibly unbroken sentence moving from topic to topic so that no one had a chance to interrupt or to say anything";
    Object p = "one long incredibly unbroken sentence moving from topic to topic so that no one had a chance to interrupt or to say anything".new Inner();
    java.util.function.IntSupplier s = "one long incredibly unbroken sentence moving from topic to topic so that no one had a chance to interrupt or to say anything"::length;
    String fine = "one long incredibly unbroken sentence moving from topic to topic so that no one had a chance to interrupt or to say anything";
}

open-java-format T.java fails on each of the first three fields on its own. --skip-reflowing-long-strings formats the file.

The scanner that picks the literals to reflow, LongStringsAndTextBlockScanner.visitLiteral, already leaves alone a literal that is the expression of a member select ("…".length()), for the same reason. It does not check the other contexts.

Why it matters

The check does its job: no wrong code comes out. But a single cast of a long literal is enough to make the formatter refuse the file, in the IDE as on the command line. (CharSequence) "…" as a method argument does it too.

Upstream has the same behaviour. google-java-format#1533, open, keeps the exception and pins it with wrap_typeCastParentFallsBackToFullAstCheck and wrap_newClassEnclosingExpressionFallsBackToFullAstCheck. The change for #250 ports both tests as stopsAReflowThatWouldMoveACastToTheFirstPiece and stopsAReflowThatWouldMoveAnInnerClassCreationToTheLastPiece. They pin the safety check, not the behaviour we want.

Proposal

  • LongStringsAndTextBlockScanner.visitLiteral skips a literal whose parent is a cast, a NewClassTree it is the enclosing expression of, or a method reference, as it skips the expression of a member select. The literal stays on its line, as the formatter lays it out.
  • Wrapping the pieces in parentheses, (Object) ("a" + "b"), would also keep the meaning. But it adds code that nobody wrote, and the member-select case already chooses to leave the literal alone.
  • The two ported tests then expect the input to come back as it is. A case file covers the three contexts plus a literal that is still reflowed.

After (the three literals as --skip-reflowing-long-strings lays them out, the last one reflowed):

class T {
    Object o = (Object)
            "one long incredibly unbroken sentence moving from topic to topic so that no one had a chance to interrupt or to say anything";
    Object p =
            "one long incredibly unbroken sentence moving from topic to topic so that no one had a chance to interrupt or to say anything"
            .new Inner();
    java.util.function.IntSupplier s =
            "one long incredibly unbroken sentence moving from topic to topic so that no one had a chance to interrupt or to say anything"
                    ::length;
    String fine =
            "one long incredibly unbroken sentence moving from topic to topic so that no one had a chance to interrupt"
                    + " or to say anything";
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions