@@ -3,9 +3,7 @@ package dotc
3
3
package transform
4
4
package patmat
5
5
6
- import core .*
7
- import Constants .* , Contexts .* , Decorators .* , Flags .* , NullOpsDecorator .* , Symbols .* , Types .*
8
- import Names .* , NameOps .* , StdNames .*
6
+ import core .* , Constants .* , Contexts .* , Decorators .* , Flags .* , Names .* , NameOps .* , StdNames .* , Symbols .* , Types .*
9
7
import ast .* , tpd .*
10
8
import config .Printers .*
11
9
import printing .{ Printer , * }, Texts .*
@@ -361,7 +359,7 @@ object SpaceEngine {
361
359
val funRef = fun1.tpe.asInstanceOf [TermRef ]
362
360
if (fun.symbol.name == nme.unapplySeq)
363
361
val (arity, elemTp, resultTp) = unapplySeqInfo(fun.tpe.widen.finalResultType, fun.srcPos)
364
- if fun.symbol.owner == defn.SeqFactoryClass && pat.tpe.hasClassSymbol(defn. ListClass ) then
362
+ if ( fun.symbol.owner == defn.SeqFactoryClass && defn. ListType .appliedTo(elemTp) <:< pat.tpe)
365
363
// The exhaustivity and reachability logic already handles decomposing sum types (into its subclasses)
366
364
// and product types (into its components). To get better counter-examples for patterns that are of type
367
365
// List (or a super-type of list, like LinearSeq) we project them into spaces that use `::` and Nil.
@@ -533,26 +531,14 @@ object SpaceEngine {
533
531
val mt : MethodType = unapp.widen match {
534
532
case mt : MethodType => mt
535
533
case pt : PolyType =>
536
- val locked = ctx.typerState.ownedVars
537
534
val tvars = constrained(pt)
538
535
val mt = pt.instantiate(tvars).asInstanceOf [MethodType ]
539
536
scrutineeTp <:< mt.paramInfos(0 )
540
537
// force type inference to infer a narrower type: could be singleton
541
538
// see tests/patmat/i4227.scala
542
539
mt.paramInfos(0 ) <:< scrutineeTp
543
- maximizeType(mt.paramInfos(0 ), Spans .NoSpan )
544
- if ! (ctx.typerState.ownedVars -- locked).isEmpty then
545
- // constraining can create type vars out of wildcard types
546
- // (in legalBound, by using a LevelAvoidMap)
547
- // maximise will only do one pass at maximising the type vars in the target type
548
- // which means we can maximise to types that include other type vars
549
- // this fails TreeChecker's "non-empty constraint at end of $fusedPhase" check
550
- // e.g. run-macros/string-context-implicits
551
- // I can't prove that a second call won't also create type vars,
552
- // but I'd rather have an unassigned new-new type var, than an infinite loop.
553
- // After all, there's nothing strictly "wrong" with unassigned type vars,
554
- // it just fails TreeChecker's linting.
555
- maximizeType(mt.paramInfos(0 ), Spans .NoSpan )
540
+ instantiateSelected(mt, tvars)
541
+ isFullyDefined(mt, ForceDegree .all)
556
542
mt
557
543
}
558
544
@@ -566,7 +552,7 @@ object SpaceEngine {
566
552
// Case unapplySeq:
567
553
// 1. return the type `List[T]` where `T` is the element type of the unapplySeq return type `Seq[T]`
568
554
569
- val resTp = wildApprox( ctx.typeAssigner.safeSubstMethodParams(mt, scrutineeTp :: Nil ).finalResultType)
555
+ val resTp = ctx.typeAssigner.safeSubstMethodParams(mt, scrutineeTp :: Nil ).finalResultType
570
556
571
557
val sig =
572
558
if (resTp.isRef(defn.BooleanClass ))
@@ -587,14 +573,14 @@ object SpaceEngine {
587
573
if (arity > 0 )
588
574
productSelectorTypes(resTp, unappSym.srcPos)
589
575
else {
590
- val getTp = extractorMemberType( resTp, nme.get, unappSym.srcPos)
576
+ val getTp = resTp.select( nme.get).finalResultType.widenTermRefExpr
591
577
if (argLen == 1 ) getTp :: Nil
592
578
else productSelectorTypes(getTp, unappSym.srcPos)
593
579
}
594
580
}
595
581
}
596
582
597
- sig.map { case tp : WildcardType => tp.bounds.hi case tp => tp }
583
+ sig.map(_.annotatedToRepeated)
598
584
}
599
585
600
586
/** Whether the extractor covers the given type */
@@ -632,36 +618,14 @@ object SpaceEngine {
632
618
case tp if tp.classSymbol.isAllOf(JavaEnumTrait ) => tp.classSymbol.children.map(_.termRef)
633
619
// the class of a java enum value is the enum class, so this must follow SingletonType to not loop infinitely
634
620
635
- case Childless ( tp @ AppliedType (Parts (parts), targs)) =>
621
+ case tp @ AppliedType (Parts (parts), targs) if tp.classSymbol.children.isEmpty =>
636
622
// It might not obvious that it's OK to apply the type arguments of a parent type to child types.
637
623
// But this is guarded by `tp.classSymbol.children.isEmpty`,
638
624
// meaning we'll decompose to the same class, just not the same type.
639
625
// For instance, from i15029, `decompose((X | Y).Field[T]) = [X.Field[T], Y.Field[T]]`.
640
626
parts.map(tp.derivedAppliedType(_, targs))
641
627
642
- case tpOriginal if tpOriginal.isDecomposableToChildren =>
643
- // isDecomposableToChildren uses .classSymbol.is(Sealed)
644
- // But that classSymbol could be from an AppliedType
645
- // where the type constructor is a non-class type
646
- // E.g. t11620 where `?1.AA[X]` returns as "sealed"
647
- // but using that we're not going to infer A1[X] and A2[X]
648
- // but end up with A1[<?>] and A2[<?>].
649
- // So we widen (like AppliedType superType does) away
650
- // non-class type constructors.
651
- //
652
- // Can't use `tpOriginal.baseType(cls)` because it causes
653
- // i15893 to return exhaustivity warnings, because instead of:
654
- // <== refineUsingParent(N, class Succ, []) = Succ[<? <: NatT>]
655
- // <== isSub(Succ[<? <: NatT>] <:< Succ[Succ[<?>]]) = true
656
- // we get
657
- // <== refineUsingParent(NatT, class Succ, []) = Succ[NatT]
658
- // <== isSub(Succ[NatT] <:< Succ[Succ[<?>]]) = false
659
- def getAppliedClass (tp : Type ): Type = tp match
660
- case tp @ AppliedType (_ : HKTypeLambda , _) => tp
661
- case tp @ AppliedType (tycon : TypeRef , _) if tycon.symbol.isClass => tp
662
- case tp @ AppliedType (tycon : TypeProxy , _) => getAppliedClass(tycon.superType.applyIfParameterized(tp.args))
663
- case tp => tp
664
- val tp = getAppliedClass(tpOriginal)
628
+ case tp if tp.isDecomposableToChildren =>
665
629
def getChildren (sym : Symbol ): List [Symbol ] =
666
630
sym.children.flatMap { child =>
667
631
if child eq sym then List (sym) // i3145: sealed trait Baz, val x = new Baz {}, Baz.children returns Baz...
@@ -714,12 +678,6 @@ object SpaceEngine {
714
678
final class PartsExtractor (val get : List [Type ]) extends AnyVal :
715
679
def isEmpty : Boolean = get == ListOfNoType
716
680
717
- object Childless :
718
- def unapply (tp : Type )(using Context ): Result =
719
- Result (if tp.classSymbol.children.isEmpty then tp else NoType )
720
- class Result (val get : Type ) extends AnyVal :
721
- def isEmpty : Boolean = ! get.exists
722
-
723
681
/** Show friendly type name with current scope in mind
724
682
*
725
683
* E.g. C.this.B --> B if current owner is C
@@ -816,15 +774,12 @@ object SpaceEngine {
816
774
doShow(s)
817
775
}
818
776
819
- extension (self : Type ) private def stripUnsafeNulls ()(using Context ): Type =
820
- if Nullables .unsafeNullsEnabled then self.stripNull else self
821
-
822
- private def exhaustivityCheckable (sel : Tree )(using Context ): Boolean = trace(i " exhaustivityCheckable( $sel ${sel.className}) " ) {
777
+ private def exhaustivityCheckable (sel : Tree )(using Context ): Boolean = {
823
778
val seen = collection.mutable.Set .empty[Symbol ]
824
779
825
780
// Possible to check everything, but be compatible with scalac by default
826
- def isCheckable (tp : Type ): Boolean = trace( i " isCheckable( $tp ${tp.className} ) " ) :
827
- val tpw = tp.widen.dealias.stripUnsafeNulls()
781
+ def isCheckable (tp : Type ): Boolean =
782
+ val tpw = tp.widen.dealias
828
783
val classSym = tpw.classSymbol
829
784
classSym.is(Sealed ) && ! tpw.isLargeGenericTuple || // exclude large generic tuples from exhaustivity
830
785
// requires an unknown number of changes to make work
@@ -859,7 +814,7 @@ object SpaceEngine {
859
814
/** Return the underlying type of non-module, non-constant, non-enum case singleton types.
860
815
* Also widen ExprType to its result type, and rewrap any annotation wrappers.
861
816
* For example, with `val opt = None`, widen `opt.type` to `None.type`. */
862
- def toUnderlying (tp : Type )(using Context ): Type = trace(i " toUnderlying( $tp ${tp.className} ) " )(tp match {
817
+ def toUnderlying (tp : Type )(using Context ): Type = trace(i " toUnderlying( $tp) " )(tp match {
863
818
case _ : ConstantType => tp
864
819
case tp : TermRef if tp.symbol.is(Module ) => tp
865
820
case tp : TermRef if tp.symbol.isAllOf(EnumCase ) => tp
@@ -870,7 +825,7 @@ object SpaceEngine {
870
825
})
871
826
872
827
def checkExhaustivity (m : Match )(using Context ): Unit = trace(i " checkExhaustivity( $m) " ) {
873
- val selTyp = toUnderlying(m.selector.tpe.stripUnsafeNulls() ).dealias
828
+ val selTyp = toUnderlying(m.selector.tpe).dealias
874
829
val targetSpace = trace(i " targetSpace( $selTyp) " )(project(selTyp))
875
830
876
831
val patternSpace = Or (m.cases.foldLeft(List .empty[Space ]) { (acc, x) =>
@@ -948,6 +903,9 @@ object SpaceEngine {
948
903
}
949
904
950
905
def checkMatch (m : Match )(using Context ): Unit =
951
- if exhaustivityCheckable(m.selector) then checkExhaustivity (m)
906
+ checkMatchExhaustivityOnly (m)
952
907
if reachabilityCheckable(m.selector) then checkReachability(m)
908
+
909
+ def checkMatchExhaustivityOnly (m : Match )(using Context ): Unit =
910
+ if exhaustivityCheckable(m.selector) then checkExhaustivity(m)
953
911
}
0 commit comments