Skip to content

Commit ea41b79

Browse files
committed
Disallow singleton types in unions
For the moment, we do not know how to handle something like 1 | 2 or x.type | y.type correctly. So it's better to disallow these situations until we find a proper solution.
1 parent e678fd4 commit ea41b79

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,13 @@ trait Checking {
542542
errorTree(tpt, ex"missing type parameter for ${tpt.tpe}")
543543
}
544544
else tpt
545+
546+
/** Check that `tpt` does not refer to a singleton type */
547+
def checkNotSingleton(tpt: Tree, where: String)(implicit ctx: Context): Tree =
548+
if (tpt.tpe.isInstanceOf[SingletonType]) {
549+
errorTree(tpt, ex"Singleton type ${tpt.tpe} is not allowed $where")
550+
}
551+
else tpt
545552
}
546553

547554
trait NoChecking extends Checking {
@@ -556,4 +563,5 @@ trait NoChecking extends Checking {
556563
override def checkNoDoubleDefs(cls: Symbol)(implicit ctx: Context): Unit = ()
557564
override def checkParentCall(call: Tree, caller: ClassSymbol)(implicit ctx: Context) = ()
558565
override def checkSimpleKinded(tpt: Tree)(implicit ctx: Context): Tree = tpt
566+
override def checkNotSingleton(tpt: Tree, where: String)(implicit ctx: Context): Tree = tpt
559567
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -994,8 +994,9 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
994994
}
995995

996996
def typedOrTypeTree(tree: untpd.OrTypeTree)(implicit ctx: Context): OrTypeTree = track("typedOrTypeTree") {
997-
val left1 = typed(tree.left)
998-
val right1 = typed(tree.right)
997+
val where = "in a union type"
998+
val left1 = checkNotSingleton(typed(tree.left), where)
999+
val right1 = checkNotSingleton(typed(tree.right), where)
9991000
assignType(cpy.OrTypeTree(tree)(left1, right1), left1, right1)
10001001
}
10011002

tests/neg/singletonOrs.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
object Test {
2+
def foo: 1 | 2 = 1 // error // error
3+
def bar: 3 | 4 = foo // error // error
4+
def foo: 1 | 2 = 1 // error // error
5+
def bar: 1 = foo
6+
}

0 commit comments

Comments
 (0)