Skip to content

Commit b0a8d11

Browse files
committed
Improve message for nested package missing braces
Move the caret closer to the real problem; clarify the error.
1 parent 25092c7 commit b0a8d11

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4692,6 +4692,10 @@ object Parsers {
46924692
def packaging(start: Int): Tree =
46934693
val pkg = qualId()
46944694
possibleTemplateStart()
4695+
if in.token != INDENT && in.token != LBRACE then
4696+
val prefix = "':' or "
4697+
val suffix = "\nNested package statements that are not at the beginning of the file require braces or `:` with an indented body."
4698+
syntaxErrorOrIncomplete(ExpectedTokenButFound(LBRACE, in.token, prefix = prefix, suffix = suffix), in.lastOffset)
46954699
val stats = inDefScopeBraces(topStatSeq(), rewriteWithColon = true)
46964700
makePackaging(start, pkg, stats)
46974701

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,7 +1231,7 @@ extends ReferenceMsg(ForwardReferenceExtendsOverDefinitionID) {
12311231
|"""
12321232
}
12331233

1234-
class ExpectedTokenButFound(expected: Token, found: Token, prefix: String = "")(using Context)
1234+
class ExpectedTokenButFound(expected: Token, found: Token, prefix: String = "", suffix: String = "")(using Context)
12351235
extends SyntaxMsg(ExpectedTokenButFoundID) {
12361236

12371237
private def foundText = Tokens.showToken(found)
@@ -1240,7 +1240,7 @@ extends SyntaxMsg(ExpectedTokenButFoundID) {
12401240
val expectedText =
12411241
if (Tokens.isIdentifier(expected)) "an identifier"
12421242
else Tokens.showToken(expected)
1243-
i"""$prefix$expectedText expected, but $foundText found"""
1243+
i"""$prefix$expectedText expected, but $foundText found$suffix"""
12441244

12451245
def explain(using Context) =
12461246
if (Tokens.isIdentifier(expected) && Tokens.isKeyword(found))

tests/neg/i23815.check

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- [E040] Syntax Error: tests/neg/d17874.scala:7:10 --------------------------------------------------------------------
2+
7 |package t2 // error
3+
| ^
4+
| ':' or '{' expected, but 'end of statement' found
5+
| Nested package statements that are not at the beginning of the file require braces or `:` with an indented body.
6+
-- [E040] Syntax Error: tests/neg/d17874.scala:10:0 --------------------------------------------------------------------
7+
10 |
8+
|^
9+
|'}' expected, but eof found

tests/neg/i23815.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
package test6
3+
4+
package t1
5+
trait T1
6+
7+
package t2 // error
8+
trait T2
9+
// anypos-error

0 commit comments

Comments
 (0)