Skip to content

Commit a25960e

Browse files
committed
Make compile-time operations on stable arguments stable
1 parent 12a10f2 commit a25960e

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ object Types {
165165
case _: SingletonType | NoPrefix => true
166166
case tp: RefinedOrRecType => tp.parent.isStable
167167
case tp: ExprType => tp.resultType.isStable
168+
case AppliedType(tycon: TypeRef, args: List[Type])
169+
if defn.isCompiletimeAppliedType(tycon.symbol) && args.forall(_.isStable) => true
168170
case tp: AnnotatedType =>
169171
// NOTE UncheckedStableAnnot was originally meant to be put on fields,
170172
// not on types. Allowing it on types is a Scala 3 extension. See:
@@ -4242,7 +4244,7 @@ object Types {
42424244
// final val one = 1
42434245
// type Two = one.type + one.type
42444246
// ```
4245-
case tp: TermRef => tp.underlying
4247+
case tp: TypeProxy if tp.underlying.isStable => tp.underlying.fixForEvaluation
42464248
case tp => tp
42474249
}
42484250

tests/neg/singleton-ops-int.scala

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,24 @@ object Test {
116116

117117
val t83: ToDouble[1] = 1.0
118118
val t84: ToDouble[2] = 2 // error
119+
120+
// Singletons are dereferenced
121+
val t85: Int = 5
122+
val t86: t85.type = t85
123+
summon[t85.type + t85.type =:= t86.type + t86.type]
124+
125+
// Singletons are dereferenced recursively
126+
val t87: t86.type = t87
127+
summon[t85.type + t85.type =:= t87.type + t87.type]
128+
129+
// Skolems of compile-time types are dereferenced:
130+
// (?1 : (Test.x : Int) * (Test.x : Int)) --> (Test.x : Int) * (Test.x : Int)
131+
def mult(x: Int, y: Int): x.type * y.type = (x * y).asInstanceOf
132+
val t88: t85.type * t85.type * t85.type = mult(mult(t85, t85), t85)
133+
134+
// Compile-time operations with singleton arguments are singletons
135+
summon[t85.type + t86.type <:< Singleton]
136+
137+
// Compile-time operations with non-singleton arguments are not singletons
138+
summon[t85.type + Int <:< Singleton] // error
119139
}

0 commit comments

Comments
 (0)