Skip to content

Commit 0b697d5

Browse files
authored
Merge pull request #11527 from dotty-staging/fix-11350
Don't synthesize context functions with embedded wildcards
2 parents 5953af1 + 6285661 commit 0b697d5

File tree

7 files changed

+36
-17
lines changed

7 files changed

+36
-17
lines changed

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,12 +1355,10 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
13551355
* for equality would give the wrong result, so we should not use the sets
13561356
* for comparisons.
13571357
*/
1358-
def canCompare(ts: Set[Type]) = ctx.phase.isTyper || {
1359-
val hasSkolems = new ExistsAccumulator(_.isInstanceOf[SkolemType]) {
1360-
override def stopAtStatic = true
1361-
}
1362-
!ts.exists(hasSkolems(false, _))
1363-
}
1358+
def canCompare(ts: Set[Type]) =
1359+
ctx.phase.isTyper
1360+
|| !ts.exists(_.existsPart(_.isInstanceOf[SkolemType], stopAtStatic = true))
1361+
13641362
def verified(result: Boolean): Boolean =
13651363
if Config.checkAtomsComparisons then
13661364
try

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -422,8 +422,8 @@ object Types {
422422

423423
/** Returns true if there is a part of this type that satisfies predicate `p`.
424424
*/
425-
final def existsPart(p: Type => Boolean, forceLazy: Boolean = true)(using Context): Boolean =
426-
new ExistsAccumulator(p, forceLazy).apply(false, this)
425+
final def existsPart(p: Type => Boolean, stopAtStatic: Boolean = false, forceLazy: Boolean = true)(using Context): Boolean =
426+
new ExistsAccumulator(p, stopAtStatic, forceLazy).apply(false, this)
427427

428428
/** Returns true if all parts of this type satisfy predicate `p`.
429429
*/
@@ -5686,11 +5686,12 @@ object Types {
56865686
protected def traverseChildren(tp: Type): Unit = foldOver((), tp)
56875687
}
56885688

5689-
class ExistsAccumulator(p: Type => Boolean, forceLazy: Boolean = true)(using Context) extends TypeAccumulator[Boolean] {
5690-
override def stopAtStatic: Boolean = false
5689+
class ExistsAccumulator(
5690+
p: Type => Boolean,
5691+
override val stopAtStatic: Boolean,
5692+
forceLazy: Boolean)(using Context) extends TypeAccumulator[Boolean]:
56915693
def apply(x: Boolean, tp: Type): Boolean =
56925694
x || p(tp) || (forceLazy || !tp.isInstanceOf[LazyRef]) && foldOver(x, tp)
5693-
}
56945695

56955696
class ForeachAccumulator(p: Type => Unit, override val stopAtStatic: Boolean)(using Context) extends TypeAccumulator[Unit] {
56965697
def apply(x: Unit, tp: Type): Unit = foldOver(p(tp), tp)

compiler/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -669,12 +669,11 @@ class Scala2Unpickler(bytes: Array[Byte], classRoot: ClassDenotation, moduleClas
669669
}
670670
// Cannot use standard `existsPart` method because it calls `lookupRefined`
671671
// which can cause CyclicReference errors.
672-
val isBoundAccumulator = new ExistsAccumulator(isBound) {
673-
override def foldOver(x: Boolean, tp: Type): Boolean = tp match {
672+
val isBoundAccumulator = new ExistsAccumulator(isBound, stopAtStatic = true, forceLazy = true):
673+
override def foldOver(x: Boolean, tp: Type): Boolean = tp match
674674
case tp: TypeRef => applyToPrefix(x, tp)
675675
case _ => super.foldOver(x, tp)
676-
}
677-
}
676+
678677
def removeSingleton(tp: Type): Type =
679678
if (tp isRef defn.SingletonClass) defn.AnyType else tp
680679
def elim(tp: Type): Type = tp match {

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,7 +1217,7 @@ class Typer extends Namer
12171217
* @post: If result exists, `paramIndex` is defined for the name of
12181218
* every parameter in `params`.
12191219
*/
1220-
lazy val calleeType: Type = untpd.stripAnnotated(fnBody) match {
1220+
lazy val calleeType: Type = untpd.stripAnnotated(untpd.unsplice(fnBody)) match {
12211221
case ident: untpd.Ident if isContextual =>
12221222
val ident1 = typedIdent(ident, WildcardType)
12231223
val tp = ident1.tpe.widen
@@ -2714,7 +2714,7 @@ class Typer extends Namer
27142714
// see tests/pos/i7778b.scala
27152715

27162716
val paramTypes = {
2717-
val hasWildcard = formals.exists(_.isInstanceOf[WildcardType])
2717+
val hasWildcard = formals.exists(_.existsPart(_.isInstanceOf[WildcardType], stopAtStatic = true))
27182718
if hasWildcard then formals.map(_ => untpd.TypeTree())
27192719
else formals.map(untpd.TypeTree)
27202720
}

tests/neg/i11350.check

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
-- [E081] Type Error: tests/neg/i11350.scala:1:39 ----------------------------------------------------------------------
2+
1 |class A1[T](action: A1[T] ?=> String = "") // error
3+
| ^
4+
| Missing parameter type
5+
|
6+
| I could not infer the type of the parameter evidence$1.
7+
| What I could infer was: A1[<?>]
8+
-- [E081] Type Error: tests/neg/i11350.scala:2:39 ----------------------------------------------------------------------
9+
2 |class A2[T](action: A1[T] ?=> String = summon[A1[T]]) // error
10+
| ^
11+
| Missing parameter type
12+
|
13+
| I could not infer the type of the parameter evidence$2.
14+
| What I could infer was: A1[<?>]

tests/neg/i11350.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class A1[T](action: A1[T] ?=> String = "") // error
2+
class A2[T](action: A1[T] ?=> String = summon[A1[T]]) // error

tests/pos/i11350.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
case class A[T](action: A[T] ?=> String) // error
2+
3+
class A1[T](action: A1[T] ?=> String = (_: A1[T]) ?=> "") // works
4+
case class A2[T](action: A2[?] ?=> String) // works
5+
case class A3[T](action: A3[T] => String) // works as well

0 commit comments

Comments
 (0)