Skip to content

Commit acb9b8f

Browse files
Fix inline of unapply nodes
1 parent c54e7fb commit acb9b8f

File tree

4 files changed

+51
-2
lines changed

4 files changed

+51
-2
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -962,9 +962,10 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
962962

963963
val dummyArg = dummyTreeOfType(ownType)
964964
val unapplyApp = typedExpr(untpd.TypedSplice(Apply(unapplyFn, dummyArg :: Nil)))
965-
val unapplyImplicits = unapplyApp match {
965+
def unapplyImplicits(unapp: Tree = unapplyApp): List[Tree] = unapp match {
966966
case Apply(Apply(unapply, `dummyArg` :: Nil), args2) => assert(args2.nonEmpty); args2
967967
case Apply(unapply, `dummyArg` :: Nil) => Nil
968+
case Inlined(u, _, _) => unapplyImplicits(u)
968969
}
969970

970971
var argTypes = unapplyArgs(unapplyApp.tpe, unapplyFn, args, tree.pos)
@@ -982,7 +983,7 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
982983
List.fill(argTypes.length - args.length)(WildcardType)
983984
}
984985
val unapplyPatterns = (bunchedArgs, argTypes).zipped map (typed(_, _))
985-
val result = assignType(cpy.UnApply(tree)(unapplyFn, unapplyImplicits, unapplyPatterns), ownType)
986+
val result = assignType(cpy.UnApply(tree)(unapplyFn, unapplyImplicits(), unapplyPatterns), ownType)
986987
unapp.println(s"unapply patterns = $unapplyPatterns")
987988
if ((ownType eq selType) || ownType.isError) result
988989
else tryWithClassTag(Typed(result, TypeTree(ownType)), selType)

tests/pos/inline-i1773.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
object Test {
2+
implicit class Foo(sc: StringContext) {
3+
object q {
4+
inline def unapply(arg: Any): Option[(Any, Any)] =
5+
Some((sc.parts(0), sc.parts(1)))
6+
}
7+
}
8+
9+
def main(args: Array[String]): Unit = {
10+
val q"class $name extends $parent" = new Object
11+
println(name)
12+
println(parent)
13+
}
14+
}

tests/pos/inline-t9232a.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
final class Foo(val value: Int)
2+
3+
object Foo {
4+
inline def unapply(foo: Foo): Some[Int] = Some(foo.value)
5+
}
6+
7+
object Test {
8+
def transformTree(f: Foo): Any = f match {
9+
case Foo(_) => ???
10+
}
11+
}

tests/pos/inline-t9232b.scala

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
final class Foo(val value: Int)
2+
3+
object Foo {
4+
inline def unapplySeq(foo: Foo): Some[Seq[Int]] = Some(List(foo.value))
5+
}
6+
7+
sealed trait Tree
8+
case class Node1(foo: Foo) extends Tree
9+
case class Node2() extends Tree
10+
11+
object Test {
12+
def transformTree(tree: Tree): Any = tree match {
13+
case Node1(Foo(_: _*)) => ???
14+
}
15+
16+
def transformTree2(tree: Tree): Any = tree match {
17+
case Node1(Foo(1, _: _*)) => ???
18+
}
19+
20+
def transformTree3(tree: Tree): Any = tree match {
21+
case Node1(Foo(x, _: _*)) => ???
22+
}
23+
}

0 commit comments

Comments
 (0)