Skip to content

Commit df9e4de

Browse files
committed
Fix-scala#4867 Update union tests to improve inference when ascribed
Splits union.scala test from negative to negative and positive tests, exhibiting the preservation of the union when the union has been explicitly ascribed as per fix scala#4867.
1 parent 4b11b33 commit df9e4de

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

tests/neg/union.scala

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,12 @@ object Test {
66
class D extends A
77

88
val b = true
9-
val x = if (b) new B else new C
10-
val y: B | C = x // error
11-
}
12-
13-
object O {
14-
class A
15-
class B
16-
def f[T](x: T, y: T): T = x
17-
18-
val x: A = f(new A { }, new A)
19-
20-
val y1: A | B = f(new A { }, new B) // error
21-
val y2: A | B = f[A | B](new A { }, new B) // ok
229

23-
val z = if (???) new A{} else new B
10+
val x: B | C = if (b) new B else new C
2411

25-
val z1: A | B = z // error
12+
val y: B | C = x // Explicit union ascription is kept
13+
val ok: B | C = y // ok
2614

27-
val z2: A | B = if (???) new A else new B // ok
15+
val z = x // We lose the Union ascription as it's not explicit
16+
val error: B | C = z // error as z has lost union ascription
2817
}

tests/pos/union.scala

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
object Test {
2+
3+
class A
4+
class B extends A
5+
class C extends A
6+
class D extends A
7+
8+
val b = true
9+
val x: B | C = if (b) new B else new C
10+
val y: B | C = x
11+
}
12+
13+
object O {
14+
class A
15+
class B
16+
def f[T](x: T, y: T): T = x
17+
18+
val x: A = f(new A { }, new A)
19+
20+
val y1: A | B = f(new A { }, new B)
21+
val y2: A | B = f[A | B](new A { }, new B)
22+
23+
val z: A | B = if (???) new A{} else new B
24+
25+
val z1: A | B = z
26+
val z2: A | B = if (???) new A else new B
27+
}

0 commit comments

Comments
 (0)