Skip to content

Commit 6c437e3

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

File tree

10 files changed

+23
-24
lines changed

10 files changed

+23
-24
lines changed

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

+1-9
Original file line numberDiff line numberDiff line change
@@ -1353,15 +1353,7 @@ 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), " ")
1356+
newLineOptWhenFollowedBy(LBRACE)
13651357

13661358
def possibleTemplateStart(isNew: Boolean = false): Unit =
13671359
in.observeColonEOL(inTemplate = true)

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

+4-5
Original file line numberDiff line numberDiff line change
@@ -566,11 +566,10 @@ 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 =

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/neg/i1707.scala

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ object DepBug {
1717
import d.*
1818
a m (b)
1919
}
20+
2021
{
2122
import dep.*
2223
a m (b) // error: not found: a

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
}

tests/pos/indented-parens.scala

+1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ def f(x: Int)
77
(2)
88
+ f(1)
99
{2}
10+
1011
{ println(x) }
1112

0 commit comments

Comments
 (0)