Skip to content

Commit 5eff0ae

Browse files
committed
Also consider @TargetNAME when checking private overrides
Fixes #18244
1 parent 3c95134 commit 5eff0ae

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

compiler/src/dotty/tools/dotc/core/SymDenotations.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1353,7 +1353,7 @@ object SymDenotations {
13531353
*
13541354
* site: Subtype of both inClass and C
13551355
*/
1356-
final def matchingDecl(inClass: Symbol, site: Type)(using Context): Symbol = {
1356+
final def matchingDecl(inClass: Symbol, site: Type, name: Name = this.name)(using Context): Symbol = {
13571357
var denot = inClass.info.nonPrivateDecl(name)
13581358
if (denot.isTerm) // types of the same name always match
13591359
denot = denot.matchingDenotation(site, site.memberInfo(symbol), symbol.targetName)

compiler/src/dotty/tools/dotc/typer/RefChecks.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -982,7 +982,9 @@ object RefChecks {
982982
then
983983
val cls = sym.owner.asClass
984984
for bc <- cls.baseClasses.tail do
985-
val other = sym.matchingDecl(bc, cls.thisType)
985+
var other = sym.matchingDecl(bc, cls.thisType)
986+
if !other.exists && sym.targetName != sym.name then
987+
other = sym.matchingDecl(bc, cls.thisType, sym.targetName)
986988
if other.exists then
987989
report.error(em"private $sym cannot override ${other.showLocated}", sym.srcPos)
988990
end checkNoPrivateOverrides

tests/neg/i18244.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import scala.annotation.*
2+
3+
class A:
4+
def foo: Int = 1
5+
class B extends A:
6+
@targetName("foo") private[this] def bla: Int = 2 // error
7+
class C extends A:
8+
@targetName("foo") private def bla: Int = 2 // error
9+
10+
@main def Test =
11+
val b = new B
12+
println(b.foo)

0 commit comments

Comments
 (0)