Skip to content

Commit 3463d17

Browse files
committed
Check indentation also at TopLevel
Issue "too far to the right" errors also at toplevel. Previously this was done only inside braces.
1 parent f33ade7 commit 3463d17

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ object Parsers {
618618
* statement that's indented relative to the current region.
619619
*/
620620
def checkNextNotIndented(): Unit = in.currentRegion match
621-
case r: InBraces if in.isNewLine =>
621+
case r: IndentSignificantRegion if in.isNewLine =>
622622
val nextIndentWidth = in.indentWidth(in.next.offset)
623623
if r.indentWidth < nextIndentWidth then
624624
warning(i"Line is indented too far to the right, or a `{' is missing", in.next.offset)

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -546,8 +546,7 @@ object Scanners {
546546
if indentSyntax && isNewLine then
547547
val nextWidth = indentWidth(next.offset)
548548
val lastWidth = currentRegion match
549-
case r: Indented => r.width
550-
case r: InBraces => r.width
549+
case r: IndentSignificantRegion => r.indentWidth
551550
case _ => nextWidth
552551

553552
if lastWidth < nextWidth then
@@ -1331,12 +1330,16 @@ object Scanners {
13311330
def enclosing: Region = outer.asInstanceOf[Region]
13321331

13331332
/** If this is an InBraces or Indented region, its indentation width, or Zero otherwise */
1334-
def indentWidth = IndentWidth.Zero
1333+
def indentWidth: IndentWidth = IndentWidth.Zero
13351334
}
13361335

13371336
case class InString(multiLine: Boolean, outer: Region) extends Region
13381337
case class InParens(prefix: Token, outer: Region) extends Region
1339-
case class InBraces(var width: IndentWidth | Null, outer: Region) extends Region {
1338+
1339+
abstract class IndentSignificantRegion extends Region
1340+
1341+
case class InBraces(var width: IndentWidth | Null, outer: Region)
1342+
extends IndentSignificantRegion {
13401343
override def indentWidth = width
13411344
}
13421345

@@ -1345,7 +1348,8 @@ object Scanners {
13451348
* @param others Other indendation widths > width of lines in the same region
13461349
* @param prefix The token before the initial <indent> of the region
13471350
*/
1348-
case class Indented(width: IndentWidth, others: Set[IndentWidth], prefix: Token, outer: Region | Null) extends Region {
1351+
case class Indented(width: IndentWidth, others: Set[IndentWidth], prefix: Token, outer: Region | Null)
1352+
extends IndentSignificantRegion {
13491353
override def indentWidth = width
13501354
}
13511355

tests/neg-custom-args/indentRight.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
trait A
2+
case class B() extends A // error: Line is indented too far to the right
3+
case object C extends A // error: Line is indented too far to the right
4+
15
object Test {
26

37
if (true)
@@ -27,3 +31,4 @@ object Test {
2731

2832
println("!") // error: expected a toplevel definition
2933
}
34+

0 commit comments

Comments
 (0)