Skip to content

Commit

Permalink
Backport Allow infix operators on their own line
Browse files Browse the repository at this point in the history
  • Loading branch information
som-snytt committed Apr 11, 2021
1 parent ee9f390 commit 86bca17
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/compiler/scala/tools/nsc/ast/parser/Scanners.scala
Original file line number Diff line number Diff line change
Expand Up @@ -438,11 +438,11 @@ trait Scanners extends ScannersCommon {
*/
def isLeadingInfixOperator =
allowLeadingInfixOperators &&
(token == BACKQUOTED_IDENT ||
token == IDENTIFIER && isOperatorPart(name.charAt(name.length - 1))) &&
(ch == ' ') && lookingAhead {
(token == BACKQUOTED_IDENT || token == IDENTIFIER && isOperatorPart(name.charAt(name.length - 1))) &&
ch <= ' ' && lookingAhead {
// force a NEWLINE after current token if it is on its own line
isSimpleExprIntroToken(token)
isSimpleExprIntroToken(token) ||
token == NEWLINE && { nextToken() ; isSimpleExprIntroToken(token) }
}

/* Insert NEWLINE or NEWLINES if
Expand Down
19 changes: 19 additions & 0 deletions test/files/pos/leading-infix-op.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

// scalac: -Xsource:3

trait T {
def f(x: Int): Boolean =
x < 0
||
x > 0
&&
x != 3

def g(x: Option[Int]) = x match {
case Some(err) =>
println("hi")
???
case None =>
???
}
}
5 changes: 3 additions & 2 deletions test/files/run/multiLineOps.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// scalac: -Xsource:3
//
// without backticks, "not found: value +"
// was: without backticks, "not found: value +" (but parsed here as +a * 6, where backticks fool the lexer)
// now: + is taken as "solo" infix op
//
object Test extends App {
val a = 7
val x = 1
+ //
`a` * 6

assert(x == 1)
assert(x == 1 + 42, x) // was: 1
}

0 comments on commit 86bca17

Please sign in to comment.