Skip to content

Commit 92bbb6e

Browse files
authored
Merge pull request #5485 from dotty-staging/fix-#5481
Fix #5481: Follow opaque aliases in prototypes of functions
2 parents 0b90430 + 9e4d433 commit 92bbb6e

File tree

4 files changed

+54
-4
lines changed

4 files changed

+54
-4
lines changed

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

+12
Original file line numberDiff line numberDiff line change
@@ -1083,6 +1083,18 @@ object Types {
10831083
/** Like `dealiasKeepAnnots`, but keeps only refining annotations */
10841084
final def dealiasKeepRefiningAnnots(implicit ctx: Context): Type = dealias1(keepIfRefining)
10851085

1086+
/** If this is a synthetic opaque type seen from inside the opaque companion object,
1087+
* its opaque alias, otherwise the type itself.
1088+
*/
1089+
final def followSyntheticOpaque(implicit ctx: Context): Type = this match {
1090+
case tp: TypeProxy if tp.typeSymbol.is(SyntheticOpaque) =>
1091+
tp.superType match {
1092+
case AndType(alias, _) => alias // in this case we are inside the companion object
1093+
case _ => this
1094+
}
1095+
case _ => this
1096+
}
1097+
10861098
/** The result of normalization using `tryNormalize`, or the type itself if
10871099
* tryNormlize yields NoType
10881100
*/

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

+3-4
Original file line numberDiff line numberDiff line change
@@ -730,13 +730,12 @@ class Typer extends Namer
730730
case _: WildcardType => untpd.TypeTree()
731731
case _ => untpd.TypeTree(tp)
732732
}
733-
pt.stripTypeVar match {
734-
case _ if defn.isNonDepFunctionType(pt) =>
733+
pt.stripTypeVar.dealias.followSyntheticOpaque match {
734+
case pt1 if defn.isNonDepFunctionType(pt1) =>
735735
// if expected parameter type(s) are wildcards, approximate from below.
736736
// if expected result type is a wildcard, approximate from above.
737737
// this can type the greatest set of admissible closures.
738-
val funType = pt.dealias
739-
(funType.argTypesLo.init, typeTree(funType.argTypesHi.last))
738+
(pt1.argTypesLo.init, typeTree(pt1.argTypesHi.last))
740739
case SAMType(sam @ MethodTpe(_, formals, restpe)) =>
741740
(formals,
742741
if (sam.isResultDependent)

tests/neg/i5481.scala

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
object TypeAlias {
2+
3+
type Set[A] = A => Boolean
4+
5+
object Set {
6+
def singleton[A](a: A): Set[A] = _ == a
7+
}
8+
}
9+
10+
object OpaqueType {
11+
12+
opaque type Set[A] = A => Boolean
13+
14+
object Set {
15+
def singleton[A](a: A): Set[A] = _ == a
16+
def singleton0[A](a: A): Set[A] = (_: A) == a
17+
}
18+
19+
def singleton[A](a: A): Set[A] = _ == a // error: missing parameter type
20+
}

tests/pos/i5481.scala

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
object TypeAlias {
2+
3+
type Set[A] = A => Boolean
4+
5+
object Set {
6+
def singleton[A](a: A): Set[A] = _ == a
7+
}
8+
}
9+
10+
object OpaqueType {
11+
12+
opaque type Set[A] = A => Boolean
13+
14+
object Set {
15+
def singleton[A](a: A): Set[A] = _ == a
16+
def singleton0[A](a: A): Set[A] = (_: A) == a
17+
}
18+
19+
}

0 commit comments

Comments
 (0)