Skip to content

Commit 9ceeb96

Browse files
cknittclaude
andauthored
Keep the header of a let with locally abstract types on one line (#9082)
Why: A binding annotated with `type a.` was printed as one group, so a line break in its body, such as a switch, broke the header after `:` and `=` as well and indented the body twice. This is how every function over a GADT is written. The header now stays on one line when its first line fits, as for other annotated bindings. Format modified (let with locally abstract types): Example: let rec eval: type a. expr<a> => a = e => switch e { | Int(n) => n | Bool(b) => b } Before (rescript): let rec eval: type a. expr<a> => a = e => switch e { | Int(n) => n | Bool(b) => b } After (rescript): let rec eval: type a. expr<a> => a = e => switch e { | Int(n) => n | Bool(b) => b } Review: What happens to a header that doesn't fit? It is printed as before, broken after `:` and `=`; the printer fixture keeps such a case, also with attributes in front, which stay outside the layout choice so that the header's width is measured from the `let`. Fixes: #9070 Claude-Session: https://claude.ai/code/session_01Kbf3gLMawVgtmiKy3TMBc3 Claude-Session: https://claude.ai/code/session_01Kr4cjoUi2f5VpMvE6wT9bo Signed-off-by: Christoph Knittel <ck@cca.io> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent d03a32b commit 9ceeb96

9 files changed

Lines changed: 226 additions & 165 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- The formatter keeps `let f: type a. t => a = x =>` on one line when it fits, instead of breaking after `:` and `=` whenever the body spans several lines.

‎compiler/syntax/src/res_printer.ml‎

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2494,27 +2494,55 @@ and print_value_binding ~state ~rec_flag (vb : Parsetree.value_binding) cmt_tbl
24942494
print_comments (print_ident_like txt) cmt_tbl loc)
24952495
pvc_newtypes)
24962496
in
2497-
Doc.group
2498-
(Doc.concat
2499-
[
2500-
attrs;
2501-
header;
2502-
print_pattern ~state pattern cmt_tbl;
2503-
Doc.text ":";
2504-
Doc.indent
2505-
(Doc.concat
2506-
[
2507-
Doc.line;
2508-
Doc.text "type ";
2509-
newtypes;
2510-
Doc.dot;
2511-
Doc.space;
2512-
print_typ_expr ~state pvc_type cmt_tbl;
2513-
Doc.text " =";
2514-
Doc.line;
2515-
print_expression_with_comments_and_parens ~state expr cmt_tbl;
2516-
]);
2517-
])
2497+
let pattern_doc = print_pattern ~state pattern cmt_tbl in
2498+
let typ_doc = print_typ_expr ~state pvc_type cmt_tbl in
2499+
let expr_doc =
2500+
print_expression_with_comments_and_parens ~state expr cmt_tbl
2501+
in
2502+
(* Keep `let f: type a. t = body` on one line up to the body's first
2503+
line break when it fits; otherwise break after `:` and `=`. The
2504+
attributes stay outside, so that [fits] measures from the `let`. *)
2505+
Doc.concat
2506+
[
2507+
attrs;
2508+
Doc.custom_layout
2509+
[
2510+
(* not a group: a group would be marked broken by the body's line
2511+
breaks, and [fits] would then accept a header that overflows *)
2512+
Doc.concat
2513+
[
2514+
header;
2515+
pattern_doc;
2516+
Doc.text ": type ";
2517+
newtypes;
2518+
Doc.dot;
2519+
Doc.space;
2520+
typ_doc;
2521+
Doc.text " = ";
2522+
expr_doc;
2523+
];
2524+
Doc.group
2525+
(Doc.concat
2526+
[
2527+
header;
2528+
pattern_doc;
2529+
Doc.text ":";
2530+
Doc.indent
2531+
(Doc.concat
2532+
[
2533+
Doc.line;
2534+
Doc.text "type ";
2535+
newtypes;
2536+
Doc.dot;
2537+
Doc.space;
2538+
typ_doc;
2539+
Doc.text " =";
2540+
Doc.line;
2541+
expr_doc;
2542+
]);
2543+
]);
2544+
];
2545+
]
25182546
| {
25192547
pvb_pat =
25202548
{

‎tests/syntax_tests/data/printer/expr/expected/newtype.res.txt‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,32 @@ let cancel_and_collect_callbacks:
2626
'a 'u 'c. (list<packed_callbacks>, promise<'a, 'u, 'c>) => list<packed_callbacks> =
2727
(type x, callbacks_accumulator, p: promise<_, _, c>) => ()
2828

29+
// the header stays on one line when it fits
30+
let rec eval: type a. expr<a> => a = e =>
31+
switch e {
32+
| Int(n) => n
33+
| Bool(b) => b
34+
}
35+
36+
let id: type a. a => a = x => x
37+
38+
/** Docs */
39+
@inline
40+
let rec veryLongFunctionNameForWrapping:
41+
type a b c. (someLongType<a, b>, anotherLongType<c>) => resultType<a> =
42+
(x, y) =>
43+
switch x {
44+
| _ => 1
45+
}
46+
47+
module M = {
48+
let default: type a. ty<a> => a = ty =>
49+
switch ty {
50+
| TInt => 0
51+
| TString => ""
52+
}
53+
}
54+
2955
// type parameters written between term parameters are hoisted to the front
3056
let g = (type t, x, y: t) => y
3157
let g = (@attr type t u v, x: int, y: t, z: v) => (y, z)

‎tests/syntax_tests/data/printer/expr/newtype.res‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,32 @@ let cancel_and_collect_callbacks:
2121
=
2222
(type x, callbacks_accumulator, p: promise<_, _, c>) => ();
2323

24+
// the header stays on one line when it fits
25+
let rec eval:
26+
type a. expr<a> => a =
27+
e =>
28+
switch e {
29+
| Int(n) => n
30+
| Bool(b) => b
31+
}
32+
33+
let id: type a. a => a = x => x
34+
35+
/** Docs */
36+
@inline
37+
let rec veryLongFunctionNameForWrapping: type a b c. (someLongType<a, b>, anotherLongType<c>) => resultType<a> = (x, y) =>
38+
switch x {
39+
| _ => 1
40+
}
41+
42+
module M = {
43+
let default: type a. ty<a> => a = ty =>
44+
switch ty {
45+
| TInt => 0
46+
| TString => ""
47+
}
48+
}
49+
2450
// type parameters written between term parameters are hoisted to the front
2551
let g = (x, type t, y: t) => y
2652
let g = (x: int, @attr type t u, y: t, type v, z: v) => (y, z)

‎tests/tests/src/extensible_variant_test.mjs‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -89,17 +89,17 @@ function makeException() {
8989
}
9090

9191
Mocha.describe("Extensible_variant_test", () => {
92-
Mocha.test("test_int", () => Test_utils.eq("File \"extensible_variant_test.res\", line 73, characters 7-14", 3, to_int({
92+
Mocha.test("test_int", () => Test_utils.eq("File \"extensible_variant_test.res\", line 71, characters 7-14", 3, to_int({
9393
RE_EXN_ID: Int,
9494
_1: 3,
9595
_2: 0
9696
})));
97-
Mocha.test("test_int2", () => Test_utils.eq("File \"extensible_variant_test.res\", line 77, characters 7-14", 0, to_int({
97+
Mocha.test("test_int2", () => Test_utils.eq("File \"extensible_variant_test.res\", line 75, characters 7-14", 0, to_int({
9898
RE_EXN_ID: Int$1,
9999
_1: 3,
100100
_2: 0
101101
})));
102-
Mocha.test("test_string", () => Test_utils.eq("File \"extensible_variant_test.res\", line 81, characters 7-14", -1, to_int({
102+
Mocha.test("test_string", () => Test_utils.eq("File \"extensible_variant_test.res\", line 79, characters 7-14", -1, to_int({
103103
RE_EXN_ID: Str,
104104
_1: "x"
105105
})));
@@ -109,15 +109,15 @@ Mocha.describe("Extensible_variant_test", () => {
109109
"x-coord": 1,
110110
y: 2
111111
};
112-
Test_utils.eq("File \"extensible_variant_test.res\", line 86, characters 7-14", 1, point["x-coord"]);
113-
Test_utils.eq("File \"extensible_variant_test.res\", line 88, characters 6-13", 3, Point === Point ? 3 : 0);
112+
Test_utils.eq("File \"extensible_variant_test.res\", line 84, characters 7-14", 1, point["x-coord"]);
113+
Test_utils.eq("File \"extensible_variant_test.res\", line 86, characters 6-13", 3, Point === Point ? 3 : 0);
114114
});
115115
Mocha.test("rebind", () => {
116-
Test_utils.eq("File \"extensible_variant_test.res\", line 98, characters 7-14", -1, to_int({
116+
Test_utils.eq("File \"extensible_variant_test.res\", line 96, characters 7-14", -1, to_int({
117117
RE_EXN_ID: Str,
118118
_1: "x"
119119
}));
120-
Test_utils.eq("File \"extensible_variant_test.res\", line 99, characters 7-14", true, Primitive_object.equal({
120+
Test_utils.eq("File \"extensible_variant_test.res\", line 97, characters 7-14", true, Primitive_object.equal({
121121
RE_EXN_ID: Str,
122122
_1: "x"
123123
}, {
@@ -126,18 +126,18 @@ Mocha.describe("Extensible_variant_test", () => {
126126
}));
127127
});
128128
Mocha.test("indexed extension", () => {
129-
Test_utils.eq("File \"extensible_variant_test.res\", line 103, characters 7-14", 0, defaultOf({
129+
Test_utils.eq("File \"extensible_variant_test.res\", line 101, characters 7-14", 0, defaultOf({
130130
RE_EXN_ID: IntKey
131131
}));
132-
Test_utils.eq("File \"extensible_variant_test.res\", line 104, characters 7-14", "", defaultOf({
132+
Test_utils.eq("File \"extensible_variant_test.res\", line 102, characters 7-14", "", defaultOf({
133133
RE_EXN_ID: StringKey
134134
}));
135135
});
136136
Mocha.test("private extensible", () => {
137-
Test_utils.eq("File \"extensible_variant_test.res\", line 108, characters 7-14", "A", privateName({
137+
Test_utils.eq("File \"extensible_variant_test.res\", line 106, characters 7-14", "A", privateName({
138138
RE_EXN_ID: A
139139
}));
140-
Test_utils.eq("File \"extensible_variant_test.res\", line 109, characters 7-14", "other", privateName(b));
140+
Test_utils.eq("File \"extensible_variant_test.res\", line 107, characters 7-14", "other", privateName(b));
141141
});
142142
Mocha.test("local exceptions are generative", () => {
143143
let match = makeException();
@@ -149,10 +149,10 @@ Mocha.describe("Extensible_variant_test", () => {
149149
caught = Primitive_exceptions.internalToException(raw_exn);
150150
}
151151
if (caught !== undefined) {
152-
Test_utils.eq("File \"extensible_variant_test.res\", line 120, characters 9-16", true, match[1](caught));
153-
return Test_utils.eq("File \"extensible_variant_test.res\", line 121, characters 9-16", false, match$1[1](caught));
152+
Test_utils.eq("File \"extensible_variant_test.res\", line 118, characters 9-16", true, match[1](caught));
153+
return Test_utils.eq("File \"extensible_variant_test.res\", line 119, characters 9-16", false, match$1[1](caught));
154154
} else {
155-
return Test_utils.ok("File \"extensible_variant_test.res\", line 122, characters 17-24", false);
155+
return Test_utils.ok("File \"extensible_variant_test.res\", line 120, characters 17-24", false);
156156
}
157157
});
158158
});

‎tests/tests/src/extensible_variant_test.res‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,12 @@ type key<_> = ..
3030
type key<_> += IntKey: key<int>
3131
type key<_> += StringKey: key<string>
3232

33-
let defaultOf:
34-
type a. key<a> => option<a> =
35-
key =>
36-
switch key {
37-
| IntKey => Some(0)
38-
| StringKey => Some("")
39-
| _ => None
40-
}
33+
let defaultOf: type a. key<a> => option<a> = key =>
34+
switch key {
35+
| IntKey => Some(0)
36+
| StringKey => Some("")
37+
| _ => None
38+
}
4139

4240
// Outside code can match a private extensible type but not extend it.
4341
module Private: {

‎tests/tests/src/gadt_test.mjs‎

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ function depth(n) {
231231

232232
Mocha.describe("Gadt_test", () => {
233233
Mocha.test("evaluator", () => {
234-
Test_utils.eq("File \"gadt_test.res\", line 203, characters 7-14", 10, $$eval({
234+
Test_utils.eq("File \"gadt_test.res\", line 187, characters 7-14", 10, $$eval({
235235
TAG: "If",
236236
_0: {
237237
TAG: "Equal",
@@ -260,7 +260,7 @@ Mocha.describe("Gadt_test", () => {
260260
_0: 20
261261
}
262262
}));
263-
Test_utils.eq("File \"gadt_test.res\", line 204, characters 7-14", true, $$eval({
263+
Test_utils.eq("File \"gadt_test.res\", line 188, characters 7-14", true, $$eval({
264264
TAG: "First",
265265
_0: {
266266
TAG: "Pair",
@@ -274,7 +274,7 @@ Mocha.describe("Gadt_test", () => {
274274
}
275275
}
276276
}));
277-
Test_utils.eq("File \"gadt_test.res\", line 205, characters 7-14", [
277+
Test_utils.eq("File \"gadt_test.res\", line 189, characters 7-14", [
278278
3,
279279
false
280280
], $$eval({
@@ -304,7 +304,7 @@ Mocha.describe("Gadt_test", () => {
304304
}));
305305
});
306306
Mocha.test("type witnesses", () => {
307-
Test_utils.eq("File \"gadt_test.res\", line 210, characters 6-13", `(1, "a")::(2, "b")::[]`, show({
307+
Test_utils.eq("File \"gadt_test.res\", line 194, characters 6-13", `(1, "a")::(2, "b")::[]`, show({
308308
TAG: "TList",
309309
_0: {
310310
TAG: "TPair",
@@ -318,29 +318,29 @@ Mocha.describe("Gadt_test", () => {
318318
2,
319319
"b"
320320
], tl: /* [] */0}}));
321-
Test_utils.eq("File \"gadt_test.res\", line 214, characters 7-14", "Some(None)", show({
321+
Test_utils.eq("File \"gadt_test.res\", line 198, characters 7-14", "Some(None)", show({
322322
TAG: "TOption",
323323
_0: {
324324
TAG: "TOption",
325325
_0: "TInt"
326326
}
327327
}, Primitive_option.some(undefined)));
328-
Test_utils.eq("File \"gadt_test.res\", line 215, characters 7-14", 0, 0);
329-
Test_utils.eq("File \"gadt_test.res\", line 216, characters 7-14", "", "");
330-
Test_utils.eq("File \"gadt_test.res\", line 217, characters 7-14", /* [] */0, /* [] */0);
331-
Test_utils.eq("File \"gadt_test.res\", line 218, characters 7-14", undefined, undefined);
332-
Test_utils.eq("File \"gadt_test.res\", line 219, characters 7-14", "int", "int");
328+
Test_utils.eq("File \"gadt_test.res\", line 199, characters 7-14", 0, 0);
329+
Test_utils.eq("File \"gadt_test.res\", line 200, characters 7-14", "", "");
330+
Test_utils.eq("File \"gadt_test.res\", line 201, characters 7-14", /* [] */0, /* [] */0);
331+
Test_utils.eq("File \"gadt_test.res\", line 202, characters 7-14", undefined, undefined);
332+
Test_utils.eq("File \"gadt_test.res\", line 203, characters 7-14", "int", "int");
333333
});
334334
Mocha.test("equality witness", () => {
335-
Test_utils.eq("File \"gadt_test.res\", line 223, characters 7-14", 42, 42);
336-
Test_utils.ok("File \"gadt_test.res\", line 224, characters 7-14", equalTy({
335+
Test_utils.eq("File \"gadt_test.res\", line 207, characters 7-14", 42, 42);
336+
Test_utils.ok("File \"gadt_test.res\", line 208, characters 7-14", equalTy({
337337
TAG: "TList",
338338
_0: "TInt"
339339
}, {
340340
TAG: "TList",
341341
_0: "TInt"
342342
}) !== undefined);
343-
Test_utils.ok("File \"gadt_test.res\", line 225, characters 7-14", equalTy({
343+
Test_utils.ok("File \"gadt_test.res\", line 209, characters 7-14", equalTy({
344344
TAG: "TPair",
345345
_0: "TInt",
346346
_1: "TString"
@@ -371,20 +371,20 @@ Mocha.describe("Gadt_test", () => {
371371
_1: {hd: 1, tl: {hd: 2, tl: /* [] */0}}
372372
}
373373
];
374-
Test_utils.eq("File \"gadt_test.res\", line 230, characters 7-14", [
374+
Test_utils.eq("File \"gadt_test.res\", line 214, characters 7-14", [
375375
1,
376376
undefined,
377377
undefined
378378
], values.map(v => fromDynamic("TInt", v)));
379-
Test_utils.eq("File \"gadt_test.res\", line 231, characters 7-14", [
379+
Test_utils.eq("File \"gadt_test.res\", line 215, characters 7-14", [
380380
undefined,
381381
undefined,
382382
{hd: 1, tl: {hd: 2, tl: /* [] */0}}
383383
], values.map(v => fromDynamic({
384384
TAG: "TList",
385385
_0: "TInt"
386386
}, v)));
387-
Test_utils.eq("File \"gadt_test.res\", line 233, characters 6-13", [
387+
Test_utils.eq("File \"gadt_test.res\", line 217, characters 6-13", [
388388
"1",
389389
"s"
390390
], showAll([
@@ -399,13 +399,13 @@ Mocha.describe("Gadt_test", () => {
399399
show: s => s
400400
}
401401
]));
402-
Test_utils.eq("File \"gadt_test.res\", line 240, characters 7-14", "7", showBoxed({
402+
Test_utils.eq("File \"gadt_test.res\", line 224, characters 7-14", "7", showBoxed({
403403
TAG: "Boxed",
404404
_0: ShowInt,
405405
_1: 7
406406
}));
407407
});
408-
Mocha.test("typed printf", () => Test_utils.eq("File \"gadt_test.res\", line 244, characters 7-14", "x=1, y=b", format({
408+
Mocha.test("typed printf", () => Test_utils.eq("File \"gadt_test.res\", line 228, characters 7-14", "x=1, y=b", format({
409409
TAG: "Text",
410410
_0: "x=",
411411
_1: {
@@ -421,28 +421,28 @@ Mocha.describe("Gadt_test", () => {
421421
}
422422
}, "")(1)("b")));
423423
Mocha.test("representation attributes", () => {
424-
Test_utils.eq("File \"gadt_test.res\", line 248, characters 7-14", 3, 3);
425-
Test_utils.eq("File \"gadt_test.res\", line 249, characters 7-14", "s", "s");
426-
Test_utils.eq("File \"gadt_test.res\", line 250, characters 7-14", {
424+
Test_utils.eq("File \"gadt_test.res\", line 232, characters 7-14", 3, 3);
425+
Test_utils.eq("File \"gadt_test.res\", line 233, characters 7-14", "s", "s");
426+
Test_utils.eq("File \"gadt_test.res\", line 234, characters 7-14", {
427427
TAG: "int",
428428
v: 3
429429
}, {
430430
TAG: "int",
431431
v: 3
432432
});
433-
Test_utils.eq("File \"gadt_test.res\", line 251, characters 7-14", {
433+
Test_utils.eq("File \"gadt_test.res\", line 235, characters 7-14", {
434434
kind: "Circle",
435435
radius: 1.5
436436
}, {
437437
kind: "Circle",
438438
radius: 1.5
439439
});
440-
Test_utils.eq("File \"gadt_test.res\", line 252, characters 7-14", "hi", "hi");
441-
Test_utils.eq("File \"gadt_test.res\", line 253, characters 7-14", 2, 2);
442-
Test_utils.eq("File \"gadt_test.res\", line 254, characters 7-14", "u", "u");
443-
Test_utils.eq("File \"gadt_test.res\", line 255, characters 7-14", "u", "u");
440+
Test_utils.eq("File \"gadt_test.res\", line 236, characters 7-14", "hi", "hi");
441+
Test_utils.eq("File \"gadt_test.res\", line 237, characters 7-14", 2, 2);
442+
Test_utils.eq("File \"gadt_test.res\", line 238, characters 7-14", "u", "u");
443+
Test_utils.eq("File \"gadt_test.res\", line 239, characters 7-14", "u", "u");
444444
});
445-
Mocha.test("polymorphic recursion", () => Test_utils.eq("File \"gadt_test.res\", line 259, characters 7-14", 2, depth({
445+
Mocha.test("polymorphic recursion", () => Test_utils.eq("File \"gadt_test.res\", line 243, characters 7-14", 2, depth({
446446
TAG: "Nest",
447447
_0: {
448448
TAG: "Nest",

0 commit comments

Comments
 (0)