Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/locally-abstract-let-format.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +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.
70 changes: 49 additions & 21 deletions compiler/syntax/src/res_printer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2494,27 +2494,55 @@ and print_value_binding ~state ~rec_flag (vb : Parsetree.value_binding) cmt_tbl
print_comments (print_ident_like txt) cmt_tbl loc)
pvc_newtypes)
in
Doc.group
(Doc.concat
[
attrs;
header;
print_pattern ~state pattern cmt_tbl;
Doc.text ":";
Doc.indent
(Doc.concat
[
Doc.line;
Doc.text "type ";
newtypes;
Doc.dot;
Doc.space;
print_typ_expr ~state pvc_type cmt_tbl;
Doc.text " =";
Doc.line;
print_expression_with_comments_and_parens ~state expr cmt_tbl;
]);
])
let pattern_doc = print_pattern ~state pattern cmt_tbl in
let typ_doc = print_typ_expr ~state pvc_type cmt_tbl in
let expr_doc =
print_expression_with_comments_and_parens ~state expr cmt_tbl
in
(* Keep `let f: type a. t = body` on one line up to the body's first
line break when it fits; otherwise break after `:` and `=`. The
attributes stay outside, so that [fits] measures from the `let`. *)
Doc.concat
[
attrs;
Doc.custom_layout
[
(* not a group: a group would be marked broken by the body's line
breaks, and [fits] would then accept a header that overflows *)
Doc.concat
[
header;
pattern_doc;
Doc.text ": type ";
newtypes;
Doc.dot;
Doc.space;
typ_doc;
Doc.text " = ";
expr_doc;
];
Doc.group
(Doc.concat
[
header;
pattern_doc;
Doc.text ":";
Doc.indent
(Doc.concat
[
Doc.line;
Doc.text "type ";
newtypes;
Doc.dot;
Doc.space;
typ_doc;
Doc.text " =";
Doc.line;
expr_doc;
]);
]);
];
]
| {
pvb_pat =
{
Expand Down
26 changes: 26 additions & 0 deletions tests/syntax_tests/data/printer/expr/expected/newtype.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,32 @@ let cancel_and_collect_callbacks:
'a 'u 'c. (list<packed_callbacks>, promise<'a, 'u, 'c>) => list<packed_callbacks> =
(type x, callbacks_accumulator, p: promise<_, _, c>) => ()

// the header stays on one line when it fits
let rec eval: type a. expr<a> => a = e =>
switch e {
| Int(n) => n
| Bool(b) => b
}

let id: type a. a => a = x => x

/** Docs */
@inline
let rec veryLongFunctionNameForWrapping:
type a b c. (someLongType<a, b>, anotherLongType<c>) => resultType<a> =
(x, y) =>
switch x {
| _ => 1
}

module M = {
let default: type a. ty<a> => a = ty =>
switch ty {
| TInt => 0
| TString => ""
}
}

// type parameters written between term parameters are hoisted to the front
let g = (type t, x, y: t) => y
let g = (@attr type t u v, x: int, y: t, z: v) => (y, z)
Expand Down
26 changes: 26 additions & 0 deletions tests/syntax_tests/data/printer/expr/newtype.res
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,32 @@ let cancel_and_collect_callbacks:
=
(type x, callbacks_accumulator, p: promise<_, _, c>) => ();

// the header stays on one line when it fits
let rec eval:
type a. expr<a> => a =
e =>
switch e {
| Int(n) => n
| Bool(b) => b
}

let id: type a. a => a = x => x

/** Docs */
@inline
let rec veryLongFunctionNameForWrapping: type a b c. (someLongType<a, b>, anotherLongType<c>) => resultType<a> = (x, y) =>
switch x {
| _ => 1
}

module M = {
let default: type a. ty<a> => a = ty =>
switch ty {
| TInt => 0
| TString => ""
}
}

// type parameters written between term parameters are hoisted to the front
let g = (x, type t, y: t) => y
let g = (x: int, @attr type t u, y: t, type v, z: v) => (y, z)
Expand Down
28 changes: 14 additions & 14 deletions tests/tests/src/extensible_variant_test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,17 @@ function makeException() {
}

Mocha.describe("Extensible_variant_test", () => {
Mocha.test("test_int", () => Test_utils.eq("File \"extensible_variant_test.res\", line 73, characters 7-14", 3, to_int({
Mocha.test("test_int", () => Test_utils.eq("File \"extensible_variant_test.res\", line 71, characters 7-14", 3, to_int({
RE_EXN_ID: Int,
_1: 3,
_2: 0
})));
Mocha.test("test_int2", () => Test_utils.eq("File \"extensible_variant_test.res\", line 77, characters 7-14", 0, to_int({
Mocha.test("test_int2", () => Test_utils.eq("File \"extensible_variant_test.res\", line 75, characters 7-14", 0, to_int({
RE_EXN_ID: Int$1,
_1: 3,
_2: 0
})));
Mocha.test("test_string", () => Test_utils.eq("File \"extensible_variant_test.res\", line 81, characters 7-14", -1, to_int({
Mocha.test("test_string", () => Test_utils.eq("File \"extensible_variant_test.res\", line 79, characters 7-14", -1, to_int({
RE_EXN_ID: Str,
_1: "x"
})));
Expand All @@ -109,15 +109,15 @@ Mocha.describe("Extensible_variant_test", () => {
"x-coord": 1,
y: 2
};
Test_utils.eq("File \"extensible_variant_test.res\", line 86, characters 7-14", 1, point["x-coord"]);
Test_utils.eq("File \"extensible_variant_test.res\", line 88, characters 6-13", 3, Point === Point ? 3 : 0);
Test_utils.eq("File \"extensible_variant_test.res\", line 84, characters 7-14", 1, point["x-coord"]);
Test_utils.eq("File \"extensible_variant_test.res\", line 86, characters 6-13", 3, Point === Point ? 3 : 0);
});
Mocha.test("rebind", () => {
Test_utils.eq("File \"extensible_variant_test.res\", line 98, characters 7-14", -1, to_int({
Test_utils.eq("File \"extensible_variant_test.res\", line 96, characters 7-14", -1, to_int({
RE_EXN_ID: Str,
_1: "x"
}));
Test_utils.eq("File \"extensible_variant_test.res\", line 99, characters 7-14", true, Primitive_object.equal({
Test_utils.eq("File \"extensible_variant_test.res\", line 97, characters 7-14", true, Primitive_object.equal({
RE_EXN_ID: Str,
_1: "x"
}, {
Expand All @@ -126,18 +126,18 @@ Mocha.describe("Extensible_variant_test", () => {
}));
});
Mocha.test("indexed extension", () => {
Test_utils.eq("File \"extensible_variant_test.res\", line 103, characters 7-14", 0, defaultOf({
Test_utils.eq("File \"extensible_variant_test.res\", line 101, characters 7-14", 0, defaultOf({
RE_EXN_ID: IntKey
}));
Test_utils.eq("File \"extensible_variant_test.res\", line 104, characters 7-14", "", defaultOf({
Test_utils.eq("File \"extensible_variant_test.res\", line 102, characters 7-14", "", defaultOf({
RE_EXN_ID: StringKey
}));
});
Mocha.test("private extensible", () => {
Test_utils.eq("File \"extensible_variant_test.res\", line 108, characters 7-14", "A", privateName({
Test_utils.eq("File \"extensible_variant_test.res\", line 106, characters 7-14", "A", privateName({
RE_EXN_ID: A
}));
Test_utils.eq("File \"extensible_variant_test.res\", line 109, characters 7-14", "other", privateName(b));
Test_utils.eq("File \"extensible_variant_test.res\", line 107, characters 7-14", "other", privateName(b));
});
Mocha.test("local exceptions are generative", () => {
let match = makeException();
Expand All @@ -149,10 +149,10 @@ Mocha.describe("Extensible_variant_test", () => {
caught = Primitive_exceptions.internalToException(raw_exn);
}
if (caught !== undefined) {
Test_utils.eq("File \"extensible_variant_test.res\", line 120, characters 9-16", true, match[1](caught));
return Test_utils.eq("File \"extensible_variant_test.res\", line 121, characters 9-16", false, match$1[1](caught));
Test_utils.eq("File \"extensible_variant_test.res\", line 118, characters 9-16", true, match[1](caught));
return Test_utils.eq("File \"extensible_variant_test.res\", line 119, characters 9-16", false, match$1[1](caught));
} else {
return Test_utils.ok("File \"extensible_variant_test.res\", line 122, characters 17-24", false);
return Test_utils.ok("File \"extensible_variant_test.res\", line 120, characters 17-24", false);
}
});
});
Expand Down
14 changes: 6 additions & 8 deletions tests/tests/src/extensible_variant_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,12 @@ type key<_> = ..
type key<_> += IntKey: key<int>
type key<_> += StringKey: key<string>

let defaultOf:
type a. key<a> => option<a> =
key =>
switch key {
| IntKey => Some(0)
| StringKey => Some("")
| _ => None
}
let defaultOf: type a. key<a> => option<a> = key =>
switch key {
| IntKey => Some(0)
| StringKey => Some("")
| _ => None
}

// Outside code can match a private extensible type but not extend it.
module Private: {
Expand Down
54 changes: 27 additions & 27 deletions tests/tests/src/gadt_test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ function depth(n) {

Mocha.describe("Gadt_test", () => {
Mocha.test("evaluator", () => {
Test_utils.eq("File \"gadt_test.res\", line 203, characters 7-14", 10, $$eval({
Test_utils.eq("File \"gadt_test.res\", line 187, characters 7-14", 10, $$eval({
TAG: "If",
_0: {
TAG: "Equal",
Expand Down Expand Up @@ -260,7 +260,7 @@ Mocha.describe("Gadt_test", () => {
_0: 20
}
}));
Test_utils.eq("File \"gadt_test.res\", line 204, characters 7-14", true, $$eval({
Test_utils.eq("File \"gadt_test.res\", line 188, characters 7-14", true, $$eval({
TAG: "First",
_0: {
TAG: "Pair",
Expand All @@ -274,7 +274,7 @@ Mocha.describe("Gadt_test", () => {
}
}
}));
Test_utils.eq("File \"gadt_test.res\", line 205, characters 7-14", [
Test_utils.eq("File \"gadt_test.res\", line 189, characters 7-14", [
3,
false
], $$eval({
Expand Down Expand Up @@ -304,7 +304,7 @@ Mocha.describe("Gadt_test", () => {
}));
});
Mocha.test("type witnesses", () => {
Test_utils.eq("File \"gadt_test.res\", line 210, characters 6-13", `(1, "a")::(2, "b")::[]`, show({
Test_utils.eq("File \"gadt_test.res\", line 194, characters 6-13", `(1, "a")::(2, "b")::[]`, show({
TAG: "TList",
_0: {
TAG: "TPair",
Expand All @@ -318,29 +318,29 @@ Mocha.describe("Gadt_test", () => {
2,
"b"
], tl: /* [] */0}}));
Test_utils.eq("File \"gadt_test.res\", line 214, characters 7-14", "Some(None)", show({
Test_utils.eq("File \"gadt_test.res\", line 198, characters 7-14", "Some(None)", show({
TAG: "TOption",
_0: {
TAG: "TOption",
_0: "TInt"
}
}, Primitive_option.some(undefined)));
Test_utils.eq("File \"gadt_test.res\", line 215, characters 7-14", 0, 0);
Test_utils.eq("File \"gadt_test.res\", line 216, characters 7-14", "", "");
Test_utils.eq("File \"gadt_test.res\", line 217, characters 7-14", /* [] */0, /* [] */0);
Test_utils.eq("File \"gadt_test.res\", line 218, characters 7-14", undefined, undefined);
Test_utils.eq("File \"gadt_test.res\", line 219, characters 7-14", "int", "int");
Test_utils.eq("File \"gadt_test.res\", line 199, characters 7-14", 0, 0);
Test_utils.eq("File \"gadt_test.res\", line 200, characters 7-14", "", "");
Test_utils.eq("File \"gadt_test.res\", line 201, characters 7-14", /* [] */0, /* [] */0);
Test_utils.eq("File \"gadt_test.res\", line 202, characters 7-14", undefined, undefined);
Test_utils.eq("File \"gadt_test.res\", line 203, characters 7-14", "int", "int");
});
Mocha.test("equality witness", () => {
Test_utils.eq("File \"gadt_test.res\", line 223, characters 7-14", 42, 42);
Test_utils.ok("File \"gadt_test.res\", line 224, characters 7-14", equalTy({
Test_utils.eq("File \"gadt_test.res\", line 207, characters 7-14", 42, 42);
Test_utils.ok("File \"gadt_test.res\", line 208, characters 7-14", equalTy({
TAG: "TList",
_0: "TInt"
}, {
TAG: "TList",
_0: "TInt"
}) !== undefined);
Test_utils.ok("File \"gadt_test.res\", line 225, characters 7-14", equalTy({
Test_utils.ok("File \"gadt_test.res\", line 209, characters 7-14", equalTy({
TAG: "TPair",
_0: "TInt",
_1: "TString"
Expand Down Expand Up @@ -371,20 +371,20 @@ Mocha.describe("Gadt_test", () => {
_1: {hd: 1, tl: {hd: 2, tl: /* [] */0}}
}
];
Test_utils.eq("File \"gadt_test.res\", line 230, characters 7-14", [
Test_utils.eq("File \"gadt_test.res\", line 214, characters 7-14", [
1,
undefined,
undefined
], values.map(v => fromDynamic("TInt", v)));
Test_utils.eq("File \"gadt_test.res\", line 231, characters 7-14", [
Test_utils.eq("File \"gadt_test.res\", line 215, characters 7-14", [
undefined,
undefined,
{hd: 1, tl: {hd: 2, tl: /* [] */0}}
], values.map(v => fromDynamic({
TAG: "TList",
_0: "TInt"
}, v)));
Test_utils.eq("File \"gadt_test.res\", line 233, characters 6-13", [
Test_utils.eq("File \"gadt_test.res\", line 217, characters 6-13", [
"1",
"s"
], showAll([
Expand All @@ -399,13 +399,13 @@ Mocha.describe("Gadt_test", () => {
show: s => s
}
]));
Test_utils.eq("File \"gadt_test.res\", line 240, characters 7-14", "7", showBoxed({
Test_utils.eq("File \"gadt_test.res\", line 224, characters 7-14", "7", showBoxed({
TAG: "Boxed",
_0: ShowInt,
_1: 7
}));
});
Mocha.test("typed printf", () => Test_utils.eq("File \"gadt_test.res\", line 244, characters 7-14", "x=1, y=b", format({
Mocha.test("typed printf", () => Test_utils.eq("File \"gadt_test.res\", line 228, characters 7-14", "x=1, y=b", format({
TAG: "Text",
_0: "x=",
_1: {
Expand All @@ -421,28 +421,28 @@ Mocha.describe("Gadt_test", () => {
}
}, "")(1)("b")));
Mocha.test("representation attributes", () => {
Test_utils.eq("File \"gadt_test.res\", line 248, characters 7-14", 3, 3);
Test_utils.eq("File \"gadt_test.res\", line 249, characters 7-14", "s", "s");
Test_utils.eq("File \"gadt_test.res\", line 250, characters 7-14", {
Test_utils.eq("File \"gadt_test.res\", line 232, characters 7-14", 3, 3);
Test_utils.eq("File \"gadt_test.res\", line 233, characters 7-14", "s", "s");
Test_utils.eq("File \"gadt_test.res\", line 234, characters 7-14", {
TAG: "int",
v: 3
}, {
TAG: "int",
v: 3
});
Test_utils.eq("File \"gadt_test.res\", line 251, characters 7-14", {
Test_utils.eq("File \"gadt_test.res\", line 235, characters 7-14", {
kind: "Circle",
radius: 1.5
}, {
kind: "Circle",
radius: 1.5
});
Test_utils.eq("File \"gadt_test.res\", line 252, characters 7-14", "hi", "hi");
Test_utils.eq("File \"gadt_test.res\", line 253, characters 7-14", 2, 2);
Test_utils.eq("File \"gadt_test.res\", line 254, characters 7-14", "u", "u");
Test_utils.eq("File \"gadt_test.res\", line 255, characters 7-14", "u", "u");
Test_utils.eq("File \"gadt_test.res\", line 236, characters 7-14", "hi", "hi");
Test_utils.eq("File \"gadt_test.res\", line 237, characters 7-14", 2, 2);
Test_utils.eq("File \"gadt_test.res\", line 238, characters 7-14", "u", "u");
Test_utils.eq("File \"gadt_test.res\", line 239, characters 7-14", "u", "u");
});
Mocha.test("polymorphic recursion", () => Test_utils.eq("File \"gadt_test.res\", line 259, characters 7-14", 2, depth({
Mocha.test("polymorphic recursion", () => Test_utils.eq("File \"gadt_test.res\", line 243, characters 7-14", 2, depth({
TAG: "Nest",
_0: {
TAG: "Nest",
Expand Down
Loading
Loading