ppxlib icon indicating copy to clipboard operation
ppxlib copied to clipboard

ppxlib.traverse mishandles types named with module prefixes

Open dwang20151005 opened this issue 5 years ago • 3 comments

Would it be possible to extend the ppx s.t. it would include the module prefixes into its method names via some kind of mangling scheme? E.g., type Foo.Bar.baz goes to foo__bar__baz instead of to baz.

(* This is correct. *)
type direct = Direct of int * string [@@deriving_inline traverse_iter]

let _ = fun (_ : direct) -> ()

class virtual iter =
  object (self)
    method virtual int : int -> unit

    method virtual string : string -> unit

    method direct : direct -> unit =
      fun x ->
      match x with
      | Direct (a, b) ->
        self#int a;
        self#string b
  end

[@@@end]

(* This generates incorrect code. *)
type with_module_prefixes = With_module_prefixes of Int.t * String.t
[@@deriving_inline traverse_iter]

let _ = fun (_ : with_module_prefixes) -> ()

class virtual iter =
  object (self)
    method virtual t : t -> unit

    method virtual t : t -> unit

    method with_module_prefixes : with_module_prefixes -> unit =
      fun x ->
      match x with
      | With_module_prefixes (a, b) ->
        self#t a;
        self#t b
  end

[@@@end]

(* The workaround is to alias the types so I can refer to them without
module prefixes. *)
type int__t = Int.t
type string__t = String.t

type with_aliases = With_aliases of int__t * string__t
[@@deriving_inline traverse_iter]

let _ = fun (_ : with_aliases) -> ()

class virtual iter =
  object (self)
    method virtual int__t : int__t -> unit

    method virtual string__t : string__t -> unit

    method with_aliases : with_aliases -> unit =
      fun x ->
      match x with
      | With_aliases (a, b) ->
        self#int__t a;
        self#string__t b
  end

[@@@end]

dwang20151005 avatar Jan 09 '20 16:01 dwang20151005

That seems acceptable yes.

ghost avatar Jan 13 '20 12:01 ghost

Is ppxlib_traverse meant for use outside of ppxlib? I believe it is currently used to generate ast traversal classes. I know in the new ppx we are doing this a different way, using cinaps. I would expect we would then drop ppxlib_traverse. @dwang20151005, would it cause you problems if this library simply went away?

ceastlund avatar Jan 21 '20 16:01 ceastlund

Never mind that comment, @dwang20151005 showed me some other places ppxlib_traverse is used.

ceastlund avatar Jan 21 '20 16:01 ceastlund