Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Very confusing compile error due to inadvertent shadowing #41

Open
jchavarri opened this issue Nov 28, 2024 · 0 comments
Open

Very confusing compile error due to inadvertent shadowing #41

jchavarri opened this issue Nov 28, 2024 · 0 comments

Comments

@jchavarri
Copy link
Member

jchavarri commented Nov 28, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant