From 7596819162a6feec801a455d55727c36cbbf9d6c Mon Sep 17 00:00:00 2001 From: Hamza Remmal <56235032+hamzaremmal@users.noreply.github.com> Date: Sun, 26 Nov 2023 00:30:45 +0100 Subject: [PATCH 1/3] Check targetName when subtyping Refined Types --- compiler/src/dotty/tools/dotc/core/TypeComparer.scala | 2 +- tests/neg/i18922.check | 7 +++++++ tests/neg/i18922.scala | 11 +++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 tests/neg/i18922.check create mode 100644 tests/neg/i18922.scala diff --git a/compiler/src/dotty/tools/dotc/core/TypeComparer.scala b/compiler/src/dotty/tools/dotc/core/TypeComparer.scala index 76236d635182..a95425a67854 100644 --- a/compiler/src/dotty/tools/dotc/core/TypeComparer.scala +++ b/compiler/src/dotty/tools/dotc/core/TypeComparer.scala @@ -2084,7 +2084,7 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling ExprType(info1.resType) case info1 => info1 - isSubInfo(info1, info2, m.symbol.info.orElse(info1)) + (isSubInfo(info1, info2, m.symbol.info.orElse(info1)) && m.symbol.hasTargetName(m.symbol.name)) || matchAbstractTypeMember(m.info) || (tp1.isStable && m.symbol.isStableMember && isSubType(TermRef(tp1, m.symbol), tp2.refinedInfo)) end qualifies diff --git a/tests/neg/i18922.check b/tests/neg/i18922.check new file mode 100644 index 000000000000..f686d8e2f619 --- /dev/null +++ b/tests/neg/i18922.check @@ -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` diff --git a/tests/neg/i18922.scala b/tests/neg/i18922.scala new file mode 100644 index 000000000000..9c76177a0c7a --- /dev/null +++ b/tests/neg/i18922.scala @@ -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 \ No newline at end of file From 582192df0149e617fded08119beaab87be59be91 Mon Sep 17 00:00:00 2001 From: Hamza Remmal <56235032+hamzaremmal@users.noreply.github.com> Date: Wed, 29 Nov 2023 13:23:52 +0100 Subject: [PATCH 2/3] Move targetName-refine from pos/ to neg/ --- tests/neg/targetName-refine.check | 7 +++++++ tests/{pos => neg}/targetName-refine.scala | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 tests/neg/targetName-refine.check rename tests/{pos => neg}/targetName-refine.scala (76%) diff --git a/tests/neg/targetName-refine.check b/tests/neg/targetName-refine.check new file mode 100644 index 000000000000..fe0dd71dfb0a --- /dev/null +++ b/tests/neg/targetName-refine.check @@ -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` diff --git a/tests/pos/targetName-refine.scala b/tests/neg/targetName-refine.scala similarity index 76% rename from tests/pos/targetName-refine.scala rename to tests/neg/targetName-refine.scala index eaa02b8b7976..f9b8f0d72740 100644 --- a/tests/pos/targetName-refine.scala +++ b/tests/neg/targetName-refine.scala @@ -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 From ea509f43ef038e981de66d0c9dc2dab57aff9084 Mon Sep 17 00:00:00 2001 From: Hamza Remmal <56235032+hamzaremmal@users.noreply.github.com> Date: Thu, 30 Nov 2023 14:17:39 +0100 Subject: [PATCH 3/3] Add extend the check for targetName when checking matching members --- compiler/src/dotty/tools/dotc/core/TypeComparer.scala | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/core/TypeComparer.scala b/compiler/src/dotty/tools/dotc/core/TypeComparer.scala index a95425a67854..7c1b6570b0e8 100644 --- a/compiler/src/dotty/tools/dotc/core/TypeComparer.scala +++ b/compiler/src/dotty/tools/dotc/core/TypeComparer.scala @@ -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)) && m.symbol.hasTargetName(m.symbol.name)) - || 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)