Skip to content

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

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

Closed
Closed
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
1 change: 0 additions & 1 deletion compiler/src/dotty/tools/dotc/core/SymDenotations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1066,7 +1066,6 @@ object SymDenotations {
final def isEffectivelyFinal(using Context): Boolean =
isOneOf(EffectivelyFinalFlags)
|| is(Inline, butNot = Deferred)
|| is(JavaDefinedVal, butNot = Method)
|| !owner.isExtensibleClass

/** A class is effectively sealed if has the `final` or `sealed` modifier, or it
Expand Down
6 changes: 4 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/RefChecks.scala
Original file line number Diff line number Diff line change
Expand Up @@ -778,8 +778,10 @@ object RefChecks {
)
def classDecls = inclazz.info.nonPrivateDecl(member.name)

(inclazz != clazz) &&
classDecls.hasAltWith(d => isSignatureMatch(d.symbol) && javaAccessCheck(d.symbol))
(inclazz != clazz) && classDecls.hasAltWith(d =>
isSignatureMatch(d.symbol) && javaAccessCheck(d.symbol) &&
!d.symbol.is(JavaDefinedVal, butNot = Method) // Java fields cannot be overriden
)
}

// 4. Check that every defined member with an `override` modifier overrides some other member.
Expand Down
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; }
}
17 changes: 17 additions & 0 deletions tests/pos/i9392/S.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class S1 extends pkg.J {
override def i(): Int = 2
}

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
}