Skip to content

Check that union types do not contain singleton types. #888

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/dotty/tools/dotc/typer/Checking.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
4 changes: 2 additions & 2 deletions src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
1 change: 1 addition & 0 deletions test/dotc/tests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions tests/neg/singletons-lubs.scala
Original file line number Diff line number Diff line change
@@ -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)
}
}