Skip to content

Commit c13e7af

Browse files
committed
Fix #2712 and #2421: Check inapplicable modifiers
Check modifiers `lazy` and `inline` for inapplicability
1 parent 0734ce9 commit c13e7af

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,10 @@ object Checking {
315315
fail(AbstractMemberMayNotHaveModifier(sym, flag))
316316
def checkNoConflict(flag1: FlagSet, flag2: FlagSet) =
317317
if (sym.is(allOf(flag1, flag2)))
318-
fail(i"illegal combination of modifiers: $flag1 and $flag2 for: $sym")
318+
fail(i"illegal combination of modifiers: `$flag1` and `$flag2` for: $sym")
319+
def checkApplicable(flag: FlagSet, ok: Boolean) =
320+
if (!ok && !sym.is(Synthetic))
321+
fail(i"modifier `$flag` is not allowed for this definition")
319322

320323
if (sym.is(ImplicitCommon)) {
321324
if (sym.owner.is(Package))
@@ -345,6 +348,9 @@ object Checking {
345348
checkNoConflict(Final, Sealed)
346349
checkNoConflict(Private, Protected)
347350
checkNoConflict(Abstract, Override)
351+
checkNoConflict(Lazy, Inline)
352+
if (sym.is(Inline)) checkApplicable(Inline, sym.is(Method, butNot = Mutable))
353+
if (sym.is(Lazy)) checkApplicable(Lazy, !sym.is(Method | Mutable))
348354
if (sym.isType && !sym.is(Deferred))
349355
for (cls <- sym.allOverriddenSymbols.filter(_.isClass)) {
350356
fail(CannotHaveSameNameAs(sym, cls, CannotHaveSameNameAs.CannotBeOverridden))

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,10 @@ class Namer { typer: Typer =>
243243
def privateWithinClass(mods: Modifiers) =
244244
enclosingClassNamed(mods.privateWithin, mods.pos)
245245

246+
/** Check that flags are OK for symbol. This is done early to avoid
247+
* catastrophic failure when we create a TermSymbol with TypeFlags, or vice versa.
248+
* A more complete check is done in checkWellformed.
249+
*/
246250
def checkFlags(flags: FlagSet) =
247251
if (flags.isEmpty) flags
248252
else {

0 commit comments

Comments
 (0)