Skip to content

Commit 3e1a0ac

Browse files
author
Szymon Rodziewicz
committed
Review fixes
1 parent b1dea8f commit 3e1a0ac

File tree

10 files changed

+74
-46
lines changed

10 files changed

+74
-46
lines changed

compiler/src/dotty/tools/dotc/transform/SymUtils.scala

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ object SymUtils:
7979
* parameter section.
8080
*/
8181
def whyNotGenericProduct(using Context): String =
82-
if (!self.is(CaseClass)) i"$self is not a case class"
83-
else if (self.is(Abstract)) i"$self is an abstract class"
84-
else if (self.primaryConstructor.info.paramInfoss.length != 1) i"$self takes more than one parameter list"
85-
else if (isDerivedValueClass(self)) i"$self is a value class"
82+
if (!self.is(CaseClass)) "it is not a case class"
83+
else if (self.is(Abstract)) "it is an abstract class"
84+
else if (self.primaryConstructor.info.paramInfoss.length != 1) "it takes more than one parameter list"
85+
else if (isDerivedValueClass(self)) "it is a value class"
8686
else ""
8787

8888
def isGenericProduct(using Context): Boolean = whyNotGenericProduct.isEmpty
@@ -144,9 +144,9 @@ object SymUtils:
144144
*/
145145
def whyNotGenericSum(declScope: Symbol)(using Context): String =
146146
if (!self.is(Sealed))
147-
s"$self is not a sealed ${self.kindString}"
147+
s"it is not a sealed ${self.kindString}"
148148
else if (!self.isOneOf(AbstractOrTrait))
149-
s"$self is not an abstract class"
149+
"it is not an abstract class"
150150
else {
151151
val children = self.children
152152
val companionMirror = self.useCompanionAsSumMirror
@@ -157,20 +157,20 @@ object SymUtils:
157157
(self.isContainedIn(sym) && (companionMirror || declScope.isContainedIn(sym)))
158158
|| sym.is(Module) && isAccessible(sym.owner)
159159

160-
if (child == self) i"$self has anonymous or inaccessible subclasses"
161-
else if (!isAccessible(child.owner)) i"$self's child $child is not accessible"
160+
if (child == self) "it has anonymous or inaccessible subclasses"
161+
else if (!isAccessible(child.owner)) "its child $child is not accessible"
162162
else if (!child.isClass) ""
163163
else {
164164
val s = child.whyNotGenericProduct
165165
if (s.isEmpty) s
166166
else if (child.is(Sealed)) {
167167
val s = child.whyNotGenericSum(if child.useCompanionAsSumMirror then child.linkedClass else ctx.owner)
168168
if (s.isEmpty) s
169-
else i"$self's child $child is not a generic sum because $s"
170-
} else i"$self's child $child is not a generic product because $s"
169+
else i"its child $child is not a generic sum because $s"
170+
} else i"its child $child is not a generic product because $s"
171171
}
172172
}
173-
if (children.isEmpty) i"$self does not have subclasses"
173+
if (children.isEmpty) "it does not have subclasses"
174174
else children.map(problem).find(!_.isEmpty).getOrElse("")
175175
}
176176

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

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -323,14 +323,15 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
323323
withNoErrors(mirrorRef.cast(mirrorType))
324324
end makeProductMirror
325325

326-
def getErrors(cls: Symbol): List[String] =
327-
if !cls.isGenericProduct then
328-
List(cls.whyNotGenericProduct)
326+
def getError(cls: Symbol): String =
327+
val reason = if !cls.isGenericProduct then
328+
i"because ${cls.whyNotGenericProduct}"
329329
else if !canAccessCtor(cls) then
330-
List(i"Constructor of $cls is unnaccessible from the calling scope.")
330+
i"because constructor of $cls is unnaccessible from the calling scope."
331331
else
332-
List.empty
333-
end getErrors
332+
""
333+
i"$cls is not a generic product $reason"
334+
end getError
334335

335336
mirroredType match
336337
case AndType(tp1, tp2) =>
@@ -354,7 +355,7 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
354355
then
355356
makeProductMirror(cls)
356357
else
357-
(EmptyTree, getErrors(cls))
358+
(EmptyTree, List(getError(cls)))
358359
end productMirror
359360

