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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions compiler/src/dotty/tools/dotc/core/Denotations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -976,8 +976,22 @@ object Denotations {
case FullMatch =>
true
case MethodNotAMethodMatch =>
// Java allows defining both a field and a zero-parameter method with the same name
!ctx.erasedTypes && !(symbol.is(JavaDefined) && other.symbol.is(JavaDefined))
!ctx.erasedTypes && {
val isJava = symbol.is(JavaDefined)
val otherIsJava = other.symbol.is(JavaDefined)
// A Scala zero-parameter method and a Scala non-method always match.
if !isJava && !otherIsJava then
true
// Java allows defining both a field and a zero-parameter method with the same name,
// so they must not match.
else if isJava && otherIsJava then
false
// A Java field never matches a Scala method.
else if isJava then
symbol.is(Method)
else // otherIsJava
other.symbol.is(Method)
}
case ParamMatch =>
// The signatures do not tell us enough to be sure about matching
!ctx.erasedTypes && info.matches(other.info)
Expand Down
2 changes: 1 addition & 1 deletion tests/neg/i9115/B.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class B extends A {
override val foo: String = "B" // error
}
}
6 changes: 6 additions & 0 deletions tests/pos/i9392/J.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package pkg;

public class J {
int i = 0;
public int i() { return 1; }
}
20 changes: 20 additions & 0 deletions tests/pos/i9392/S.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class S1 extends pkg.J {
override def i(): Int = 2
}

// Unlike Scala 2 this doesn't compile, because this override of `i()`
// also matches the non-overridable field `i`.
// class S2 extends pkg.J {
// override def i: Int = 2
// }

object Test {
val s1 = new S1

val i1 = s1.i
val i2 = s1.i()

// val s2 = new S2

// val i3 = s2.i
}