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 @@ -548,6 +548,28 @@ trait Checking {
548
548
tp
549
549
}
550
550
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
+
551
573
/** Check that `tree` is a pure expression of constant type */
552
574
def checkInlineConformant (tree : Tree , what : => String )(implicit ctx : Context ): Unit =
553
575
tree.tpe match {
Original file line number Diff line number Diff line change @@ -1179,6 +1179,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
1179
1179
case rhs => typedExpr(rhs, tpt1.tpe)
1180
1180
}
1181
1181
val vdef1 = assignType(cpy.ValDef (vdef)(name, tpt1, rhs1), sym)
1182
+ checkInlineKeyword(vdef1)
1182
1183
if (sym.is(Inline , butNot = DeferredOrParamAccessor ))
1183
1184
checkInlineConformant(rhs1, em " right-hand side of inline $sym" )
1184
1185
patchIfLazy(vdef1)
@@ -1261,7 +1262,9 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
1261
1262
case rhs =>
1262
1263
typedType(rhs)
1263
1264
}
1264
- assignType(cpy.TypeDef (tdef)(name, rhs1), sym)
1265
+ val tdef1 = assignType(cpy.TypeDef (tdef)(name, rhs1), sym)
1266
+ checkInlineKeyword(tdef1)
1267
+ tdef1
1265
1268
}
1266
1269
1267
1270
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
1341
1344
// check value class constraints
1342
1345
checkDerivedValueClass(cls, body1)
1343
1346
1347
+ checkInlineKeyword(cdef1)
1348
+
1344
1349
cdef1
1345
1350
1346
1351
// todo later: check that
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