Skip to content

Commit be3992d

Browse files
committed
Add restriction on phantom latice definition.
1 parent db2e156 commit be3992d

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
@@ -1296,4 +1296,12 @@ object messages {
12961296
val explanation =
12971297
hl"""|""".stripMargin
12981298
}
1299+
1300+
case class PhantomObjectIsInPackageOrObject()(implicit ctx: Context) extends Message(PhantomObjectIsInPackageOrObjectID) {
1301+
val kind = "Phantom restriction"
1302+
val msg = s"An ${"object"} extending ${"scala.Phantom"} must be a top level ${"object"} or in another object"
1303+
1304+
val explanation =
1305+
hl"""|""".stripMargin
1306+
}
12991307
}

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

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

1361-
if (!cls.is(Module) && cls.classParents.exists(_.classSymbol eq defn.PhantomClass))
1362-
ctx.error(PhantomIsInObject(), cdef.pos)
1361+
if (cls.classParents.exists(_.classSymbol eq defn.PhantomClass)) {
1362+
if (!cls.is(Module))
1363+
ctx.error(PhantomIsInObject(), cdef.pos)
1364+
else if (!cls.owner.is(Module) && !cls.owner.is(Package))
1365+
ctx.error(PhantomObjectIsInPackageOrObject(), cdef.pos)
1366+
}
13631367

13641368
// check value class constraints
13651369
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)