Skip to content

Commit 5e4d1c6

Browse files
committed
Indentation warning is a syntax warning
1 parent d394833 commit 5e4d1c6

File tree

5 files changed

+28
-14
lines changed

5 files changed

+28
-14
lines changed

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -710,9 +710,7 @@ object Parsers {
710710
if in.isNewLine && !(nextIndentWidth < startIndentWidth) then
711711
warning(
712712
if startIndentWidth <= nextIndentWidth then
713-
em"""Line is indented too far to the right, or a `{` is missing before:
714-
|
715-
|${t.tryToShow}"""
713+
IndentationWarning(missing = LBRACE, before = t.tryToShow)
716714
else
717715
in.spaceTabMismatchMsg(startIndentWidth, nextIndentWidth),
718716
in.next.offset
@@ -727,7 +725,7 @@ object Parsers {
727725
if in.isNewLine then
728726
val nextIndentWidth = in.indentWidth(in.next.offset)
729727
if in.currentRegion.indentWidth < nextIndentWidth && in.currentRegion.closedBy == OUTDENT then
730-
warning(em"Line is indented too far to the right, or a `{` or `:` is missing", in.next.offset)
728+
warning(IndentationWarning(missing = Seq(LBRACE, COLONop)*), in.next.offset)
731729

732730
/* -------- REWRITES ----------------------------------------------------------- */
733731

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import config.Feature
2020
import config.Feature.{migrateTo3, sourceVersion}
2121
import config.SourceVersion.{`3.0`, `3.0-migration`}
2222
import config.MigrationVersion
23-
import reporting.{NoProfile, Profile, Message}
23+
import reporting.*
2424

2525
import java.util.Objects
2626
import dotty.tools.dotc.reporting.Message.rewriteNotice
@@ -650,7 +650,7 @@ object Scanners {
650650
if r.enclosing.isClosedByUndentAt(nextWidth) then
651651
insert(OUTDENT, offset)
652652
else if r.isInstanceOf[InBraces] && !closingRegionTokens.contains(token) then
653-
report.warning("Line is indented too far to the left, or a `}` is missing", sourcePos())
653+
report.warning(IndentationWarning(isLeft = true, missing = RBRACE), sourcePos())
654654
else if lastWidth < nextWidth
655655
|| lastWidth == nextWidth && (lastToken == MATCH || lastToken == CATCH) && token == CASE then
656656
if canStartIndentTokens.contains(lastToken) then

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ enum ErrorMessageID(val isActive: Boolean = true) extends java.lang.Enum[ErrorMe
236236
case DefaultShadowsGivenID // errorNumber: 220
237237
case RecurseWithDefaultID // errorNumber: 221
238238
case AmbiguousTemplateNameID // errorNumber: 222
239+
case IndentationWarningID // errorNumber: 223
239240

240241
def errorNumber = ordinal - 1
241242

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import Denotations.SingleDenotation
99
import SymDenotations.SymDenotation
1010
import NameKinds.{WildcardParamName, ContextFunctionParamName}
1111
import parsing.Scanners.Token
12-
import parsing.Tokens
12+
import parsing.Tokens, Tokens.showToken
1313
import printing.Highlighting.*
1414
import printing.Formatting
1515
import ErrorMessageID.*
@@ -1233,12 +1233,12 @@ extends ReferenceMsg(ForwardReferenceExtendsOverDefinitionID) {
12331233
class ExpectedTokenButFound(expected: Token, found: Token, prefix: String = "", suffix: String = "")(using Context)
12341234
extends SyntaxMsg(ExpectedTokenButFoundID) {
12351235

1236-
private def foundText = Tokens.showToken(found)
1236+
private def foundText = showToken(found)
12371237

12381238
def msg(using Context) =
12391239
val expectedText =
12401240
if (Tokens.isIdentifier(expected)) "an identifier"
1241-
else Tokens.showToken(expected)
1241+
else showToken(expected)
12421242
i"""$prefix$expectedText expected, but $foundText found$suffix"""
12431243

12441244
def explain(using Context) =
@@ -1940,7 +1940,7 @@ class ExtendFinalClass(clazz:Symbol, finalClazz: Symbol)(using Context)
19401940

19411941
class ExpectedTypeBoundOrEquals(found: Token)(using Context)
19421942
extends SyntaxMsg(ExpectedTypeBoundOrEqualsID) {
1943-
def msg(using Context) = i"${hl("=")}, ${hl(">:")}, or ${hl("<:")} expected, but ${Tokens.showToken(found)} found"
1943+
def msg(using Context) = i"${hl("=")}, ${hl(">:")}, or ${hl("<:")} expected, but ${showToken(found)} found"
19441944

19451945
def explain(using Context) =
19461946
i"""Type parameters and abstract types may be constrained by a type bound.
@@ -3745,3 +3745,14 @@ class AmbiguousTemplateName(tree: NamedDefTree[?])(using Context) extends Syntax
37453745
override protected def msg(using Context) = i"name `${tree.name}` should be enclosed in backticks"
37463746
override protected def explain(using Context): String =
37473747
"Names with trailing operator characters may fuse with a subsequent colon if not set off by backquotes or spaces."
3748+
3749+
class IndentationWarning(isLeft: Boolean = false, before: String = "", missing: Token*)(using Context)
3750+
extends SyntaxMsg(IndentationWarningID):
3751+
override protected def msg(using Context) =
3752+
s"Line is indented too far to the ${if isLeft then "left" else "right"}, or a ${
3753+
missing.map(showToken).mkString(" or ")
3754+
} is missing${
3755+
if !before.isEmpty then i" before:\n\n$before" else ""
3756+
}"
3757+
override protected def explain(using Context): String =
3758+
"Indentation that does not reflect syntactic nesting may be due to a typo such as missing punctuation."

tests/warn/i16072.check

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
-- Warning: tests/warn/i16072.scala:4:2 --------------------------------------------------------------------------------
1+
-- [E223] Syntax Warning: tests/warn/i16072.scala:4:2 ------------------------------------------------------------------
22
4 | def x = 1 // warn too far right
33
| ^
4-
| Line is indented too far to the right, or a `{` or `:` is missing
4+
| Line is indented too far to the right, or a '{' or ':' is missing
5+
|
6+
| longer explanation available when compiling with `-explain`
57
-- [E222] Syntax Warning: tests/warn/i16072.scala:3:7 ------------------------------------------------------------------
68
3 |object Hello_: // warn colon in name without backticks because the body is empty
79
| ^^^^^^^
@@ -12,10 +14,12 @@
1214
12 |object :: : // warn deprecated colon without backticks for operator name
1315
| ^
1416
| `:` after symbolic operator is deprecated; use backticks around operator instead
15-
-- Warning: tests/warn/i16072.scala:21:2 -------------------------------------------------------------------------------
17+
-- [E223] Syntax Warning: tests/warn/i16072.scala:21:2 -----------------------------------------------------------------
1618
21 | def y = 1 // warn
1719
| ^
18-
| Line is indented too far to the right, or a `{` or `:` is missing
20+
| Line is indented too far to the right, or a '{' or ':' is missing
21+
|
22+
| longer explanation available when compiling with `-explain`
1923
-- [E222] Syntax Warning: tests/warn/i16072.scala:20:6 -----------------------------------------------------------------
2024
20 |class Uhoh_: // warn
2125
| ^^^^^^

0 commit comments

Comments
 (0)