Skip to content

Add explicit support for labeled tuples - #349

Merged
NathanReb merged 4 commits into
OCamlPro:masterfrom
NathanReb:support-labeled-tuples
Aug 4, 2026
Merged

Add explicit support for labeled tuples#349
NathanReb merged 4 commits into
OCamlPro:masterfrom
NathanReb:support-labeled-tuples

Conversation

@NathanReb

Copy link
Copy Markdown
Contributor

This PR adds tests for ocp-indent's behaviour with the labeled tuples syntax (introduced in OCaml 5.4: ocaml/ocaml#13498).

In expressions and patterns, ocp-indent was already correctly handling labeled tuples and no update were required.

In type expressions though it didn't as the type expression syntax for labels was too closely tied to arrows which resulted in the incorrect indentation of the following example:

a:
int
*
b:
int

which was misintepreted as a: (int * b:int), assuming the label a was a function argument label.

Properly handling this isn't a trivial fix since determining how to indent it ultimately depends on whether there is an arrow up ahead.

e.g.:

a:
int
*
b:
int

should be indented as follows:

a:
  int
*
b:
  int

but

a:
int
*
b:
int
-> unit

should be indented as follows:

a:
  int
  *
  b:
    int
-> unit

to reflect -> and * priorities/associativity with the label.

ocp-indent doesn't handle backtracking and avoids lookahead as much as possible. I came to the conclusion that in this specific case, the lookahead was unavoidable so I tried to:

  1. Avoid it as much as possible by eliminating unambiguously tuple cases: a label directly past a * cannot be associated with a ->
  2. Limit the lookahead search range and assume no arrow is coming up past it. In theory, the code could be arbitrarily long between a label and the arrow its associated with + the code can be incomplete.

I'll happily update and improve this approach based on user feedback. My gut feeling is that in practice this will work reasonably well though it might fail us in some corner cases.

Signed-off-by: Nathan Rebours <nathan.rebours@ocamlpro.com>
@NathanReb
NathanReb force-pushed the support-labeled-tuples branch from b6404ea to 799f34a Compare July 31, 2026 14:30

@AltGr AltGr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Seems pretty reasonable ; the depth limit to 100 might be a bit high, but that shouldn't affect anything. Thanks!

Comment thread CHANGELOG.md Outdated
Signed-off-by: Nathan Rebours <nathan.rebours@ocamlpro.com>
Signed-off-by: Nathan Rebours <nathan.rebours@ocamlpro.com>
@NathanReb
NathanReb force-pushed the support-labeled-tuples branch from 799f34a to a37292f Compare August 4, 2026 07:48
Signed-off-by: Nathan Rebours <nathan.rebours@ocamlpro.com>
@NathanReb
NathanReb merged commit 2cf9284 into OCamlPro:master Aug 4, 2026
7 checks passed
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.

2 participants