360361
private def sumMirror(mirroredType: Type, formal: Type, span: Span)(using Context): TreeWithErrors =
@@ -426,7 +427,7 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
426427
else anonymousMirror(monoType, ExtendsSumMirror, span)
427428
withNoErrors(mirrorRef.cast(mirrorType))
428429
else if !clsIsGenericSum then
429-
(EmptyTree, List(cls.whyNotGenericSum(declScope)))
430+
(EmptyTree, List(i"$cls is not a generic sum because ${cls.whyNotGenericSum(declScope)}"))
430431
else
431432
EmptyTreeNoError
432433
end sumMirror
@@ -456,15 +457,7 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
456457
* where `T` is a generic sum or product or singleton type.
457458
*/
458459
val synthesizedMirror: SpecialHandler = (formal, span) =>
459-
formal.member(tpnme.MirroredType).info match
460-
case TypeBounds(mirroredType, _) =>
461-
if mirroredType.termSymbol.is(CaseVal)
462-
|| mirroredType.classSymbol.isGenericProduct
463-
then
464-
synthesizedProductMirror(formal, span)
465-
else
466-
synthesizedSumMirror(formal, span)
467-
case _ => EmptyTreeNoError
460+
orElse(synthesizedProductMirror(formal, span), synthesizedSumMirror(formal, span))
468461

469462
private def escapeJavaArray(tp: Type)(using Context): Type = tp match
470463
case JavaArrayType(elemTp) => defn.ArrayOf(escapeJavaArray(elemTp))
@@ -609,18 +602,27 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
609602
orElse(result, recur(rest))
610603
case Nil =>
611604
EmptyTreeNoError
612-
recur(specialHandlers)
605+
val result = recur(specialHandlers)
606+
clearErrorsIfNotEmpty(result)
613607

614608
end Synthesizer
615609

616610
object Synthesizer:
617611

618-
/** Tuple used to store the synthesis result with a list of errors */
612+
/** Tuple used to store the synthesis result with a list of errors. */
619613
type TreeWithErrors = (Tree, List[String])
620614
private def withNoErrors(tree: Tree): TreeWithErrors = (tree, List.empty)
621615

622616
private val EmptyTreeNoError: TreeWithErrors = withNoErrors(EmptyTree)
617+
623618
private def orElse(treeWithErrors1: TreeWithErrors, treeWithErrors2: => TreeWithErrors): TreeWithErrors = treeWithErrors1 match
624-
case (tree, errors) if tree eq genericEmptyTree => (treeWithErrors2._1, treeWithErrors2._2 ::: errors)
625-
case _ => treeWithErrors1
619+
case (tree, errors) if tree eq genericEmptyTree =>
620+
val (tree2, errors2) = treeWithErrors2
621+
(tree2, errors2 ::: errors)
622+
case _ => treeWithErrors1
623+
624+
private def clearErrorsIfNotEmpty(treeWithErrors: TreeWithErrors) = treeWithErrors match
625+
case (tree, _) if tree eq genericEmptyTree => treeWithErrors
626+
case (tree, _) => withNoErrors(tree)
627+
626628
end Synthesizer

tests/neg/i14025.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-- Error: tests/neg/i14025.scala:1:88 ----------------------------------------------------------------------------------
22
1 |val foo = summon[deriving.Mirror.Product { type MirroredType = [X] =>> [Y] =>> (X, Y) }] // error
33
| ^
4-
|No given instance of type deriving.Mirror.Product{MirroredType[X] = [Y] =>> (X, Y)} was found for parameter x of method summon in object Predef
4+
|No given instance of type deriving.Mirror.Product{MirroredType[X] = [Y] =>> (X, Y)} was found for parameter x of method summon in object Predef. Failed to synthesize an instance of type deriving.Mirror.Product{MirroredType[X] = [Y] =>> (X, Y)}: class Tuple2 is not a generic product
55
-- Error: tests/neg/i14025.scala:2:90 ----------------------------------------------------------------------------------
66
2 |val bar = summon[deriving.Mirror.Sum { type MirroredType = [X] =>> [Y] =>> List[(X, Y)] }] // error
77
| ^

tests/neg/i14432.check

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
-- Error: tests/neg/i14432.scala:13:33 ---------------------------------------------------------------------------------
22
13 |val mFoo = summon[Mirror.Of[Foo]] // error: no mirror found
33
| ^
4-
|No given instance of type deriving.Mirror.Of[example.Foo] was found for parameter x of method summon in object Predef. Failed to synthesize an instance of type deriving.Mirror.Of[example.Foo]: Constructor of class Foo is unnaccessible from the calling scope.
4+
|No given instance of type deriving.Mirror.Of[example.Foo] was found for parameter x of method summon in object Predef. Failed to synthesize an instance of type deriving.Mirror.Of[example.Foo]:
5+
| * class Foo is not a generic sum because it is not a sealed class
6+
| * class Foo is not a generic product because constructor of class Foo is unnaccessible from the calling scope.

