Skip to content

Commit 37f2267

Browse files
committed
Indentation warning is a syntax warning
1 parent 957c8be commit 37f2267

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
@@ -712,9 +712,7 @@ object Parsers {
712712
if in.isNewLine && !(nextIndentWidth < startIndentWidth) then
713713
warning(
714714
if startIndentWidth <= nextIndentWidth then
715-
em"""Line is indented too far to the right, or a `{` is missing before:
716-
|
717-
|${t.tryToShow}"""
715+
IndentationWarning(missing = LBRACE, before = t.tryToShow)
718716
else
719717
in.spaceTabMismatchMsg(startIndentWidth, nextIndentWidth),
720718
in.next.offset
@@ -729,7 +727,7 @@ object Parsers {
729727
if in.isNewLine then
730728
val nextIndentWidth = in.indentWidth(in.next.offset)
731729
if in.currentRegion.indentWidth < nextIndentWidth && in.currentRegion.closedBy == OUTDENT then
732-
warning(em"Line is indented too far to the right, or a `{` or `:` is missing", in.next.offset)
730+
warning(IndentationWarning(missing = Seq(LBRACE, COLONop)*), in.next.offset)
733731

734732
/* -------- REWRITES ----------------------------------------------------------- */
735733

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
@@ -652,7 +652,7 @@ object Scanners {
652652
if r.enclosing.isClosedByUndentAt(nextWidth) then
653653
insert(OUTDENT, offset)
654654
else if r.isInstanceOf[InBraces] && !closingRegionTokens.contains(token) then
655-
report.warning("Line is indented too far to the left, or a `}` is missing", sourcePos())
655+
report.warning(IndentationWarning(isLeft = true, missing = RBRACE), sourcePos())
656656
else if lastWidth < nextWidth
657657
|| lastWidth == nextWidth && (lastToken == MATCH || lastToken == CATCH) && token == CASE then
658658
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
@@ -237,6 +237,7 @@ enum ErrorMessageID(val isActive: Boolean = true) extends java.lang.Enum[ErrorMe
237237
case RecurseWithDefaultID // errorNumber: 221
238238
case EncodedPackageNameID // errorNumber: 222
239239
case AmbiguousTemplateNameID // errorNumber: 223
240+
case IndentationWarningID // errorNumber: 224
240241

241242
def errorNumber = ordinal - 1
242243

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.*
@@ -1232,12 +1232,12 @@ extends ReferenceMsg(ForwardReferenceExtendsOverDefinitionID) {
12321232
class ExpectedTokenButFound(expected: Token, found: Token, prefix: String = "", suffix: String = "")(using Context)
12331233
extends SyntaxMsg(ExpectedTokenButFoundID) {
12341234

1235-
private def foundText = Tokens.showToken(found)
1235+
private def foundText = showToken(found)
12361236

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

12431243
def explain(using Context) =
@@ -1928,7 +1928,7 @@ class ExtendFinalClass(clazz:Symbol, finalClazz: Symbol)(using Context)
19281928

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

19331933
def explain(using Context) =
19341934
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)