Skip to content

Commit 084cff8

Browse files
committed
Improved error messages in Desugar.scala
1 parent 0fdd4e3 commit 084cff8

File tree

2 files changed

+66
-4
lines changed

2 files changed

+66
-4
lines changed

src/dotty/tools/dotc/ast/Desugar.scala

+4-4
Original file line numberDiff line numberDiff line change
@@ -433,11 +433,11 @@ object desugar {
433433
if (!mods.is(Implicit))
434434
Nil
435435
else if (ctx.owner is Package) {
436-
ctx.error("implicit classes may not be toplevel", cdef.pos)
436+
ctx.error(TopLevelImplicitClass(cdef), cdef.pos)
437437
Nil
438438
}
439439
else if (isCaseClass) {
440-
ctx.error("implicit classes may not be case classes", cdef.pos)
440+
ctx.error(ImplicitCaseClass(cdef), cdef.pos)
441441
Nil
442442
}
443443
else
@@ -497,7 +497,7 @@ object desugar {
497497
.withPos(mdef.pos)
498498
val ValDef(selfName, selfTpt, _) = tmpl.self
499499
val selfMods = tmpl.self.mods
500-
if (!selfTpt.isEmpty) ctx.error("object definition may not have a self type", tmpl.self.pos)
500+
if (!selfTpt.isEmpty) ctx.error(ObjectMayNotHaveSelfType(mdef), tmpl.self.pos)
501501
val clsSelf = ValDef(selfName, SingletonTypeTree(Ident(name)), tmpl.self.rhs)
502502
.withMods(selfMods)
503503
.withPos(tmpl.self.pos orElse tmpl.pos.startPos)
@@ -931,7 +931,7 @@ object desugar {
931931
val arity = ts.length
932932
def tupleTypeRef = defn.TupleType(arity)
933933
if (arity > Definitions.MaxTupleArity) {
934-
ctx.error(s"tuple too long (max allowed: ${Definitions.MaxTupleArity})", tree.pos)
934+
ctx.error(TupleTooLong(ts), tree.pos)
935935
unitLiteral
936936
} else if (arity == 1) ts.head
937937
else if (ctx.mode is Mode.Type) AppliedTypeTree(ref(tupleTypeRef), ts)

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

+62
Original file line numberDiff line numberDiff line change
@@ -318,4 +318,66 @@ object messages {
318318
|$code2
319319
|""".stripMargin
320320
}
321+
322+
case class TopLevelImplicitClass(cdef: untpd.TypeDef)(implicit ctx: Context)
323+
extends Message(10) {
324+
val kind = "Syntax"
325+
326+
val msg = hl"""|An ${"implicit class"} may not be top-level"""
327+
328+
val explanation =
329+
hl"""|implicit class are not allowed to be defined at the top-level.
330+
|
331+
|Declare ${cdef.name} inside of an ${"object"} or ${"package object"}
332+
|then import the members of the object at the use site if needed, for example:
333+
|
334+
|package object Implicits {
335+
| implicit class ${cdef.name}...
336+
|}
337+
|
338+
|import Implicits.${cdef.name}""".stripMargin
339+
}
340+
341+
case class ImplicitCaseClass(cdef: untpd.TypeDef)(implicit ctx: Context)
342+
extends Message(11) {
343+
val kind = "Syntax"
344+
345+
val msg = hl"""|A ${"case class"} may not be defined as ${"implicit"}"""
346+
347+
val explanation =
348+
hl"""|implicit classes may not be case classes. Instead use a plain class:
349+
|
350+
|implicit class ${cdef.name}...""".stripMargin
351+
}
352+
353+
case class ObjectMayNotHaveSelfType(mdef: untpd.ModuleDef)(implicit ctx: Context)
354+
extends Message(12) {
355+
val kind = "Syntax"
356+
357+
val msg = hl"""|An ${"object"} must not have a ${"self type"}"""
358+
359+
val explanation =
360+
hl"""|objects ${mdef.name} must not have a ${"self type"}:
361+
|
362+
|Consider these alternative solutions:
363+
| Extend the object with the desired type
364+
| Use a trait or a class instead of an object""".stripMargin
365+
}
366+
367+
case class TupleTooLong(ts: List[untpd.Tree])(implicit ctx: Context)
368+
extends Message(13) {
369+
import Definitions.MaxTupleArity
370+
val kind = "Syntax"
371+
372+
val msg = hl"""|A ${"tuple"} cannot have more than ${MaxTupleArity} members"""
373+
374+
val explanation = {
375+
val members = ts.map(_.showSummary).grouped(MaxTupleArity)
376+
val nestedRepresentation = members.map(_.mkString(", ")).mkString(")(")
377+
hl"""|This restriction will be removed in the future.
378+
|Currently it is possible to use nested tuples when more than ${MaxTupleArity} are needed, for example:
379+
|
380+
| ((${nestedRepresentation}))""".stripMargin
381+
}
382+
}
321383
}

0 commit comments

Comments
 (0)