From 289e273697f9304d716ed9fc834b3b2016df6f7d Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Sun, 23 Aug 2015 23:36:02 +0200 Subject: Better error message for Null and 'sym singleton types. Null and 'sym are not legal as singleton types because the underlying values are not stable. They are rejected now outright instead of issuing a cryptic "X is not stable" error message. --- src/dotty/tools/dotc/typer/Typer.scala | 5 ++++- tests/neg/singletons.scala | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/dotty/tools/dotc/typer/Typer.scala b/src/dotty/tools/dotc/typer/Typer.scala index 33ec156a1..4441adcfb 100644 --- a/src/dotty/tools/dotc/typer/Typer.scala +++ b/src/dotty/tools/dotc/typer/Typer.scala @@ -782,7 +782,10 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit def typedSingletonTypeTree(tree: untpd.SingletonTypeTree)(implicit ctx: Context): SingletonTypeTree = track("typedSingletonTypeTree") { val ref1 = typedExpr(tree.ref) - checkStable(ref1.tpe, tree.pos) + val illegal = Set[Symbol](defn.NullClass, defn.SymbolClass) + val refSym = ref1.tpe.widen.typeSymbol + if (illegal contains refSym) ctx.error(i"$refSym is not a legal singleton constant type", tree.pos) + else checkStable(ref1.tpe, tree.pos) assignType(cpy.SingletonTypeTree(tree)(ref1), ref1) } diff --git a/tests/neg/singletons.scala b/tests/neg/singletons.scala index 3bf7ee050..e4c6db060 100644 --- a/tests/neg/singletons.scala +++ b/tests/neg/singletons.scala @@ -2,4 +2,8 @@ object Test { val a: 42 = 43 // error: different constant val x = 42 val z: 42 = x // error: x is not final + + val n: null = null // error: Null is not a legal singleton type + + val sym: 'sym = 'sym // error: Symbol is a legal singleton type } -- cgit v1.2.3