Skip to content

Commit e28b0f3

Browse files
committed
Fix #2421: Add errors for inline on non supported trees
1 parent 8a9aedc commit e28b0f3

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
@@ -548,6 +548,28 @@ trait Checking {
548548
tp
549549
}
550550

551+
/** Check that inline is only used on valid trees */
552+
def checkInlineKeyword(tree: tpd.Tree)(implicit ctx: Context) = {
553+
def errorOn(what: String) =
554+
ctx.error("inlined keyword cannot be used on " + what, tree.pos)
555+
val sym = tree.symbol
556+
if (sym is Inline) {
557+
tree match {
558+
case _: TypeDef =>
559+
if (!sym.isClass) errorOn("type")
560+
else if (sym.is(Trait)) errorOn("trait")
561+
else if (sym.is(Abstract)) errorOn("abstract class")
562+
else errorOn("class") // Remove this to allow inline classes
563+
case _: ValDef =>
564+
if (sym.is(Module)) errorOn("object")
565+
else if (sym.is(Mutable)) errorOn("var")
566+
else if (sym.is(Lazy)) errorOn("lazy val")
567+
case _ =>
568+
569+
}
570+
}
571+
}
572+
551573
/** Check that `tree` is a pure expression of constant type */
552574
def checkInlineConformant(tree: Tree, what: => String)(implicit ctx: Context): Unit =
553575
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
@@ -1179,6 +1179,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
11791179
case rhs => typedExpr(rhs, tpt1.tpe)
11801180
}
11811181
val vdef1 = assignType(cpy.ValDef(vdef)(name, tpt1, rhs1), sym)
1182+
checkInlineKeyword(vdef1)
11821183
if (sym.is(Inline, butNot = DeferredOrParamAccessor))
11831184
checkInlineConformant(rhs1, em"right-hand side of inline $sym")
11841185
patchIfLazy(vdef1)
@@ -1261,7 +1262,9 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
12611262
case rhs =>
12621263
typedType(rhs)
12631264
}
1264-
assignType(cpy.TypeDef(tdef)(name, rhs1), sym)
1265+
val tdef1 = assignType(cpy.TypeDef(tdef)(name, rhs1), sym)
1266+
checkInlineKeyword(tdef1)
1267+
tdef1
12651268
}
12661269

12671270
def typedClassDef(cdef: untpd.TypeDef, cls: ClassSymbol)(implicit ctx: Context) = track("typedClassDef") {
@@ -1341,6 +1344,8 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
13411344
// check value class constraints
13421345
checkDerivedValueClass(cls, body1)
13431346

1347+
checkInlineKeyword(cdef1)
1348+
13441349
cdef1
13451350

13461351
// todo later: check that

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)