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

Do not compute protoFormal if param.tpt is empty #18288

Merged
merged 1 commit into from
Jul 26, 2023
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
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 @@ -1606,32 +1606,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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line moved into the else branch. The rest should not have changed.

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, inferredType = formal, expectedType = pt), 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, inferredType = formal, expectedType = pt), param.srcPos)
)
cpy.ValDef(param)(tpt = paramTpt)
if isErased then param0.withAddedFlags(Flags.Erased) else param0
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this was moved into the else branch asl well.

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(Nil, inferredParams, fnBody, resultTpt, 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))