Skip to content

Refine parameter adaptation logic for arrays #23591

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

Open
wants to merge 1 commit 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
9 changes: 7 additions & 2 deletions compiler/src/dotty/tools/dotc/transform/Erasure.scala
Original file line number Diff line number Diff line change
Expand Up @@ -483,8 +483,13 @@ object Erasure {
def sameClass(tp1: Type, tp2: Type) = tp1.classSymbol == tp2.classSymbol

val paramAdaptationNeeded =
implParamTypes.lazyZip(samParamTypes).exists((implType, samType) =>
!sameClass(implType, samType) && !autoAdaptedParam(implType))
implParamTypes.lazyZip(samParamTypes).exists: (implType, samType) =>
!sameClass(implType, samType) && !autoAdaptedParam(implType)
|| (samType, implType).match {
case (defn.ArrayOf(_), defn.ArrayOf(_)) => false
case (defn.ArrayOf(_), _) => true // see #23179
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't there be the reverse order as well ((_, defn.ArrayOf(_)))? Like if the SAM takes an Array[T] where T <: AnyRef but the lambda is specialized to T = String, for example?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm unable to construct such a reversed example that will be accepted by the compiler.

Copy link
Member

Choose a reason for hiding this comment

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

Do we need something similar for the result type (resultAdaptationNeeded)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure, I also fail here to construct an example that would cause a run time crash (which is of course weak evidence).

case _ => false
}
val resultAdaptationNeeded =
!sameClass(implResultType, samResultType) && !autoAdaptedResult

Expand Down
8 changes: 8 additions & 0 deletions tests/run/i23179.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
object Test {
trait A { def f(a: Array[AnyRef]): Any }
def g(a: A) = a.f(Array.empty[AnyRef])

def main(args: Array[String]): Unit = {
g((x: Array[? >: AnyRef]) => x.headOption)
}
}
Loading