Skip to content

Commit d884df3

Browse files
committed
Remove typed vars logic
1 parent a3cf927 commit d884df3

File tree

3 files changed

+13
-38
lines changed

3 files changed

+13
-38
lines changed

compiler/src/dotty/tools/dotc/ast/Desugar.scala

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,15 +1450,13 @@ object desugar {
14501450
sel
14511451
end match
14521452

1453-
case class TuplePatternInfo(arity: Int, varNum: Int, wildcardNum: Int, typedVarNum: Int, typedWildcardNum: Int)
1453+
case class TuplePatternInfo(arity: Int, varNum: Int, wildcardNum: Int)
14541454
object TuplePatternInfo:
14551455
def apply(pat: Tree)(using Context): TuplePatternInfo = pat match
14561456
case Tuple(pats) =>
14571457
var arity = 0
14581458
var varNum = 0
14591459
var wildcardNum = 0
1460-
var typedVarNum = 0
1461-
var typedWildcardNum = 0
14621460
pats.foreach: p =>
14631461
arity += 1
14641462
p match
@@ -1467,15 +1465,10 @@ object desugar {
14671465
varNum += 1
14681466
if id.name == nme.WILDCARD then
14691467
wildcardNum += 1
1470-
case Typed(id: Ident, _) if !isBackquoted(id) =>
1471-
if id.name.isVarPattern then
1472-
typedVarNum += 1
1473-
if id.name == nme.WILDCARD then
1474-
typedWildcardNum += 1
14751468
case _ =>
1476-
TuplePatternInfo(arity, varNum, wildcardNum, typedVarNum, typedWildcardNum)
1469+
TuplePatternInfo(arity, varNum, wildcardNum)
14771470
case _ =>
1478-
TuplePatternInfo(-1, -1, -1, -1, -1)
1471+
TuplePatternInfo(-1, -1, -1)
14791472
end TuplePatternInfo
14801473

14811474
/** If `pat` is a variable pattern,
@@ -1514,7 +1507,7 @@ object desugar {
15141507
val tuplePatternInfo = TuplePatternInfo(pat)
15151508

15161509
// When desugaring a PatDef in general, we use pattern matching on the rhs
1517-
// and collect the variable values in a tuple, then outside the match
1510+
// and collect the variable values in a tuple, then outside the match,
15181511
// we destructure the tuple to get the individual variables.
15191512
// We can achieve two kinds of tuple optimizations if the pattern is a tuple
15201513
// of simple variables or wildcards:
@@ -1524,8 +1517,8 @@ object desugar {
15241517
// For example: `val (x, y) = if ... then (1, "a") else (2, "b")` becomes
15251518
// `val $1$ = if ...; val x = $1$._1; val y = $1$._2`.
15261519
// 2. Partial optimization:
1527-
// If the rhs can be typed as a tuple and matched with correct arity,
1528-
// we can return the tuple itself if there are no more than one variable
1520+
// If the rhs can be typed as a tuple and matched with correct arity, we can
1521+
// return the tuple itself in the case if there are no more than one variable
15291522
// in the pattern, or return the the value if there is only one variable.
15301523

15311524
val fullTupleOptimizable =
@@ -1539,10 +1532,10 @@ object desugar {
15391532

15401533
val partialTupleOptimizable =
15411534
tuplePatternInfo.arity > 0
1542-
&& tuplePatternInfo.arity == tuplePatternInfo.varNum + tuplePatternInfo.typedVarNum
1535+
&& tuplePatternInfo.arity == tuplePatternInfo.varNum
15431536
// We exclude the case where there is only one variable,
15441537
// because it should be handled by `makeTuple` directly.
1545-
&& tuplePatternInfo.wildcardNum + tuplePatternInfo.typedWildcardNum < tuplePatternInfo.arity - 1
1538+
&& tuplePatternInfo.wildcardNum < tuplePatternInfo.arity - 1
15461539

15471540
val inAliasGenerator = original match
15481541
case _: GenAlias => true
@@ -1551,10 +1544,7 @@ object desugar {
15511544
val vars: List[VarInfo] =
15521545
if fullTupleOptimizable || partialTupleOptimizable then // include `_`
15531546
pat match
1554-
case Tuple(pats) => pats.map {
1555-
case id: Ident => (id, TypeTree())
1556-
case Typed(id: Ident, tpt) => (id, tpt)
1557-
}
1547+
case Tuple(pats) => pats.map { case id: Ident => (id, TypeTree()) }
15581548
else
15591549
getVariables(
15601550
tree = pat,
@@ -1578,10 +1568,7 @@ object desugar {
15781568
// Replace all variables with wildcards in the pattern
15791569
val pat1 = pat match
15801570
case Tuple(pats) =>
1581-
val wildcardPats = pats.map {
1582-
case id: Ident => Ident(nme.WILDCARD).withSpan(id.span)
1583-
case p @ Typed(_: Ident, tpt) => Typed(Ident(nme.WILDCARD), tpt).withSpan(p.span)
1584-
}
1571+
val wildcardPats = pats.map(p => Ident(nme.WILDCARD).withSpan(p.span))
15851572
Tuple(wildcardPats).withSpan(pat.span)
15861573
CaseDef(
15871574
Bind(tmpTuple, pat1),
@@ -1591,8 +1578,6 @@ object desugar {
15911578
else CaseDef(pat, EmptyTree, makeTuple(ids).withAttachment(ForArtifact, ()))
15921579
Match(makeSelector(rhs, MatchCheck.IrrefutablePatDef), caseDef :: Nil)
15931580

1594-
// println(i"matchExpr = $matchExpr")
1595-
15961581
vars match {
15971582
case Nil if !mods.is(Lazy) =>
15981583
matchExpr

compiler/src/dotty/tools/dotc/ast/TreeInfo.scala

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -207,17 +207,6 @@ trait TreeInfo[T <: Untyped] { self: Trees.Instance[T] =>
207207
case _ => false
208208
}
209209

210-
/** Is tree a wildcard pattern? Not including `x @ _` */
211-
def isWildcardPattern(pat: Tree): Boolean = unsplice(pat) match {
212-
case x: Ident => x.name == nme.WILDCARD && !isBackquoted(x)
213-
case _ => false
214-
}
215-
216-
def isTypedVarPattern(pat: Tree): Boolean = unsplice(pat) match {
217-
case Typed(id: Ident, _) if id.name.isVarPattern && !isBackquoted(id) => true
218-
case _ => false
219-
}
220-
221210
/** The first constructor definition in `stats` */
222211
def firstConstructor(stats: List[Tree]): Tree = stats match {
223212
case (meth: DefDef) :: _ if meth.name.isConstructorName => meth
@@ -417,8 +406,6 @@ trait TreeInfo[T <: Untyped] { self: Trees.Instance[T] =>
417406
tree.tpe.isInstanceOf[ThisType]
418407
}
419408

420-
421-
422409
/** Under x.modularity: Extractor for `annotation.internal.WitnessNames(name_1, ..., name_n)`
423410
* represented as an untyped or typed tree.
424411
*/

tests/pos/simple-tuple-extract.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ class Test:
1717
// val c: AnyRef = $2$._3
1818
a + b.length() + c.toString.length()
1919

20+
// This pattern will not be optimized:
21+
// val (a1, b1, c1: String) = f1
22+
2023
def test2 =
2124
val (_, b, c) = f1
2225
b.length() + c.toString.length()

0 commit comments

Comments
 (0)