Skip to content

Commit

Permalink
Also consider @TargetNAME when checking private overrides (#18361)
Browse files Browse the repository at this point in the history
Fixes #18244
  • Loading branch information
smarter authored Aug 9, 2023
2 parents 16e3bb4 + 5eff0ae commit 5383ff6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/core/SymDenotations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/RefChecks.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions tests/neg/i18244.scala
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 5383ff6

Please sign in to comment.