Skip to content

Commit ff6f041

Browse files
committed
Fix error messages in case of missing type arguments when using opaque types
1 parent ab777af commit ff6f041

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

jsoniter-scala-macros/shared/src/main/scala-3/com/github/plokhotnyuk/jsoniter_scala/macros/JsonCodecMaker.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -685,9 +685,11 @@ object JsonCodecMaker {
685685
case AppliedType(_, typeArgs) => typeArgs.map(_.dealias)
686686
case _ => Nil
687687

688-
def typeArg1(tpe: TypeRepr): TypeRepr = typeArgs(tpe).head
688+
def typeArg1(tpe: TypeRepr): TypeRepr =
689+
typeArgs(tpe).headOption.getOrElse(fail(s"Cannot get 1st type argument in '${tpe.show}'"))
689690

690-
def typeArg2(tpe: TypeRepr): TypeRepr = typeArgs(tpe).tail.head
691+
def typeArg2(tpe: TypeRepr): TypeRepr =
692+
typeArgs(tpe).tail.headOption.getOrElse(fail(s"Cannot get 2nd type argument in '${tpe.show}'"))
691693

692694
def isTuple(tpe: TypeRepr): Boolean = tpe <:< TypeRepr.of[Tuple]
693695

jsoniter-scala-macros/shared/src/test/scala-3/com/github/plokhotnyuk/jsoniter_scala/macros/JsonCodecMakerNewTypeSpec.scala

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,5 +309,28 @@ class JsonCodecMakerNewTypeSpec extends VerifyingSpec {
309309
"Only concrete (no free type parameters) Scala classes & objects are supported for ADT leaf classes."
310310
})
311311
}
312+
"don't generate codecs for opaque types that hides type arguments" in {
313+
assert(intercept[TestFailedException](assertCompiles {
314+
"""object Wrappers {
315+
| opaque type ReqId <: Option[String] = Option[String]
316+
|
317+
| object ReqId {
318+
| inline def apply(x: Option[String]): ReqId = x
319+
|
320+
| given JsonValueCodec[ReqId] = JsonCodecMaker.make[Option[String]]
321+
| }
322+
|}
323+
|
324+
|import Wrappers.*
325+
|import Wrappers.ReqId.*
326+
|
327+
|case class A(r: ReqId)
328+
|
329+
|given JsonValueCodec[A] = JsonCodecMaker.make
330+
|""".stripMargin
331+
}).getMessage.contains {
332+
"Cannot get 1st type argument in 'Wrappers.ReqId'"
333+
})
334+
}
312335
}
313336
}

0 commit comments

Comments
 (0)