@@ -4,7 +4,7 @@ package inlines
44
55import ast .* , core .*
66import Flags .* , Symbols .* , Types .* , Decorators .* , Constants .* , Contexts .*
7- import StdNames .tpnme
7+ import StdNames .{ tpnme , nme }
88import transform .SymUtils ._
99import typer .*
1010import NameKinds .BodyRetainerName
@@ -189,28 +189,33 @@ object Inlines:
189189 // transforms the patterns into terms, the `inlinePatterns` phase removes this anonymous class by β-reducing
190190 // the call to the `unapply`.
191191
192- val UnApply (fun, trailingImplicits, patterns) = unapp
193-
194- val sym = unapp.symbol
195-
196- var unapplySym1 : Symbol = NoSymbol // created from within AnonClass() and used afterwards
192+ val fun = unapp.fun
193+ val sym = fun.symbol
197194
198195 val newUnapply = AnonClass (ctx.owner, List (defn.ObjectType ), sym.coord) { cls =>
199196 // `fun` is a partially applied method that contains all type applications of the method.
200197 // The methodic type `fun.tpe.widen` is the type of the function starting from the scrutinee argument
201198 // and its type parameters are instantiated.
202- val unapplySym = newSymbol(cls, sym.name.toTermName, Synthetic | Method , fun.tpe.widen, coord = sym.coord).entered
203- val unapply = DefDef (unapplySym.asTerm, argss =>
204- val body = fun.appliedToArgss(argss).withSpan(unapp.span)
205- if body.symbol.is(Transparent ) then inlineCall(body)(using ctx.withOwner(unapplySym))
206- else body
207- )
208- unapplySym1 = unapplySym
209- List (unapply)
199+ val unapplyInfo = fun.tpe.widen
200+ val unapplySym = newSymbol(cls, sym.name.toTermName, Synthetic | Method , unapplyInfo, coord = sym.coord).entered
201+
202+ val unapply = DefDef (unapplySym.asTerm, argss => fun.appliedToArgss(argss).withSpan(unapp.span))
203+
204+ if sym.is(Transparent ) then
205+ // Inline the body and refine the type of the unapply method
206+ val inlinedBody = inlineCall(unapply.rhs)(using ctx.withOwner(unapplySym))
207+ val refinedResultType = inlinedBody.tpe.widen
208+ def refinedResult (info : Type ): Type = info match
209+ case info : LambdaType => info.newLikeThis(info.paramNames, info.paramInfos, refinedResult(info.resultType))
210+ case _ => refinedResultType
211+ unapplySym.info = refinedResult(unapplyInfo)
212+ List (cpy.DefDef (unapply)(tpt = TypeTree (refinedResultType), rhs = inlinedBody))
213+ else
214+ List (unapply)
210215 }
211216
212- val newFun = newUnapply.select(unapplySym1 ).withSpan(unapp.span)
213- cpy.UnApply (unapp)(newFun, trailingImplicits, patterns )
217+ val newFun = newUnapply.select(sym.name ).withSpan(unapp.span)
218+ cpy.UnApply (unapp)(fun = newFun )
214219 end inlinedUnapply
215220
216221 /** For a retained inline method, another method that keeps track of
0 commit comments