Skip to content

Commit 05352ac

Browse files
Backport "Check @TargetNAME when subtyping Refined Types" to LTS (#20781)
Backports #19081 to the LTS branch. PR submitted by the release tooling. [skip ci]
2 parents a8d4ac7 + 9575e6f commit 05352ac

File tree

5 files changed

+31
-4
lines changed

5 files changed

+31
-4
lines changed

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -1989,9 +1989,11 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
19891989

19901990
def qualifies(m: SingleDenotation): Boolean =
19911991
val info1 = m.info.widenExpr
1992-
isSubInfo(info1, tp2.refinedInfo.widenExpr, m.symbol.info.orElse(info1))
1993-
|| matchAbstractTypeMember(m.info)
1994-
|| (tp1.isStable && isSubType(TermRef(tp1, m.symbol), tp2.refinedInfo))
1992+
m.symbol.hasTargetName(m.symbol.name) && (
1993+
isSubInfo(info1, tp2.refinedInfo.widenExpr, m.symbol.info.orElse(info1))
1994+
|| matchAbstractTypeMember(m.info)
1995+
|| (tp1.isStable && isSubType(TermRef(tp1, m.symbol), tp2.refinedInfo))
1996+
)
19951997

19961998
tp1.member(name) match // inlined hasAltWith for performance
19971999
case mbr: SingleDenotation => qualifies(mbr)

tests/neg/i18922.check

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- [E007] Type Mismatch Error: tests/neg/i18922.scala:11:27 ------------------------------------------------------------
2+
11 |def test = doClose(Resource()) // error
3+
| ^^^^^^^^^^
4+
| Found: Resource
5+
| Required: Object{def close(): Unit}
6+
|
7+
| longer explanation available when compiling with `-explain`

tests/neg/i18922.scala

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import scala.annotation.targetName
2+
3+
def doClose(closable: { def close(): Unit }): Unit =
4+
import reflect.Selectable.reflectiveSelectable
5+
closable.close()
6+
7+
class Resource:
8+
@targetName("foo")
9+
def close(): Unit = ???
10+
11+
def test = doClose(Resource()) // error

tests/neg/targetName-refine.check

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- [E007] Type Mismatch Error: tests/neg/targetName-refine.scala:7:27 --------------------------------------------------
2+
7 |val x: T { def f: Int } = C() // error
3+
| ^^^
4+
| Found: C
5+
| Required: T{def f: Int}
6+
|
7+
| longer explanation available when compiling with `-explain`

tests/pos/targetName-refine.scala renamed to tests/neg/targetName-refine.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ trait T:
44
class C extends T:
55
@targetName("f2") def f: Int = 1
66

7-
val x: T { def f: Int } = C()
7+
val x: T { def f: Int } = C() // error
88

0 commit comments

Comments
 (0)