tests/neg/i14432a.check

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
-- Error: tests/neg/i14432a.scala:14:43 --------------------------------------------------------------------------------
22
14 | val mFoo = summon[Mirror.Of[example.Foo]] // error: no mirror found
33
| ^
4-
|No given instance of type deriving.Mirror.Of[example.Foo] was found for parameter x of method summon in object Predef. Failed to synthesize an instance of type deriving.Mirror.Of[example.Foo]: Constructor of class Foo is unnaccessible from the calling scope.
4+
|No given instance of type deriving.Mirror.Of[example.Foo] was found for parameter x of method summon in object Predef. Failed to synthesize an instance of type deriving.Mirror.Of[example.Foo]:
5+
| * class Foo is not a generic sum because it is not a sealed class
6+
| * class Foo is not a generic product because constructor of class Foo is unnaccessible from the calling scope.

tests/neg/i14432b.check

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
-- Error: tests/neg/i14432b.scala:15:43 --------------------------------------------------------------------------------
22
15 | val mFoo = summon[Mirror.Of[example.Foo]] // error: no mirror found
33
| ^
4-
|No given instance of type deriving.Mirror.Of[example.Foo] was found for parameter x of method summon in object Predef. Failed to synthesize an instance of type deriving.Mirror.Of[example.Foo]: Constructor of class Foo is unnaccessible from the calling scope.
4+
|No given instance of type deriving.Mirror.Of[example.Foo] was found for parameter x of method summon in object Predef. Failed to synthesize an instance of type deriving.Mirror.Of[example.Foo]:
5+
| * class Foo is not a generic sum because it is not a sealed class
6+
| * class Foo is not a generic product because constructor of class Foo is unnaccessible from the calling scope.

tests/neg/i14432c.check

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@
99
-- Error: tests/neg/i14432c.scala:16:43 --------------------------------------------------------------------------------
1010
16 | val mFoo = summon[Mirror.Of[example.Foo]] // error: no mirror
1111
| ^
12-
|No given instance of type deriving.Mirror.Of[example.Foo] was found for parameter x of method summon in object Predef. Failed to synthesize an instance of type deriving.Mirror.Of[example.Foo]: Constructor of class Foo is unnaccessible from the calling scope.
12+
|No given instance of type deriving.Mirror.Of[example.Foo] was found for parameter x of method summon in object Predef. Failed to synthesize an instance of type deriving.Mirror.Of[example.Foo]:
13+
| * class Foo is not a generic sum because it is not a sealed class
14+
| * class Foo is not a generic product because constructor of class Foo is unnaccessible from the calling scope.

tests/neg/i14432d.check

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
-- Error: tests/neg/i14432d.scala:17:45 --------------------------------------------------------------------------------
22
17 | val mFoo = summon[Mirror.Of[example.Foo]] // error
33
| ^
4-
|No given instance of type deriving.Mirror.Of[example.Foo] was found for parameter x of method summon in object Predef. Failed to synthesize an instance of type deriving.Mirror.Of[example.Foo]: Constructor of class Foo is unnaccessible from the calling scope.
4+
|No given instance of type deriving.Mirror.Of[example.Foo] was found for parameter x of method summon in object Predef. Failed to synthesize an instance of type deriving.Mirror.Of[example.Foo]:
5+
| * class Foo is not a generic sum because it is not a sealed class
6+
| * class Foo is not a generic product because constructor of class Foo is unnaccessible from the calling scope.

