Skip to content

Commit 78dfc7a

Browse files
committed
Add restriction on phantom latice definition.
1 parent f382f40 commit 78dfc7a

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public enum ErrorMessageID {
6464
MatchOnPhantomID,
6565
IfElsePhantomID,
6666
PhantomIsInObjectID,
67+
PhantomObjectIsInPackageOrObjectID,
6768
;
6869

6970
public int errorNumber() {

compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,4 +1299,12 @@ object messages {
12991299
val explanation =
13001300
hl"""|""".stripMargin
13011301
}
1302+
1303+
case class PhantomObjectIsInPackageOrObject()(implicit ctx: Context) extends Message(PhantomObjectIsInPackageOrObjectID) {
1304+
val kind = "Phantom restriction"
1305+
val msg = s"An ${"object"} extending ${"scala.Phantom"} must be a top level ${"object"} or in another object"
1306+
1307+
val explanation =
1308+
hl"""|""".stripMargin
1309+
}
13021310
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1352,8 +1352,12 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
13521352
cls, isRequired, cdef.pos)
13531353
}
13541354

1355-
if (!cls.is(Module) && cls.classParents.exists(_.classSymbol eq defn.PhantomClass))
1356-
ctx.error(PhantomIsInObject(), cdef.pos)
1355+
if (cls.classParents.exists(_.classSymbol eq defn.PhantomClass)) {
1356+
if (!cls.is(Module))
1357+
ctx.error(PhantomIsInObject(), cdef.pos)
1358+
else if (!cls.owner.is(Module) && !cls.owner.is(Package))
1359+
ctx.error(PhantomObjectIsInPackageOrObject(), cdef.pos)
1360+
}
13571361

13581362
// check value class constraints
13591363
checkDerivedValueClass(cls, body1)

tests/neg/phantom-trait-4.scala

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
class Foo {
3+
4+
object Boo1 extends Phantom // error
5+
6+
def foo = {
7+
object Boo2 extends Phantom // error
8+
42
9+
}
10+
}
11+
12+
object Foo {
13+
object Boo1 extends Phantom
14+
15+
def foo = {
16+
object Boo2 extends Phantom // error
17+
42
18+
}
19+
}
20+
21+
package foo {
22+
object Boo1 extends Phantom
23+
}

0 commit comments

Comments
 (0)