Skip to content

Fix #5191: Don't lift the receiver of a call to a method with varargs #5192

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

Merged
merged 1 commit into from
Oct 2, 2018
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
24 changes: 16 additions & 8 deletions compiler/src/dotty/tools/dotc/typer/Applications.scala
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,23 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>

/** Whether `liftFun` is needed? It is the case if default arguments are used.
*/
protected def needLiftFun: Boolean =
protected def needLiftFun: Boolean = {
def requiredArgNum(tp: Type): Int = tp.widen match {
case funType: MethodType =>
val paramInfos = funType.paramInfos
val argsNum = paramInfos.size
if (argsNum >= 1 && paramInfos.last.isRepeatedParam)
// Repeated arguments do not count as required arguments
argsNum - 1
else
argsNum
case funType: PolyType => requiredArgNum(funType.resultType)
case tp => args.size
}

!isJavaAnnotConstr(methRef.symbol) &&
args.size < reqiredArgNum(funType)
args.size < requiredArgNum(funType)
}

/** A flag signalling that the typechecking the application was so far successful */
private[this] var _ok = true
Expand All @@ -250,12 +264,6 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
case tp => tp //was: funType
}

def reqiredArgNum(tp: Type): Int = tp.widen match {
case funType: MethodType => funType.paramInfos.size
case funType: PolyType => reqiredArgNum(funType.resultType)
case tp => args.size
}

lazy val liftedFunType =
if (needLiftFun) {
liftFun()
Expand Down
25 changes: 25 additions & 0 deletions compiler/test/dotty/tools/backend/jvm/DottyBytecodeTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -475,4 +475,29 @@ class TestBCode extends DottyBytecodeTest {
diffInstructions(testInstructions, refInstructions))
}
}

/** Test that the receiver of a call to a method with varargs is not unnecessarily lifted */
@Test def i5191 = {
val source =
"""class Test {
| def foo(args: String*): String = ""
| def self = this
|
| def test = self.foo()
|}
""".stripMargin

checkBCode(source) { dir =>
val clsIn = dir.lookupName("Test.class", directory = false).input
val clsNode = loadClassNode(clsIn)
val method = getMethod(clsNode, "test")

val liftReceiver = instructionsFromMethod(method).exists {
case VarOp(Opcodes.ASTORE, _) => true // receiver lifted in local val
case _ => false
}
assertFalse("Receiver of a call to a method with varargs is unnecessarily lifted",
liftReceiver)
}
}
}