Skip to content

Commit 0ec9c92

Browse files
committed
Fix checking whether types are instantiable.
The logic for checking aginst the self type was wrong, as demonstrated by pos/checkInstantiable.scala.
1 parent 78fae11 commit 0ec9c92

File tree

4 files changed

+10
-2
lines changed

4 files changed

+10
-2
lines changed

src/dotty/tools/dotc/typer/Checking.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,11 @@ object Checking {
4949
if (cls.is(AbstractOrTrait))
5050
ctx.error(d"$cls is abstract; cannot be instantiated", pos)
5151
if (!cls.is(Module)) {
52-
val selfType = tp.givenSelfType.asSeenFrom(tref.prefix, cls.owner)
53-
if (selfType.exists && !(tp <:< selfType))
52+
// Create a synthetic singleton type instance, and check whether
53+
// it conforms to the self type of the class as seen from that instance.
54+
val stp = SkolemType(tp)
55+
val selfType = tref.givenSelfType.asSeenFrom(stp, cls)
56+
if (selfType.exists && !(stp <:< selfType))
5457
ctx.error(d"$tp does not conform to its self type $selfType; cannot be instantiated")
5558
}
5659
case _ =>

tests/pos/checkInstantiable.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// check instantiable of parameterized self type
2+
class LS[T] { self: LS[T] => }
3+
object Test {
4+
new LS[Int]
5+
}
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)