Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit 45693d2

Browse files
committed
Use x?: t
1 parent c63422d commit 45693d2

File tree

7 files changed

+21
-29
lines changed

7 files changed

+21
-29
lines changed

compiler-libs-406/oprint.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ and print_typargs ppf =
390390
pp_close_box ppf ();
391391
pp_print_space ppf ()
392392
and print_out_label ppf (name, mut, opt, arg) =
393-
fprintf ppf "@[<2>%s%s%s :@ %a@];" (if opt then "@optional " else "") (if mut then "mutable " else "") name
393+
fprintf ppf "@[<2>%s%s%s :@ %a@];" (if mut then "mutable " else "") name (if opt then "?" else "")
394394
print_out_type arg
395395

396396
let out_type = ref print_out_type

src/res_core.ml

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,6 @@ let makePatternOptional ~optional (p : Parsetree.pattern) =
170170
if optional then {p with ppat_attributes = optionalAttr :: p.ppat_attributes}
171171
else p
172172

173-
let makeTypeOptional ~optional (t : Parsetree.core_type) =
174-
if optional then {t with ptyp_attributes = optionalAttr :: t.ptyp_attributes}
175-
else t
176-
177173
let suppressFragileMatchWarningAttr =
178174
( Location.mknoloc "warning",
179175
Parsetree.PStr
@@ -4292,32 +4288,20 @@ and parseFieldDeclarationRegion p =
42924288
| Lident _ ->
42934289
let lident, loc = parseLident p in
42944290
let name = Location.mkloc lident loc in
4291+
let optional = parseOptionalLabel p in
42954292
let typ =
42964293
match p.Parser.token with
42974294
| Colon ->
42984295
Parser.next p;
4299-
let optional = parseOptionalLabel p in
4300-
let t = parsePolyTypeExpr p in
4301-
makeTypeOptional ~optional t
4296+
parsePolyTypeExpr p
43024297
| _ ->
4303-
Ast_helper.Typ.constr ~loc:name.loc {name with txt = Lident name.txt} []
4298+
Ast_helper.Typ.constr ~loc:name.loc ~attrs
4299+
{name with txt = Lident name.txt}
4300+
[]
43044301
in
43054302
let loc = mkLoc startPos typ.ptyp_loc.loc_end in
4303+
let attrs = if optional then optionalAttr :: attrs else attrs in
43064304
Some (Ast_helper.Type.field ~attrs ~loc ~mut name typ)
4307-
| Question -> (
4308-
Parser.next p;
4309-
match p.token with
4310-
| Lident _ ->
4311-
let lident, loc = parseLident p in
4312-
let name = Location.mkloc lident loc in
4313-
let typ =
4314-
Ast_helper.Typ.constr ~loc:name.loc ~attrs:[optionalAttr]
4315-
{name with txt = Lident name.txt}
4316-
[]
4317-
in
4318-
let loc = mkLoc startPos typ.ptyp_loc.loc_end in
4319-
Some (Ast_helper.Type.field ~attrs ~loc ~mut name typ)
4320-
| _ -> None)
43214305
| _ -> None
43224306

43234307
(* record-decl ::=

src/res_printer.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1430,15 +1430,15 @@ and printLabelDeclaration (ld : Parsetree.label_declaration) cmtTbl =
14301430
let doc = printIdentLike ld.pld_name.txt in
14311431
printComments doc cmtTbl ld.pld_name.loc
14321432
in
1433-
let optional = printOptionalLabel ld.pld_type.ptyp_attributes in
1433+
let optional = printOptionalLabel ld.pld_attributes in
14341434
Doc.group
14351435
(Doc.concat
14361436
[
14371437
attrs;
14381438
mutableFlag;
14391439
name;
1440-
Doc.text ": ";
14411440
optional;
1441+
Doc.text ": ";
14421442
printTypExpr ld.pld_type cmtTbl;
14431443
])
14441444

tests/parsing/grammar/expressions/expected/record.res.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ let _ =
3232
z = (((None : tt))[@ns.optional ]) } -> 11
3333
| { name = ((name)[@ns.optional ]); x = 3 } -> 42
3434
| { name = ((name)[@ns.optional ]); x = 3 } -> 4242
35+
type nonrec tt = {
36+
x: int ;
37+
y: string [@ns.opttinal ]}
3538
type nonrec ttt = {
3639
x: int ;
37-
y: ((string)[@ns.optional ]) }
40+
y: string [@ns.optional ]}

tests/parsing/grammar/expressions/record.res

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,6 @@ let _ = switch z {
4343
| {? name, x: 3} => 4242
4444
}
4545

46-
type ttt = {x:int, y: ?string}
46+
type tt = {x:int, @ns.opttinal y : string}
47+
48+
type ttt = {x:int, y?: string}

tests/printer/expr/expected/record.res.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,6 @@ let _ = switch z {
9393
| {?name, x: 3} => 4242
9494
}
9595

96-
type ttt = {x: int, y: ?string}
96+
type tt = {x: int, y?: string}
97+
98+
type ttt = {x: int, y?: string}

tests/printer/expr/record.res

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,6 @@ let _ = switch z {
8383
| {? name, x: 3} => 4242
8484
}
8585

86-
type ttt = {x:int, y: ?string}
86+
type tt = {x:int, @ns.optional y: string}
8787

88+
type ttt = {x:int, y?: string}

0 commit comments

Comments
 (0)