Skip to content

Commit 20d2e2a

Browse files
committed
Disallow : after return
Treat instead indentation as significant. I.e. return 2 // one statement return println(2) // two statements
1 parent 1d07c11 commit 20d2e2a

File tree

4 files changed

+20
-9
lines changed

4 files changed

+20
-9
lines changed

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1971,7 +1971,6 @@ object Parsers {
19711971
atSpan(in.skipToken()) { Throw(expr()) }
19721972
case RETURN =>
19731973
atSpan(in.skipToken()) {
1974-
colonAtEOLOpt()
19751974
Return(if (isExprIntro) expr() else EmptyTree, EmptyTree)
19761975
}
19771976
case FOR =>
@@ -2240,10 +2239,6 @@ object Parsers {
22402239
case MACRO =>
22412240
val start = in.skipToken()
22422241
MacroTree(simpleExpr())
2243-
case COLONEOL =>
2244-
syntaxError("':' not allowed here")
2245-
in.nextToken()
2246-
simpleExpr()
22472242
case _ =>
22482243
if (isLiteral) literal()
22492244
else {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -401,8 +401,8 @@ object Scanners {
401401
true
402402
}
403403

404-
def isContinuingParens() =
405-
openParensTokens.contains(token)
404+
def isContinuing(lastToken: Token) =
405+
(openParensTokens.contains(token) || lastToken == RETURN)
406406
&& !pastBlankLine
407407
&& !migrateTo3
408408
&& !noindentSyntax
@@ -504,7 +504,7 @@ object Scanners {
504504
&& canEndStatTokens.contains(lastToken)
505505
&& canStartStatTokens.contains(token)
506506
&& !isLeadingInfixOperator()
507-
&& !(lastWidth < nextWidth && isContinuingParens())
507+
&& !(lastWidth < nextWidth && isContinuing(lastToken))
508508
then
509509
insert(if (pastBlankLine) NEWLINES else NEWLINE, lineOffset)
510510
else if indentIsSignificant then

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ object Tokens extends TokensCommon {
273273
final val closingRegionTokens = BitSet(RBRACE, RPAREN, RBRACKET, CASE) | statCtdTokens
274274

275275
final val canStartIndentTokens: BitSet =
276-
statCtdTokens | BitSet(COLONEOL, WITH, EQUALS, ARROW, CTXARROW, LARROW, WHILE, TRY, FOR, IF, THROW)
276+
statCtdTokens | BitSet(COLONEOL, WITH, EQUALS, ARROW, CTXARROW, LARROW, WHILE, TRY, FOR, IF, THROW, RETURN)
277277

278278
/** Faced with the choice between a type and a formal parameter, the following
279279
* tokens determine it's a formal parameter.

tests/run/returns.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def foo(x: Int): Int =
2+
if x == 1 then
3+
return
4+
x
5+
else
6+
2
7+
8+
def bar(): Unit =
9+
return
10+
assert(false)
11+
12+
@main def Test =
13+
assert(foo(1) == 1)
14+
bar()
15+
16+

0 commit comments

Comments
 (0)