Skip to content

Formatter moves an attribute from a type constraint to the first operand #9497

Description

@cknitt

The formatter moves an attribute on a type-constrained expression onto the expression's first operand, which changes what the attribute applies to.

let a = @warning("-102") ([1] == [2]: bool)

is formatted as

let a = (@warning("-102") [1] == [2]: bool)

In the input, the attribute is on the constraint node:

Pexp_constraint      <- attribute "warning"
  Pexp_apply  ==
    [1]
    [2]

In the formatted output, it is on the array [1], the first operand of ==:

Pexp_constraint
  Pexp_apply  ==
    [1]               <- attribute "warning"
    [2]

So formatting changes the program. Here @warning("-102") no longer covers the comparison. The same happens with a braced expression: @warning("-102") {([1] == [2]: bool)} is printed as {(@warning("-102") [1] == [2]: bool)}.

The printer prints the attributes of the Pexp_constraint and then the constraint e: t, and the caller adds the parentheses around both. To keep the attribute on the constraint, it has to be printed outside the parentheses: @warning("-102") (e: t).

Found while writing a test for #9491, whose fixture has to pass the format check.

(Filed by Claude.)

Activity

  1. added theissue type on Dec 1, 2026
  2. added 2 commits that reference this issue on Dec 1, 2026
    719bc8d
    738c317
  3. added a commit that references this issue on Dec 2, 2026
    ceb3fc0
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

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions