File tree 3 files changed +16
-13
lines changed
compiler/src/dotty/tools/dotc
3 files changed +16
-13
lines changed Original file line number Diff line number Diff line change @@ -1674,23 +1674,19 @@ object Types {
1674
1674
case _ => resultType
1675
1675
}
1676
1676
1677
- /** Find the function type in union.
1678
- * If there are multiple function types, NoType is returned.
1677
+ /** Determine the expected function type from the prototype. If multiple
1678
+ * function types are found in a union or intersection, their intersection
1679
+ * is returned. If no function type is found, Any is returned.
1679
1680
*/
1680
- def findFunctionTypeInUnion (using Context ): Type = this match {
1681
- case t : OrType =>
1682
- val t1 = t.tp1.findFunctionTypeInUnion
1683
- if t1 == NoType then t.tp2.findFunctionTypeInUnion else
1684
- val t2 = t.tp2.findFunctionTypeInUnion
1685
- // Returen NoType if the union contains multiple function types
1686
- if t2 == NoType then t1 else NoType
1681
+ def findFunctionType (using Context ): Type = dealias match
1682
+ case tp : AndOrType =>
1683
+ tp.tp1.findFunctionType & tp.tp2.findFunctionType
1687
1684
case t if defn.isNonRefinedFunction(t) =>
1688
1685
t
1689
1686
case t @ SAMType (_) =>
1690
1687
t
1691
1688
case _ =>
1692
- NoType
1693
- }
1689
+ defn.AnyType
1694
1690
1695
1691
/** This type seen as a TypeBounds */
1696
1692
final def bounds (using Context ): TypeBounds = this match {
Original file line number Diff line number Diff line change @@ -1169,7 +1169,7 @@ class Typer extends Namer
1169
1169
pt1 match {
1170
1170
case tp : TypeParamRef =>
1171
1171
decomposeProtoFunction(ctx.typerState.constraint.entry(tp).bounds.hi, defaultArity, pos)
1172
- case _ => pt1.findFunctionTypeInUnion match {
1172
+ case _ => pt1.findFunctionType match {
1173
1173
case pt1 if defn.isNonRefinedFunction(pt1) =>
1174
1174
// if expected parameter type(s) are wildcards, approximate from below.
1175
1175
// if expected result type is a wildcard, approximate from above.
@@ -1444,7 +1444,7 @@ class Typer extends Namer
1444
1444
if (tree.tpt.isEmpty)
1445
1445
meth1.tpe.widen match {
1446
1446
case mt : MethodType =>
1447
- pt.findFunctionTypeInUnion match {
1447
+ pt.findFunctionType match {
1448
1448
case pt @ SAMType (sam)
1449
1449
if ! defn.isFunctionType(pt) && mt <:< sam =>
1450
1450
// SAMs of the form C[?] where C is a class cannot be conversion targets.
Original file line number Diff line number Diff line change @@ -4,12 +4,19 @@ def test1 = {
4
4
5
5
def f21 : (Int => Int ) | Null = x => x + 1
6
6
def f22 : Null | (Int => Int ) = x => x + 1
7
+
8
+ def f31 : (Int => Int ) | (Int => Int ) = x => x + 1
9
+ def f32 : (Int => Int ) | (Int => Int ) | Unit = x => x + 1
10
+
11
+ def f41 : (Int => Int ) & (Int => Int ) = x => x + 1
12
+ def f42 : (Int => Int ) & (Int => Int ) & Any = x => x + 1
7
13
}
8
14
9
15
def test2 = {
10
16
def f1 : (Int => String ) | (Int => Int ) | Null = x => x + 1 // error
11
17
def f2 : (Int => String ) | Function [String , Int ] | Null = x => " " + x // error
12
18
def f3 : Function [Int , Int ] | Function [String , Int ] | Null = x => x + 1 // error
19
+ def f4 : (Int => Int ) & (Int => Int ) & Unit = x => x + 1 // error
13
20
}
14
21
15
22
def test3 = {
You can’t perform that action at this time.
0 commit comments