Skip to content

Commit 5df723f

Browse files
committed
Do consider underlying types of some singleton types
Do consider underlying types of singleton types that are not path prefixes. This is necessary since arguments in apply prototypes are often singeletons, and in this case we want to include the implicit scope of their underlying type.
1 parent f72b800 commit 5df723f

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

compiler/src/dotty/tools/dotc/core/Types.scala

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5329,7 +5329,7 @@ object Types {
53295329

53305330
protected def applyToAnnot(x: T, annot: Annotation): T = x // don't go into annotations
53315331

5332-
protected final def applyToPrefix(x: T, tp: NamedType): T =
5332+
protected def applyToPrefix(x: T, tp: NamedType): T =
53335333
atVariance(variance max 0)(this(x, tp.prefix)) // see remark on NamedType case in TypeMap
53345334

53355335
def foldOver(x: T, tp: Type): T = {
@@ -5452,15 +5452,29 @@ object Types {
54525452
}
54535453

54545454
class NamedPartsAccumulator(p: NamedType => Boolean,
5455-
widenSingletons: Boolean = false,
5455+
widenSingletons: Boolean = false, // if set, also consider underlying types in singleton path prefixes
54565456
excludeLowerBounds: Boolean = false)
54575457
(implicit ctx: Context) extends TypeAccumulator[mutable.Set[NamedType]] {
5458+
54585459
override def stopAtStatic: Boolean = false
5459-
def maybeAdd(x: mutable.Set[NamedType], tp: NamedType): mutable.Set[NamedType] = if (p(tp)) x += tp else x
5460+
5461+
def maybeAdd(x: mutable.Set[NamedType], tp: NamedType): mutable.Set[NamedType] =
5462+
if (p(tp)) x += tp else x
5463+
54605464
val seen: util.HashSet[Type] = new util.HashSet[Type](64) {
54615465
override def hash(x: Type): Int = System.identityHashCode(x)
54625466
override def isEqual(x: Type, y: Type) = x.eq(y)
54635467
}
5468+
5469+
override def applyToPrefix(x: mutable.Set[NamedType], tp: NamedType): mutable.Set[NamedType] =
5470+
tp.prefix match
5471+
case pre: TermRef if !widenSingletons =>
5472+
foldOver(maybeAdd(x, pre), pre)
5473+
case pre: ThisType if !widenSingletons =>
5474+
x
5475+
case _ =>
5476+
super.applyToPrefix(x, tp)
5477+
54645478
def apply(x: mutable.Set[NamedType], tp: Type): mutable.Set[NamedType] =
54655479
if (seen contains tp) x
54665480
else {
@@ -5469,10 +5483,9 @@ object Types {
54695483
case tp: TypeRef =>
54705484
foldOver(maybeAdd(x, tp), tp)
54715485
case tp: TermRef =>
5472-
val x1 = foldOver(maybeAdd(x, tp), tp)
5473-
if widenSingletons then apply(x1, tp.underlying) else x1
5486+
apply(foldOver(maybeAdd(x, tp), tp), tp.underlying)
54745487
case tp: ThisType =>
5475-
if widenSingletons then apply(x, tp.tref) else x
5488+
apply(x, tp.tref)
54765489
case NoPrefix =>
54775490
foldOver(x, tp)
54785491
case tp: AppliedType =>

tests/pos/i9103.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@ object a:
44

55
val b = a
66

7-
def Test = summon[b.Foo[Unit]]
7+
def Test = summon[b.Foo[Unit]]
8+
9+
val n: Long = 1
10+
val total: BigInt = 2
11+
val remainder: BigInt = n % identity(total)

0 commit comments

Comments
 (0)