Skip to content

Commit b8825a2

Browse files
authored
Merge pull request #9488 from dotty-staging/widen-union-hk
Fix #9479: Widen unions in inferred type lambdas
2 parents ab68d43 + cdc8435 commit b8825a2

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

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

+7-5
Original file line numberDiff line numberDiff line change
@@ -1118,15 +1118,15 @@ object Types {
11181118

11191119
/** Widen this type and if the result contains embedded union types, replace
11201120
* them by their joins.
1121-
* "Embedded" means: inside intersectons or recursive types, or in prefixes of refined types.
1121+
* "Embedded" means: inside type lambdas, intersections or recursive types, or in prefixes of refined types.
11221122
* If an embedded union is found, we first try to simplify or eliminate it by
11231123
* re-lubbing it while allowing type parameters to be constrained further.
11241124
* Any remaining union types are replaced by their joins.
11251125
*
1126-
* For instance, if `A` is an unconstrained type variable, then
1127-
*
1128-
* ArrayBuffer[Int] | ArrayBuffer[A]
1129-
*
1126+
* For instance, if `A` is an unconstrained type variable, then
1127+
*
1128+
* ArrayBuffer[Int] | ArrayBuffer[A]
1129+
*
11301130
* is approximated by constraining `A` to be =:= to `Int` and returning `ArrayBuffer[Int]`
11311131
* instead of `ArrayBuffer[? >: Int | A <: Int & A]`
11321132
*
@@ -1155,6 +1155,8 @@ object Types {
11551155
tp.derivedRefinedType(tp.parent.widenUnion, tp.refinedName, tp.refinedInfo)
11561156
case tp: RecType =>
11571157
tp.rebind(tp.parent.widenUnion)
1158+
case tp: HKTypeLambda =>
1159+
tp.derivedLambdaType(resType = tp.resType.widenUnion)
11581160
case tp =>
11591161
tp
11601162
}

tests/pos/i9479.scala

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
trait Applicative[F[_]]
2+
3+
def traverse[F[_]: Applicative, A, B](as: List[A])(f: A => F[B]) = ???
4+
5+
object Test {
6+
implicit def eitherApplicative[A]: Applicative[[X] =>> Either[A, X]] = ???
7+
8+
// Used to fail looking for `Applicative[[X] =>> Right[Nothing, X] | Left[Int, X]]`
9+
traverse(List(1, 2))(i => if (true) Right(i) else Left(i))
10+
}

0 commit comments

Comments
 (0)