Skip to content

Fix findFunctionType for OrTypes #15478

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

Merged
merged 4 commits into from
Jun 30, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 8 additions & 2 deletions compiler/src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1690,14 +1690,20 @@ object Types {
* is returned. If no function type is found, Any is returned.
*/
def findFunctionType(using Context): Type = dealias match
case tp: AndOrType =>
case tp: AndType =>
tp.tp1.findFunctionType & tp.tp2.findFunctionType
case tp: OrType =>
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)