@@ -280,11 +280,25 @@ trait SpaceLogic {
280280 }
281281}
282282
283+ object SpaceEngine {
284+
285+ /** Is the unapply irrefutable?
286+ * @param unapp The unapply function reference
287+ */
288+ def isIrrefutableUnapply (unapp : tpd.Tree )(implicit ctx : Context ): Boolean = {
289+ val unappResult = unapp.tpe.widen.finalResultType
290+ unappResult.isRef(defn.SomeClass ) ||
291+ unappResult =:= ConstantType (Constant (true )) ||
292+ (unapp.symbol.is(Synthetic ) && unapp.symbol.owner.linkedClass.is(Case )) ||
293+ productArity(unappResult) > 0
294+ }
295+ }
296+
283297/** Scala implementation of space logic */
284298class SpaceEngine (implicit ctx : Context ) extends SpaceLogic {
285299 import tpd ._
300+ import SpaceEngine ._
286301
287- private val scalaSomeClass = ctx.requiredClass(" scala.Some" )
288302 private val scalaSeqFactoryClass = ctx.requiredClass(" scala.collection.generic.SeqFactory" )
289303 private val scalaListType = ctx.requiredClassRef(" scala.collection.immutable.List" )
290304 private val scalaNilType = ctx.requiredModuleRef(" scala.collection.immutable.Nil" )
@@ -309,15 +323,6 @@ class SpaceEngine(implicit ctx: Context) extends SpaceLogic {
309323 else Typ (AndType (tp1, tp2), true )
310324 }
311325
312- /** Whether the extractor is irrefutable */
313- def irrefutable (unapp : Tree ): Boolean = {
314- // TODO: optionless patmat
315- unapp.tpe.widen.finalResultType.isRef(scalaSomeClass) ||
316- unapp.tpe.widen.finalResultType =:= ConstantType (Constant (true )) ||
317- (unapp.symbol.is(Synthetic ) && unapp.symbol.owner.linkedClass.is(Case )) ||
318- productArity(unapp.tpe.widen.finalResultType) > 0
319- }
320-
321326 /** Return the space that represents the pattern `pat` */
322327 def project (pat : Tree ): Space = pat match {
323328 case Literal (c) =>
@@ -340,12 +345,12 @@ class SpaceEngine(implicit ctx: Context) extends SpaceLogic {
340345 else {
341346 val (arity, elemTp, resultTp) = unapplySeqInfo(fun.tpe.widen.finalResultType, fun.sourcePos)
342347 if (elemTp.exists)
343- Prod (erase(pat.tpe.stripAnnots), fun.tpe, fun.symbol, projectSeq(pats) :: Nil , irrefutable (fun))
348+ Prod (erase(pat.tpe.stripAnnots), fun.tpe, fun.symbol, projectSeq(pats) :: Nil , isIrrefutableUnapply (fun))
344349 else
345- Prod (erase(pat.tpe.stripAnnots), fun.tpe, fun.symbol, pats.take(arity - 1 ).map(project) :+ projectSeq(pats.drop(arity - 1 )), irrefutable (fun))
350+ Prod (erase(pat.tpe.stripAnnots), fun.tpe, fun.symbol, pats.take(arity - 1 ).map(project) :+ projectSeq(pats.drop(arity - 1 )),isIrrefutableUnapply (fun))
346351 }
347352 else
348- Prod (erase(pat.tpe.stripAnnots), fun.tpe, fun.symbol, pats.map(project), irrefutable (fun))
353+ Prod (erase(pat.tpe.stripAnnots), fun.tpe, fun.symbol, pats.map(project), isIrrefutableUnapply (fun))
349354 case Typed (pat @ UnApply (_, _, _), _) => project(pat)
350355 case Typed (expr, tpt) =>
351356 Typ (erase(expr.tpe.stripAnnots), true )
0 commit comments