Skip to content

Commit

Permalink
Check is inline unapply has leading implicits
Browse files Browse the repository at this point in the history
We patch the crash in the compiler but do not support this feature yet.

To support it see #13158

Fixes #12991
  • Loading branch information
nicolasstucki committed Jul 4, 2022
1 parent 7d6d42f commit 776e4d6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
12 changes: 10 additions & 2 deletions compiler/src/dotty/tools/dotc/inlines/Inlines.scala
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,15 @@ object Inlines:
// transforms the patterns into terms, the `inlinePatterns` phase removes this anonymous class by β-reducing
// the call to the `unapply`.

val UnApply(fun, implicits, patterns) = unapp
object SplitFunAndGivenArgs:
def unapply(tree: Tree): (Tree, List[List[Tree]]) = tree match
case Apply(SplitFunAndGivenArgs(fn, argss), args) => (fn, argss :+ args)
case _ => (tree, Nil)
val UnApply(SplitFunAndGivenArgs(fun, leadingImplicits), trailingImplicits, patterns) = unapp
if leadingImplicits.flatten.nonEmpty then
// To support them see https://github.com/lampepfl/dotty/pull/13158
report.error("inline unapply methods with given parameters before the scrutinee are not supported", fun)

val sym = unapp.symbol
val cls = newNormalizedClassSymbol(ctx.owner, tpnme.ANON_CLASS, Synthetic | Final, List(defn.ObjectType), coord = sym.coord)
val constr = newConstructor(cls, Synthetic, Nil, Nil, coord = sym.coord).entered
Expand All @@ -204,7 +212,7 @@ object Inlines:
val cdef = ClassDef(cls, DefDef(constr), List(unapply))
val newUnapply = Block(cdef :: Nil, New(cls.typeRef, Nil))
val newFun = newUnapply.select(unappplySym).withSpan(unapp.span)
cpy.UnApply(unapp)(newFun, implicits, patterns)
cpy.UnApply(unapp)(newFun, trailingImplicits, patterns)
end inlinedUnapply

/** For a retained inline method, another method that keeps track of
Expand Down
7 changes: 7 additions & 0 deletions tests/neg/i12991.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
object Foo:
inline def unapply(using String)(i: Int): Some[Int] = Some(i)

given String = ""

val i = 10 match
case Foo(x) => x // error

0 comments on commit 776e4d6

Please sign in to comment.