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

A example of typechecking failure in generated code #31

Open
mlasson opened this issue Aug 19, 2021 · 0 comments
Open

A example of typechecking failure in generated code #31

mlasson opened this issue Aug 19, 2021 · 0 comments
Assignees

Comments

@mlasson
Copy link
Member

mlasson commented Aug 19, 2021

How to reproduce:

Run the ppx in automatic mode on :

  module M = struct
    type t = A | B
  end

  class a =
    object
      method f = M.A
    end

  class b =
    object
      inherit a

      method! f = B
    end

What happens ?

Class b get translated into:

    class b =
      object
        inherit  a
        method! f =
          Landmark.enter __generated_landmark;
          let r = try B with e -> (Landmark.exit  __generated_landmark; Landmark.raise e) in
          Landmark.exit  __generated_landmark;
          r
   end

which triggers "Error: Unbound constructor B" because the typechecker does not know at this point that the B has the same type as the method's returned type.

Proposed Solution

We could add type annotations ("'a") to guide the inference (we have to make sure that 'a is fresh of course).

    class b =
      object
        inherit  a
        method! f : 'a =
          Landmark.enter __generated_landmark;
          let r = try (B : 'a) with e -> (Landmark.exit  __generated_landmark; Landmark.raise e) in
          Landmark.exit  __generated_landmark;
          r
   end

If there's an annotation on the returned type we probably should copy it around the body in a similar fashion.

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