Skip to content

Commit 031fa49

Browse files
committed
missing cases for less eager dealiasing after following type alias
1 parent a4b11d1 commit 031fa49

File tree

5 files changed

+50
-22
lines changed

5 files changed

+50
-22
lines changed

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

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,23 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
289289
}
290290
}
291291

292+
def realiasConstraint(param: Type, alias: NamedType, dealiased: Type) =
293+
if alias.symbol.isStatic then
294+
param.stripTypeVar match
295+
case param: TypeParamRef =>
296+
constraint.entry(param) match
297+
case TypeBounds(lo, hi) =>
298+
val aliasLo = (alias ne lo) && (dealiased eq lo)
299+
val aliasHi = (alias ne hi) && (dealiased eq hi)
300+
if aliasLo || aliasHi then
301+
constraint = constraint.updateEntry(param, TypeBounds(
302+
if aliasLo then alias else lo,
303+
if aliasHi then alias else hi))
304+
case tp =>
305+
if (alias ne tp) && (dealiased eq tp) then
306+
constraint = constraint.updateEntry(param, alias)
307+
case _ =>
308+
292309
def firstTry: Boolean = tp2 match {
293310
case tp2: NamedType =>
294311
def compareNamed(tp1: Type, tp2: NamedType): Boolean =
@@ -297,14 +314,18 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
297314
val info2 = tp2.info
298315
info2 match
299316
case info2: TypeAlias =>
300-
if recur(tp1, info2.alias) then return true
317+
val res = recur(tp1, info2.alias)
318+
realiasConstraint(tp1, tp2, info2.alias)
319+
if res then return true
301320
if tp2.asInstanceOf[TypeRef].canDropAlias then return false
302321
case _ =>
303322
tp1 match
304323
case tp1: NamedType =>
305324
tp1.info match {
306325
case info1: TypeAlias =>
307-
if recur(info1.alias, tp2) then return true
326+
val res = recur(info1.alias, tp2)
327+
realiasConstraint(tp2, tp1, info1.alias)
328+
if res then return true
308329
if tp1.asInstanceOf[TypeRef].canDropAlias then return false
309330
case _ =>
310331
}
@@ -413,26 +434,10 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
413434
case tp1: NamedType =>
414435
tp1.info match {
415436
case info1: TypeAlias =>
416-
def realiasConstraint() = tp2 match {
417-
case tp2: TypeParamRef =>
418-
constraint.entry(tp2) match {
419-
case TypeBounds(lo, hi) =>
420-
val aliasLo = (tp1 ne lo) && (info1.alias eq lo)
421-
val aliasHi = (tp1 ne hi) && (info1.alias eq hi)
422-
if aliasLo || aliasHi then
423-
constraint = constraint.updateEntry(tp2, TypeBounds(
424-
if aliasLo then tp1 else lo,
425-
if aliasHi then tp1 else hi))
426-
case tp =>
427-
if (tp1 ne tp) && (info1.alias eq tp) then
428-
constraint = constraint.updateEntry(tp2, tp1)
429-
}
430-
case _ =>
431-
}
432437
val res = recur(info1.alias, tp2)
433-
if (tp1.symbol.isStatic) realiasConstraint()
434-
if (res) return true
435-
if (tp1.prefix.isStable) return tryLiftedToThis1
438+
realiasConstraint(tp2, tp1, info1.alias)
439+
if res then return true
440+
if tp1.prefix.isStable then return tryLiftedToThis1
436441
case _ =>
437442
if (tp1 eq NothingType) || isBottom(tp1) then return true
438443
}

tests/run-macros/i15304.check

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Test.MyString
2+
Test.MyString
3+
Test.MyString
4+
scala.Seq[Test.MyString]
5+
scala.Tuple2[Test.MyString, scala.Int]

tests/run-macros/i15304/Macro_1.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import scala.quoted._
2+
3+
def typeNameOf[A: Type](using Quotes): Expr[String] =
4+
val name = Type.show[A]
5+
Expr(name)
6+
7+
inline def typeNameOf[A]: String = ${ typeNameOf[A] }
8+
9+
inline def typeNameOfF1[A](f: A => Unit): String = ${ typeNameOf[A] }

tests/run-macros/i15304/Test_2.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
object Test:
2+
type MyString = String
3+
4+
def main(args: Array[String]): Unit =
5+
println(typeNameOf[MyString])
6+
println(typeNameOfF1 { (x: MyString) => })
7+
println(typeNameOfF1[MyString] { (x: MyString) => })
8+
println(typeNameOfF1 { (x: Seq[MyString]) => })
9+
println(typeNameOfF1 { (x: (MyString, Int)) => })

tests/run-macros/quote-matcher-runtime.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ Pattern: scala.StringContext.apply("abc", "xyz")
245245
Result: Some(List())
246246

247247
Scrutinee: scala.StringContext.apply("abc", "xyz")
248-
Pattern: scala.StringContext.apply(scala.quoted.runtime.Patterns.patternHole[java.lang.String], scala.quoted.runtime.Patterns.patternHole[java.lang.String])
248+
Pattern: scala.StringContext.apply(scala.quoted.runtime.Patterns.patternHole[scala.Predef.String], scala.quoted.runtime.Patterns.patternHole[scala.Predef.String])
249249
Result: Some(List(Expr("abc"), Expr("xyz")))
250250

251251
Scrutinee: scala.StringContext.apply("abc", "xyz")

0 commit comments

Comments
 (0)