Skip to content

Commit 0d1bc7a

Browse files
committed
Avoid overwriting type var instance in instantiateWith
This is a trial as a first step for other refactorings down the line.
1 parent d373b40 commit 0d1bc7a

File tree

3 files changed

+37
-16
lines changed

3 files changed

+37
-16
lines changed

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

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4910,20 +4910,21 @@ object Types extends TypeUtils {
49104910
def isInstantiated(using Context): Boolean = instanceOpt.exists
49114911

49124912
/** Instantiate variable with given type */
4913-
def instantiateWith(tp: Type)(using Context): Type = {
4914-
assert(tp ne this, i"self instantiation of $origin, constraint = ${ctx.typerState.constraint}")
4915-
assert(!myInst.exists, i"$origin is already instantiated to $myInst but we attempted to instantiate it to $tp")
4916-
typr.println(i"instantiating $this with $tp")
4913+
def instantiateWith(tp: Type)(using Context): Type =
4914+
if myInst.exists then myInst
4915+
else
4916+
assert(tp ne this, i"self instantiation of $origin, constraint = ${ctx.typerState.constraint}")
4917+
assert(!myInst.exists, i"$origin is already instantiated to $myInst but we attempted to instantiate it to $tp")
4918+
typr.println(i"instantiating $this with $tp")
49174919

4918-
if Config.checkConstraintsSatisfiable then
4919-
assert(currentEntry.bounds.contains(tp),
4920-
i"$origin is constrained to be $currentEntry but attempted to instantiate it to $tp")
4920+
if Config.checkConstraintsSatisfiable then
4921+
assert(currentEntry.bounds.contains(tp),
4922+
i"$origin is constrained to be $currentEntry but attempted to instantiate it to $tp")
49214923

4922-
if ((ctx.typerState eq owningState.nn.get.uncheckedNN) && !TypeComparer.subtypeCheckInProgress)
4923-
setInst(tp)
4924-
ctx.typerState.constraint = ctx.typerState.constraint.replace(origin, tp)
4925-
tp
4926-
}
4924+
if ((ctx.typerState eq owningState.nn.get.uncheckedNN) && !TypeComparer.subtypeCheckInProgress)
4925+
setInst(tp)
4926+
ctx.typerState.constraint = ctx.typerState.constraint.replace(origin, tp)
4927+
tp
49274928

49284929
def typeToInstantiateWith(fromBelow: Boolean)(using Context): Type =
49294930
TypeComparer.instanceType(origin, fromBelow, widenUnions, nestingLevel)
@@ -4937,10 +4938,7 @@ object Types extends TypeUtils {
49374938
*/
49384939
def instantiate(fromBelow: Boolean)(using Context): Type =
49394940
val tp = typeToInstantiateWith(fromBelow)
4940-
if myInst.exists then // The line above might have triggered instantiation of the current type variable
4941-
myInst
4942-
else
4943-
instantiateWith(tp)
4941+
instantiateWith(tp)
49444942

49454943
/** Widen unions when instantiating this variable in the current context? */
49464944
def widenUnions(using Context): Boolean = !ctx.typerState.constraint.isHard(this)

tests/neg/showtest.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
trait Show[T]:
2+
extension (x: T) def show: String
3+
4+
given showAnx: Show[Any] = _.toString
5+
6+
def print[T: Show](x: T) = println(x.show)
7+
8+
@main def Test =
9+
println("hello".show) // ok
10+
print("hello".show) // error

tests/pos/showtest.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
trait Show[T]:
2+
extension (x: T) def show: String
3+
4+
given showAny[T]: Show[T] = _.toString
5+
given showInt: Show[Int] = x => s"INT $x"
6+
7+
def print[T: Show](x: T) = println(x.show)
8+
9+
@main def Test =
10+
println("hello".show)
11+
println(1.show)
12+
print("hello".show)
13+
print(1)

0 commit comments

Comments
 (0)