Skip to content

Commit 8f11a99

Browse files
committed
chore: first draft of a better error recovery in local defs
Co-Authored-By: <[email protected]>
1 parent 9450855 commit 8f11a99

File tree

5 files changed

+45
-3
lines changed

5 files changed

+45
-3
lines changed

compiler/src/dotty/tools/dotc/core/Flags.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,4 +624,6 @@ object Flags {
624624
val SyntheticParam: FlagSet = Synthetic | Param
625625
val SyntheticTermParam: FlagSet = Synthetic | TermParam
626626
val SyntheticTypeParam: FlagSet = Synthetic | TypeParam
627+
628+
val NonAllowedLocalModifier: FlagSet = Private | Abstract //| Final | Sealed | Implicit | Lazy
627629
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4709,7 +4709,7 @@ object Parsers {
47094709
}
47104710

47114711
def localDef(start: Int, implicitMods: Modifiers = EmptyModifiers): Tree = {
4712-
var mods = defAnnotsMods(localModifierTokens)
4712+
var mods = defAnnotsMods(modifierTokens)
47134713
for (imod <- implicitMods.mods) mods = addMod(mods, imod)
47144714
if (mods.is(Final))
47154715
// A final modifier means the local definition is "class-like". // FIXME: Deal with modifiers separately
@@ -4726,7 +4726,7 @@ object Parsers {
47264726

47274727
/** BlockStatSeq ::= { BlockStat semi } [Expr]
47284728
* BlockStat ::= Import
4729-
* | Annotations [implicit] [lazy] Def
4729+
* | Annotations [implicit] [lazy] ValOrDef
47304730
* | Annotations LocalModifiers TmplDef
47314731
* | Extension
47324732
* | Expr1
@@ -4744,7 +4744,7 @@ object Parsers {
47444744
stats += closure(in.offset, Location.InBlock, modifiers(BitSet(IMPLICIT)))
47454745
else if isIdent(nme.extension) && followingIsExtension() then
47464746
stats += extension()
4747-
else if isDefIntro(localModifierTokens,
4747+
else if isDefIntro(modifierTokens,
47484748
excludedSoftModifiers =
47494749
// Allow opaque definitions at outermost level in REPL.
47504750
if outermost && ctx.mode.is(Mode.Interactive)

compiler/src/dotty/tools/dotc/typer/Checking.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,10 @@ object Checking {
591591
checkWithDeferred(Private)
592592
checkWithDeferred(Final)
593593
}
594+
595+
if !sym.owner.isClass && sym.is(Method) && sym.isOneOf(NonAllowedLocalModifier) then
596+
report.error("not allowed", sym.srcPos)
597+
594598
if (sym.isValueClass && sym.is(Trait) && !sym.isRefinementClass)
595599
fail(CannotExtendAnyVal(sym))
596600
if (sym.isConstructor && !sym.isPrimaryConstructor && sym.owner.is(Trait, butNot = JavaDefined))

tests/neg/i22631.check

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
-- Error: tests/neg/i22631.scala:3:16 ----------------------------------------------------------------------------------
2+
3 | private def bar: Int = 0 // error
3+
| ^
4+
| not allowed
5+
-- Error: tests/neg/i22631.scala:8:16 ----------------------------------------------------------------------------------
6+
8 | private def bar: Int = 0 // error
7+
| ^
8+
| not allowed
9+
-- Error: tests/neg/i22631.scala:13:16 ---------------------------------------------------------------------------------
10+
13 | private def bar: Int = 0 // error
11+
| ^
12+
| not allowed
13+
-- Error: tests/neg/i22631.scala:18:16 ---------------------------------------------------------------------------------
14+
18 | private def bar: Int = 0 // error
15+
| ^
16+
| not allowed

tests/neg/i22631.scala

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
object Foo:
2+
def foo1: Int = {
3+
private def bar: Int = 0 // error
4+
bar
5+
}
6+
7+
def foo2: Int = {
8+
private def bar: Int = 0 // error
9+
bar
10+
}
11+
12+
def foo3: Int = {
13+
private def bar: Int = 0 // error
14+
bar
15+
}
16+
17+
def foo4: Int = {
18+
private def bar: Int = 0 // error
19+
bar
20+
}

0 commit comments

Comments
 (0)