Skip to content

Commit 96cde59

Browse files
committed
Fix #2421: Add errors for inline on non supported trees
1 parent 1f7af24 commit 96cde59

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
@@ -1204,6 +1204,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
12041204
case rhs => typedExpr(rhs, tpt1.tpe)
12051205
}
12061206
val vdef1 = assignType(cpy.ValDef(vdef)(name, tpt1, rhs1), sym)
1207+
checkInlineKeyword(vdef1)
12071208
if (sym.is(Inline, butNot = DeferredOrParamAccessor))
12081209
checkInlineConformant(rhs1, em"right-hand side of inline $sym")
12091210
patchIfLazy(vdef1)
@@ -1286,7 +1287,9 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
12861287
case rhs =>
12871288
typedType(rhs)
12881289
}
1289-
assignType(cpy.TypeDef(tdef)(name, rhs1), sym)
1290+
val tdef1 = assignType(cpy.TypeDef(tdef)(name, rhs1), sym)
1291+
checkInlineKeyword(tdef1)
1292+
tdef1
12901293
}
12911294

12921295
def typedClassDef(cdef: untpd.TypeDef, cls: ClassSymbol)(implicit ctx: Context) = track("typedClassDef") {
@@ -1388,11 +1391,15 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
13881391
// check value class constraints
13891392
checkDerivedValueClass(cls, body1)
13901393

1394+
checkInlineKeyword(cdef1)
1395+
13911396
if (ctx.settings.YretainTrees.value) {
13921397
cls.myTree = cdef1
13931398
}
13941399
cdef1
13951400

1401+
cdef1
1402+
13961403
// todo later: check that
13971404
// 1. If class is non-abstract, it is instantiatable:
13981405
// - 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)