Skip to content

Commit

Permalink
Better ghost locs for default parameters.
Browse files Browse the repository at this point in the history
When translating a parse tree to the corresponding typed tree
a function definition

  fun ?(arg = foo) -> e

is translated to (using ocaml syntax):

  fun *opt* ->
    let arg =
      match *opt* with
      | Some sth -> sth
      | None -> foo
    in
    e

Currently the match term is given the location of the whole function term
(including e), which is incorrect and trips cmt-based tools that work based on
location.

This patch gives a location to the match construct corresponding to
the fragment 'arg = foo' in the original program.
  • Loading branch information
nojb committed Jun 2, 2016
1 parent a4e355a commit 0c15e25
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions typing/typecore.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2071,11 +2071,16 @@ and type_expect_ ?in_function ?(recarg=Rejected) env sexp ty_expected =
default;
]
in
let sloc =
{ Location.loc_start = spat.ppat_loc.Location.loc_start;
loc_end = default_loc.Location.loc_end;
loc_ghost = true }
in
let smatch =
Exp.match_ ~loc (Exp.ident ~loc (mknoloc (Longident.Lident "*opt*")))
Exp.match_ ~loc:sloc (Exp.ident ~loc (mknoloc (Longident.Lident "*opt*")))
scases
in
let pat = Pat.var ~loc (mknoloc "*opt*") in
let pat = Pat.var ~loc:sloc (mknoloc "*opt*") in
let body =
Exp.let_ ~loc Nonrecursive ~attrs:[mknoloc "#default",PStr []]
[Vb.mk spat smatch] sbody
Expand Down

0 comments on commit 0c15e25

Please sign in to comment.