From 5eff0ae9aeb2c0717cb0ee2226be93abb6428816 Mon Sep 17 00:00:00 2001 From: odersky Date: Tue, 8 Aug 2023 16:32:32 +0200 Subject: [PATCH] Also consider @targetName when checking private overrides Fixes #18244 --- .../src/dotty/tools/dotc/core/SymDenotations.scala | 2 +- compiler/src/dotty/tools/dotc/typer/RefChecks.scala | 4 +++- tests/neg/i18244.scala | 12 ++++++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 tests/neg/i18244.scala diff --git a/compiler/src/dotty/tools/dotc/core/SymDenotations.scala b/compiler/src/dotty/tools/dotc/core/SymDenotations.scala index 5a14dfa316c4..3dfa5225df5b 100644 --- a/compiler/src/dotty/tools/dotc/core/SymDenotations.scala +++ b/compiler/src/dotty/tools/dotc/core/SymDenotations.scala @@ -1353,7 +1353,7 @@ object SymDenotations { * * site: Subtype of both inClass and C */ - final def matchingDecl(inClass: Symbol, site: Type)(using Context): Symbol = { + final def matchingDecl(inClass: Symbol, site: Type, name: Name = this.name)(using Context): Symbol = { var denot = inClass.info.nonPrivateDecl(name) if (denot.isTerm) // types of the same name always match denot = denot.matchingDenotation(site, site.memberInfo(symbol), symbol.targetName) diff --git a/compiler/src/dotty/tools/dotc/typer/RefChecks.scala b/compiler/src/dotty/tools/dotc/typer/RefChecks.scala index 74ad3489952b..1b816dfc9307 100644 --- a/compiler/src/dotty/tools/dotc/typer/RefChecks.scala +++ b/compiler/src/dotty/tools/dotc/typer/RefChecks.scala @@ -982,7 +982,9 @@ object RefChecks { then val cls = sym.owner.asClass for bc <- cls.baseClasses.tail do - val other = sym.matchingDecl(bc, cls.thisType) + var other = sym.matchingDecl(bc, cls.thisType) + if !other.exists && sym.targetName != sym.name then + other = sym.matchingDecl(bc, cls.thisType, sym.targetName) if other.exists then report.error(em"private $sym cannot override ${other.showLocated}", sym.srcPos) end checkNoPrivateOverrides diff --git a/tests/neg/i18244.scala b/tests/neg/i18244.scala new file mode 100644 index 000000000000..c7059f1db401 --- /dev/null +++ b/tests/neg/i18244.scala @@ -0,0 +1,12 @@ +import scala.annotation.* + +class A: + def foo: Int = 1 +class B extends A: + @targetName("foo") private[this] def bla: Int = 2 // error +class C extends A: + @targetName("foo") private def bla: Int = 2 // error + +@main def Test = + val b = new B + println(b.foo)