From 594e168c9a1be731c2d46bde555a242998626505 Mon Sep 17 00:00:00 2001 From: Matt Bovel Date: Tue, 1 Feb 2022 19:35:49 +0100 Subject: [PATCH] Keep softness when widening unions --- compiler/src/dotty/tools/dotc/core/Types.scala | 6 +++--- tests/pos/widen-union.scala | 7 ++++++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/core/Types.scala b/compiler/src/dotty/tools/dotc/core/Types.scala index 769defc3e42c..0f7e87213d08 100644 --- a/compiler/src/dotty/tools/dotc/core/Types.scala +++ b/compiler/src/dotty/tools/dotc/core/Types.scala @@ -3263,10 +3263,10 @@ object Types { if myUnionPeriod != ctx.period then myUnion = if isSoft then - TypeComparer.lub(tp1.widenUnionWithoutNull, tp2.widenUnionWithoutNull, canConstrain = true) match + TypeComparer.lub(tp1.widenUnionWithoutNull, tp2.widenUnionWithoutNull, canConstrain = true, isSoft = isSoft) match case union: OrType => union.join case res => res - else derivedOrType(tp1.widenUnionWithoutNull, tp2.widenUnionWithoutNull) + else derivedOrType(tp1.widenUnionWithoutNull, tp2.widenUnionWithoutNull, soft = isSoft) if !isProvisional then myUnionPeriod = ctx.period myUnion @@ -3282,7 +3282,7 @@ object Types { else tp1.atoms | tp2.atoms val tp1w = tp1.widenSingletons val tp2w = tp2.widenSingletons - myWidened = if ((tp1 eq tp1w) && (tp2 eq tp2w)) this else tp1w | tp2w + myWidened = if ((tp1 eq tp1w) && (tp2 eq tp2w)) this else TypeComparer.lub(tp1w, tp2w, isSoft = isSoft) atomsRunId = ctx.runId override def atoms(using Context): Atoms = diff --git a/tests/pos/widen-union.scala b/tests/pos/widen-union.scala index b0b64f0dc6c6..ec58f3da1941 100644 --- a/tests/pos/widen-union.scala +++ b/tests/pos/widen-union.scala @@ -5,6 +5,11 @@ object Test1: val z: Int | String = y object Test2: + val x: 3 | "a" = 3 + val y = x + val z: Int | String = y + +object Test3: type Sig = Int | String def consistent(x: Sig, y: Sig): Boolean = ???// x == y @@ -12,7 +17,7 @@ object Test2: xs.corresponds(ys)(consistent) // OK || xs.corresponds(ys)(consistent(_, _)) // error, found: Any, required: Int | String -object Test3: +object Test4: def g[X](x: X | String): Int = ??? def y: Boolean | String = ???