Skip to content

Fix #4935: make desugaring optimization sound #6309

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 2 commits into from
Apr 16, 2019
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
28 changes: 23 additions & 5 deletions compiler/src/dotty/tools/dotc/ast/Desugar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -913,16 +913,34 @@ object desugar {
case IdPattern(named, tpt) =>
derivedValDef(original, named, tpt, rhs, mods)
case _ =>
val rhsUnchecked = makeAnnotated("scala.unchecked", rhs)
val vars = getVariables(pat)
def isTuplePattern(arity: Int): Boolean = pat match {
case Tuple(pats) if pats.size == arity =>
pats.forall(isVarPattern)
case _ => false
}
val isMatchingTuple: Tree => Boolean = {
case Tuple(es) => es.length == vars.length
case Tuple(es) => isTuplePattern(es.length)
case _ => false
}

// We can only optimize `val pat = if (...) e1 else e2` if:
// - `e1` and `e2` are both tuples of arity N
// - `pat` is a tuple of N variables or wildcard patterns like `(x1, x2, ..., xN)`
val tupleOptimizable = forallResults(rhs, isMatchingTuple)

def rhsUnchecked = makeAnnotated("scala.unchecked", rhs)
val vars =
if (tupleOptimizable) // include `_`
pat match {
case Tuple(pats) =>
pats.map { case id: Ident => id -> TypeTree() }
}
else getVariables(pat) // no `_`

val ids = for ((named, _) <- vars) yield Ident(named.name)
val caseDef = CaseDef(pat, EmptyTree, makeTuple(ids))
val matchExpr =
if (forallResults(rhs, isMatchingTuple)) rhs
if (tupleOptimizable) rhs
else Match(rhsUnchecked, caseDef :: Nil)
vars match {
case Nil =>
Expand All @@ -938,7 +956,7 @@ object desugar {
.withSpan(pat.span.union(rhs.span)).withMods(patMods)
def selector(n: Int) = Select(Ident(tmpName), nme.selectorName(n))
val restDefs =
for (((named, tpt), n) <- vars.zipWithIndex)
for (((named, tpt), n) <- vars.zipWithIndex if named.name != nme.WILDCARD)
yield
if (mods is Lazy) derivedDefDef(original, named, tpt, selector(n), mods &~ Lazy)
else derivedValDef(original, named, tpt, selector(n), mods)
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2405,7 +2405,7 @@ object Parsers {
}
makeConstructor(Nil, vparamss, rhs).withMods(mods).setComment(in.getDocComment(start))
} else {
val (leadingParamss: List[List[ValDef]], flags: FlagSet) =
val (leadingParamss, flags) =
if (in.token == LPAREN)
(paramClause(prefix = true) :: Nil, Method | Extension)
else
Expand Down
3 changes: 3 additions & 0 deletions tests/neg/i4935.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
object Foo {
val (A, B) = () // error // error
}
5 changes: 5 additions & 0 deletions tests/run/i4935.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
object Test {
val (Some(a), b) = (Some(3), 6)

def main(args: Array[String]) = assert(a == 3)
}
5 changes: 5 additions & 0 deletions tests/run/i4935b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
object Test {
val (a, _, b) = (Some(3), 6, Some(9))

def main(args: Array[String]) = assert(a == Some(3) && b == Some(9))
}