Skip to content

Commit d65aa23

Browse files
committed
Inline error messages.
1 parent 09205df commit d65aa23

File tree

3 files changed

+25
-114
lines changed

3 files changed

+25
-114
lines changed

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,6 @@ public enum ErrorMessageID {
5656
CyclicReferenceInvolvingID,
5757
CyclicReferenceInvolvingImplicitID,
5858
SuperQualMustBeParentID,
59-
ErasedPhantomsSignatureCollisionID,
60-
PhantomInheritanceID,
61-
PhantomMixedBoundsID,
62-
PhantomCrossedMixedBoundsID,
63-
MatchPhantomID,
64-
MatchOnPhantomID,
65-
IfElsePhantomID,
66-
PhantomIsInObjectID,
67-
PhantomObjectIsInPackageOrObjectID,
6859
;
6960

7061
public int errorNumber() {

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

Lines changed: 1 addition & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import Symbols._
1010
import Names._
1111
import NameOps._
1212
import Types._
13-
import Flags._
1413
import util.SourcePosition
1514
import config.Settings.Setting
1615
import interfaces.Diagnostic.{ERROR, INFO, WARNING}
@@ -136,7 +135,7 @@ object messages {
136135
}
137136

138137
case class EmptyCatchBlock(tryBody: untpd.Tree)(implicit ctx: Context)
139-
extends EmptyCatchOrFinallyBlock(tryBody, EmptyCatchBlockID) {
138+
extends EmptyCatchOrFinallyBlock(tryBody, EmptyCatchBlockID) {
140139
val kind = "Syntax"
141140
val msg =
142141
hl"""|The ${"catch"} block does not contain a valid expression, try
@@ -1219,93 +1218,4 @@ object messages {
12191218
|Attempting to define a field in a method signature after a varargs field is an error.
12201219
|""".stripMargin
12211220
}
1222-
1223-
case class ErasedPhantomsSignatureCollision(decls: Iterable[Symbol], erased: (Name, Type))(implicit ctx: Context)
1224-
extends Message(ErasedPhantomsSignatureCollisionID) {
1225-
val kind = "Phantom restriction"
1226-
val msg = em"After phantom erasure methods $methodsString will have the same signature: ${erased._1}${erased._2}"
1227-
1228-
private def methodsString = decls.map(decl => em"${decl.name}${decl.info}").mkString(", ")
1229-
1230-
val explanation =
1231-
hl"""|Phantom erasure removes all phantom parameters/arguments from methods and functions.
1232-
|""".stripMargin
1233-
}
1234-
1235-
case class PhantomInheritance(cdef: tpd.TypeDef)(implicit ctx: Context)
1236-
extends Message(PhantomInheritanceID) {
1237-
val kind = "Phantom restriction"
1238-
val msg = perfix + " cannot extend both Any and PhantomAny."
1239-
1240-
def perfix =
1241-
if (cdef.symbol.flags.is(Flags.Trait)) "A trait"
1242-
else if (cdef.symbol.flags.is(Flags.Abstract)) "An abstract class"
1243-
else "A class"
1244-
1245-
val explanation =
1246-
hl"""|""".stripMargin
1247-
}
1248-
1249-
case class PhantomMixedBounds(op: untpd.Ident)(implicit ctx: Context)
1250-
extends Message(PhantomMixedBoundsID) {
1251-
val kind = "Phantom restriction"
1252-
val msg = hl"Can not mix types of ${"Any"} and ${"Phantom.Any"} with ${op.show}."
1253-
1254-
val explanation =
1255-
hl"""|""".stripMargin
1256-
}
1257-
1258-
case class PhantomCrossedMixedBounds(lo: untpd.Tree, hi: untpd.Tree)(implicit ctx: Context)
1259-
extends Message(PhantomCrossedMixedBoundsID) {
1260-
val kind = "Phantom restriction"
1261-
val msg = hl"Type can not be bounded at the same time by types in the ${"Any"} and ${"Phantom.Any"} latices."
1262-
1263-
val explanation =
1264-
hl"""|""".stripMargin
1265-
}
1266-
1267-
case class MatchPhantom(tpdCase: tpd.CaseDef, expected: Type)(implicit ctx: Context) extends Message(MatchPhantomID) {
1268-
val kind = "Phantom restriction"
1269-
val msg = s"Pattern expected case to return a ${expected.show} but was ${tpdCase.tpe.show}"
1270-
1271-
val explanation =
1272-
hl"""|""".stripMargin
1273-
}
1274-
1275-
1276-
case class MatchOnPhantom()(implicit ctx: Context) extends Message(MatchOnPhantomID) {
1277-
val kind = "Phantom restriction"
1278-
val msg = "Cannot pattern match on phantoms"
1279-
1280-
val explanation =
1281-
hl"""|""".stripMargin
1282-
}
1283-
1284-
case class IfElsePhantom(thenp: tpd.Tree, elsep: tpd.Tree)(implicit ctx: Context) extends Message(IfElsePhantomID) {
1285-
val kind = "Phantom restriction"
1286-
val msg =
1287-
s"""if/else cannot have branches with types in different lattices:
1288-
| ${thenp.tpe.show} of lattice ${thenp.tpe.topType.show}
1289-
| ${elsep.tpe.show} of lattice ${elsep.tpe.topType.show}
1290-
""".stripMargin
1291-
1292-
val explanation =
1293-
hl"""|""".stripMargin
1294-
}
1295-
1296-
case class PhantomIsInObject()(implicit ctx: Context) extends Message(PhantomIsInObjectID) {
1297-
val kind = "Phantom restriction"
1298-
val msg = s"Only ${"object"} can extend ${"scala.Phantom"}"
1299-
1300-
val explanation =
1301-
hl"""|""".stripMargin
1302-
}
1303-
1304-
case class PhantomObjectIsInPackageOrObject()(implicit ctx: Context) extends Message(PhantomObjectIsInPackageOrObjectID) {
1305-
val kind = "Phantom restriction"
1306-
val msg = s"An ${"object"} extending ${"scala.Phantom"} must be a top level ${"object"} or in another object"
1307-
1308-
val explanation =
1309-
hl"""|""".stripMargin
1310-
}
13111221
}

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

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -665,8 +665,12 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
665665
val cond1 = typed(tree.cond, defn.BooleanType)
666666
val thenp1 = typed(tree.thenp, pt.notApplied)
667667
val elsep1 = typed(tree.elsep orElse (untpd.unitLiteral withPos tree.pos), pt.notApplied)
668-
if (thenp1.tpe.topType != elsep1.tpe.topType)
669-
ctx.error(IfElsePhantom(thenp1, elsep1), tree.pos)
668+
if (thenp1.tpe.topType != elsep1.tpe.topType) {
669+
ctx.error(s"""if/else cannot have branches with types in different lattices:
670+
| ${thenp1.tpe.show} of lattice ${thenp1.tpe.topType.show}
671+
| ${elsep1.tpe.show} of lattice ${elsep1.tpe.topType.show}
672+
""".stripMargin, tree.pos)
673+
}
670674
val thenp2 :: elsep2 :: Nil = harmonize(thenp1 :: elsep1 :: Nil)
671675
assignType(cpy.If(tree)(cond1, thenp2, elsep2), thenp2, elsep2)
672676
}
@@ -839,7 +843,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
839843
case _ =>
840844
val sel1 = typedExpr(tree.selector)
841845
if (sel1.tpe.isPhantom)
842-
ctx.error(MatchOnPhantom(), sel1.pos)
846+
ctx.error("Cannot pattern match on phantoms", sel1.pos)
843847
val selType = fullyDefinedType(sel1.tpe, "pattern selector", tree.pos).widen
844848

845849
val cases1 = typedCases(tree.cases, selType, pt.notApplied)
@@ -875,7 +879,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
875879
if (tpdCases.nonEmpty) {
876880
val top = tpdCases.head.tpe.topType
877881
for (tpdCase <- tpdCases if tpdCase.tpe.topType != top)
878-
ctx.error(MatchPhantom(tpdCase, top), tpdCase.pos)
882+
ctx.error(s"Pattern expected case to return a ${top.show} but was ${tpdCase.tpe.show}", tpdCase.pos)
879883
}
880884

881885
tpdCases
@@ -1362,9 +1366,9 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
13621366

13631367
if (cls.classParents.exists(_.classSymbol eq defn.PhantomClass)) {
13641368
if (!cls.is(Module))
1365-
ctx.error(PhantomIsInObject(), cdef.pos)
1369+
ctx.error("Only object can extend scala.Phantom", cdef.pos)
13661370
else if (!cls.owner.is(Module) && !cls.owner.is(Package))
1367-
ctx.error(PhantomObjectIsInPackageOrObject(), cdef.pos)
1371+
ctx.error("An object extending scala.Phantom must be a top level object or in another object.", cdef.pos)
13681372
}
13691373

13701374
// check value class constraints
@@ -1675,17 +1679,23 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
16751679
* this includes Phantom.Any of different universes.
16761680
*/
16771681
def checkedTops(tree: untpd.Tree): Set[Type] = {
1678-
def checkedTops2(tree1: untpd.Tree, tree2: untpd.Tree, msg: => Message, pos: Position): Set[Type] = {
1679-
val allTops = checkedTops(tree1) union checkedTops(tree2)
1680-
if (allTops.size > 1)
1681-
ctx.error(msg, tree.pos)
1682-
allTops
1683-
}
16841682
tree match {
16851683
case TypeBoundsTree(lo, hi) =>
1686-
checkedTops2(lo, hi, PhantomCrossedMixedBounds(lo, hi), tree.pos)
1684+
val allTops = checkedTops(hi) union checkedTops(lo)
1685+
if (allTops.size <= 1) allTops
1686+
else {
1687+
ctx.error("Type can not be bounded at the same time by types in different latices: " +
1688+
allTops.map(_.show).mkString(", "), tree.pos)
1689+
Set.empty
1690+
}
16871691
case untpd.InfixOp(left, op, right) =>
1688-
checkedTops2(left, right, PhantomCrossedMixedBounds(left, right), tree.pos)
1692+
val allTops = checkedTops(left) union checkedTops(right)
1693+
if (allTops.size <= 1) allTops
1694+
else {
1695+
ctx.error(s"Can not use ${op.show} mix types of different lattices: " +
1696+
allTops.map(_.show).mkString(", "), tree.pos)
1697+
Set.empty
1698+
}
16891699
case EmptyTree => Set.empty
16901700
case _ => Set(tree.typeOpt.topType)
16911701
}

0 commit comments

Comments
 (0)