@@ -280,11 +280,25 @@ trait SpaceLogic {
280
280
}
281
281
}
282
282
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
+
283
297
/** Scala implementation of space logic */
284
298
class SpaceEngine (implicit ctx : Context ) extends SpaceLogic {
285
299
import tpd ._
300
+ import SpaceEngine ._
286
301
287
- private val scalaSomeClass = ctx.requiredClass(" scala.Some" )
288
302
private val scalaSeqFactoryClass = ctx.requiredClass(" scala.collection.generic.SeqFactory" )
289
303
private val scalaListType = ctx.requiredClassRef(" scala.collection.immutable.List" )
290
304
private val scalaNilType = ctx.requiredModuleRef(" scala.collection.immutable.Nil" )
@@ -309,15 +323,6 @@ class SpaceEngine(implicit ctx: Context) extends SpaceLogic {
309
323
else Typ (AndType (tp1, tp2), true )
310
324
}
311
325
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
-
321
326
/** Return the space that represents the pattern `pat` */
322
327
def project (pat : Tree ): Space = pat match {
323
328
case Literal (c) =>
@@ -340,12 +345,12 @@ class SpaceEngine(implicit ctx: Context) extends SpaceLogic {
340
345
else {
341
346
val (arity, elemTp, resultTp) = unapplySeqInfo(fun.tpe.widen.finalResultType, fun.sourcePos)
342
347
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))
344
349
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))
346
351
}
347
352
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))
349
354
case Typed (pat @ UnApply (_, _, _), _) => project(pat)
350
355
case Typed (expr, tpt) =>
351
356
Typ (erase(expr.tpe.stripAnnots), true )
0 commit comments