Skip to content

Commit d295be1

Browse files
type nonrec infix ext bugfix (#2712)
* fix a crash with type%e nonrec * add attributes test * changelog * fmt
1 parent e879af1 commit d295be1

File tree

11 files changed

+46
-8
lines changed

11 files changed

+46
-8
lines changed

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ profile. This started with version 0.26.0.
7777

7878
- Fix a crash on `type 'a t = A : 'a. {a: 'a} -> 'a t`. (#2710, @EmileTrotignon)
7979

80+
- Fix a crash where `type%e nonrec t = t` was formatted as `type nonrec%e t = t`,
81+
which is invalid syntax. (#2712, @EmileTrotignon)
82+
8083
### Changed
8184

8285
- `|> begin`, `~arg:begin`, `begin if`, `lazy begin`, `begin match`,

lib/Fmt_ast.ml

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3457,7 +3457,8 @@ and fmt_class_params c ctx params =
34573457
( wrap_fits_breaks c.conf "[" "]" (list_fl params fmt_param)
34583458
$ space_break ) )
34593459

3460-
and fmt_type_declaration c ?(pre = "") ?name ?(eq = "=") {ast= decl; _} =
3460+
and fmt_type_declaration c ?(kw = "") ?(nonrec_kw = "") ?name ?(eq = "=")
3461+
{ast= decl; _} =
34613462
protect c (Td decl)
34623463
@@
34633464
let { ptype_name= {txt; loc}
@@ -3497,10 +3498,10 @@ and fmt_type_declaration c ?(pre = "") ?name ?(eq = "=") {ast= decl; _} =
34973498
in
34983499
let box_manifest k =
34993500
hvbox c.conf.fmt_opts.type_decl_indent.v
3500-
( str pre
3501+
( str kw
35013502
$ fmt_extension_suffix c ext
35023503
$ fmt_attributes c attrs_before
3503-
$ str " "
3504+
$ str nonrec_kw $ str " "
35043505
$ hvbox_if
35053506
(not (List.is_empty ptype_params))
35063507
0
@@ -4265,12 +4266,12 @@ and fmt_module_statement c ~attributes ?keyword mod_expr =
42654266

42664267
and fmt_with_constraint c ctx ~pre = function
42674268
| Pwith_type (lid, td) ->
4268-
fmt_type_declaration ~pre:(pre ^ " type") c ~name:lid (sub_td ~ctx td)
4269+
fmt_type_declaration ~kw:(pre ^ " type") c ~name:lid (sub_td ~ctx td)
42694270
| Pwith_module (m1, m2) ->
42704271
str pre $ str " module " $ fmt_longident_loc c m1 $ str " = "
42714272
$ fmt_longident_loc c m2
42724273
| Pwith_typesubst (lid, td) ->
4273-
fmt_type_declaration ~pre:(pre ^ " type") c ~eq:":=" ~name:lid
4274+
fmt_type_declaration ~kw:(pre ^ " type") c ~eq:":=" ~name:lid
42744275
(sub_td ~ctx td)
42754276
| Pwith_modsubst (m1, m2) ->
42764277
str pre $ str " module " $ fmt_longident_loc c m1 $ str " := "
@@ -4550,10 +4551,12 @@ and fmt_type c ?eq rec_flag decls ctx =
45504551
let is_rec = Asttypes.is_recursive rec_flag in
45514552
let fmt_decl c ctx ~prev ~next:_ decl =
45524553
let first = Option.is_none prev in
4553-
let pre =
4554-
if first then if is_rec then "type" else "type nonrec" else "and"
4554+
let kw, nonrec_kw =
4555+
if first then
4556+
if is_rec then ("type", None) else ("type", Some " nonrec")
4557+
else ("and", None)
45554558
in
4556-
fmt_type_declaration c ~pre ?eq (sub_td ~ctx decl)
4559+
fmt_type_declaration c ~kw ?nonrec_kw ?eq (sub_td ~ctx decl)
45574560
in
45584561
let ast x = Td x in
45594562
fmt_item_list c ctx update_config ast fmt_decl decls

test/passing/refs.ahrefs/extensions-indent.ml.ref

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,3 +551,7 @@ let xxxxxx =
551551
yyyyyyyy
552552
in
553553
{ zzzzzzzzzzzzz }
554+
555+
type%e nonrec t = t
556+
557+
type%e[@a] nonrec t = t

test/passing/refs.ahrefs/extensions.ml.ref

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,3 +551,7 @@ let xxxxxx =
551551
yyyyyyyy
552552
in
553553
{ zzzzzzzzzzzzz }
554+
555+
type%e nonrec t = t
556+
557+
type%e[@a] nonrec t = t

test/passing/refs.default/extensions-indent.ml.ref

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,3 +465,6 @@ let xxxxxx =
465465
yyyyyyyy
466466
in
467467
{ zzzzzzzzzzzzz }
468+
469+
type%e nonrec t = t
470+
type%e[@a] nonrec t = t

test/passing/refs.default/extensions.ml.ref

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,3 +465,6 @@ let xxxxxx =
465465
yyyyyyyy
466466
in
467467
{ zzzzzzzzzzzzz }
468+
469+
type%e nonrec t = t
470+
type%e[@a] nonrec t = t

test/passing/refs.janestreet/extensions-indent.ml.ref

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,3 +697,6 @@ let xxxxxx =
697697
__________ *) () = yyyyyyyy in
698698
{ zzzzzzzzzzzzz }
699699
;;
700+
701+
type%e nonrec t = t
702+
type%e[@a] nonrec t = t

test/passing/refs.janestreet/extensions.ml.ref

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,3 +697,6 @@ let xxxxxx =
697697
__________ *) () = yyyyyyyy in
698698
{ zzzzzzzzzzzzz }
699699
;;
700+
701+
type%e nonrec t = t
702+
type%e[@a] nonrec t = t

test/passing/refs.ocamlformat/extensions-indent.ml.ref

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,3 +509,7 @@ let xxxxxx =
509509
yyyyyyyy
510510
in
511511
{zzzzzzzzzzzzz}
512+
513+
type%e nonrec t = t
514+
515+
type%e[@a] nonrec t = t

test/passing/refs.ocamlformat/extensions.ml.ref

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,3 +509,7 @@ let xxxxxx =
509509
yyyyyyyy
510510
in
511511
{zzzzzzzzzzzzz}
512+
513+
type%e nonrec t = t
514+
515+
type%e[@a] nonrec t = t

0 commit comments

Comments
 (0)