Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix findFunctionType for OrTypes #15478

Merged
merged 4 commits into from
Jun 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions compiler/src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1685,19 +1685,30 @@ object Types {
case _ => resultType
}

/** Determine the expected function type from the prototype. If multiple
* function types are found in a union or intersection, their intersection
* is returned. If no function type is found, Any is returned.
/** Determine the expected function type from the prototype.
* If no function type is found, NoType is returned. If multiple
* function types are found in an intersection, their intersection
* is returned. This works since `&` invokes `TypeComparer.distributeAnd`, which
* ensures that `(A1 => B1) & (A2 => B2)` simplifies to `(A1 | A2) => (B1 & B2)`,
* so the result is again a function type. An analogous distribution mechanism
* does not exist for `|`. Therefore, a union of function types also yields `NoType`,
* since we cannot determine a single expected function type.
*/
def findFunctionType(using Context): Type = dealias match
case tp: AndOrType =>
case tp: AndType =>
tp.tp1.findFunctionType & tp.tp2.findFunctionType
case tp: OrType =>
odersky marked this conversation as resolved.
Show resolved Hide resolved
val tf1 = tp.tp1.findFunctionType
val tf2 = tp.tp2.findFunctionType
if !tf1.exists then tf2
else if !tf2.exists then tf1
else NoType
case t if defn.isNonRefinedFunction(t) =>
t
case t @ SAMType(_) =>
t
case _ =>
defn.AnyType
NoType

/** This type seen as a TypeBounds */
final def bounds(using Context): TypeBounds = this match {
Expand Down
4 changes: 2 additions & 2 deletions tests/neg/i11694.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ def test1 = {
def f21: (Int => Int) | Null = x => x + 1
def f22: Null | (Int => Int) = x => x + 1

def f31: (Int => Int) | (Int => Int) = x => x + 1
def f32: (Int => Int) | (Int => Int) | Unit = x => x + 1
def f31: (Int => Int) | (Int => Int) = x => x + 1 // error
def f32: (Int => Int) | (Int => Int) | Unit = x => x + 1 // error

def f41: (Int => Int) & (Int => Int) = x => x + 1
def f42: (Int => Int) & (Int => Int) & Any = x => x + 1
Expand Down
5 changes: 5 additions & 0 deletions tests/pos/i15460.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type C = (() => Int) | (() => String)

def foo(c: C): Unit = ()

val _ = foo(() => 1)