Skip to content

Commit d47e36d

Browse files
committed
Revert to Scala 2 rules for handling opening brace on next line
Fixes #16760
1 parent d63e572 commit d47e36d

File tree

8 files changed

+26
-28
lines changed

8 files changed

+26
-28
lines changed

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

-9
Original file line numberDiff line numberDiff line change
@@ -1353,15 +1353,6 @@ object Parsers {
13531353

13541354
def argumentStart(): Unit =
13551355
colonAtEOLOpt()
1356-
if migrateTo3 && in.token == NEWLINE && in.next.token == LBRACE then
1357-
in.nextToken()
1358-
if in.indentWidth(in.offset) == in.currentRegion.indentWidth then
1359-
report.errorOrMigrationWarning(
1360-
em"""This opening brace will start a new statement in Scala 3.
1361-
|It needs to be indented to the right to keep being treated as
1362-
|an argument to the previous expression.${rewriteNotice()}""",
1363-
in.sourcePos(), from = `3.0`)
1364-
patch(source, Span(in.offset), " ")
13651356

13661357
def possibleTemplateStart(isNew: Boolean = false): Unit =
13671358
in.observeColonEOL(inTemplate = true)

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

+10-9
Original file line numberDiff line numberDiff line change
@@ -566,19 +566,20 @@ object Scanners {
566566
* - current line starts with a token that can start a statement
567567
* - current line does not start with a leading infix operator
568568
* The answer is different for Scala-2 and Scala-3.
569-
* - In Scala 2: Only `{` is treated as continuing, irrespective of indentation.
570-
* But this is in fact handled by Parser.argumentStart which skips a NEWLINE,
571-
* so we always assume false here.
572-
* - In Scala 3: Only indented statements are treated as continuing, as long as
573-
* they start with `(`, `[` or `{`, or the last statement ends in a `return`.
569+
* - In Scala 2: Only `{` is treated as continuing, irrespective of indentation,
570+
* but not if it follows a `}`.
571+
* - In Scala 3: In addition, indented statements are treated as continuing, as long as
572+
* they start with `(`, or `[`, or the last statement ends in a `return`.
574573
* The Scala 2 rules apply under source `3.0-migration` or under `-no-indent`.
575574
*/
576575
inline def isContinuing =
577-
lastWidth < nextWidth
578-
&& (openParensTokens.contains(token) || lastToken == RETURN)
576+
( token == LBRACE && lastToken != RBRACE
577+
|| lastWidth < nextWidth
578+
&& (token == LPAREN || token == LBRACKET || lastToken == RETURN)
579+
&& !migrateTo3
580+
&& !noindentSyntax
581+
)
579582
&& !pastBlankLine
580-
&& !migrateTo3
581-
&& !noindentSyntax
582583

583584
currentRegion match
584585
case r: Indented =>

compiler/src/dotty/tools/dotc/reporting/trace.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ trait TraceSyntax:
5555
apply(question, printer, {
5656
val showOp: T => String = inline if show == true then
5757
val showT = summonInline[Show[T]]
58-
{
58+
locally {
5959
given Show[T] = showT
6060
t => i"$t"
6161
}

compiler/test/dotty/tools/dotc/CompilationTests.scala

-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,6 @@ class CompilationTests {
149149
compileFile("tests/neg-custom-args/i3246.scala", scala2CompatMode),
150150
compileFile("tests/neg-custom-args/overrideClass.scala", scala2CompatMode),
151151
compileFile("tests/neg-custom-args/ovlazy.scala", scala2CompatMode.and("-Xfatal-warnings")),
152-
compileFile("tests/neg-custom-args/newline-braces.scala", scala2CompatMode.and("-Xfatal-warnings")),
153152
compileFile("tests/neg-custom-args/autoTuplingTest.scala", defaultOptions.andLanguageFeature("noAutoTupling")),
154153
compileFile("tests/neg-custom-args/i1650.scala", allowDeepSubtypes),
155154
compileFile("tests/neg-custom-args/i3882.scala", allowDeepSubtypes),

tests/neg-custom-args/newline-braces.scala

-6
This file was deleted.

tests/neg/i12554a.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ object Test {
1717

1818
def i: Unit = {
1919
f("Foo")
20-
{} // ok, error in Scala 2
20+
{} // error, error in Scala 2
2121
}
2222

2323
def j: Int = {

tests/new/i16760.scala

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
object Test {
2+
def main(args: Array[String]): Unit = {
3+
def process(arg: Int)(block: => Unit): Int = arg
4+
val x = process(1)
5+
{
6+
}
7+
println(x)
8+
val y = process(2)
9+
( ()
10+
)
11+
println(y)
12+
}
13+
}

tests/pos-with-compiler-cc/dotc/reporting/trace.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ trait TraceSyntax:
5555
apply(question, printer, {
5656
val showOp: T => String = inline if show == true then
5757
val showT = summonInline[Show[T]]
58-
{
58+
locally {
5959
given Show[T] = showT
6060
t => i"$t"
6161
}

0 commit comments

Comments
 (0)