Skip to content

Commit c493dfe

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

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-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: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1185,6 +1185,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
11851185
case rhs => typedExpr(rhs, tpt1.tpe)
11861186
}
11871187
val vdef1 = assignType(cpy.ValDef(vdef)(name, tpt1, rhs1), sym)
1188+
checkInlineKeyword(vdef1)
11881189
if (sym.is(Inline, butNot = DeferredOrParamAccessor))
11891190
checkInlineConformant(rhs1, em"right-hand side of inline $sym")
11901191
patchIfLazy(vdef1)
@@ -1267,7 +1268,9 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
12671268
case rhs =>
12681269
typedType(rhs)
12691270
}
1270-
assignType(cpy.TypeDef(tdef)(name, rhs1), sym)
1271+
val tdef1 = assignType(cpy.TypeDef(tdef)(name, rhs1), sym)
1272+
checkInlineKeyword(tdef1)
1273+
tdef1
12711274
}
12721275

12731276
def typedClassDef(cdef: untpd.TypeDef, cls: ClassSymbol)(implicit ctx: Context) = track("typedClassDef") {
@@ -1347,6 +1350,8 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
13471350
// check value class constraints
13481351
checkDerivedValueClass(cls, body1)
13491352

1353+
checkInlineKeyword(cdef1)
1354+
13501355
if (ctx.settings.YretainTrees.value) {
13511356
cls.myTree = cdef1
13521357
}

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)