Skip to content

Commit e53ab8a

Browse files
committed
Capture pc_loc instead of pc_bar
1 parent b11f460 commit e53ab8a

14 files changed

+56
-29
lines changed

analysis/src/Codemod.ml

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ let transform ~path ~pos ~debug ~typ ~hint =
1919
let cases =
2020
collectPatterns pattern
2121
|> List.map (fun (p : Parsetree.pattern) ->
22-
Ast_helper.Exp.case p (TypeUtils.Codegen.mkFailWithExp ()))
22+
Ast_helper.Exp.case p.ppat_loc p
23+
(TypeUtils.Codegen.mkFailWithExp ()))
2324
in
2425
let result = ref None in
2526
let mkIterator ~pos ~result =

analysis/src/TypeUtils.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1044,7 +1044,7 @@ module Codegen = struct
10441044
Some
10451045
(patterns
10461046
|> List.map (fun (pat : Parsetree.pattern) ->
1047-
Ast_helper.Exp.case pat (mkFailWithExp ())))
1047+
Ast_helper.Exp.case pat.ppat_loc pat (mkFailWithExp ())))
10481048
end
10491049

10501050
let getModulePathRelativeToEnv ~debug ~(env : QueryEnv.t) ~envFromItem path =

analysis/src/Xform.ml

+7-2
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,13 @@ module IfThenElse = struct
105105
let mkMatch ~arg ~pat =
106106
let cases =
107107
[
108-
Ast_helper.Exp.case pat e1;
109-
Ast_helper.Exp.case (Ast_helper.Pat.any ()) e2;
108+
Ast_helper.Exp.case
109+
{
110+
pat.Parsetree.ppat_loc with
111+
Location.loc_end = e1.pexp_loc.loc_end;
112+
}
113+
pat e1;
114+
Ast_helper.Exp.case e2.pexp_loc (Ast_helper.Pat.any ()) e2;
110115
]
111116
in
112117
Ast_helper.Exp.match_ ~loc:e.pexp_loc ~attrs:e.pexp_attributes arg

compiler/frontend/bs_ast_mapper.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -539,9 +539,9 @@ let default_mapper =
539539
~attrs:(this.attributes this pld_attributes));
540540
cases = (fun this l -> List.map (this.case this) l);
541541
case =
542-
(fun this {pc_bar; pc_lhs; pc_guard; pc_rhs} ->
542+
(fun this {pc_loc; pc_lhs; pc_guard; pc_rhs} ->
543543
{
544-
pc_bar;
544+
pc_loc = this.location this pc_loc;
545545
pc_lhs = this.pat this pc_lhs;
546546
pc_guard = map_opt (this.expr this) pc_guard;
547547
pc_rhs = this.expr this pc_rhs;

compiler/frontend/bs_builtin_ppx.ml

+9-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,15 @@ let expr_mapper ~async_context ~in_function_def (self : mapper)
167167
pexp_desc =
168168
Pexp_match
169169
( pvb_expr,
170-
[{pc_bar = None; pc_lhs = p; pc_guard = None; pc_rhs = body}] );
170+
[
171+
{
172+
pc_loc =
173+
{p.ppat_loc with Location.loc_end = body.pexp_loc.loc_end};
174+
pc_lhs = p;
175+
pc_guard = None;
176+
pc_rhs = body;
177+
};
178+
] );
171179
pexp_attributes = e.pexp_attributes @ pvb_attributes;
172180
})
173181
(* let [@warning "a"] {a;b} = c in body

compiler/ml/ast_helper.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,8 @@ module Exp = struct
208208
jsx_container_element_closing_tag = e;
209209
}))
210210

211-
let case ?bar lhs ?guard rhs =
212-
{pc_bar = bar; pc_lhs = lhs; pc_guard = guard; pc_rhs = rhs}
211+
let case loc lhs ?guard rhs =
212+
{pc_loc = loc; pc_lhs = lhs; pc_guard = guard; pc_rhs = rhs}
213213

