Skip to content

Commit daf4cd5

Browse files
committed
query imported longidents from the type environment instead of reading .cmi
This first patch does not pass the testsuite, because not all longidents used by ppx_import are actually valid (type-correct) identifiers. The testsuite contains this example: stuff.ml: module type S = sig type t = ... end use.ml: [%import Stuff.S.t] Querying a member of a type signature in this way is not valid in OCaml, although it makes sense. To support this, we will have to be more elaborate, and mix type-environment lookup with manual splitting of the path and exploration of the returned signature.
1 parent cabf2d7 commit daf4cd5

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

src/ppx_import.cppo.ml

+25-2
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,27 @@ let is_self_reference lid =
234234
in fn = mn
235235
| _ -> false
236236

237+
let lazy_env = lazy (
238+
(* It is important that the typing environment is not evaluated
239+
right away, but only once the ppx-context has been loaded from
240+
the AST, so that Config.load_path and the rest of the environment
241+
context are correctly set.
242+
243+
The environment setting should happen when reading the
244+
ppx-context attribute which is the very first structure/signature
245+
item sent to ppx rewriters. In particular, this happens before
246+
the [%import ] extensions are traversed, which are the places in
247+
this code where 'env' is forced.
248+
249+
We would also have the option to not have a globale environment, but
250+
recompute the typing environment on each [%import ] extension. We don't
251+
see any advantage in doing this, given that we compute the global/initial
252+
environment that is the same at all program points.
253+
*)
254+
Compmisc.init_path false;
255+
Compmisc.initial_env ()
256+
)
257+
237258
let type_declaration mapper type_decl =
238259
match type_decl with
239260
| { ptype_attributes; ptype_name; ptype_manifest = Some {
@@ -248,7 +269,8 @@ let type_declaration mapper type_decl =
248269
{ type_decl with ptype_manifest = Some manifest }
249270
else
250271
with_default_loc loc (fun () ->
251-
let ttype_decl = locate_ttype_decl ~loc (locate_sig ~loc lid) lid in
272+
let env = Lazy.force lazy_env in
273+
let ttype_decl = Typetexp.find_type env loc lid |> snd in
252274
let m, s = if is_self_reference lid then
253275
None, []
254276
else begin
@@ -326,7 +348,8 @@ let module_type mapper modtype_decl =
326348
{ modtype_decl with pmty_desc = Pmty_alias alias }
327349
else
328350
with_default_loc loc (fun () ->
329-
match locate_tmodtype_decl ~loc (locate_sig ~loc lid) lid with
351+
let env = Lazy.force lazy_env in
352+
match Typetexp.find_modtype env loc lid |> snd with
330353
| { mtd_type = Some (Mty_signature tsig) } ->
331354
let subst = List.map (fun ({ txt; }, typ) -> `Lid txt, typ) subst in
332355
let psig = psig_of_tsig ~subst tsig in

0 commit comments

Comments
 (0)