8000 Do not generate default implicit for sealed non-abstract non-case class by dhoepelman · Pull Request #80 · scala-tsi/scala-tsi · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Do not generate default implicit for sealed non-abstract non-case class #80

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 2 commits into from
Aug 28, 2020
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
9 changes: 6 additions & 3 deletions macros/src/main/scala/com/scalatsi/Macros.scala
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private[scalatsi] class Macros(val c: blackbox.Context) {
val classSymbol = symbol.asClass
if (classSymbol.isCaseClass)
generateInterfaceFromCaseClass[T]
else if (classSymbol.isSealed)
else if (isSealedTraitOrAbstractClass(classSymbol))
generateUnionFromSealedTrait[T]
else err()
}
Expand Down Expand Up @@ -149,12 +149,15 @@ private[scalatsi] class Macros(val c: blackbox.Context) {
symbol.asClass
}

private def isSealedTraitOrAbstractClass(symbol: ClassSymbol): Boolean =
symbol.isSealed && (symbol.isTrait || symbol.isAbstract)

def generateUnionFromSealedTrait[T: c.WeakTypeTag]: Tree = {
val T = c.weakTypeOf[T]
val symbol = getClassOrTraitSymbol(T)

if (!symbol.isSealed)
c.abort(c.enclosingPosition, s"Expected sealed trait or sealed class, but found: $T")
if (!isSealedTraitOrAbstractClass(symbol))
c.abort(c.enclosingPosition, s"Expected sealed trait or sealed abstract class, but found: $T")

val name = symbol.name.toString

Expand Down
6 changes: 6 additions & 0 deletions src/test/scala/com/scalatsi/MacroTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@ class MacroTests extends FlatSpec with Matchers with DefaultTSTypes {
"TSType.getOrGenerate[RecursiveA]" shouldNot compile
}

it should "give an error on non-abstract sealed class" in {
@nowarn("cat=unused") sealed class Something {}

"TSType.getOrGenerate[Something]" shouldNot compile
}

"TSIType.getOrGenerate" should "use available implicit if in scope" in {
case class A(foo: String)

Expand Down
0