File tree Expand file tree Collapse file tree 5 files changed +45
-3
lines changed
compiler/src/dotty/tools/dotc Expand file tree Collapse file tree 5 files changed +45
-3
lines changed Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff 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 )
Original file line number Diff line number Diff 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 ))
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments