Skip to content

Commit eb69396

Browse files
committed
Inline error messages.
1 parent 3338d82 commit eb69396

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}
@@ -134,7 +133,7 @@ object messages {
134133
}
135134

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

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

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -658,8 +658,12 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
658658
val cond1 = typed(tree.cond, defn.BooleanType)
659659
val thenp1 = typed(tree.thenp, pt.notApplied)
660660
val elsep1 = typed(tree.elsep orElse (untpd.unitLiteral withPos tree.pos), pt.notApplied)
661-
if (thenp1.tpe.topType != elsep1.tpe.topType)
662-
ctx.error(IfElsePhantom(thenp1, elsep1), tree.pos)
661+
if (thenp1.tpe.topType != elsep1.tpe.topType) {
662+
ctx.error(s"""if/else cannot have branches with types in different lattices:
663+
| ${thenp1.tpe.show} of lattice ${thenp1.tpe.topType.show}
664+
| ${elsep1.tpe.show} of lattice ${elsep1.tpe.topType.show}
665+
""".stripMargin, tree.pos)
666+
}
663667
val thenp2 :: elsep2 :: Nil = harmonize(thenp1 :: elsep1 :: Nil)
664668
assignType(cpy.If(tree)(cond1, thenp2, elsep2), thenp2, elsep2)
665669
}
@@ -832,7 +836,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
832836
case _ =>
833837
val sel1 = typedExpr(tree.selector)
834838
if (sel1.tpe.isPhantom)
835-
ctx.error(MatchOnPhantom(), sel1.pos)
839+
ctx.error("Cannot pattern match on phantoms", sel1.pos)
836840
val selType = widenForMatchSelector(
837841
fullyDefinedType(sel1.tpe, "pattern selector", tree.pos))
838842

@@ -869,7 +873,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
869873
if (tpdCases.nonEmpty) {
870874
val top = tpdCases.head.tpe.topType
871875
for (tpdCase <- tpdCases if tpdCase.tpe.topType != top)
872-
ctx.error(MatchPhantom(tpdCase, top), tpdCase.pos)
876+
ctx.error(s"Pattern expected case to return a ${top.show} but was ${tpdCase.tpe.show}", tpdCase.pos)
873877
}
874878

875879
tpdCases
@@ -1356,9 +1360,9 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
13561360

13571361
if (cls.classParents.exists(_.classSymbol eq defn.PhantomClass)) {
13581362
if (!cls.is(Module))
1359-
ctx.error(PhantomIsInObject(), cdef.pos)
1363+
ctx.error("Only object can extend scala.Phantom", cdef.pos)
13601364
else if (!cls.owner.is(Module) && !cls.owner.is(Package))
1361-
ctx.error(PhantomObjectIsInPackageOrObject(), cdef.pos)
1365+
ctx.error("An object extending scala.Phantom must be a top level object or in another object.", cdef.pos)
13621366
}
13631367

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

0 commit comments

Comments
 (0)