Better ghost locs for default parameters.

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.
master
Nicolas Ojeda Bar 2016-06-02 12:37:11 +02:00
parent a4e355a757
commit 0c15e25394
1 changed files with 7 additions and 2 deletions

View File

@ -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