Skip to content

Better disambiguation of leading operators #11376

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions compiler/src/dotty/tools/dotc/parsing/Scanners.scala
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ object Scanners {

/** Is current token first one after a newline? */
def isAfterLineEnd: Boolean = lineOffset >= 0

def isOperator =
token == IDENTIFIER && isOperatorPart(name(name.length - 1))
}

abstract class ScannerCommon(source: SourceFile)(using Context) extends CharArrayReader with TokenData {
Expand Down Expand Up @@ -348,12 +351,27 @@ object Scanners {
&& (isWhitespace(ch) || ch == LF)
&& !pastBlankLine
&& {
// Is current lexeme assumed to start an expression?
// This is the case if the lexime is one of the tokens that
// starts an expression. Furthermore, if the previous token is
// in backticks, the lexeme may not be a binary operator.
// I.e. in
//
// a
// `x` += 1
//
// `+=` is not assumed to start an expression since it follows an identifier
// in backticks and is a binary operator. Hence, `x` is not classified as a
// leading infix operator.
def assumeStartsExpr(lexeme: TokenData) =
canStartExprTokens.contains(lexeme.token)
&& (token != BACKQUOTED_IDENT || !lexeme.isOperator || nme.raw.isUnary(lexeme.name))
val lookahead = LookaheadScanner()
lookahead.allowLeadingInfixOperators = false
// force a NEWLINE a after current token if it is on its own line
lookahead.nextToken()
canStartExprTokens.contains(lookahead.token)
|| lookahead.token == NEWLINE && canStartExprTokens.contains(lookahead.next.token)
assumeStartsExpr(lookahead)
|| lookahead.token == NEWLINE && assumeStartsExpr(lookahead.next)
}
&& {
if migrateTo3 then
Expand Down
19 changes: 19 additions & 0 deletions tests/pos/i11371.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
object HelloWorld {
def whileLoop: Int = {
var i = 0
var acc = 0
while (i < 3) {
var `i'` = 0
while (`i'` < 4) {
acc += (i * `i'`)
`i'` += 1
}
i += 1
}
acc
}

def main(args: Array[String]): Unit = {
println(s"hello world: ${whileLoop}")
}
}
3 changes: 1 addition & 2 deletions tests/run/i7031.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
val a = 5
val x = 1

+ //
`a` * 6
Copy link

@catap catap Feb 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey,

Are you sure that it is related to this issue?

I feel that it introduces another one similar to this: #7031 / #7024

Copy link
Contributor Author

@odersky odersky Feb 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This issue is interesting. It worked somewhat by accident before. First, if the backticked "`a`" gets changed to "a" this never compiles. We do not skip a newline after a prefix operator, so we have a prefix operator + with nothing that follows it --> error. What happened previously was that the "`a`" was interpreted as a leading infix operator, so the lexical analyzer saw a noun +, followed by an operator "`a`", and did not insert a new line. Then the parser made different sense of it and reclassified + to a prefix operator and "`a`" to its operand. But now we do not interpret "`a`" as a leading operator anymore, so we get an error (as we should).

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see.

I've also plaid a bit this with scala 2 and can't compile it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha. I stared at that test while bringing -Xsource:3 into better alignment. Thanks!

+ `a` * 6

assert(x == 1, x)
}
Expand Down