Skip to content

Commit ee331a6

Browse files
committed
Fix #2421: Add errors for inline on non supported trees
1 parent b90c324 commit ee331a6

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

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

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

553+
/** Check that inline is only used on valid trees */
554+
def checkInlineKeyword(tree: tpd.Tree)(implicit ctx: Context) = {
555+
def errorOn(what: String) =
556+
ctx.error("inlined keyword cannot be used on " + what, tree.pos)
557+
val sym = tree.symbol
558+
if (sym is Inline) {
559+
tree match {
560+
case _: TypeDef =>
561+
if (!sym.isClass) errorOn("type")
562+
else if (sym.is(Trait)) errorOn("trait")
563+
else if (sym.is(Abstract)) errorOn("abstract class")
564+
else errorOn("class") // Remove this to allow inline classes
565+
case _: ValDef =>
566+
if (sym.is(Module)) errorOn("object")
567+
else if (sym.is(Mutable)) errorOn("var")
568+
else if (sym.is(Lazy)) errorOn("lazy val")
569+
case _ =>
570+
571+
}
572+
}
573+
}
574+
553575
/** Check that `tree` is a pure expression of constant type */
554576
def checkInlineConformant(tree: Tree, what: => String)(implicit ctx: Context): Unit =
555577
tree.tpe match {

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1228,6 +1228,7 @@ 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+
checkInlineKeyword(vdef1)
12311232
if (sym.is(Inline, butNot = DeferredOrParamAccessor))
12321233
checkInlineConformant(rhs1, em"right-hand side of inline $sym")
12331234
patchIfLazy(vdef1)
@@ -1310,7 +1311,9 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
13101311
case rhs =>
13111312
typedType(rhs)
13121313
}
1313-
assignType(cpy.TypeDef(tdef)(name, rhs1), sym)
1314+
val tdef1 = assignType(cpy.TypeDef(tdef)(name, rhs1), sym)
1315+
checkInlineKeyword(tdef1)
1316+
tdef1
13141317
}
13151318

13161319
def typedClassDef(cdef: untpd.TypeDef, cls: ClassSymbol)(implicit ctx: Context) = track("typedClassDef") {
@@ -1412,11 +1415,15 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
14121415
// check value class constraints
14131416
checkDerivedValueClass(cls, body1)
14141417

1418+
checkInlineKeyword(cdef1)
1419+
14151420
if (ctx.settings.YretainTrees.value) {
14161421
cls.myTree = cdef1
14171422
}
14181423
cdef1
14191424

1425+
cdef1
1426+
14201427
// todo later: check that
14211428
// 1. If class is non-abstract, it is instantiatable:
14221429
// - self type is s supertype of own type

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: 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
5+
6+
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
10+
}

0 commit comments

Comments
 (0)