Skip to content

Dealias type when type parameters inference occurs #23242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions compiler/src/dotty/tools/dotc/core/TypeComparer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1208,8 +1208,15 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
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
// 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 && {
Expand All @@ -1234,7 +1241,7 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
otherTycon
rollbackConstraintsUnless:
(assumedTrue(tycon) || directionalIsSubType(tycon, adaptedTycon))
&& directionalRecur(adaptedTycon.appliedTo(args), other)
&& directionalRecur(adaptedTycon.appliedTo(args), otherDealiased)
}
}
end compareAppliedTypeParamRef
Expand Down
8 changes: 8 additions & 0 deletions tests/pos/dealias-hk-typeparam.scala
Original file line number Diff line number Diff line change
@@ -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)
Loading