Skip to content

Better error message if derived trait does not have companion #11077

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/Deriving.scala
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,11 @@ trait Deriving {
companionRef(tp.underlying)
}
val resultType = instantiated(sym.info)
val module = untpd.ref(companionRef(resultType)).withSpan(sym.span)
val companion = companionRef(resultType)
val module = untpd.ref(companion).withSpan(sym.span)
val rhs = untpd.Select(module, nme.derived)
typed(rhs, resultType)
if companion.termSymbol.exists then typed(rhs, resultType)
else errorTree(rhs, em"$resultType cannot be derived since ${resultType.typeSymbol} has no companion object")
end typeclassInstance

def syntheticDef(sym: Symbol): Tree = inContext(ctx.fresh.setOwner(sym).setNewScope) {
Expand Down
4 changes: 4 additions & 0 deletions tests/neg/i11072.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- Error: tests/neg/i11072.scala:3:17 ----------------------------------------------------------------------------------
3 |enum Foo derives Sealed { // error
| ^
| Sealed[Foo] cannot be derived since trait Sealed has no companion object
5 changes: 5 additions & 0 deletions tests/neg/i11072.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
trait Sealed[A]

enum Foo derives Sealed { // error
case A, B, C
}