Skip to content

Commit 42409bf

Browse files
committed
Allow multi-variable pattern declarations
Allow multi-variable pattern declarations of the form val p1, ..., pn: T
1 parent fb5cb3c commit 42409bf

File tree

3 files changed

+19
-16
lines changed

3 files changed

+19
-16
lines changed

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

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,6 @@ object desugar {
4949
case None, Exhaustive, IrrefutablePatDef, IrrefutableGenFrom
5050
}
5151

52-
/** Info of a variable in a pattern: The named tree and its type */
53-
private type VarInfo = (NameTree, Tree)
54-
5552
/** Is `name` the name of a method that can be invalidated as a compiler-generated
5653
* case class method if it clashes with a user-defined method?
5754
*/
@@ -1685,16 +1682,6 @@ object desugar {
16851682
TypeDef(tpnme.REFINE_CLASS, impl).withFlags(Trait)
16861683
}
16871684

1688-
/** If tree is of the form `id` or `id: T`, return its name and type, otherwise return None.
1689-
*/
1690-
private object IdPattern {
1691-
def unapply(tree: Tree)(implicit ctx: Context): Option[VarInfo] = tree match {
1692-
case id: Ident if id.name != nme.WILDCARD => Some(id, TypeTree())
1693-
case Typed(id: Ident, tpt) => Some((id, tpt))
1694-
case _ => None
1695-
}
1696-
}
1697-
16981685
/** Returns list of all pattern variables, possibly with their types,
16991686
* without duplicates
17001687
*/

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,18 @@ trait UntypedTreeInfo extends TreeInfo[Untyped] { self: Trees.Instance[Untyped]
345345
def bodyKind(body: List[Tree])(implicit ctx: Context): FlagSet =
346346
body.foldLeft(NoInitsInterface)((fs, stat) => fs & defKind(stat))
347347

348+
/** Info of a variable in a pattern: The named tree and its type */
349+
type VarInfo = (NameTree, Tree)
350+
351+
/** An extractor for trees of the form `id` or `id: T` */
352+
object IdPattern {
353+
def unapply(tree: Tree)(implicit ctx: Context): Option[VarInfo] = tree match {
354+
case id: Ident if id.name != nme.WILDCARD => Some(id, TypeTree())
355+
case Typed(id: Ident, tpt) => Some((id, tpt))
356+
case _ => None
357+
}
358+
}
359+
348360
// todo: fill with other methods from TreeInfo that only apply to untpd.Tree's
349361
}
350362

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3071,12 +3071,16 @@ object Parsers {
30713071
}
30723072
else EmptyTree
30733073
lhs match {
3074-
case (id @ Ident(name: TermName)) :: Nil if name != nme.WILDCARD =>
3075-
val vdef = ValDef(name, tpt, rhs)
3074+
case IdPattern(id, t) :: Nil if t.isEmpty =>
3075+
val vdef = ValDef(id.name.asTermName, tpt, rhs)
30763076
if (isBackquoted(id)) vdef.pushAttachment(Backquoted, ())
30773077
finalizeDef(vdef, mods, start)
30783078
case _ =>
3079-
if rhs.isEmpty then
3079+
def isAllIds = lhs.forall {
3080+
case IdPattern(id, t) => t.isEmpty
3081+
case _ => false
3082+
}
3083+
if rhs.isEmpty && !isAllIds then
30803084
syntaxError(ExpectedTokenButFound(EQUALS, in.token), Span(in.lastOffset))
30813085
PatDef(mods, lhs, tpt, rhs)
30823086
}

0 commit comments

Comments
 (0)