Skip to content

Commit 68c762a

Browse files
committed
Allow infix operators on their own line
1 parent 869c8ee commit 68c762a

File tree

5 files changed

+26
-12
lines changed

5 files changed

+26
-12
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,14 +345,15 @@ object Scanners {
345345
allowLeadingInfixOperators
346346
&& ( token == BACKQUOTED_IDENT
347347
|| token == IDENTIFIER && isOperatorPart(name(name.length - 1)))
348-
&& ch == ' '
348+
&& ch <= ' '
349349
&& !pastBlankLine
350350
&& {
351351
val lookahead = LookaheadScanner()
352352
lookahead.allowLeadingInfixOperators = false
353353
// force a NEWLINE a after current token if it is on its own line
354354
lookahead.nextToken()
355355
canStartExprTokens.contains(lookahead.token)
356+
|| lookahead.token == NEWLINE && canStartExprTokens.contains(lookahead.next.token)
356357
}
357358
&& {
358359
if migrateTo3 then

docs/docs/reference/changed-features/operators.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ val str = "hello"
114114

115115
def condition =
116116
x > 0
117-
|| xs.exists(_ > 0)
117+
||
118+
xs.exists(_ > 0)
118119
|| xs.isEmpty
119120
```
120121
Previously, those expressions would have been rejected, since the compiler's semicolon inference
@@ -124,8 +125,8 @@ To make this syntax work, the rules are modified to not infer semicolons in fron
124125
A _leading infix operator_ is
125126
- a symbolic identifier such as `+`, or `approx_==`, or an identifier in backticks,
126127
- that starts a new line,
127-
- that precedes a token on the same line that can start an expression,
128-
- and that is immediately followed by at least one space character `' '`.
128+
- that precedes a token on the same or the next line that can start an expression,
129+
- and that is immediately followed by at least one whitespace character.
129130

130131
Example:
131132

docs/docs/reference/other-new-features/indentation.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,18 +159,17 @@ Indentation prefixes can consist of spaces and/or tabs. Indentation widths are t
159159

160160
### Indentation and Braces
161161

162-
Indentation can be mixed freely with braces and parentheses. For interpreting indentation inside braces and parentheses, the following rules apply.
162+
Indentation can be mixed freely with braces `{...}`, as well as brackets `[...]` and parentheses `(...)`. For interpreting indentation inside such regions, the following rules apply.
163163

164164
1. The assumed indentation width of a multiline region enclosed in braces is the
165165
indentation width of the first token that starts a new line after the opening brace.
166166

167-
2. The assumed indentation width of a multiline region inside parentheses is:
167+
2. The assumed indentation width of a multiline region inside brackets or parentheses is:
168168

169-
- if the opening parenthesis is at the end of a line, the indentation width of token following it,
169+
- if the opening bracket or parenthesis is at the end of a line, the indentation width of token following it,
170170
- otherwise, the indentation width of the enclosing region.
171171

172-
3. On encountering a closing brace `}` or parenthesis `)`, as many `<outdent>` tokens as necessary are
173-
inserted to close all open indentation regions inside the pair of braces or parentheses.
172+
3. On encountering a closing brace `}`, bracket `]` or parenthesis `)`, as many `<outdent>` tokens as necessary are inserted to close all open nested indentation regions.
174173

175174
For instance, consider:
176175
```scala
@@ -190,8 +189,7 @@ statement starting with `val`).
190189
parenthesis is not at the end of a line.
191190
- The indentation width of the region in parentheses around `y + 1` is 9
192191
(i.e. the indentation width of `y + 1`).
193-
- Finally, the indentation width of the last region in parentheses starting with `(x` is 6 (i.e. the indentation
194-
width of the indented region following the `=>`.
192+
- Finally, the indentation width of the last region in parentheses starting with `(x` is 6 (i.e. the indentation width of the indented region following the `=>`.
195193

196194
### Special Treatment of Case Clauses
197195

tests/pos/leading-infix-op.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
def f(x: Int): Boolean =
2+
x < 0
3+
||
4+
x > 0
5+
&&
6+
x != 3
7+
8+
def g(x: Option[Int]) = x match
9+
case Some(err) =>
10+
println("hi")
11+
???
12+
case None =>
13+
???

tests/run/i7031.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
@main def Test = {
22
val a = 5
33
val x = 1
4+
45
+ //
56
`a` * 6
67

7-
assert(x == 1)
8+
assert(x == 1, x)
89
}
910

0 commit comments

Comments
 (0)