File tree 3 files changed +77
-6
lines changed
compiler/src/dotty/tools/dotc/core
3 files changed +77
-6
lines changed Original file line number Diff line number Diff line change @@ -459,9 +459,14 @@ object Types {
459
459
case _ => f(this )
460
460
}
461
461
462
- /** Map function `f` over elements of an OrType, rebuilding with function `g` */
462
+ /** Map function `f` over elements of an OrType, rebuilding with function `g`
463
+ * Bottom types are skipped.
464
+ */
463
465
final def mapReduceOr [T ](f : Type => T )(g : (T , T ) => T )(using Context ): T = stripTypeVar match {
464
- case OrType (tp1, tp2) => g(tp1.mapReduceOr(f)(g), tp2.mapReduceOr(f)(g))
466
+ case OrType (tp1, tp2) =>
467
+ if tp1.isBottomType then tp2.mapReduceOr(f)(g)
468
+ else if tp2.isBottomType then tp1.mapReduceOr(f)(g)
469
+ else g(tp1.mapReduceOr(f)(g), tp2.mapReduceOr(f)(g))
465
470
case _ => f(this )
466
471
}
467
472
@@ -3148,10 +3153,7 @@ object Types {
3148
3153
/** Replace or type by the closest non-or type above it */
3149
3154
def join (using Context ): Type = {
3150
3155
if (myJoinPeriod != ctx.period) {
3151
- myJoin =
3152
- if tp1 frozen_<:< tp2 then tp2
3153
- else if tp2 frozen_<:< tp1 then tp1
3154
- else TypeOps .orDominator(this )
3156
+ myJoin = TypeOps .orDominator(this )
3155
3157
core.println(i " join of $this == $myJoin" )
3156
3158
assert(myJoin != this )
3157
3159
myJoinPeriod = ctx.period
Original file line number Diff line number Diff line change
1
+
2
+ class A {
3
+ def get (): Int = 0
4
+ }
5
+
6
+ class B extends A {}
7
+
8
+ class C extends A {}
9
+
10
+ def test1 = {
11
+ val s : String | Null = ???
12
+ val l = s.length
13
+
14
+ val a : A | Null = new A
15
+ a.get()
16
+
17
+ val bc : B | C = new B
18
+ bc.get()
19
+
20
+ val bcn : B | (C | Null ) = new C
21
+ bcn.get()
22
+
23
+ val bnc : (B | Null ) | C = null
24
+ bnc.get()
25
+
26
+ val abcn : A | B | C | Null = new A
27
+ abcn.get()
28
+ }
29
+
30
+ def test2 = {
31
+ val s : String | Nothing = ???
32
+ val l = s.length
33
+
34
+ val a : A | Nothing = new A
35
+ a.get()
36
+
37
+ val bc : B | C = new B
38
+ bc.get()
39
+
40
+ val bcn : B | (C | Nothing ) = new C
41
+ bcn.get()
42
+
43
+ val bnc : (B | Nothing ) | C = new B
44
+ bnc.get()
45
+
46
+ val abcn : A | B | C | Nothing = new A
47
+ abcn.get()
48
+ }
Original file line number Diff line number Diff line change
1
+ object Main :
2
+ class Null
3
+ type Optional [A ] = A | Null
4
+
5
+ val maybeInt : Optional [Int ] = 1
6
+
7
+ // simplest typeclass
8
+ trait TC [F [_]]
9
+
10
+ // given instances for our Optional and standard Option[_]
11
+ given g1 : TC [Optional ] = ???
12
+ given g2 : TC [Option ] = ???
13
+
14
+ def summonTC [F [_], A ](f : F [A ])(using TC [F ]): Unit = ???
15
+
16
+ summonTC(Option (42 )) // OK
17
+
18
+ summonTC[Optional , Int ](maybeInt) // OK
19
+
20
+ summonTC(maybeInt)
21
+
You can’t perform that action at this time.
0 commit comments