Skip to content

Commit c9781fa

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 af3fd3f commit c9781fa

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
@@ -4919,20 +4919,21 @@ object Types extends TypeUtils {
49194919
def isInstantiated(using Context): Boolean = instanceOpt.exists
49204920

49214921
/** Instantiate variable with given type */
4922-
def instantiateWith(tp: Type)(using Context): Type = {
4923-
assert(tp ne this, i"self instantiation of $origin, constraint = ${ctx.typerState.constraint}")
4924-
assert(!myInst.exists, i"$origin is already instantiated to $myInst but we attempted to instantiate it to $tp")
4925-
typr.println(i"instantiating $this with $tp")
4922+
def instantiateWith(tp: Type)(using Context): Type =
4923+
if myInst.exists then myInst
4924+
else
4925+
assert(tp ne this, i"self instantiation of $origin, constraint = ${ctx.typerState.constraint}")
4926+
assert(!myInst.exists, i"$origin is already instantiated to $myInst but we attempted to instantiate it to $tp")
4927+
typr.println(i"instantiating $this with $tp")
49264928

4927-
if Config.checkConstraintsSatisfiable then
4928-
assert(currentEntry.bounds.contains(tp),
4929-
i"$origin is constrained to be $currentEntry but attempted to instantiate it to $tp")
4929+
if Config.checkConstraintsSatisfiable then
4930+
assert(currentEntry.bounds.contains(tp),
4931+
i"$origin is constrained to be $currentEntry but attempted to instantiate it to $tp")
49304932

4931-
if ((ctx.typerState eq owningState.nn.get.uncheckedNN) && !TypeComparer.subtypeCheckInProgress)
4932-
setInst(tp)
4933-
ctx.typerState.constraint = ctx.typerState.constraint.replace(origin, tp)
4934-
tp
4935-
}
4933+
if ((ctx.typerState eq owningState.nn.get.uncheckedNN) && !TypeComparer.subtypeCheckInProgress)
4934+
setInst(tp)
4935+
ctx.typerState.constraint = ctx.typerState.constraint.replace(origin, tp)
4936+
tp
49364937

49374938
def typeToInstantiateWith(fromBelow: Boolean)(using Context): Type =
49384939
TypeComparer.instanceType(origin, fromBelow, widenUnions, nestingLevel)
@@ -4946,10 +4947,7 @@ object Types extends TypeUtils {
49464947
*/
49474948
def instantiate(fromBelow: Boolean)(using Context): Type =
49484949
val tp = typeToInstantiateWith(fromBelow)
4949-
if myInst.exists then // The line above might have triggered instantiation of the current type variable
4950-
myInst
4951-
else
4952-
instantiateWith(tp)
4950+
instantiateWith(tp)
49534951

49544952
/** Widen unions when instantiating this variable in the current context? */
49554953
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)