Skip to content

Commit eb0ce8f

Browse files
authored
Backport "Do not compute protoFormal if param.tpt is empty" (#18411)
Backports #18288
2 parents f5fc096 + 0305d88 commit eb0ce8f

File tree

3 files changed

+48
-25
lines changed

3 files changed

+48
-25
lines changed

compiler/src/dotty/tools/dotc/typer/Typer.scala

+24-25
Original file line numberDiff line numberDiff line change
@@ -1596,32 +1596,31 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
15961596
if desugared.isEmpty then
15971597
val inferredParams: List[untpd.ValDef] =
15981598
for ((param, i) <- params.zipWithIndex) yield
1599-
val (formalBounds, isErased) = protoFormal(i)
1600-
val param0 =
1601-
if (!param.tpt.isEmpty) param
1602-
else
1603-
val formal = formalBounds.loBound
1604-
val isBottomFromWildcard = (formalBounds ne formal) && formal.isExactlyNothing
1605-
val knownFormal = isFullyDefined(formal, ForceDegree.failBottom)
1606-
// If the expected formal is a TypeBounds wildcard argument with Nothing as lower bound,
1607-
// try to prioritize inferring from target. See issue 16405 (tests/run/16405.scala)
1608-
val paramType =
1609-
// Strip inferred erased annotation, to avoid accidentally inferring erasedness
1610-
val formal0 = if !isErased then formal.stripAnnots(_.symbol != defn.ErasedParamAnnot) else formal
1611-
if knownFormal && !isBottomFromWildcard then
1612-
formal0
1613-
else
1614-
inferredFromTarget(param, formal, calleeType, isErased, paramIndex).orElse(
1615-
if knownFormal then formal0
1616-
else errorType(AnonymousFunctionMissingParamType(param, tree, formal), param.srcPos)
1617-
)
1618-
val paramTpt = untpd.TypedSplice(
1619-
(if knownFormal then InferredTypeTree() else untpd.TypeTree())
1620-
.withType(paramType.translateFromRepeated(toArray = false))
1621-
.withSpan(param.span.endPos)
1599+
if (!param.tpt.isEmpty) param
1600+
else
1601+
val (formalBounds, isErased) = protoFormal(i)
1602+
val formal = formalBounds.loBound
1603+
val isBottomFromWildcard = (formalBounds ne formal) && formal.isExactlyNothing
1604+
val knownFormal = isFullyDefined(formal, ForceDegree.failBottom)
1605+
// If the expected formal is a TypeBounds wildcard argument with Nothing as lower bound,
1606+
// try to prioritize inferring from target. See issue 16405 (tests/run/16405.scala)
1607+
val paramType =
1608+
// Strip inferred erased annotation, to avoid accidentally inferring erasedness
1609+
val formal0 = if !isErased then formal.stripAnnots(_.symbol != defn.ErasedParamAnnot) else formal
1610+
if knownFormal && !isBottomFromWildcard then
1611+
formal0
1612+
else
1613+
inferredFromTarget(param, formal, calleeType, isErased, paramIndex).orElse(
1614+
if knownFormal then formal0
1615+
else errorType(AnonymousFunctionMissingParamType(param, tree, formal), param.srcPos)
16221616
)
1623-
cpy.ValDef(param)(tpt = paramTpt)
1624-
if isErased then param0.withAddedFlags(Flags.Erased) else param0
1617+
val paramTpt = untpd.TypedSplice(
1618+
(if knownFormal then InferredTypeTree() else untpd.TypeTree())
1619+
.withType(paramType.translateFromRepeated(toArray = false))
1620+
.withSpan(param.span.endPos)
1621+
)
1622+
val param0 = cpy.ValDef(param)(tpt = paramTpt)
1623+
if isErased then param0.withAddedFlags(Flags.Erased) else param0
16251624
desugared = desugar.makeClosure(inferredParams, fnBody, resultTpt, isContextual, tree.span)
16261625

16271626
typed(desugared, pt)

tests/pos/i18276a.scala

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import scala.language.implicitConversions
2+
3+
case class Assign(left: String, right: String)
4+
class SyntaxAnalyser extends ParsersBase {
5+
val x: Parser[String ~ String] = ???
6+
val y: Parser[Assign] = x.map(Assign.apply)
7+
}
8+
9+
class ParsersBase {
10+
trait ~[+T, +U]
11+
abstract class Parser[+T]:
12+
def map[U](f: T => U): Parser[U] = ???
13+
14+
given [A, B, X]: Conversion[(A, B) => X, (A ~ B) => X] = ???
15+
}

tests/pos/i18276b.scala

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import scala.language.implicitConversions
2+
3+
def foo(a: Int): Int = ???
4+
def bar(f: () => Int): Int = ???
5+
6+
given f: Conversion[Int => Int, () => Int] = ???
7+
8+
def test1: Int = bar(foo) // implicit conversion applied to foo
9+
def test2: Int = bar(f(foo))

0 commit comments

Comments
 (0)