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

Make overload pruning based on result types less aggressive #21744

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 7 additions & 1 deletion compiler/src/dotty/tools/dotc/inlines/Inlines.scala
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,13 @@ object Inlines:

/** Can a call to method `meth` be inlined? */
def isInlineable(meth: Symbol)(using Context): Boolean =
meth.is(Inline) && meth.hasAnnotation(defn.BodyAnnot) && !inInlineMethod
meth.is(Inline)
&& (meth.hasAnnotation(defn.BodyAnnot)
// Ensure `isInlineable` works with temporary symbols created during
// overloading resolution by `Applications#resolveMapped`.
// Testcase: tests/pos/i21410c.scala
|| meth.hasAnnotation(defn.MappedAlternativeAnnot))
Copy link
Member

Choose a reason for hiding this comment

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

Does being mapped imply hasAnnotation(defn.BodyAnnot)? Why not make the symbol copying in resolveMapped copy the annotations too? Or (and I'm not sure if this is possible) add a backreference from the MappedAlternativeAnnot symbol to the original?

&& !inInlineMethod

/** Should call be inlined in this context? */
def needsInlining(tree: Tree)(using Context): Boolean = tree match {
Expand Down
17 changes: 14 additions & 3 deletions compiler/src/dotty/tools/dotc/typer/Applications.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2076,16 +2076,27 @@ trait Applications extends Compatibility {
def resolveOverloaded(alts: List[TermRef], pt: Type)(using Context): List[TermRef] =
record("resolveOverloaded")

/** Is `alt` a method or polytype whose result type after the first value parameter
/** Is `alt` a method or polytype whose approximated result type after the first value parameter
* section conforms to the expected type `resultType`? If `resultType`
* is a `IgnoredProto`, pick the underlying type instead.
*
* Using an approximated result types is necessary to avoid false negatives
Copy link
Member

Choose a reason for hiding this comment

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

Your grammar as right the first time, either "approximated result types" or "an approximated result type"

* due to incomplete type inference such as in tests/pos/i21410.scala and tests/pos/i21410b.scala.
*/
def resultConforms(altSym: Symbol, altType: Type, resultType: Type)(using Context): Boolean =
resultType.revealIgnored match {
case resultType: ValueType =>
altType.widen match {
case tp: PolyType => resultConforms(altSym, instantiateWithTypeVars(tp), resultType)
case tp: MethodType => constrainResult(altSym, tp.resultType, resultType)
case tp: PolyType => resultConforms(altSym, tp.resultType, resultType)
case tp: MethodType =>
val wildRes = wildApprox(tp.resultType)
Copy link
Member

@dwijnand dwijnand Oct 28, 2024

Choose a reason for hiding this comment

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

It's :cough: wild that wildApprox/WildApproxMap is not an AvoidWildcardsMap or even an ApproximatingTypeMap... Running two maps seems like a lot, but I'm just prematurely optimising.


class ResultApprox extends AvoidWildcardsMap:
// Avoid false negatives by approximating to a lower bound
variance = -1

val approx = ResultApprox()(wildRes)
constrainResult(altSym, approx, resultType)
case _ => true
}
case _ => true
Expand Down
12 changes: 12 additions & 0 deletions tests/pos/i21410.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class A
object Test:
type F[X] <: Any = X match
case A => Int

def foo[T](x: String): T = ???
def foo[U](x: U): F[U] = ???

val x1 = foo(A())
val y: Int = x1

val x2: Int = foo(A()) // error
10 changes: 10 additions & 0 deletions tests/pos/i21410b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
object Test:
def foo[T](x: Option[T]): T = ???
def foo[T <: Tuple](x: T): Tuple.Map[T, List] = ???

val tup: (Int, String) = (1, "")

val x = foo(tup)
val y: (List[Int], List[String]) = x

val x2: (List[Int], List[String]) = foo(tup) // error
11 changes: 11 additions & 0 deletions tests/pos/i21410c.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class AppliedPIso[A, B]()
case class User(age: Int)

object Test:
extension [From, To](from: From)
def focus(): AppliedPIso[From, From] = ???
transparent inline def focus(inline lambda: (From => To)): Any = ???


val u = User(1)
val ap: AppliedPIso[User, User] = u.focus(_.age) // error