214214
let make_list_expression loc seq ext_opt =
215215
let rec handle_seq = function

compiler/ml/ast_helper.mli

+1-2
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,7 @@ module Exp : sig
231231
Parsetree.jsx_closing_container_tag option ->
232232
expression
233233

234-
val case :
235-
?bar:Lexing.position -> pattern -> ?guard:expression -> expression -> case
234+
val case : Location.t -> pattern -> ?guard:expression -> expression -> case
236235
val await : ?loc:loc -> ?attrs:attrs -> expression -> expression
237236

238237
val make_list_expression :

compiler/ml/ast_mapper.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -488,9 +488,9 @@ let default_mapper =
488488
~attrs:(this.attributes this pld_attributes));
489489
cases = (fun this l -> List.map (this.case this) l);
490490
case =
491-
(fun this {pc_bar; pc_lhs; pc_guard; pc_rhs} ->
491+
(fun this {pc_loc; pc_lhs; pc_guard; pc_rhs} ->
492492
{
493-
pc_bar;
493+
pc_loc = this.location this pc_loc;
494494
pc_lhs = this.pat this pc_lhs;
495495
pc_guard = map_opt (this.expr this) pc_guard;
496496
pc_rhs = this.expr this pc_rhs;

compiler/ml/ast_mapper_from0.ml

+3-1
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,9 @@ let default_mapper =
665665
case =
666666
(fun this {pc_lhs; pc_guard; pc_rhs} ->
667667
{
668-
pc_bar = None;
668+
pc_loc =
669+
this.location this
670+
{pc_lhs.ppat_loc with Location.loc_end = pc_rhs.pexp_loc.loc_end};
669671
pc_lhs = this.pat this pc_lhs;
670672
pc_guard = map_opt (this.expr this) pc_guard;
671673
pc_rhs = this.expr this pc_rhs;

compiler/ml/parsetree.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ and jsx_closing_container_tag = {
381381

382382
and case = {
383383
(* (P -> E) or (P when E0 -> E) *)
384-
pc_bar: Lexing.position option;
384+
pc_loc: Location.t;
385385
pc_lhs: pattern;
386386
pc_guard: expression option;
387387
pc_rhs: expression;

compiler/ml/printast.ml

+2-4
Original file line numberDiff line numberDiff line change
@@ -681,10 +681,8 @@ and longident_x_pattern i ppf (li, p, opt) =
681681
line i ppf "%a%s\n" fmt_longident_loc li (if opt then "?" else "");
682682
pattern (i + 1) ppf p
683683

684-
and case i ppf {pc_bar; pc_lhs; pc_guard; pc_rhs} =
685-
line i ppf "<case>\n";
686-
pc_bar
687-
|> Option.iter (fun bar -> line i ppf "| %a\n" (fmt_position false) bar);
684+
and case i ppf {pc_loc; pc_lhs; pc_guard; pc_rhs} =
685+
line i ppf "<case> %a\n" fmt_location pc_loc;
688686
pattern (i + 1) ppf pc_lhs;
689687
(match pc_guard with
690688
| None -> ()

compiler/ml/typecore.ml

+16-5
Original file line numberDiff line numberDiff line change
@@ -2371,7 +2371,14 @@ and type_expect_ ?type_clash_context ?in_function ?(recarg = Rejected) env sexp
23712371
type_expect ?in_function env
23722372
{
23732373
sexp with
2374-
pexp_desc = Pexp_match (sval, [Ast_helper.Exp.case spat sbody]);
2374+
pexp_desc =
2375+
Pexp_match
2376+
( sval,
2377+
[
2378+
Ast_helper.Exp.case
2379+
{spat.ppat_loc with Location.loc_end = sbody.pexp_loc.loc_end}
2380+
spat sbody;
2381+
] );
23752382
}
23762383
ty_expected
23772384
| Pexp_let (rec_flag, spat_sexp_list, sbody) ->
@@ -2414,12 +2421,12 @@ and type_expect_ ?type_clash_context ?in_function ?(recarg = Rejected) env sexp
24142421
let default_loc = default.pexp_loc in
24152422
let scases =
24162423
[
2417-
Exp.case
2424+
Exp.case default_loc
24182425
(Pat.construct ~loc:default_loc
24192426
(mknoloc Longident.(Ldot (Lident "*predef*", "Some")))
24202427
(Some (Pat.var ~loc:default_loc (mknoloc "*sth*"))))
24212428
(Exp.ident ~loc:default_loc (mknoloc (Longident.Lident "*sth*")));
2422-
Exp.case
2429+
Exp.case default_loc
24232430
(Pat.construct ~loc:default_loc
24242431
(mknoloc Longident.(Ldot (Lident "*predef*", "None")))
24252432
None)
@@ -2447,13 +2454,17 @@ and type_expect_ ?type_clash_context ?in_function ?(recarg = Rejected) env sexp
24472454
in
24482455
type_function ?in_function ~arity ~async loc sexp.pexp_attributes env
24492456
ty_expected l
2450-
[Exp.case pat body]
2457+
[Exp.case sloc pat body]
24512458
| Pexp_fun
24522459
{arg_label = l; default = None; lhs = spat; rhs = sbody; arity; async} ->
24532460
let l = Asttypes.to_noloc l in
24542461
type_function ?in_function ~arity ~async loc sexp.pexp_attributes env
24552462
ty_expected l
2456-
[Ast_helper.Exp.case spat sbody]
2463+
[
2464+
Ast_helper.Exp.case
2465+
{spat.ppat_loc with Location.loc_end = sbody.pexp_loc.loc_end}
2466+
spat sbody;
2467+
]
24572468
| Pexp_apply {funct = sfunct; args = sargs; partial} ->
24582469
assert (sargs <> []);
24592470
begin_def ();

compiler/syntax/src/jsx_v4.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -511,12 +511,12 @@ let vb_match ~expr (name, default, _, alias, loc, _) =
511511
(Exp.match_
512512
(Exp.ident {txt = Lident ("__" ^ alias); loc = Location.none})
513513
[
514-
Exp.case
514+
Exp.case Location.none
515515
(Pat.construct
516516
(Location.mknoloc @@ Lident "Some")
517517
(Some (Pat.var (Location.mknoloc label))))
518518
(Exp.ident (Location.mknoloc @@ Lident label));
519-
Exp.case
519+
Exp.case Location.none
520520
(Pat.construct (Location.mknoloc @@ Lident "None") None)
521521
default;
522522
])

compiler/syntax/src/res_core.ml

+6-3
Original file line numberDiff line numberDiff line change
@@ -3431,8 +3431,10 @@ and parse_if_let_expr start_pos p =
34313431
~attrs:[if_let_attr; suppress_fragile_match_warning_attr]
34323432
~loc condition_expr
34333433
[
3434-
Ast_helper.Exp.case pattern then_expr;
3435-
Ast_helper.Exp.case (Ast_helper.Pat.any ()) else_expr;
3434+
Ast_helper.Exp.case
3435+
{pattern.ppat_loc with Location.loc_end = loc.loc_end}
3436+
pattern then_expr;
3437+
Ast_helper.Exp.case else_expr.pexp_loc (Ast_helper.Pat.any ()) else_expr;
34363438
]
34373439

34383440
and parse_if_or_if_let_expression p =
@@ -3557,7 +3559,8 @@ and parse_pattern_match_case p =
35573559
let rhs = parse_expr_block p in
35583560
Parser.end_region p;
35593561
Parser.eat_breadcrumb p;
3560-
Some (Ast_helper.Exp.case ~bar lhs ?guard rhs)
3562+
let loc = mk_loc bar rhs.pexp_loc.loc_end in
3563+
Some (Ast_helper.Exp.case loc lhs ?guard rhs)
35613564
| _ ->
35623565
Parser.end_region p;
35633566
Parser.eat_breadcrumb p;

0 commit comments

Comments
 (0)