Skip to content

Commit

Permalink
Backport "Check @TargetNAME when subtyping Refined Types" to LTS (#20781
Browse files Browse the repository at this point in the history
)

Backports #19081 to the LTS branch.

PR submitted by the release tooling.
[skip ci]
  • Loading branch information
WojciechMazur authored Jun 26, 2024
2 parents a8d4ac7 + 9575e6f commit 05352ac
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 4 deletions.
8 changes: 5 additions & 3 deletions compiler/src/dotty/tools/dotc/core/TypeComparer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1989,9 +1989,11 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling

def qualifies(m: SingleDenotation): Boolean =
val info1 = m.info.widenExpr
isSubInfo(info1, tp2.refinedInfo.widenExpr, m.symbol.info.orElse(info1))
|| matchAbstractTypeMember(m.info)
|| (tp1.isStable && isSubType(TermRef(tp1, m.symbol), tp2.refinedInfo))
m.symbol.hasTargetName(m.symbol.name) && (
isSubInfo(info1, tp2.refinedInfo.widenExpr, m.symbol.info.orElse(info1))
|| matchAbstractTypeMember(m.info)
|| (tp1.isStable && isSubType(TermRef(tp1, m.symbol), tp2.refinedInfo))
)

tp1.member(name) match // inlined hasAltWith for performance
case mbr: SingleDenotation => qualifies(mbr)
Expand Down
7 changes: 7 additions & 0 deletions tests/neg/i18922.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- [E007] Type Mismatch Error: tests/neg/i18922.scala:11:27 ------------------------------------------------------------
11 |def test = doClose(Resource()) // error
| ^^^^^^^^^^
| Found: Resource
| Required: Object{def close(): Unit}
|
| longer explanation available when compiling with `-explain`
11 changes: 11 additions & 0 deletions tests/neg/i18922.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import scala.annotation.targetName

def doClose(closable: { def close(): Unit }): Unit =
import reflect.Selectable.reflectiveSelectable
closable.close()

class Resource:
@targetName("foo")
def close(): Unit = ???

def test = doClose(Resource()) // error
7 changes: 7 additions & 0 deletions tests/neg/targetName-refine.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- [E007] Type Mismatch Error: tests/neg/targetName-refine.scala:7:27 --------------------------------------------------
7 |val x: T { def f: Int } = C() // error
| ^^^
| Found: C
| Required: T{def f: Int}
|
| longer explanation available when compiling with `-explain`
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ trait T:
class C extends T:
@targetName("f2") def f: Int = 1

val x: T { def f: Int } = C()
val x: T { def f: Int } = C() // error

0 comments on commit 05352ac

Please sign in to comment.