Skip to content

Commit 888ec2b

Browse files
committed
improve error message
1 parent 49b8bac commit 888ec2b

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

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

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
302302
case ClassSymbol(cls) => cls
303303
case GenericTuple(arity, _) =>
304304
if arity <= Definitions.MaxTupleArity then defn.TupleType(arity).nn.classSymbol
305-
else defn.TupleXXLClass
305+
else defn.PairClass
306306

307307
object MirrorSource:
308308
def tuple(tps: List[Type]): MirrorSource.GenericTuple = MirrorSource.GenericTuple(tps.size, tps)
@@ -340,21 +340,24 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
340340
else None
341341

342342
/** do all parts match the class symbol? */
343-
def whyNotAcceptableType(tp: Type, msrc: MirrorSource): String = tp match
344-
case tp: HKTypeLambda if tp.resultType.isInstanceOf[HKTypeLambda] =>
345-
i"its subpart `$tp` is not a supported kind (either `*` or `* -> *`)"
346-
case OrType(tp1, tp2) => i"its subpart `$tp` is a top-level union type."
347-
case GenericTupleType(args) if args.size <= Definitions.MaxTupleArity =>
348-
val tup = MirrorSource.tuple(args)
349-
if tup.equiv(msrc) then ""
350-
else i"a subpart reduces to the unrelated tuple ${tup.asClass}, expected ${msrc.asClass}"
351-
case tp: TypeProxy => whyNotAcceptableType(tp.underlying, msrc)
352-
case _ =>
353-
mirrorSource(tp) match
354-
case Some(msrc2) =>
355-
if msrc2.equiv(msrc) then ""
356-
else i"a subpart reduces to the more precise ${msrc2.asClass}, expected ${msrc.asClass}"
357-
case _ => "???" // caught early by initial `mirrorSource` that made `msrc`
343+
def whyNotAcceptableType(tp: Type, msrc: MirrorSource): String =
344+
def compareMirrorSrc(msrc1: MirrorSource): String =
345+
if msrc1.equiv(msrc) then ""
346+
else i"a subpart reduces to the unrelated ${msrc1.asClass}, expected ${msrc.asClass}"
347+
tp match
348+
case tp: HKTypeLambda if tp.resultType.isInstanceOf[HKTypeLambda] =>
349+
i"its subpart $tp is not a supported kind (either `*` or `* -> *`)"
350+
case OrType(tp1, tp2) => i"its subpart `$tp` is a top-level union type."
351+
case GenericTupleType(args) =>
352+
val maxArity = Definitions.MaxTupleArity
353+
val tup = MirrorSource.tuple(args)
354+
if tup.arity <= maxArity then compareMirrorSrc(tup)
355+
else i"a subpart `$tp` reduces to a tuple with arity ${tup.arity}, expected arity <= $maxArity"
356+
case tp: TypeProxy => whyNotAcceptableType(tp.underlying, msrc)
357+
case _ =>
358+
mirrorSource(tp) match
359+
case Some(msrc2) => compareMirrorSrc(msrc2)
360+
case _ => i"a subpart `$tp` does not reduce to a class or generic tuple type"
358361

359362
/** widen TermRef to see if they are an alias to an enum singleton */
360363
def isEnumSingletonRef(tp: Type)(using Context): Boolean = tp match
@@ -418,7 +421,7 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
418421
withErrors(i"${msrc.asClass} is not a generic product because ${msrc.asClass.whyNotGenericProduct}")
419422
else withErrors(i"type `$mirroredType` is not a generic product because $acceptableMsg")
420423
case None =>
421-
withErrors(i"type `$mirroredType` does not reduce to a class or generic tuple type")
424+
withErrors(i"type $mirroredType does not reduce to a class or generic tuple type")
422425
end productMirror
423426

424427
private def sumMirror(mirroredType: Type, formal: Type, span: Span)(using Context): TreeWithErrors =
@@ -434,7 +437,7 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
434437
case OrType(tp1, tp2) => i"its subpart `$tp` is a top-level union type."
435438
case _ =>
436439
if tp.classSymbol eq cls then ""
437-
else i"a subpart reduces to the more precise ${tp.classSymbol}, expected $cls"
440+
else i"a subpart reduces to the unrelated ${tp.classSymbol}, expected $cls"
438441

439442

440443
val acceptableMsg = whyNotAcceptableType(mirroredType)

tests/neg/i14127.check

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-- Error: tests/neg/i14127.scala:6:55 ----------------------------------------------------------------------------------
2+
6 | *: Int *: Int *: Int *: Int *: Int *: EmptyTuple)]] // error
3+
| ^
4+
|No given instance of type deriving.Mirror.Of[(Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int,
5+
| Int
6+
|, Int, Int)] was found for parameter x of method summon in object Predef. Failed to synthesize an instance of type deriving.Mirror.Of[(Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int,
7+
| Int
8+
|, Int, Int)]:
9+
| * type (Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int) is not a generic product because a subpart `(Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int)` reduces to a tuple with arity 23, expected arity <= 22
10+
| * class *: is not a generic sum because it does not have subclasses

0 commit comments

Comments
 (0)