Skip to content

Fix functor module type result-signature brace formatting#8519

Open
MavenRain wants to merge 1 commit into
rescript-lang:masterfrom
MavenRain:fix/7949-functor-result-brace
Open

Fix functor module type result-signature brace formatting#8519
MavenRain wants to merge 1 commit into
rescript-lang:masterfrom
MavenRain:fix/7949-functor-result-brace

Conversation

@MavenRain

Copy link
Copy Markdown

Fix functor module type result-signature brace formatting

Fixes #7949.

Problem

When a functor module type returns a signature, the formatter broke the
opening brace onto its own line:

// input
module Make: Pattern => {
  let fmt: event => string
}

// formatted (before)
module Make: Pattern =>
{
  let fmt: event => string
}

This is inconsistent with functor module expressions, which already keep the
brace on the same line as =>:

module Make = (P: Pattern) => {
  let x = 1
}

Cause

In print_mod_type (the Pmty_functor arm of compiler/syntax/src/res_printer.ml),
the arrow was followed by a breakable Doc.line. Because a signature result
force-breaks internally, the enclosing group always broke at that Doc.line,
pushing { to the next line. The sibling print_mod_functor (module
expressions) uses an unconditional hard space (Doc.text " => ") and so never
had this problem.

Fix

When the functor result is a signature, use a hard space before it (hug the
brace); otherwise keep the breakable Doc.line so long non-signature results
(idents, with constraints, curried arrows) can still wrap:

let arrow_sep =
  match return_type.pmty_desc with
  | Pmty_signature _ -> Doc.space
  | _ -> Doc.line
in

Testing

  • New printer test tests/syntax_tests/data/printer/modType/functorInterface.resi
    covering the reported .resi case plus curried params, a nested/curried arrow,
    an empty signature, and non-signature results (which must remain unchanged).
  • Two existing golden outputs that had captured the wrong behavior are updated
    (modType/functor.res.txt module type B = () => {, and
    modExpr/structure.res.txt module G0: (X: {}) => {).
  • make test-syntax and make test-syntax-roundtrip pass; the printer +
    idempotency corpus round-trips with no diff, and comments around => / inside
    the result signature are preserved.

Drafted with AI assistance; the change, tests, and verification were reviewed by me.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 77aaf8f19b

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread CHANGELOG.md

#### :bug: Bug fix

- Fix formatter breaking the opening brace of a functor module type's result signature onto a new line (e.g. `module Make: Pattern => {`). https://github.com/rescript-lang/rescript/pull/PLACEHOLDER_PR

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Replace the placeholder PR link

If this changelog entry is merged as-is, the published changelog will contain a dead https://github.com/rescript-lang/rescript/pull/PLACEHOLDER_PR link instead of the actual PR reference required for user-facing changes.

AGENTS.md reference: AGENTS.md:L129-L129

Useful? React with 👍 / 👎.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

formatter formats braces incorrectly for functors in resi

1 participant