Skip to content

Commit 754fa51

Browse files
Merge pull request #7370 from dotty-staging/check-deferred-patdef
Flag pattern definitions with empty RHS as errors
2 parents d67af24 + 24ace94 commit 754fa51

File tree

4 files changed

+24
-15
lines changed

4 files changed

+24
-15
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: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3052,11 +3052,17 @@ object Parsers {
30523052
}
30533053
else EmptyTree
30543054
lhs match {
3055-
case (id @ Ident(name: TermName)) :: Nil if name != nme.WILDCARD =>
3056-
val vdef = ValDef(name, tpt, rhs)
3055+
case IdPattern(id, t) :: Nil if t.isEmpty =>
3056+
val vdef = ValDef(id.name.asTermName, tpt, rhs)
30573057
if (isBackquoted(id)) vdef.pushAttachment(Backquoted, ())
30583058
finalizeDef(vdef, mods, start)
30593059
case _ =>
3060+
def isAllIds = lhs.forall {
3061+
case IdPattern(id, t) => t.isEmpty
3062+
case _ => false
3063+
}
3064+
if rhs.isEmpty && !isAllIds then
3065+
syntaxError(ExpectedTokenButFound(EQUALS, in.token), Span(in.lastOffset))
30603066
PatDef(mods, lhs, tpt, rhs)
30613067
}
30623068
}

tests/neg/deferred-patdef.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
object M {
2+
val (x, y): (Int, Int) // error: `=` expected
3+
val 1: Int // error: `=` expected
4+
}

0 commit comments

Comments
 (0)