melange-community / melange-community/jsonkit

Very confusing compile error due to inadvertent shadowing

Open
#41 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
OCaml
Stars
24
Forks
7
Avg merge
1d 15h
Merged PRs (30d)
5

Description

I was working with a code like this:

type 'string string_element =
  | String of 'string
[@@deriving json]

type 't input = {
  default: 't;
} [@@deriving json]

type input_element = string input string_element [@@deriving json]

and started getting an error like:

11 | type input_element = string input string_element [@@deriving json]
                          ^^^^^^^^^^^^
Error: This expression has type string input -> Js__Js_json.t
       but an expression was expected of type string -> 'a
       Type string input is not compatible with type string 

After some fiddling, I found out the error went away if I renamed the type variable to something other than 'string, e.g.:

type 'str string_element =
  | String of 'str
[@@deriving json]

My hypothesis about why the error happens is that the generated arguments take the name from the type argument. As I had 'string, the ppx generates args like string_to_json and string_of_json, and at some point the type checker infers the wrong types.

As the ppx can generate invalid names, maybe it could generate names with invalid chars so there's no this kind of confusion?

Side note: unfortunately, I've not been able to reproduce in an isolated test.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reducing the supplied OCaml example into an isolated reproduction and inspect the generated bindings produced by the PPX. Done means the shadowing case is covered by a regression test and the generated names no longer cause the reported type-checking confusion.

Written by the indexing model from the issue text.

Assessment

Tech stack
ocaml
Domain
compilers, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.