tests/neg/i14823.check

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
-- Error: tests/neg/i14823.scala:8:50 ----------------------------------------------------------------------------------
22
8 |val baz = summon[Mirror.Of[SubA[Int] | SubB[Int]]] // error
33
| ^
4-
|No given instance of type deriving.Mirror.Of[SubA[Int] | SubB[Int]] was found for parameter x of method summon in object Predef
4+
|No given instance of type deriving.Mirror.Of[SubA[Int] | SubB[Int]] was found for parameter x of method summon in object Predef. Failed to synthesize an instance of type deriving.Mirror.Of[SubA[Int] | SubB[Int]]:
5+
| * class Cov is not a generic sum because it is not a sealed class
6+
| * class Cov is not a generic product
Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,42 @@
11
-- Error: tests/neg/mirror-synthesis-errors.scala:21:32 ----------------------------------------------------------------
22
21 |val testA = summon[Mirror.Of[A]] // error: Not a sealed trait
33
| ^
4-
|No given instance of type deriving.Mirror.Of[A] was found for parameter x of method summon in object Predef. Failed to synthesize an instance of type deriving.Mirror.Of[A]: trait A is not a sealed trait
4+
|No given instance of type deriving.Mirror.Of[A] was found for parameter x of method summon in object Predef. Failed to synthesize an instance of type deriving.Mirror.Of[A]:
5+
| * trait A is not a generic sum because it is not a sealed trait
6+
| * trait A is not a generic product because it is not a case class
57
-- Error: tests/neg/mirror-synthesis-errors.scala:22:32 ----------------------------------------------------------------
68
22 |val testC = summon[Mirror.Of[C]] // error: Does not have subclasses
79
| ^
8-
|No given instance of type deriving.Mirror.Of[C] was found for parameter x of method summon in object Predef. Failed to synthesize an instance of type deriving.Mirror.Of[C]: trait C does not have subclasses
10+
|No given instance of type deriving.Mirror.Of[C] was found for parameter x of method summon in object Predef. Failed to synthesize an instance of type deriving.Mirror.Of[C]:
11+
| * trait C is not a generic sum because it does not have subclasses
12+
| * trait C is not a generic product because it is not a case class
913
-- Error: tests/neg/mirror-synthesis-errors.scala:23:32 ----------------------------------------------------------------
1014
23 |val testD = summon[Mirror.Of[D]] // error: child SubD takes more than one parameter list
1115
| ^
12-
|No given instance of type deriving.Mirror.Of[D] was found for parameter x of method summon in object Predef. Failed to synthesize an instance of type deriving.Mirror.Of[D]: class D's child class SubD is not a generic product because class SubD takes more than one parameter list
16+
|No given instance of type deriving.Mirror.Of[D] was found for parameter x of method summon in object Predef. Failed to synthesize an instance of type deriving.Mirror.Of[D]:
17+
| * class D is not a generic sum because its child class SubD is not a generic product because it takes more than one parameter list
18+
| * class D is not a generic product because it is not a case class
1319
-- Error: tests/neg/mirror-synthesis-errors.scala:24:38 ----------------------------------------------------------------
1420
24 |val testSubD = summon[Mirror.Of[SubD]] // error: takes more than one parameter list
1521
| ^
16-
|No given instance of type deriving.Mirror.Of[SubD] was found for parameter x of method summon in object Predef. Failed to synthesize an instance of type deriving.Mirror.Of[SubD]: class SubD is not a sealed class
22+
|No given instance of type deriving.Mirror.Of[SubD] was found for parameter x of method summon in object Predef. Failed to synthesize an instance of type deriving.Mirror.Of[SubD]:
23+
| * class SubD is not a generic sum because it is not a sealed class
24+
| * class SubD is not a generic product because it takes more than one parameter list
1725
-- Error: tests/neg/mirror-synthesis-errors.scala:25:32 ----------------------------------------------------------------
1826
25 |val testE = summon[Mirror.Of[E]] // error: Not an abstract class
1927
| ^
20-
|No given instance of type deriving.Mirror.Of[E] was found for parameter x of method summon in object Predef. Failed to synthesize an instance of type deriving.Mirror.Of[E]: class E is not an abstract class
28+
|No given instance of type deriving.Mirror.Of[E] was found for parameter x of method summon in object Predef. Failed to synthesize an instance of type deriving.Mirror.Of[E]:
29+
| * class E is not a generic sum because it is not an abstract class
30+
| * class E is not a generic product because it is not a case class
2131
-- Error: tests/neg/mirror-synthesis-errors.scala:26:32 ----------------------------------------------------------------
2232
26 |val testF = summon[Mirror.Of[F]] // error: No children
2333
| ^
24-
|No given instance of type deriving.Mirror.Of[F] was found for parameter x of method summon in object Predef. Failed to synthesize an instance of type deriving.Mirror.Of[F]: trait F does not have subclasses
34+
|No given instance of type deriving.Mirror.Of[F] was found for parameter x of method summon in object Predef. Failed to synthesize an instance of type deriving.Mirror.Of[F]:
35+
| * trait F is not a generic sum because it does not have subclasses
36+
| * trait F is not a generic product because it is not a case class
2537
-- Error: tests/neg/mirror-synthesis-errors.scala:27:36 ----------------------------------------------------------------
2638
27 |val testG = summon[Mirror.Of[Foo.G]] // error: Has anonymous subclasses
2739
| ^
28-
|No given instance of type deriving.Mirror.Of[Foo.G] was found for parameter x of method summon in object Predef. Failed to synthesize an instance of type deriving.Mirror.Of[Foo.G]: trait G has anonymous or inaccessible subclasses
40+
|No given instance of type deriving.Mirror.Of[Foo.G] was found for parameter x of method summon in object Predef. Failed to synthesize an instance of type deriving.Mirror.Of[Foo.G]:
41+
| * trait G is not a generic sum because it has anonymous or inaccessible subclasses
42+
| * trait G is not a generic product because it is not a case class

0 commit comments

Comments
 (0)