Skip to content

Commit e75a527

Browse files
committed
Better disambiguation of leading operators
Classify something as a leading operator only if it is not followed in turn by an operator. This means that ```scala acc += (i * `i'`) `i'` += 1 ``` the `i'` is no longer classified as a leading operator.
1 parent 5b46900 commit e75a527

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,9 @@ object Scanners {
335335
this.token = token
336336
}
337337

338+
def isOperator =
339+
token == IDENTIFIER && isOperatorPart(name(name.length - 1))
340+
338341
/** A leading symbolic or backquoted identifier is treated as an infix operator if
339342
* - it does not follow a blank line, and
340343
* - it is followed by at least one whitespace character and a
@@ -343,17 +346,17 @@ object Scanners {
343346
*/
344347
def isLeadingInfixOperator(inConditional: Boolean = true) =
345348
allowLeadingInfixOperators
346-
&& ( token == BACKQUOTED_IDENT
347-
|| token == IDENTIFIER && isOperatorPart(name(name.length - 1)))
349+
&& (token == BACKQUOTED_IDENT || isOperator)
348350
&& (isWhitespace(ch) || ch == LF)
349351
&& !pastBlankLine
350352
&& {
351353
val lookahead = LookaheadScanner()
352354
lookahead.allowLeadingInfixOperators = false
353355
// force a NEWLINE a after current token if it is on its own line
354356
lookahead.nextToken()
355-
canStartExprTokens.contains(lookahead.token)
356-
|| lookahead.token == NEWLINE && canStartExprTokens.contains(lookahead.next.token)
357+
(canStartExprTokens.contains(lookahead.token)
358+
|| lookahead.token == NEWLINE && canStartExprTokens.contains(lookahead.next.token)
359+
) && !lookahead.isOperator
357360
}
358361
&& {
359362
if migrateTo3 then

tests/pos/i11371.scala

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
object HelloWorld {
2+
def whileLoop: Int = {
3+
var i = 0
4+
var acc = 0
5+
while (i < 3) {
6+
var `i'` = 0
7+
while (`i'` < 4) {
8+
acc += (i * `i'`)
9+
`i'` += 1
10+
}
11+
i += 1
12+
}
13+
acc
14+
}
15+
16+
def main(args: Array[String]): Unit = {
17+
println(s"hello world: ${whileLoop}")
18+
}
19+
}

0 commit comments

Comments
 (0)