From 0c15e25394cc45e68234a4f49798e5df7a81c08f Mon Sep 17 00:00:00 2001 From: Nicolas Ojeda Bar Date: Thu, 2 Jun 2016 12:37:11 +0200 Subject: [PATCH] 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. --- typing/typecore.ml | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/typing/typecore.ml b/typing/typecore.ml index ca43f9f93..2a0be10ae 100644 --- a/typing/typecore.ml +++ b/typing/typecore.ml @@ -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