Skip to content

Better recovery from parsing errors in local statements #22657

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compiler/src/dotty/tools/dotc/core/Flags.scala
Original file line number Diff line number Diff line change
Expand Up @@ -624,4 +624,6 @@ object Flags {
val SyntheticParam: FlagSet = Synthetic | Param
val SyntheticTermParam: FlagSet = Synthetic | TermParam
val SyntheticTypeParam: FlagSet = Synthetic | TypeParam

val NonAllowedLocalModifier: FlagSet = Private | Abstract //| Final | Sealed | Implicit | Lazy
}
6 changes: 3 additions & 3 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4709,7 +4709,7 @@ object Parsers {
}

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

/** BlockStatSeq ::= { BlockStat semi } [Expr]
* BlockStat ::= Import
* | Annotations [implicit] [lazy] Def
* | Annotations [implicit] [lazy] ValOrDef
* | Annotations LocalModifiers TmplDef
* | Extension
* | Expr1
Expand All @@ -4744,7 +4744,7 @@ object Parsers {
stats += closure(in.offset, Location.InBlock, modifiers(BitSet(IMPLICIT)))
else if isIdent(nme.extension) && followingIsExtension() then
stats += extension()
else if isDefIntro(localModifierTokens,
else if isDefIntro(modifierTokens,
excludedSoftModifiers =
// Allow opaque definitions at outermost level in REPL.
if outermost && ctx.mode.is(Mode.Interactive)
Expand Down
4 changes: 4 additions & 0 deletions compiler/src/dotty/tools/dotc/typer/Checking.scala
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,10 @@ object Checking {
checkWithDeferred(Private)
checkWithDeferred(Final)
}

if !sym.owner.isClass && sym.is(Method) && sym.isOneOf(NonAllowedLocalModifier) then
report.error("not allowed", sym.srcPos)

if (sym.isValueClass && sym.is(Trait) && !sym.isRefinementClass)
fail(CannotExtendAnyVal(sym))
if (sym.isConstructor && !sym.isPrimaryConstructor && sym.owner.is(Trait, butNot = JavaDefined))
Expand Down
16 changes: 16 additions & 0 deletions tests/neg/i22631.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- Error: tests/neg/i22631.scala:3:16 ----------------------------------------------------------------------------------
3 | private def bar: Int = 0 // error
| ^
| not allowed
-- Error: tests/neg/i22631.scala:8:16 ----------------------------------------------------------------------------------
8 | private def bar: Int = 0 // error
| ^
| not allowed
-- Error: tests/neg/i22631.scala:13:16 ---------------------------------------------------------------------------------
13 | private def bar: Int = 0 // error
| ^
| not allowed
-- Error: tests/neg/i22631.scala:18:16 ---------------------------------------------------------------------------------
18 | private def bar: Int = 0 // error
| ^
| not allowed
20 changes: 20 additions & 0 deletions tests/neg/i22631.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
object Foo:
def foo1: Int = {
private def bar: Int = 0 // error
bar
}

def foo2: Int = {
private def bar: Int = 0 // error
bar
}

def foo3: Int = {
private def bar: Int = 0 // error
bar
}

def foo4: Int = {
private def bar: Int = 0 // error
bar
}
Loading