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

Override instance variables in class types #10514

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from

Conversation

lpw25
Copy link
Contributor

@lpw25 lpw25 commented Jul 15, 2021

This is a change that was extracted from #8516. It was generally approved of, but it's a breaking change so people wanted to discuss it separately.

Previously, the behaviour of instance variables in class types was pretty strange. Given two declarations of the same variable, the second declaration would partially override and partially shadow the first declaration. I presume this is a hangover from the
change in instance variable behaviour in OCaml 3.10. This commit brings the behaviour into line with the behaviour in classes -- the second definition overrides the first. It is more restrictive than the previous behaviour, so it is not backwards compatible, but it
is much more consistent.

@xavierleroy
Copy link
Contributor

From your description, the new behavior seems to make more sense than the current behavior. However:

  1. Could you please give an example where the behavior changes?
  2. @garrigue could you please tell us what you think about this change?
  3. Ideally we'd get an OPAM-wide test to find whether this change breaks code in the wild (@kit-ty-kate, @Octachron).

@kit-ty-kate
Copy link
Member

Ideally we'd get an OPAM-wide test to find whether this change breaks code in the wild (@kit-ty-kate, @Octachron).

Sure. I just need a link to a branch cherry-picking this change on top of 4.12 if possible.
I've tried cherry-picking on top of 4.12 and 4.13 but there are too many conflicts in the type-checker for me to be confident about fixing them.

Previously, the behaviour of instance variables in class types was
pretty strange. Given two declarations of the same variable, the
second declaration would partially override and partially shadow
the first declaration. I presume this is a hangover from the
change in instance variable behvaiour in OCaml 3.10. This commit
brings the behaviour into line with the behaviour in classes -- the
second definition overrides the first. It is more restrictive than
the previous behaviour, so it is not backwards compatible, but it
is much more consistent.
@lpw25 lpw25 force-pushed the override-instance-variables-class-types branch from b361237 to 3582dd5 Compare June 24, 2022 14:51
@lpw25
Copy link
Contributor Author

lpw25 commented Jun 24, 2022

I've rebased this onto trunk.

Could you please give an example where the behavior changes?

A good example of the strange existing behaviour is:

# class type c = object
  val x : int
  val virtual x : float
end;;
class type c = object val x : float end

where the type is take from the second definition but the "virtualness" is taken from the first. That example now produces an error.

The simplest example of a program that is now rejected is:

class type c = object
  val x : int
  val x : float
end;;
Line 3, characters 2-15:
3 |   val x : float
      ^^^^^^^^^^^^^
Error: The instance variable x has type float but is expected to have type
         int

which was previously accepted.

There are further examples in the testsuite (#8516 added new tests to demonstrate the existing behaviour).

I've tried cherry-picking on top of 4.12 and 4.13 but there are too many conflicts

This PR needs to be cherry-picked onto 4.14 or later. I suspect you were misled by the fact that #8516 originally had it's changes entry under 4.13 by mistake. This was fixed before 4.13 got released but the mistake was still visible on this branch until I just rebased it.

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 this pull request may close these issues.

None yet

3 participants