From a6ccab92fe218cfe170eb5dadc2c3e898482f573 Mon Sep 17 00:00:00 2001 From: Jeon Yoonjae Date: Thu, 6 Nov 2025 00:31:34 +0900 Subject: [PATCH] Fix crash in Selectable unapply with custom applyDynamic (#24343) closes: https://github.com/scala/scala3/issues/24168 [Cherry-picked c9c7187ccaa0237d05b82a74338143f07fa6f949] --- compiler/src/dotty/tools/dotc/typer/Dynamic.scala | 4 ++-- tests/neg/i24168.scala | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 tests/neg/i24168.scala diff --git a/compiler/src/dotty/tools/dotc/typer/Dynamic.scala b/compiler/src/dotty/tools/dotc/typer/Dynamic.scala index 8d48651ad6e9..683244cc8795 100644 --- a/compiler/src/dotty/tools/dotc/typer/Dynamic.scala +++ b/compiler/src/dotty/tools/dotc/typer/Dynamic.scala @@ -43,9 +43,9 @@ object DynamicUnapply { def unapply(tree: tpd.Tree): Option[List[tpd.Tree]] = tree match case TypeApply(Select(qual, name), _) if name == nme.asInstanceOfPM => unapply(qual) - case Apply(Apply(Select(selectable, fname), Literal(Constant(name)) :: ctag :: Nil), _ :: implicits) + case Apply(Apply(Select(selectable, fname), Literal(Constant(name)) :: restArgs), _ :: implicits) if fname == nme.applyDynamic && (name == "unapply" || name == "unapplySeq") => - Some(selectable :: ctag :: implicits) + Some(selectable :: (restArgs ::: implicits)) case _ => None } diff --git a/tests/neg/i24168.scala b/tests/neg/i24168.scala new file mode 100644 index 000000000000..b554e4d817d5 --- /dev/null +++ b/tests/neg/i24168.scala @@ -0,0 +1,12 @@ +trait Generic extends Selectable: + def applyDynamic(name: String)(args: Any*): Any = () + +val foo: Generic { + def unapply(x: Int): Option[Unit] +} = new Generic: + def unapply(x: Int): Option[Unit] = Some(()) + +def x = + 42 match + case foo(()) => println("lol") // error +