Mutually recursive property warning

Mutually recursive types containing same property throws warning. This warning causes compiler to run indefinitely. Here is the playground link.

  1. Why is this a warning?
  2. Why the compiler doesn’t stop (probably some error)?

Please advise.

Hi, in the playground link, the compile succeeds with a warning i.e. it doesn’t run indefinitely. Can you show an example of the indefinite run?

As to your other question, I’m not exactly sure why it’s a warning, but the warning seems sensible to me. For mutually recursive types, you can think of them as roughly one big type split up into two or more parts, and in that sense having repeated record fields in multiple ‘parts’ of the ‘same type’ doesn’t seem to make sense.

1 Like

@yawaramin For the compiler running indefinitely, it happens when rescript compiler is run in watch mode.

For mutually recursive type, I am defining a database model where the join can happen in either way. In this case both types can have id attribute and join fetch one another. This seems to be a valid case to me for mutually recursive type. I am not sure why this must be warning.

When the compiler is running in watch mode, it is normal behaviour to run indefinitely, as it is waiting for changes to the source files. Or do you mean that it never finishes even a single compilation in watch mode?

For the database model, OK, that makes sense. You can structure the record types into mutually-recursive modules so they can refer to each other and not warn about duplicate field names. Here’s an example:

module rec Dept: {
  type t = {id: int, name: string, emps: array<Emp.t>}
} = Dept
and Emp: {
  type t = {id: int, name: string, dept: Dept.t}
} = Emp

This uses a neat trick where a module that contains only types and externals can be defined just by referring to its module type.

@yawaramin Thanks. That’s the solution that I ended up implementing. But I was curious why type level mutual recursion was an issue.

Regarding the compiler running indefinitely, Yes, compiler never finishes single compilation, it reruns compilation again and again, and it never finishes.

This warning causes compiler to run indefinitely.

This is a separte issue in the build system, I will have a look into it later.
@praveen can you confirm if it is the same issue as this one ninesunsabiu/rescript-proj-tpl: 复现一个疑似 rescript-compiler 的缺陷 (github.com)

  1. Why is this a warning?

It is a warning beacuse of ambiguity, since {id : 2} could be either type a or type b.
But you can annotate in general, ({id : 2} : b).

This is not a critical warning, you can trun it off if it gets annoying

@Hongbo That was one issue that I faced (ununsed variable causing compiler to run indefinitely). But the issue that I raised as part of this topic is the one below.

@genType
type rec a = {id: int, b: b}
and b = {id: int, a: a}

Just copy paste this code in Demo.res and don’t save the file yet. Run the compiler in watch mode and then save the file. The compiler will run indefinitely. I guess both the issues are interconnected, but I am not sure.

Thanks for the clarification.