File tree 4 files changed +36
-4
lines changed
compiler/src/dotty/tools/dotc/parsing
language-server/src/dotty/tools/languageserver
4 files changed +36
-4
lines changed Original file line number Diff line number Diff line change @@ -2546,7 +2546,12 @@ object Parsers {
2546
2546
def localDef (start : Int , implicitMods : Modifiers = EmptyModifiers ): Tree = {
2547
2547
var mods = defAnnotsMods(localModifierTokens)
2548
2548
for (imod <- implicitMods.mods) mods = addMod(mods, imod)
2549
- defOrDcl(start, mods)
2549
+ if (mods.is(Final )) {
2550
+ // A final modifier means the local definition is "class-like".
2551
+ tmplDef(start, mods)
2552
+ } else {
2553
+ defOrDcl(start, mods)
2554
+ }
2550
2555
}
2551
2556
2552
2557
/** BlockStatSeq ::= { BlockStat semi } [ResultExpr]
Original file line number Diff line number Diff line change @@ -37,11 +37,11 @@ object Memory {
37
37
}
38
38
39
39
def stats (): String = {
40
- final val M = 2 << 20
40
+ val M = 2 << 20
41
41
val runtime = Runtime .getRuntime
42
42
def total = runtime.totalMemory / M
43
43
def maximal = runtime.maxMemory / M
44
44
def free = runtime.freeMemory / M
45
45
s " total used memory: $total MB, free: $free MB, maximal available = $maximal MB "
46
46
}
47
- }
47
+ }
Original file line number Diff line number Diff line change
1
+ class Test {
2
+ def foo = {
3
+ final def bar = 1 // error: local def may not be final
4
+ final val v = 42 // error: local val may not be final
5
+ final var v2 = 100 // error: local var may not be final
6
+ final type T = Int // error: local type def may not be final
7
+ }
8
+
9
+ {
10
+ final def foo (x : Int ) = x // error: local def may not be final
11
+ }
12
+
13
+ final def foo2 (x : Int ) = x // ok: final allowed in class field
14
+
15
+ object Foo {
16
+ final def foo (x : Int ) = x // ok, but redundant
17
+ }
18
+
19
+ abstract class Bar {
20
+ def foo : Int
21
+ }
22
+
23
+ val x = new Bar {
24
+ override final def foo = 42 // ok: def is a field
25
+ }
26
+ }
Original file line number Diff line number Diff line change 2
2
import Macros ._
3
3
4
4
object Test {
5
+ final val y = 5
6
+
5
7
def main (args : Array [String ]): Unit = {
6
8
println(foo(1 )) // "Some(1)"
7
9
println(foo(1 + 7 )) // "Some(8)"
8
- final val y = 5
9
10
println(foo(y)) // "Some(5)"
10
11
println(foo(y + 1 ))
11
12
val x = 4
You can’t perform that action at this time.
0 commit comments