Skip to content

Commit

Permalink
Backport "Do not compute protoFormal if param.tpt is empty" (#18411)
Browse files Browse the repository at this point in the history
Backports #18288
  • Loading branch information
Kordyjan authored Aug 22, 2023
2 parents f5fc096 + 0305d88 commit eb0ce8f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 25 deletions.
49 changes: 24 additions & 25 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1596,32 +1596,31 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
if desugared.isEmpty then
val inferredParams: List[untpd.ValDef] =
for ((param, i) <- params.zipWithIndex) yield
val (formalBounds, isErased) = protoFormal(i)
val param0 =
if (!param.tpt.isEmpty) param
else
val formal = formalBounds.loBound
val isBottomFromWildcard = (formalBounds ne formal) && formal.isExactlyNothing
val knownFormal = isFullyDefined(formal, ForceDegree.failBottom)
// If the expected formal is a TypeBounds wildcard argument with Nothing as lower bound,
// try to prioritize inferring from target. See issue 16405 (tests/run/16405.scala)
val paramType =
// Strip inferred erased annotation, to avoid accidentally inferring erasedness
val formal0 = if !isErased then formal.stripAnnots(_.symbol != defn.ErasedParamAnnot) else formal
if knownFormal && !isBottomFromWildcard then
formal0
else
inferredFromTarget(param, formal, calleeType, isErased, paramIndex).orElse(
if knownFormal then formal0
else errorType(AnonymousFunctionMissingParamType(param, tree, formal), param.srcPos)
)
val paramTpt = untpd.TypedSplice(
(if knownFormal then InferredTypeTree() else untpd.TypeTree())
.withType(paramType.translateFromRepeated(toArray = false))
.withSpan(param.span.endPos)
if (!param.tpt.isEmpty) param
else
val (formalBounds, isErased) = protoFormal(i)
val formal = formalBounds.loBound
val isBottomFromWildcard = (formalBounds ne formal) && formal.isExactlyNothing
val knownFormal = isFullyDefined(formal, ForceDegree.failBottom)
// If the expected formal is a TypeBounds wildcard argument with Nothing as lower bound,
// try to prioritize inferring from target. See issue 16405 (tests/run/16405.scala)
val paramType =
// Strip inferred erased annotation, to avoid accidentally inferring erasedness
val formal0 = if !isErased then formal.stripAnnots(_.symbol != defn.ErasedParamAnnot) else formal
if knownFormal && !isBottomFromWildcard then
formal0
else
inferredFromTarget(param, formal, calleeType, isErased, paramIndex).orElse(
if knownFormal then formal0
else errorType(AnonymousFunctionMissingParamType(param, tree, formal), param.srcPos)
)
cpy.ValDef(param)(tpt = paramTpt)
if isErased then param0.withAddedFlags(Flags.Erased) else param0
val paramTpt = untpd.TypedSplice(
(if knownFormal then InferredTypeTree() else untpd.TypeTree())
.withType(paramType.translateFromRepeated(toArray = false))
.withSpan(param.span.endPos)
)
val param0 = cpy.ValDef(param)(tpt = paramTpt)
if isErased then param0.withAddedFlags(Flags.Erased) else param0
desugared = desugar.makeClosure(inferredParams, fnBody, resultTpt, isContextual, tree.span)

typed(desugared, pt)
Expand Down
15 changes: 15 additions & 0 deletions tests/pos/i18276a.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import scala.language.implicitConversions

case class Assign(left: String, right: String)
class SyntaxAnalyser extends ParsersBase {
val x: Parser[String ~ String] = ???
val y: Parser[Assign] = x.map(Assign.apply)
}

class ParsersBase {
trait ~[+T, +U]
abstract class Parser[+T]:
def map[U](f: T => U): Parser[U] = ???

given [A, B, X]: Conversion[(A, B) => X, (A ~ B) => X] = ???
}
9 changes: 9 additions & 0 deletions tests/pos/i18276b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import scala.language.implicitConversions

def foo(a: Int): Int = ???
def bar(f: () => Int): Int = ???

given f: Conversion[Int => Int, () => Int] = ???

def test1: Int = bar(foo) // implicit conversion applied to foo
def test2: Int = bar(f(foo))

0 comments on commit eb0ce8f

Please sign in to comment.