Skip to content

Commit

Permalink
Merge pull request #3716 from kitbellew/3713
Browse files Browse the repository at this point in the history
ScannerTokens: do not allow breaks within `for()`
  • Loading branch information
kitbellew committed May 19, 2024
2 parents ba4e4c8 + f5a7ef5 commit a015d53
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,6 @@ final class ScannerTokens(val tokens: Tokens)(implicit dialect: Dialect) {
case RegionTry :: xs
if !next.isAny[KwCatch, KwFinally] && isLeadingInfix != LeadingInfix.Yes => strip(xs)
case Nil | (_: CanProduceLF) :: _ => Some(rs)
case RegionParen :: RegionForParens :: _ => Some(rs)
case _ => None
}
val ok = lastNewlinePos >= 0 && mightStartStat(next, closeDelimOK = true) &&
Expand All @@ -657,7 +656,7 @@ final class ScannerTokens(val tokens: Tokens)(implicit dialect: Dialect) {

// https://dotty.epfl.ch/docs/reference/changed-features/operators.html#syntax-change-1
lazy val isLeadingInfix = sepRegionsOrig match {
case Nil | (_: CanProduceLF) :: _ | RegionParen :: RegionForParens :: _
case Nil | (_: CanProduceLF) :: _
if !newlines && lastNewlinePos >= 0 && dialect.allowInfixOperatorAfterNL &&
next.isSymbolicInfixOperator => isLeadingInfixArg(nextPos + 1, nextIndent)
case _ => LeadingInfix.No
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1341,16 +1341,10 @@ class TermSuite extends ParseSuite {
| if a === b
|) yield a
|""".stripMargin
val layout = "for (a <- fooa; b <- foob; if a === b) yield a"
val tree = Term.ForYield(
List(
Enumerator.Generator(Pat.Var(tname("a")), tname("fooa")),
Enumerator.Generator(Pat.Var(tname("b")), tname("foob")),
Enumerator.Guard(Term.ApplyInfix(tname("a"), tname("==="), Nil, List(tname("b"))))
),
tname("a")
)
runTestAssert[Term](code, layout)(tree)
val error = """|<input>:3: error: `)` expected but `<-` found
| b <- foob
| ^""".stripMargin
runTestError[Term](code, error)
}

test("scalafmt #3911 for in parens, NL after `(`, `;` between") {
Expand All @@ -1377,16 +1371,10 @@ class TermSuite extends ParseSuite {
| b <- foob
| if a === b) yield a
|""".stripMargin
val layout = "for (a <- fooa; b <- foob; if a === b) yield a"
val tree = Term.ForYield(
List(
Enumerator.Generator(Pat.Var(tname("a")), tname("fooa")),
Enumerator.Generator(Pat.Var(tname("b")), tname("foob")),
Enumerator.Guard(Term.ApplyInfix(tname("a"), tname("==="), Nil, List(tname("b"))))
),
tname("a")
)
runTestAssert[Term](code, layout)(tree)
val error = """|<input>:2: error: `)` expected but `<-` found
| b <- foob
| ^""".stripMargin
runTestError[Term](code, error)
}

test("scalafmt #3911 for in parens, no NL after `(`, `;` between") {
Expand Down Expand Up @@ -1436,4 +1424,27 @@ class TermSuite extends ParseSuite {
runTestAssert[Term](code, layout)(tree)
}

test("#3713 cond in parens within enums") {
val code = """|for (m <- decls
| if oneCond
| && (cond)
| && satisfiable) {}
|
|""".stripMargin
val layout = "for (m <- decls; if oneCond && cond && satisfiable) {}"
val tree = Term.For(
List(
Enumerator.Generator(Pat.Var(tname("m")), tname("decls")),
Enumerator.Guard(Term.ApplyInfix(
Term.ApplyInfix(tname("oneCond"), tname("&&"), Nil, List(tname("cond"))),
tname("&&"),
Nil,
List(tname("satisfiable"))
))
),
blk()
)
runTestAssert[Stat](code, layout)(tree)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -1148,7 +1148,7 @@ class ControlSyntaxSuite extends BaseDottySuite {
))
}

test("#3713 code in parens within enums") {
test("#3713 cond in parens within enums") {
val code = """|for (m <- decls
| if oneCond
| && (cond)
Expand All @@ -1158,20 +1158,15 @@ class ControlSyntaxSuite extends BaseDottySuite {
val layout = "for (m <- decls; if oneCond && cond && satisfiable) {}"
val tree = Term.For(
List(
Enumerator.Generator(Pat.Var(Term.Name("m")), Term.Name("decls")),
Enumerator.Generator(Pat.Var(tname("m")), tname("decls")),
Enumerator.Guard(Term.ApplyInfix(
Term.ApplyInfix(
Term.Name("oneCond"),
Term.Name("&&"),
Type.ArgClause(Nil),
Term.ArgClause(List(Term.Name("cond")), None)
),
Term.Name("&&"),
Type.ArgClause(Nil),
Term.ArgClause(List(Term.Name("satisfiable")), None)
Term.ApplyInfix(tname("oneCond"), tname("&&"), Nil, List(tname("cond"))),
tname("&&"),
Nil,
List(tname("satisfiable"))
))
),
Term.Block(Nil)
blk()
)
runTestAssert[Stat](code, layout)(tree)
}
Expand Down

0 comments on commit a015d53

Please sign in to comment.