Skip to content
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
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/ast/Desugar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1974,8 +1974,8 @@ object desugar {
if (isGenericTuple) Apply(Select(refOfDef(param), nme.apply), Literal(Constant(n)))
else Select(refOfDef(param), nme.selectorName(n))
val vdefs =
params.zipWithIndex.map {
case (param, idx) =>
params.zipWithIndex.collect {
case (param, idx) if param.name != nme.WILDCARD =>
ValDef(param.name, param.tpt, selector(idx))
.withSpan(param.span)
.withAttachment(UntupledParam, ())
Expand Down
41 changes: 41 additions & 0 deletions compiler/test/dotty/tools/backend/jvm/DottyBytecodeTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2009,6 +2009,47 @@ class DottyBytecodeTests extends DottyBytecodeTest {
assertEquals(expected, clsNode.interfaces.asScala)
}
}

// Test for https://github.com/scala/scala3/issues/24997
// Automatic untupling should not introduce extra CHECKCAST instructions for unused tuple elements
@Test def i24997 = {
val source =
"""class Wrapper[A](value: A):
| def use[B](f: A => B): B = f(value)
|
|class Test:
| def withCase: Int =
| val w = Wrapper((42, "Answer"))
| w.use { case (number, _) => number }
|
| def withoutCase: Int =
| val w = Wrapper((42, "Answer"))
| w.use { (number, _) => number }
|""".stripMargin

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

// Get instructions for both methods
def getCheckCastCount(methodPrefix: String): Int = {
clsNode.methods.asScala.filter(_.name.startsWith(methodPrefix)).map { method =>
instructionsFromMethod(method).count {
case TypeOp(Opcodes.CHECKCAST, _) => true
case _ => false
}
}.sum
}

val withCaseCasts = getCheckCastCount("withCase")
val withoutCaseCasts = getCheckCastCount("withoutCase")

// Both methods should have the same number of CHECKCAST instructions
// (specifically, they should NOT have extra ones for unused wildcard elements)
assertEquals(s"withCase should have 0 CHECKCASTs", 0, withCaseCasts)
assertEquals(s"withoutCase should have 1 CHECKCAST", 1, withoutCaseCasts)
}
}
}

object invocationReceiversTestCode {
Expand Down