Skip to content

Commit

Permalink
Check @TargetNAME when subtyping Refined Types (scala#19081)
Browse files Browse the repository at this point in the history
  • Loading branch information
hamzaremmal authored Nov 30, 2023
2 parents c88c0fe + ea509f4 commit 4d45087
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 4 deletions.
7 changes: 4 additions & 3 deletions compiler/src/dotty/tools/dotc/core/TypeComparer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2084,9 +2084,10 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
ExprType(info1.resType)
case info1 => info1

isSubInfo(info1, info2, m.symbol.info.orElse(info1))
|| matchAbstractTypeMember(m.info)
|| (tp1.isStable && m.symbol.isStableMember && isSubType(TermRef(tp1, m.symbol), tp2.refinedInfo))
m.symbol.hasTargetName(m.symbol.name) && (
isSubInfo(info1, info2, m.symbol.info.orElse(info1))
|| matchAbstractTypeMember(m.info)
|| (tp1.isStable && m.symbol.isStableMember && isSubType(TermRef(tp1, m.symbol), tp2.refinedInfo)))
end qualifies

tp1.member(name).hasAltWithInline(qualifies)
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 4d45087

Please sign in to comment.