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

Typing regression in 4.14 involving objects with constrained self type #11195

Closed
nojb opened this issue Apr 15, 2022 · 6 comments · Fixed by #11204
Closed

Typing regression in 4.14 involving objects with constrained self type #11195

nojb opened this issue Apr 15, 2022 · 6 comments · Fixed by #11204

Comments

@nojb
Copy link
Contributor

nojb commented Apr 15, 2022

class virtual c =
  object (self : 'c)
    constraint 'c = < f : int; .. >
    method g = self # f
  end

triggers a warning in 4.14:

image

while it is accepted by previous versions. EDIT: I tweaked the example for clarity.

@nojb
Copy link
Contributor Author

nojb commented Apr 15, 2022

cc @garrigue

@nojb nojb changed the title Typing regression in 4.14 involving objects with constrained self type? Typing regression in 4.14 involving objects with constrained self type Apr 16, 2022
@nojb
Copy link
Contributor Author

nojb commented Apr 16, 2022

Probably related to #8516 cc @lpw25

@nojb
Copy link
Contributor Author

nojb commented Apr 16, 2022

To give a bit of context: we use this pattern to implement mixins, as follows

class type has_f =
  object
    method f: int
  end

class virtual d =
  object (self : 'd)
  constraint 'd = #has_f
  method g = self # f
end

class my_f : has_f =
  object
    method f = 42
  end

class e =
  object
    inherit d
    inherit my_f
  end

In general, has_f can declare many methods and be used in many different class definitions allowing to share code.

@lpw25
Copy link
Contributor

lpw25 commented Apr 16, 2022

Personally, I agree with the type-checker: your virtual methods are not declared. I'd probably write your example like this instead:

class virtual has_f =
  object
    method virtual f: int
  end

class virtual d =
  object (self)
  inherit has_f
  method g = self # f
end

class my_f =
  object
    inherit has_f
    method f = 42
  end

class e =
  object
    inherit d
    inherit my_f
  end

However, we should probably avoid the regression anyway. It shouldn't be too hard to change the definition of undeclared to allow for the method to be declared via a type constraint on self.

@lpw25
Copy link
Contributor

lpw25 commented Apr 16, 2022

Playing with the example has revealed a more serious regression. This code:

class virtual has_f = object
    method virtual f: int
  end

class my_f : has_f = object
    method f = 9
  end

should be an error but is currently accepted. Previously it gave:

Error: This class should be virtual. The following methods are undefined : f

@nojb
Copy link
Contributor Author

nojb commented Apr 16, 2022

Personally, I agree with the type-checker: your virtual methods are not declared.

In my view this is a bit inconsistent with the fact that the type-checker interprets methods introduced by constraining self as virtual methods when inferring the type of the class:

# class virtual c = object (self : 'c)
  constraint 'c = < f : int; .. >
end
class virtual c : object method virtual f : int end

It shouldn't be too hard to change the definition of undeclared to allow for the method to be declared via a type constraint on self.

I gave this one a try: #11204

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

Successfully merging a pull request may close this issue.

2 participants