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

Overriding a non-abstract var is compiled in Scala 3 / fails in Scala 2 #15491

Closed
makingthematrix opened this issue Jun 21, 2022 · 5 comments
Closed

Comments

@makingthematrix
Copy link
Contributor

makingthematrix commented Jun 21, 2022

Compiler version

3.1.1

Minimized code

  trait Animal {
    var cat: String = ""
  }

  class Cat extends Animal {
    override val cat: String = "cat"
  }

Output

Compilation works.

Expectation

In Scala 2.13.8 compilation of this code fails with:

mutable variable cannot be overridden:
[error] def cat: String (defined in trait Animal)
[error]     override val cat: String = "cat"
[error]   

Here’s a Scastie for it.

I have already seen this ticket and I can confirm it is fixed now, but the code above is a bit different - the var in the superclass is not abstract. You can modify the Scastie linked above to see how that changes how Scala 3 reacts. As far as I can tell, the only case where Scala 3 shows an error is when the field in the subclass is also a var: override var cat = "cat".

error overriding variable cat in trait Animal of type String; [error] 
| variable cat of type String cannot override a mutable variable

I asked about it on the contributors forum and was advised to open a ticket: https://contributors.scala-lang.org/t/a-possible-issue-with-overriding-vars-in-scala-3/5768
It feels to me as if it was an overlooked detail and Scala 3 should behave exactly as Scala 2.

@makingthematrix makingthematrix added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Jun 21, 2022
@Kordyjan Kordyjan added compat:scala2 area:refchecks and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Jun 21, 2022
@som-snytt
Copy link
Contributor

som-snytt commented Jun 21, 2022

Current HEAD says

-- [E164] Declaration Error: i15491.scala:7:15 -------------------------------------------------------------------------
7 |  override val cat: String = "cat"
  |               ^
  |               error overriding variable cat in trait Animal of type String;
  |                 value cat of type String cannot override a mutable variable
1 error found

edit: scastie has an RC option
image
which demonstrates the latest behavior.
I think the advice on discord was to comment on the existing ticket.

@Kordyjan
Copy link
Contributor

It is correctly giving a compilation error on both 3.1.3 and 3.2.0-RC1.

@strelec
Copy link

strelec commented Jun 22, 2022

Still, a test is a good idea.

@Kordyjan
Copy link
Contributor

We already have a test for this exact case.
https://github.com/dotty-staging/dotty/blob/main/tests/neg/i14722.scala#L3

@makingthematrix
Copy link
Contributor Author

makingthematrix commented Jun 23, 2022

Okay, great. Sorry for the noise. I didn't think about that the fix for this specific error could have been made between 3.1.1 and 3.1.3. What are the odds :)

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

No branches or pull requests

4 participants