File tree Expand file tree Collapse file tree 3 files changed +38
-1
lines changed
compiler/src/dotty/tools/dotc/typer Expand file tree Collapse file tree 3 files changed +38
-1
lines changed Original file line number Diff line number Diff line change @@ -550,6 +550,28 @@ trait Checking {
550
550
tp
551
551
}
552
552
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
+
553
575
/** Check that `tree` is a pure expression of constant type */
554
576
def checkInlineConformant (tree : Tree , what : => String )(implicit ctx : Context ): Unit =
555
577
tree.tpe match {
Original file line number Diff line number Diff line change @@ -1185,6 +1185,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
1185
1185
case rhs => typedExpr(rhs, tpt1.tpe)
1186
1186
}
1187
1187
val vdef1 = assignType(cpy.ValDef (vdef)(name, tpt1, rhs1), sym)
1188
+ checkInlineKeyword(vdef1)
1188
1189
if (sym.is(Inline , butNot = DeferredOrParamAccessor ))
1189
1190
checkInlineConformant(rhs1, em " right-hand side of inline $sym" )
1190
1191
patchIfLazy(vdef1)
@@ -1267,7 +1268,9 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
1267
1268
case rhs =>
1268
1269
typedType(rhs)
1269
1270
}
1270
- assignType(cpy.TypeDef (tdef)(name, rhs1), sym)
1271
+ val tdef1 = assignType(cpy.TypeDef (tdef)(name, rhs1), sym)
1272
+ checkInlineKeyword(tdef1)
1273
+ tdef1
1271
1274
}
1272
1275
1273
1276
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
1347
1350
// check value class constraints
1348
1351
checkDerivedValueClass(cls, body1)
1349
1352
1353
+ checkInlineKeyword(cdef1)
1354
+
1350
1355
if (ctx.settings.YretainTrees .value) {
1351
1356
cls.myTree = cdef1
1352
1357
}
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments