diff --git a/src/dotty/tools/dotc/typer/Checking.scala b/src/dotty/tools/dotc/typer/Checking.scala index fa417beecf85..cbb74a1a27e2 100644 --- a/src/dotty/tools/dotc/typer/Checking.scala +++ b/src/dotty/tools/dotc/typer/Checking.scala @@ -403,6 +403,11 @@ trait Checking { else tpt.withType(alias) } else tpt + + /** Check that `tpt` does not define a singleton type */ + def checkNoSingleton(tpt: Tree)(implicit ctx: Context): Tree = + if (tpt.tpe.isInstanceOf[SingletonType]) errorTree(tpt, i"no singleton type allowed here") + else tpt } trait NoChecking extends Checking { @@ -417,4 +422,5 @@ trait NoChecking extends Checking { override def checkNoDoubleDefs(cls: Symbol)(implicit ctx: Context): Unit = () override def checkParentCall(call: Tree, caller: ClassSymbol)(implicit ctx: Context) = () override def checkSimpleKinded(tpt: Tree)(implicit ctx: Context): Tree = tpt + override def checkNoSingleton(tpt: Tree)(implicit ctx: Context): Tree = tpt } diff --git a/src/dotty/tools/dotc/typer/Typer.scala b/src/dotty/tools/dotc/typer/Typer.scala index 3ca728895da1..61026967a657 100644 --- a/src/dotty/tools/dotc/typer/Typer.scala +++ b/src/dotty/tools/dotc/typer/Typer.scala @@ -800,8 +800,8 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit } def typedOrTypeTree(tree: untpd.OrTypeTree)(implicit ctx: Context): OrTypeTree = track("typedOrTypeTree") { - val left1 = typed(tree.left) - val right1 = typed(tree.right) + val left1 = checkNoSingleton(typed(tree.left)) + val right1 = checkNoSingleton(typed(tree.right)) assignType(cpy.OrTypeTree(tree)(left1, right1), left1, right1) } diff --git a/test/dotc/tests.scala b/test/dotc/tests.scala index 381f522cfd79..15ff126227b1 100644 --- a/test/dotc/tests.scala +++ b/test/dotc/tests.scala @@ -108,6 +108,7 @@ class tests extends CompilerTest { @Test def neg_typers() = compileFile(negDir, "typers", xerrors = 14)(allowDoubleBindings) @Test def neg_privates() = compileFile(negDir, "privates", xerrors = 2) @Test def neg_rootImports = compileFile(negDir, "rootImplicits", xerrors = 2) + @Test def neg_singletonsLubs = compileFile(negDir, "singletons-lubs", xerrors = 6) @Test def neg_templateParents() = compileFile(negDir, "templateParents", xerrors = 3) @Test def neg_autoTupling = compileFile(posDir, "autoTuplingTest", args = "-language:noAutoTupling" :: Nil, xerrors = 3) @Test def neg_autoTupling2 = compileFile(negDir, "autoTuplingTest", xerrors = 3) diff --git a/tests/neg/singletons-lubs.scala b/tests/neg/singletons-lubs.scala new file mode 100644 index 000000000000..263ef82f6046 --- /dev/null +++ b/tests/neg/singletons-lubs.scala @@ -0,0 +1,7 @@ +object Test { + def oneOrTwo(x: 1 | 2): 1 | 2 = x // error // error // error // error + def test: Unit = { + val foo: 3 | 4 = 1 // error // error + oneOrTwo(foo) + } +}