Skip to content

Commit b481932

Browse files
authored
Add a specific error message for local final defs (#20557)
Fixes #17579.
2 parents d68b645 + cfbace4 commit b481932

File tree

5 files changed

+71
-2
lines changed

5 files changed

+71
-2
lines changed

Diff for: compiler/src/dotty/tools/dotc/parsing/Parsers.scala

+6
Original file line numberDiff line numberDiff line change
@@ -4560,6 +4560,12 @@ object Parsers {
45604560
for (imod <- implicitMods.mods) mods = addMod(mods, imod)
45614561
if (mods.is(Final))
45624562
// A final modifier means the local definition is "class-like". // FIXME: Deal with modifiers separately
4563+
4564+
// See test 17579. We allow `final` on `given` because these can be
4565+
// translated to class definitions, for which `final` is allowed but
4566+
// redundant--there is a seperate warning for this.
4567+
if isDclIntro && in.token != GIVEN then syntaxError(FinalLocalDef())
4568+
45634569
tmplDef(start, mods)
45644570
else
45654571
defOrDcl(start, mods)

Diff for: compiler/src/dotty/tools/dotc/reporting/ErrorMessageID.scala

+1
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ enum ErrorMessageID(val isActive: Boolean = true) extends java.lang.Enum[ErrorMe
213213
case InlinedAnonClassWarningID // errorNumber: 197
214214
case UnusedSymbolID // errorNumber: 198
215215
case TailrecNestedCallID //errorNumber: 199
216+
case FinalLocalDefID // errorNumber: 200
216217

217218
def errorNumber = ordinal - 1
218219

Diff for: compiler/src/dotty/tools/dotc/reporting/messages.scala

+8-2
Original file line numberDiff line numberDiff line change
@@ -1838,7 +1838,7 @@ class WrongNumberOfParameters(tree: untpd.Tree, foundCount: Int, pt: Type, expec
18381838

18391839
class DuplicatePrivateProtectedQualifier()(using Context)
18401840
extends SyntaxMsg(DuplicatePrivateProtectedQualifierID) {
1841-
def msg(using Context) = "Duplicate private/protected qualifier"
1841+
def msg(using Context) = "Duplicate private/protected modifier"
18421842
def explain(using Context) =
18431843
i"It is not allowed to combine `private` and `protected` modifiers even if they are qualified to different scopes"
18441844
}
@@ -1847,7 +1847,13 @@ class ExpectedStartOfTopLevelDefinition()(using Context)
18471847
extends SyntaxMsg(ExpectedStartOfTopLevelDefinitionID) {
18481848
def msg(using Context) = "Expected start of definition"
18491849
def explain(using Context) =
1850-
i"You have to provide either ${hl("class")}, ${hl("trait")}, ${hl("object")}, or ${hl("enum")} definitions after qualifiers"
1850+
i"You have to provide either ${hl("class")}, ${hl("trait")}, ${hl("object")}, or ${hl("enum")} definitions after modifiers"
1851+
}
1852+
1853+
class FinalLocalDef()(using Context)
1854+
extends SyntaxMsg(FinalLocalDefID) {
1855+
def msg(using Context) = i"The ${hl("final")} modifier is not allowed on local definitions"
1856+
def explain(using Context) = ""
18511857
}
18521858

18531859
class NoReturnFromInlineable(owner: Symbol)(using Context)

Diff for: tests/neg/17579.check

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
-- [E200] Syntax Error: tests/neg/17579.scala:5:10 ---------------------------------------------------------------------
2+
5 | final val v1 = 42 // error: final modifier is not allowed on local definitions
3+
| ^^^
4+
| The final modifier is not allowed on local definitions
5+
-- [E200] Syntax Error: tests/neg/17579.scala:6:15 ---------------------------------------------------------------------
6+
6 | final lazy val v2 = 42 // error: final modifier is not allowed on local definitions
7+
| ^^^
8+
| The final modifier is not allowed on local definitions
9+
-- [E200] Syntax Error: tests/neg/17579.scala:7:10 ---------------------------------------------------------------------
10+
7 | final def v4 = 42 // error: final modifier is not allowed on local definitions
11+
| ^^^
12+
| The final modifier is not allowed on local definitions
13+
-- [E200] Syntax Error: tests/neg/17579.scala:8:10 ---------------------------------------------------------------------
14+
8 | final var v5 = 42 // error: final modifier is not allowed on local definitions
15+
| ^^^
16+
| The final modifier is not allowed on local definitions
17+
-- [E200] Syntax Error: tests/neg/17579.scala:9:10 ---------------------------------------------------------------------
18+
9 | final type Foo = String // error: final modifier is not allowed on local definitions
19+
| ^^^^
20+
| The final modifier is not allowed on local definitions
21+
-- [E088] Syntax Error: tests/neg/17579.scala:14:10 --------------------------------------------------------------------
22+
14 | final private val v3 = 42 // error: expected start of definition
23+
| ^^^^^^^
24+
| Expected start of definition
25+
|
26+
| longer explanation available when compiling with `-explain`
27+
-- [E147] Syntax Warning: tests/neg/17579.scala:19:6 -------------------------------------------------------------------
28+
19 | final given Object with {} // warning: modifier `final` is redundant for this definition
29+
| ^^^^^
30+
| Modifier final is redundant for this definition

Diff for: tests/neg/17579.scala

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class C:
2+
final var v = 42 // ok
3+
4+
def f =
5+
final val v1 = 42 // error: final modifier is not allowed on local definitions
6+
final lazy val v2 = 42 // error: final modifier is not allowed on local definitions
7+
final def v4 = 42 // error: final modifier is not allowed on local definitions
8+
final var v5 = 42 // error: final modifier is not allowed on local definitions
9+
final type Foo = String // error: final modifier is not allowed on local definitions
10+
11+
// We get a different error message here because `private` is also not a
12+
// local modifier token. In the future, we could always parse all tokens and
13+
// then flag those that are not legal at this point.
14+
final private val v3 = 42 // error: expected start of definition
15+
16+
{
17+
// No error in this case, because the `given` is translated to a class
18+
// definition, for which `final` is redundant but not illegal.
19+
final given Object with {} // warning: modifier `final` is redundant for this definition
20+
}
21+
22+
{
23+
// Also no error in this case, because we can't easily distinguish it from
24+
// the previous case.
25+
final given Object = new Object {}
26+
}

0 commit comments

Comments
 (0)