From ba564fa75401c0819a516e49e11e9a793f9cefec Mon Sep 17 00:00:00 2001 From: Alexander Date: Thu, 22 May 2025 22:24:17 +0300 Subject: [PATCH 1/2] dealias type when type inference occurs --- compiler/src/dotty/tools/dotc/core/TypeComparer.scala | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/core/TypeComparer.scala b/compiler/src/dotty/tools/dotc/core/TypeComparer.scala index 5ecb0ee01ec0..01ab027ba2f7 100644 --- a/compiler/src/dotty/tools/dotc/core/TypeComparer.scala +++ b/compiler/src/dotty/tools/dotc/core/TypeComparer.scala @@ -1207,9 +1207,12 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling if fromBelow then isSubType(tp2, tp1) else isSubType(tp1, tp2) def directionalRecur(tp1: Type, tp2: Type): Boolean = if fromBelow then recur(tp2, tp1) else recur(tp1, tp2) - - val otherTycon = other.tycon - val otherArgs = other.args + val otherDealias = other.dealias match + case appliedType: AppliedType if appliedType.args.hasSameLengthAs(args) && !other.args.hasSameLengthAs(args) => + appliedType + case _ => other + val otherTycon = otherDealias.tycon + val otherArgs = otherDealias.args val d = otherArgs.length - args.length d >= 0 && { @@ -1234,7 +1237,7 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling otherTycon rollbackConstraintsUnless: (assumedTrue(tycon) || directionalIsSubType(tycon, adaptedTycon)) - && directionalRecur(adaptedTycon.appliedTo(args), other) + && directionalRecur(adaptedTycon.appliedTo(args), otherDealias) } } end compareAppliedTypeParamRef From 48052975a01418615740026b729179fb27e9e077 Mon Sep 17 00:00:00 2001 From: Alexander Date: Fri, 23 May 2025 21:03:46 +0300 Subject: [PATCH 2/2] add test case for dealias during hk type parameter inference --- .../dotty/tools/dotc/core/TypeComparer.scala | 18 +++++++++++------- tests/pos/dealias-hk-typeparam.scala | 8 ++++++++ 2 files changed, 19 insertions(+), 7 deletions(-) create mode 100644 tests/pos/dealias-hk-typeparam.scala diff --git a/compiler/src/dotty/tools/dotc/core/TypeComparer.scala b/compiler/src/dotty/tools/dotc/core/TypeComparer.scala index 01ab027ba2f7..309aa41e46bb 100644 --- a/compiler/src/dotty/tools/dotc/core/TypeComparer.scala +++ b/compiler/src/dotty/tools/dotc/core/TypeComparer.scala @@ -1207,12 +1207,16 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling if fromBelow then isSubType(tp2, tp1) else isSubType(tp1, tp2) def directionalRecur(tp1: Type, tp2: Type): Boolean = if fromBelow then recur(tp2, tp1) else recur(tp1, tp2) - val otherDealias = other.dealias match - case appliedType: AppliedType if appliedType.args.hasSameLengthAs(args) && !other.args.hasSameLengthAs(args) => - appliedType - case _ => other - val otherTycon = otherDealias.tycon - val otherArgs = otherDealias.args + + // If the arity of the type constructors does not match, dealias to avoid creating type lambdas. + val otherDealiased = + if other.args.hasSameLengthAs(args) then other + else other.dealias match + case dealiasedType: AppliedType if dealiasedType.args.hasSameLengthAs(args) => + dealiasedType + case _ => other + val otherTycon = otherDealiased.tycon + val otherArgs = otherDealiased.args val d = otherArgs.length - args.length d >= 0 && { @@ -1237,7 +1241,7 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling otherTycon rollbackConstraintsUnless: (assumedTrue(tycon) || directionalIsSubType(tycon, adaptedTycon)) - && directionalRecur(adaptedTycon.appliedTo(args), otherDealias) + && directionalRecur(adaptedTycon.appliedTo(args), otherDealiased) } } end compareAppliedTypeParamRef diff --git a/tests/pos/dealias-hk-typeparam.scala b/tests/pos/dealias-hk-typeparam.scala new file mode 100644 index 000000000000..e39d9ffd1d12 --- /dev/null +++ b/tests/pos/dealias-hk-typeparam.scala @@ -0,0 +1,8 @@ +type Result[F[_], Err, Res] = F[Either[Err, Res]] + +trait TypeClass[F[_]] + +def id[F[_]: TypeClass, A](x: F[A]) = x + +def test[F[_]: TypeClass](x: Result[F, String, Int]) = + id(x) \ No newline at end of file