Skip to content

Commit ce3fa05

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 fe77e76 commit ce3fa05

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
@@ -533,6 +533,13 @@ trait Checking {
533533
errorTree(tpt, ex"missing type parameter for ${tpt.tpe}")
534534
}
535535
else tpt
536+
537+
/** Check that `tpt` does not refer to a singleton type */
538+
def checkNotSingleton(tpt: Tree, where: String)(implicit ctx: Context): Tree =
539+
if (tpt.tpe.isInstanceOf[SingletonType]) {
540+
errorTree(tpt, ex"Singleton type ${tpt.tpe} is not allowed $where")
541+
}
542+
else tpt
536543
}
537544

538545
trait NoChecking extends Checking {
@@ -546,4 +553,5 @@ trait NoChecking extends Checking {
546553
override def checkNoDoubleDefs(cls: Symbol)(implicit ctx: Context): Unit = ()
547554
override def checkParentCall(call: Tree, caller: ClassSymbol)(implicit ctx: Context) = ()
548555
override def checkSimpleKinded(tpt: Tree)(implicit ctx: Context): Tree = tpt
556+
override def checkNotSingleton(tpt: Tree, where: String)(implicit ctx: Context): Tree = tpt
549557
}

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

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

981981
def typedOrTypeTree(tree: untpd.OrTypeTree)(implicit ctx: Context): OrTypeTree = track("typedOrTypeTree") {
982-
val left1 = typed(tree.left)
983-
val right1 = typed(tree.right)
982+
val where = "in a union type"
983+
val left1 = checkNotSingleton(typed(tree.left), where)
984+
val right1 = checkNotSingleton(typed(tree.right), where)
984985
assignType(cpy.OrTypeTree(tree)(left1, right1), left1, right1)
985986
}
986987

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)