Skip to content

Commit 1d31192

Browse files
committed
Add cases for union and intersection types
1 parent a9363c3 commit 1d31192

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,18 @@ class TypeUtils:
137137
names.zip(values)
138138
case tp: TypeProxy =>
139139
tp.superType.namedTupleElementTypesUpTo(bound, normalize)
140+
case tp: OrType =>
141+
val lhs = tp.tp1.namedTupleElementTypesUpTo(bound, normalize)
142+
val rhs = tp.tp2.namedTupleElementTypesUpTo(bound, normalize)
143+
if (lhs != rhs) throw TypeError(em"Malformed Union Type: Named Tuple elements must be the same, but $lhs and $rhs were found.")
144+
lhs
145+
case tp: AndType =>
146+
(tp.tp1.namedTupleElementTypesUpTo(bound, normalize), tp.tp2.namedTupleElementTypesUpTo(bound, normalize)) match
147+
case (Nil, rhs) => rhs
148+
case (lhs, Nil) => lhs
149+
case (lhs, rhs) =>
150+
if (lhs != rhs) throw TypeError(em"Malformed Intersection Type: Named Tuple elements must be the same, but $lhs and $rhs were found.")
151+
lhs
140152
case t =>
141153
Nil
142154

tests/run/i22150.check

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
0
2+
1
3+
2

tests/run/i22150.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,19 @@ val directionsNT = IArray(
88
(dx = -1, dy = 0), // left
99
)
1010
val IArray(UpNT @ _, _, _, _) = directionsNT
11+
12+
object NT:
13+
// def foo[T <: (x: Int, y: String)](tup: T): Int =
14+
// tup.x
15+
16+
def union[T](tup: (x: Int, y: String) | (x: Int, y: String)): Int =
17+
tup.x
18+
19+
def intersect[T](tup: (x: Int, y: String) & T): Int =
20+
tup.x
21+
22+
1123
@main def Test =
1224
println(UpNT.dx)
25+
println(NT.union((1, "a")))
26+
println(NT.intersect((2, "b")))

0 commit comments

Comments
 (0)