Skip to content

Commit 005704e

Browse files
committed
Fix #2421: Add errors for inline on non supported trees
1 parent a570e24 commit 005704e

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
@@ -1195,6 +1195,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
11951195
case rhs => typedExpr(rhs, tpt1.tpe)
11961196
}
11971197
val vdef1 = assignType(cpy.ValDef(vdef)(name, tpt1, rhs1), sym)
1198+
checkInlineKeyword(vdef1)
11981199
if (sym.is(Inline, butNot = DeferredOrParamAccessor))
11991200
checkInlineConformant(rhs1, em"right-hand side of inline $sym")
12001201
patchIfLazy(vdef1)
@@ -1277,7 +1278,9 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
12771278
case rhs =>
12781279
typedType(rhs)
12791280
}
1280-
assignType(cpy.TypeDef(tdef)(name, rhs1), sym)
1281+
val tdef1 = assignType(cpy.TypeDef(tdef)(name, rhs1), sym)
1282+
checkInlineKeyword(tdef1)
1283+
tdef1
12811284
}
12821285

12831286
def typedClassDef(cdef: untpd.TypeDef, cls: ClassSymbol)(implicit ctx: Context) = track("typedClassDef") {
@@ -1379,11 +1382,15 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
13791382
// check value class constraints
13801383
checkDerivedValueClass(cls, body1)
13811384

1385+
checkInlineKeyword(cdef1)
1386+
13821387
if (ctx.settings.YretainTrees.value) {
13831388
cls.myTree = cdef1
13841389
}
13851390
cdef1
13861391

1392+
cdef1
1393+
13871394
// todo later: check that
13881395
// 1. If class is non-abstract, it is instantiatable:
13891396
// - 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)