Skip to content

Commit 53e4fa3

Browse files
authored
Merge pull request #569 from NathanReb/fix-constant-loc-migration
Fix location inserted when migrating constants from 5.2 to 5.3
2 parents 37d7ee1 + ce758fe commit 53e4fa3

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
unreleased
22
----------
33

4+
- Fix 5.2 -> 5.3 migration of constants. Those used to always have a `none`
5+
location which can lead to unhelpful error messages.
6+
(#569, @NathanReb)
7+
48
0.36.0 (2025-03-03)
59
-------------------
610

astlib/migrate_502_503.ml

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,30 @@ and copy_expression :
5151
Ast_502.Parsetree.pexp_loc_stack;
5252
Ast_502.Parsetree.pexp_attributes;
5353
} ->
54+
let loc = copy_location pexp_loc in
5455
{
55-
Ast_503.Parsetree.pexp_desc = copy_expression_desc pexp_desc;
56-
Ast_503.Parsetree.pexp_loc = copy_location pexp_loc;
56+
Ast_503.Parsetree.pexp_desc = copy_expression_desc_with_loc ~loc pexp_desc;
57+
Ast_503.Parsetree.pexp_loc = loc;
5758
Ast_503.Parsetree.pexp_loc_stack = copy_location_stack pexp_loc_stack;
5859
Ast_503.Parsetree.pexp_attributes = copy_attributes pexp_attributes;
5960
}
6061

6162
and copy_expression_desc :
6263
Ast_502.Parsetree.expression_desc -> Ast_503.Parsetree.expression_desc =
63-
function
64+
fun desc -> copy_expression_desc_with_loc ~loc:Location.none desc
65+
66+
and copy_expression_desc_with_loc :
67+
loc:Location.t ->
68+
Ast_502.Parsetree.expression_desc ->
69+
Ast_503.Parsetree.expression_desc =
70+
fun ~loc desc ->
71+
match desc with
6472
| Ast_502.Parsetree.Pexp_ident x0 ->
6573
Ast_503.Parsetree.Pexp_ident (copy_loc copy_Longident_t x0)
6674
| Ast_502.Parsetree.Pexp_constant x0 ->
67-
Ast_503.Parsetree.Pexp_constant (copy_constant x0)
75+
let loc = { loc with loc_ghost = true } in
76+
let constant = { (copy_constant x0) with pconst_loc = loc } in
77+
Ast_503.Parsetree.Pexp_constant constant
6878
| Ast_502.Parsetree.Pexp_let (x0, x1, x2) ->
6979
Ast_503.Parsetree.Pexp_let
7080
(copy_rec_flag x0, List.map copy_value_binding x1, copy_expression x2)
@@ -272,24 +282,38 @@ and copy_pattern : Ast_502.Parsetree.pattern -> Ast_503.Parsetree.pattern =
272282
Ast_502.Parsetree.ppat_loc_stack;
273283
Ast_502.Parsetree.ppat_attributes;
274284
} ->
285+
let loc = copy_location ppat_loc in
275286
{
276-
Ast_503.Parsetree.ppat_desc = copy_pattern_desc ppat_desc;
277-
Ast_503.Parsetree.ppat_loc = copy_location ppat_loc;
287+
Ast_503.Parsetree.ppat_desc = copy_pattern_desc_with_loc ~loc ppat_desc;
288+
Ast_503.Parsetree.ppat_loc = loc;
278289
Ast_503.Parsetree.ppat_loc_stack = copy_location_stack ppat_loc_stack;
279290
Ast_503.Parsetree.ppat_attributes = copy_attributes ppat_attributes;
280291
}
281292

282293
and copy_pattern_desc :
283-
Ast_502.Parsetree.pattern_desc -> Ast_503.Parsetree.pattern_desc = function
294+
Ast_502.Parsetree.pattern_desc -> Ast_503.Parsetree.pattern_desc =
295+
fun desc -> copy_pattern_desc_with_loc ~loc:Location.none desc
296+
297+
and copy_pattern_desc_with_loc :
298+
loc:Location.t ->
299+
Ast_502.Parsetree.pattern_desc ->
300+
Ast_503.Parsetree.pattern_desc =
301+
fun ~loc desc ->
302+
match desc with
284303
| Ast_502.Parsetree.Ppat_any -> Ast_503.Parsetree.Ppat_any
285304
| Ast_502.Parsetree.Ppat_var x0 ->
286305
Ast_503.Parsetree.Ppat_var (copy_loc (fun x -> x) x0)
287306
| Ast_502.Parsetree.Ppat_alias (x0, x1) ->
288307
Ast_503.Parsetree.Ppat_alias (copy_pattern x0, copy_loc (fun x -> x) x1)
289308
| Ast_502.Parsetree.Ppat_constant x0 ->
290-
Ast_503.Parsetree.Ppat_constant (copy_constant x0)
309+
let loc = { loc with loc_ghost = true } in
310+
let constant = { (copy_constant x0) with pconst_loc = loc } in
311+
Ast_503.Parsetree.Ppat_constant constant
291312
| Ast_502.Parsetree.Ppat_interval (x0, x1) ->
292-
Ast_503.Parsetree.Ppat_interval (copy_constant x0, copy_constant x1)
313+
let loc = { loc with loc_ghost = true } in
314+
let constant0 = { (copy_constant x0) with pconst_loc = loc } in
315+
let constant1 = { (copy_constant x1) with pconst_loc = loc } in
316+
Ast_503.Parsetree.Ppat_interval (constant0, constant1)
293317
| Ast_502.Parsetree.Ppat_tuple x0 ->
294318
Ast_503.Parsetree.Ppat_tuple (List.map copy_pattern x0)
295319
| Ast_502.Parsetree.Ppat_construct (x0, x1) ->

0 commit comments

Comments
 (0)