Skip to content

Commit b43778f

Browse files
authored
Merge pull request #11077 from dotty-staging/fix-#11072
Better error message if derived trait does not have companion
2 parents 8345078 + 4e34f94 commit b43778f

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,9 +291,11 @@ trait Deriving {
291291
companionRef(tp.underlying)
292292
}
293293
val resultType = instantiated(sym.info)
294-
val module = untpd.ref(companionRef(resultType)).withSpan(sym.span)
294+
val companion = companionRef(resultType)
295+
val module = untpd.ref(companion).withSpan(sym.span)
295296
val rhs = untpd.Select(module, nme.derived)
296-
typed(rhs, resultType)
297+
if companion.termSymbol.exists then typed(rhs, resultType)
298+
else errorTree(rhs, em"$resultType cannot be derived since ${resultType.typeSymbol} has no companion object")
297299
end typeclassInstance
298300

299301
def syntheticDef(sym: Symbol): Tree = inContext(ctx.fresh.setOwner(sym).setNewScope) {

tests/neg/i11072.check

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-- Error: tests/neg/i11072.scala:3:17 ----------------------------------------------------------------------------------
2+
3 |enum Foo derives Sealed { // error
3+
| ^
4+
| Sealed[Foo] cannot be derived since trait Sealed has no companion object

tests/neg/i11072.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
trait Sealed[A]
2+
3+
enum Foo derives Sealed { // error
4+
case A, B, C
5+
}

0 commit comments

Comments
 (0)