Skip to content

Commit 9e39774

Browse files
IwanKaramazowIwan
andauthored
Fix printing of optional labeled args in outcome arrow types. (rescript-lang#446)
Fixes rescript-lang/syntax#445 **before** ``` (~?x: 'a, ~y: 'b) => option<'a> ``` **after** ``` (~?x: 'a, ~y: 'b) => option<'a> ``` Co-authored-by: Iwan <[email protected]>
1 parent 4e269dc commit 9e39774

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

syntax/src/res_outcome_printer.ml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ let printPolyVarIdent txt =
348348
| [], [] -> Doc.nil
349349
| labels, types ->
350350
let i = ref 0 in
351-
let package = Doc.join ~sep:Doc.line (List.map2 (fun lbl typ ->
351+
let package = Doc.join ~sep:Doc.line ((List.map2 [@doesNotRaise]) (fun lbl typ ->
352352
Doc.concat [
353353
Doc.text (if i.contents > 0 then "and " else "with ");
354354
Doc.text lbl;
@@ -376,13 +376,21 @@ let printPolyVarIdent txt =
376376
let (typArgs, typ) = collectArrowArgs typ [] in
377377
let args = Doc.join ~sep:(Doc.concat [Doc.comma; Doc.line]) (
378378
List.map (fun (lbl, typ) ->
379-
if lbl = "" then
379+
let lblLen = String.length lbl in
380+
if lblLen = 0 then
380381
printOutTypeDoc typ
381382
else
383+
let (lbl, optionalIndicator) =
384+
(* the ocaml compiler hardcodes the optional label inside the string of the label in printtyp.ml *)
385+
match String.unsafe_get lbl 0 with
386+
| '?' -> ((String.sub [@doesNotRaise]) lbl 1 (lblLen - 1) , Doc.text "=?")
387+
| _ -> (lbl, Doc.nil)
388+
in
382389
Doc.group (
383390
Doc.concat [
384391
Doc.text ("~" ^ lbl ^ ": ");
385-
printOutTypeDoc typ
392+
printOutTypeDoc typ;
393+
optionalIndicator
386394
]
387395
)
388396
) typArgs

syntax/tests/oprint/expected/oprint.resi.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,4 +491,5 @@ and ASet: {
491491
let find_last_opt: (elt => bool, t) => option<elt>
492492
let of_list: list<elt> => t
493493
}
494-
type emptyObject = {.}
494+
type emptyObject = {.}
495+
let f: (~x: 'a=?, ~y: 'b) => option<'a>

syntax/tests/oprint/oprint.res

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,3 +334,5 @@ module rec A: {
334334
and ASet: Set.S with type elt = A.t = Set.Make(A)
335335

336336
type emptyObject = {.}
337+
338+
let f = (~x=?, ~y as _) => x

0 commit comments

Comments
 (0)