Skip to content

Commit 5f11257

Browse files
Merge pull request #621 from patricoferris/encoding-505
Encode all structure items in 505 migration
2 parents 18c0c1c + 46943ec commit 5f11257

File tree

5 files changed

+57
-3
lines changed

5 files changed

+57
-3
lines changed

astlib/encoding_505.ml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module Ext_name = struct
2+
let pexp_struct_item = "ppxlib.migration.pexp_struct_item_505"
3+
end
4+
5+
let invalid_encoding ~loc name =
6+
Location.raise_errorf ~loc "Invalid %s encoding" name
7+
8+
module To_504 = struct
9+
open Ast_504.Asttypes
10+
open Ast_504.Parsetree
11+
12+
let encode_pexp_struct_item ~loc ((si, e) : structure_item * expression) =
13+
let expr_as_structure_item =
14+
let pstr_desc = Pstr_eval (e, []) in
15+
{ pstr_desc; pstr_loc = loc }
16+
in
17+
let payload =
18+
let items = [ si; expr_as_structure_item ] in
19+
PStr items
20+
in
21+
let name = { txt = Ext_name.pexp_struct_item; loc } in
22+
Pexp_extension (name, payload)
23+
24+
let decode_pexp_struct_item ~loc payload =
25+
match payload with
26+
| PStr [ si; { pstr_desc = Pstr_eval (e, []); _ } ] -> (si, e)
27+
| _ -> invalid_encoding ~loc Ext_name.pexp_struct_item
28+
end

astlib/migrate_504_505.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,10 @@ and copy_expression_desc :
495495
Ast_505.Parsetree.Pexp_struct_item (opendecl, copy_expression x1)
496496
| Ast_504.Parsetree.Pexp_letop x0 ->
497497
Ast_505.Parsetree.Pexp_letop (copy_letop x0)
498+
| Ast_504.Parsetree.Pexp_extension ({ txt; loc }, payload)
499+
when String.equal txt Encoding_505.Ext_name.pexp_struct_item ->
500+
let si, e = Encoding_505.To_504.decode_pexp_struct_item ~loc payload in
501+
Pexp_struct_item (copy_structure_item si, copy_expression e)
498502
| Ast_504.Parsetree.Pexp_extension x0 ->
499503
Ast_505.Parsetree.Pexp_extension (copy_extension x0)
500504
| Ast_504.Parsetree.Pexp_unreachable -> Ast_505.Parsetree.Pexp_unreachable

astlib/migrate_505_504.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -476,9 +476,9 @@ and copy_expression_desc :
476476
copy_module_expr mexpr,
477477
copy_expression x1 )
478478
| _ ->
479-
Location.raise_errorf ~loc:x0.pstr_loc
480-
"Only structure items [open M], [exception C] and [module B = T] \
481-
are supported in [let SI in E].")
479+
let item = copy_structure_item x0 in
480+
Encoding_505.To_504.encode_pexp_struct_item ~loc:x0.pstr_loc
481+
(item, copy_expression x1))
482482
| Ast_505.Parsetree.Pexp_assert x0 ->
483483
Ast_504.Parsetree.Pexp_assert (copy_expression x0)
484484
| Ast_505.Parsetree.Pexp_lazy x0 ->

astlib/stdlib0.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ module Option = struct
77
end
88

99
module String = struct
10+
include String
11+
1012
let is_prefix t ~prefix =
1113
let rec is_prefix_from t ~prefix ~pos ~len =
1214
pos >= len

test/505_migrations/run.t

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,26 @@ it should successfully roundtrip to 5.2 and print the source code unchanged:
2828
module T = struct let x = 1 end
2929
let f = let exception E of int in let module X = T in let open X in x
3030

31+
In addition to these items, the 5.5 AST can now encode pretty much any structure item
32+
locally, except for a few (e.g. [let let ...]). We have to handle these also during migration.
33+
34+
$ cat > extra.ml << EOF
35+
> type e = ..
36+
>
37+
> let f =
38+
> let type e += Hello in
39+
> let external id : 'a -> 'a = "identity" in
40+
> let type t = int in
41+
> ()
42+
> EOF
43+
44+
$ ./driver.exe extra.ml --use-compiler-pp
45+
type e = ..
46+
let f =
47+
let type e +=
48+
| Hello in
49+
let external id : 'a -> 'a = "identity" in let type t = int in ()
50+
3151
2. Ptyp_extension
3252

3353
A new feature of OCaml 5.5 are external types (e.g. [type t = external "t"]).

0 commit comments

Comments
 (0)