Skip to content

Commit 82ac78d

Browse files
committed
Move Inline and Lazy checks to checkFlags
1 parent b8f6c56 commit 82ac78d

File tree

6 files changed

+58
-55
lines changed

6 files changed

+58
-55
lines changed

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

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -550,27 +550,6 @@ trait Checking {
550550
tp
551551
}
552552

553-
/** Check that keywords are only used on valid trees */
554-
def checkKeywords(tree: tpd.Tree)(implicit ctx: Context): Unit = {
555-
val sym = tree.symbol
556-
if (sym is Inline) {
557-
def error() = ctx.error("inlined keyword cannot be used on " + ctx.kindString(sym), tree.pos)
558-
tree match {
559-
case _: TypeDef => error()
560-
case _: ValDef if sym.is(Module) || sym.is(Mutable) || sym.is(Lazy) => error()
561-
case _ =>
562-
}
563-
}
564-
if (sym is Lazy) {
565-
def error(kind: String) = ctx.error("lazy keyword cannot be used on " + kind, tree.pos)
566-
tree match {
567-
case _: DefDef if !sym.is(Accessor) => error("def")
568-
case _: ValDef if sym.is(Mutable) && !sym.is(Synthetic) => error("var")
569-
case _ =>
570-
}
571-
}
572-
}
573-
574553
/** Check that `tree` is a pure expression of constant type */
575554
def checkInlineConformant(tree: Tree, what: => String)(implicit ctx: Context): Unit =
576555
tree.tpe match {

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

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -246,13 +246,46 @@ class Namer { typer: Typer =>
246246
def checkFlags(flags: FlagSet) =
247247
if (flags.isEmpty) flags
248248
else {
249-
val (ok, adapted, kind) = tree match {
250-
case tree: TypeDef => (flags.isTypeFlags, flags.toTypeFlags, "type")
251-
case _ => (flags.isTermFlags, flags.toTermFlags, "value")
249+
def error(flags: FlagSet, illegalFlag: FlagSet, kind: String) = {
250+
val wrongFlag = if (illegalFlag.isEmpty) flags else illegalFlag
251+
ctx.error(i"modifier(s) `$wrongFlag' incompatible with $kind definition", tree.pos)
252+
flags &~ illegalFlag
252253
}
253-
if (!ok)
254-
ctx.error(i"modifier(s) `$flags' incompatible with $kind definition", tree.pos)
255-
adapted
254+
255+
def doCheck(fs: FlagSet, f: FlagSet): FlagSet = f match {
256+
case Inline =>
257+
tree match {
258+
case _: TypeDef => error(fs, Inline, "type")
259+
case _: ValDef =>
260+
if (fs is Mutable) error(fs, Inline, "var")
261+
else if (fs is Lazy) error(fs, Inline, "lazy val")
262+
else if (fs is Module) error(fs, Inline, "object")
263+
else fs
264+
case _ => fs
265+
}
266+
case Lazy =>
267+
tree match {
268+
case _: DefDef if !fs.is(Accessor) => error(fs, Lazy, "def")
269+
case _: ValDef if fs.is(Mutable) && !fs.is(Synthetic) => error(fs, Lazy, "var")
270+
case _ => fs
271+
}
272+
}
273+
274+
def checked(flags: FlagSet, toCheck: Array[FlagSet]): FlagSet =
275+
toCheck.foldLeft(flags)((acc, f) => if (acc.is(f)) doCheck(acc, f) else acc)
276+
277+
val adapted = tree match {
278+
case _: TypeDef =>
279+
if (!flags.isTypeFlags)
280+
error(flags, EmptyFlags, "type")
281+
flags.toTypeFlags
282+
case _ =>
283+
if (!flags.isTermFlags)
284+
error(flags, EmptyFlags, "value")
285+
flags.toTermFlags
286+
}
287+
288+
checked(adapted, Array(Inline, Lazy))
256289
}
257290

258291
/** Add moduleClass/sourceModule to completer if it is for a module val or class */

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

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,7 +1228,6 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
12281228
case rhs => typedExpr(rhs, tpt1.tpe)
12291229
}
12301230
val vdef1 = assignType(cpy.ValDef(vdef)(name, tpt1, rhs1), sym)
1231-
checkKeywords(vdef1)
12321231
if (sym.is(Inline, butNot = DeferredOrParamAccessor))
12331232
checkInlineConformant(rhs1, em"right-hand side of inline $sym")
12341233
patchIfLazy(vdef1)
@@ -1296,11 +1295,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
12961295
tpt1 = tpt1.withType(avoid(tpt1.tpe, vparamss1.flatMap(_.map(_.symbol))))
12971296
}
12981297

1299-
val ddef1 = assignType(cpy.DefDef(ddef)(name, tparams1, vparamss1, tpt1, rhs1), sym)
1300-
1301-
checkKeywords(ddef1)
1302-
1303-
ddef1
1298+
assignType(cpy.DefDef(ddef)(name, tparams1, vparamss1, tpt1, rhs1), sym)
13041299
//todo: make sure dependent method types do not depend on implicits or by-name params
13051300
}
13061301

@@ -1315,9 +1310,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
13151310
case rhs =>
13161311
typedType(rhs)
13171312
}
1318-
val tdef1 = assignType(cpy.TypeDef(tdef)(name, rhs1), sym)
1319-
checkKeywords(tdef1)
1320-
tdef1
1313+
assignType(cpy.TypeDef(tdef)(name, rhs1), sym)
13211314
}
13221315

13231316
def typedClassDef(cdef: untpd.TypeDef, cls: ClassSymbol)(implicit ctx: Context) = track("typedClassDef") {
@@ -1419,12 +1412,9 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
14191412
// check value class constraints
14201413
checkDerivedValueClass(cls, body1)
14211414

1422-
checkKeywords(cdef1)
1423-
14241415
if (ctx.settings.YretainTrees.value) {
14251416
cls.myTree = cdef1
14261417
}
1427-
14281418
cdef1
14291419

14301420
// todo later: check that

tests/neg/i2421.scala

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
inline object Foo // error: inlined keyword cannot be used on object
2-
inline class Bar // error: inlined keyword cannot be used on class
3-
inline abstract class Baz // error: inlined keyword cannot be used on abstract class
4-
inline trait Qux // error: inlined keyword cannot be used on trait
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
55

66
object Quux {
7-
inline type T // error: inlined keyword cannot be used on type
8-
inline var x: Int = 42 // error: inlined keyword cannot be used on var
9-
inline lazy val y: Int = 42 // error: inlined keyword cannot be used on lazy val
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
1010
}

tests/neg/i2712.scala

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
object Foo {
2-
lazy var x: Int = 42 // error
3-
lazy def y: Int = 42 // error
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
412
}

tests/neg/i2712b.scala

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)