Skip to content

Fix #9392: Allow overriding a Java method when a same-named field is present #9452

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

Merged
merged 1 commit into from
Aug 3, 2020

Conversation

smarter
Copy link
Member

@smarter smarter commented Jul 27, 2020

In #9126, Java fields were marked effectively finals to prevent false
overrides. This poses a problem when a Java field and method of the same
name are present, because a Scala val or def will match both of these
according to Denotation#matches. This commit changes matches to
prevent Java field from matching any Scala method, but we can't prevent
them from matching Scala definitions with NotAMethod signature without
introducing more complexity, so the following still doesn't compile
unlike Scala 2:

package pkg;

public class J {
    int i = 0;
    public int i() { return 1; }
}
class S2 extends pkg.J {
  override def i: Int = 2 // error
}

I have an alternative fix at #9412 which doesn't have this issue but is
more hacky.

…d is present

In scala#9126, Java fields were marked effectively finals to prevent false
overrides. This poses a problem when a Java field and method of the same
name are present, because a Scala val or def will match both of these
according to Denotation#matches. This commit changes `matches` to
prevent Java field from matching any Scala method, but we can't prevent
them from matching Scala definitions with NotAMethod signature without
introducing more complexity, so the following still doesn't compile
unlike Scala 2:

```java
package pkg;

public class J {
    int i = 0;
    public int i() { return 1; }
}
```

```scala
class S2 extends pkg.J {
  override def i: Int = 2 // error
}
```

I have an alternative fix at scala#9412 which doesn't have this issue but is
more hacky.
@odersky odersky merged commit a084249 into scala:master Aug 3, 2020
@odersky odersky deleted the java-field-method-override-2 branch August 3, 2020 15:39
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.

2 participants