Skip to content

Commit 76df105

Browse files
authored
Merge pull request #2476 from dotty-staging/fix-#2421
Fix #2421: Add errors for inline on non supported trees
2 parents 0734ce9 + 8213e96 commit 76df105

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-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.isTerm && !sym.is(Mutable | Module))
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 {

tests/neg/i2421.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
inline object Foo // error: modifier(s) `inline' incompatible with type definition
2+
inline class Bar // error: modifier(s) `inline' incompatible with type definition
3+
inline abstract class Baz // error: modifier(s) `inline' incompatible with type definition
4+
inline trait Qux // error: modifier(s) `inline' incompatible with type definition
5+
6+
object Quux {
7+
inline type T // error: modifier(s) `inline' incompatible with type definition
8+
inline var x: Int = 42 // error: modifier(s) `inline' incompatible with var definition
9+
inline lazy val y: Int = 43 // error: modifier(s) `inline' incompatible with lazy val definition
10+
}

tests/neg/i2712.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
object Foo {
2+
lazy var x: Int = 42 // error: modifier(s) `lazy' incompatible with var definition
3+
lazy def y: Int = 42 // error: modifier(s) `lazy' incompatible with def definition
4+
}
5+
6+
lazy class Bar // error: modifier(s) `lazy' incompatible with type definition
7+
lazy abstract class Baz // error: modifier(s) `lazy abstract' incompatible with type definition
8+
lazy trait Qux // error: modifier(s) `lazy' not allowed for trait
9+
10+
object Quux {
11+
lazy type T // error: modifier(s) `lazy' incompatible with type definition
12+
}

0 commit comments

Comments
 (0)