Skip to content

Commit 6e23a94

Browse files
committed
Fix #1975: Align valdefs and for expressions for patterns
val definitions and for expressions both distinguish whether something is a pattern or a variable binding. They no do it the same way: `ident` or an `ident: type` is a variable binding, everything else is a pattern. Previously, capitalized idents were considered as bindings in valdefs but as pattern in fors.
1 parent 47901c0 commit 6e23a94

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ object desugar {
559559
* ValDef or DefDef.
560560
*/
561561
def makePatDef(original: Tree, mods: Modifiers, pat: Tree, rhs: Tree)(implicit ctx: Context): Tree = pat match {
562-
case VarPattern(named, tpt) =>
562+
case IdPattern(named, tpt) =>
563563
derivedValDef(original, named, tpt, rhs, mods)
564564
case _ =>
565565
val rhsUnchecked = makeAnnotated("scala.unchecked", rhs)
@@ -816,7 +816,7 @@ object desugar {
816816
* Otherwise this gives { case pat => body }
817817
*/
818818
def makeLambda(pat: Tree, body: Tree): Tree = pat match {
819-
case VarPattern(named, tpt) =>
819+
case IdPattern(named, tpt) =>
820820
Function(derivedValDef(pat, named, tpt, EmptyTree, Modifiers(Param)) :: Nil, body)
821821
case _ =>
822822
makeCaseLambda(CaseDef(pat, EmptyTree, body) :: Nil, unchecked = false)
@@ -898,7 +898,9 @@ object desugar {
898898
}
899899

900900
def isIrrefutableGenFrom(gen: GenFrom): Boolean =
901-
gen.isInstanceOf[IrrefutableGenFrom] || isIrrefutable(gen.pat, gen.expr)
901+
gen.isInstanceOf[IrrefutableGenFrom] ||
902+
IdPattern.unapply(gen.pat).isDefined ||
903+
isIrrefutable(gen.pat, gen.expr)
902904

903905
/** rhs.name with a pattern filter on rhs unless `pat` is irrefutable when
904906
* matched against `rhs`.
@@ -1062,9 +1064,9 @@ object desugar {
10621064
TypeDef(tpnme.REFINE_CLASS, impl).withFlags(Trait)
10631065
}
10641066

1065-
/** If tree is a variable pattern, return its name and type, otherwise return None.
1067+
/** If tree is of the form `id` or `id: T`, return its name and type, otherwise return None.
10661068
*/
1067-
private object VarPattern {
1069+
private object IdPattern {
10681070
def unapply(tree: Tree)(implicit ctx: Context): Option[VarInfo] = tree match {
10691071
case id: Ident => Some(id, TypeTree())
10701072
case Typed(id: Ident, tpt) => Some((id, tpt))

tests/pos/i1975.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
object Test {
2+
val X = Seq(1, 2)
3+
4+
for (X <- Seq(3, 4)) yield println(X)
5+
}

0 commit comments

Comments
 (0)