Skip to content

Commit

Permalink
Convert SAM result types to function types (#17740)
Browse files Browse the repository at this point in the history
  • Loading branch information
dwijnand committed Jun 18, 2023
2 parents 889c208 + f641a87 commit 9b70af9
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 15 deletions.
10 changes: 10 additions & 0 deletions compiler/src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5577,6 +5577,16 @@ object Types {
else None
}
else None

def isSamCompatible(lhs: Type, rhs: Type)(using Context): Boolean = rhs match
case SAMType(mt) if !isParamDependentRec(mt) =>
lhs <:< mt.toFunctionType(isJava = rhs.classSymbol.is(JavaDefined))
case _ => false

def isParamDependentRec(mt: MethodType)(using Context): Boolean =
mt.isParamDependent || mt.resultType.match
case mt: MethodType => isParamDependentRec(mt)
case _ => false
}

// ----- TypeMaps --------------------------------------------------------------------
Expand Down
4 changes: 1 addition & 3 deletions compiler/src/dotty/tools/dotc/typer/Applications.scala
Original file line number Diff line number Diff line change
Expand Up @@ -695,9 +695,7 @@ trait Applications extends Compatibility {
val argtpe1 = argtpe.widen

def SAMargOK =
defn.isFunctionType(argtpe1) && formal.match
case SAMType(sam) => argtpe <:< sam.toFunctionType(isJava = formal.classSymbol.is(JavaDefined))
case _ => false
defn.isFunctionType(argtpe1) && SAMType.isSamCompatible(argtpe, formal)

isCompatible(argtpe, formal)
// Only allow SAM-conversion to PartialFunction if implicit conversions
Expand Down
22 changes: 10 additions & 12 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1324,7 +1324,10 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
case RefinedType(parent, nme.apply, mt @ MethodTpe(_, formals, restpe))
if (defn.isNonRefinedFunction(parent) || defn.isErasedFunctionType(parent)) && formals.length == defaultArity =>
(formals, untpd.DependentTypeTree(syms => restpe.substParams(mt, syms.map(_.termRef))))
case SAMType(mt @ MethodTpe(_, formals, restpe)) =>
case pt1 @ SAMType(mt @ MethodTpe(_, formals, _)) if !SAMType.isParamDependentRec(mt) =>
val restpe = mt.resultType match
case mt: MethodType => mt.toFunctionType(isJava = pt1.classSymbol.is(JavaDefined))
case tp => tp
(formals,
if (mt.isResultDependent)
untpd.DependentTypeTree(syms => restpe.substParams(mt, syms.map(_.termRef)))
Expand Down Expand Up @@ -4115,17 +4118,12 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
// convert function literal to SAM closure
tree match {
case closure(Nil, id @ Ident(nme.ANON_FUN), _)
if defn.isFunctionType(wtp) && !defn.isFunctionType(pt) =>
pt match {
case SAMType(sam)
if wtp <:< sam.toFunctionType(isJava = pt.classSymbol.is(JavaDefined)) =>
// was ... && isFullyDefined(pt, ForceDegree.flipBottom)
// but this prevents case blocks from implementing polymorphic partial functions,
// since we do not know the result parameter a priori. Have to wait until the
// body is typechecked.
return toSAM(tree)
case _ =>
}
if defn.isFunctionType(wtp) && !defn.isFunctionType(pt) && SAMType.isSamCompatible(wtp, pt) =>
// was ... && isFullyDefined(pt, ForceDegree.flipBottom)
// but this prevents case blocks from implementing polymorphic partial functions,
// since we do not know the result parameter a priori. Have to wait until the
// body is typechecked.
return toSAM(tree)
case _ =>
}

Expand Down
14 changes: 14 additions & 0 deletions tests/neg/i17183.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- [E081] Type Error: tests/neg/i17183.scala:11:24 ---------------------------------------------------------------------
11 |def test = Context(f = (_, _) => ???) // error // error
| ^
| Missing parameter type
|
| I could not infer the type of the parameter _$1 of expanded function:
| (_$1, _$2) => ???.
-- [E081] Type Error: tests/neg/i17183.scala:11:27 ---------------------------------------------------------------------
11 |def test = Context(f = (_, _) => ???) // error // error
| ^
| Missing parameter type
|
| I could not infer the type of the parameter _$2 of expanded function:
| (_$1, _$2) => ???.
11 changes: 11 additions & 0 deletions tests/neg/i17183.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
trait Dependency {
trait More
}

trait MyFunc {
def apply(a: Int, b: String)(using dep: Dependency, more: dep.More): String
}

case class Context(f: MyFunc)

def test = Context(f = (_, _) => ???) // error // error
9 changes: 9 additions & 0 deletions tests/pos/i17183.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
trait Dependency

trait MyFunc {
def apply(a: Int, b: String)(using Dependency): String
}

case class Context(f: MyFunc)

def test = Context(f = (_, _) => ???)

0 comments on commit 9b70af9

Please sign in to comment.