Skip to content

Commit e4427db

Browse files
committed
Make inline annotation @scala.inline.
Drop @dotty.annotation.inline. This will inline all @inline marked methods in Scala for which a body is known (i.e. that are either compiled in the same run or have Tasty trees available). Option -Yno-inline suppresses inlining. This is needed for the moment because some @inline methods access private members or members that are otherwise inaccessible at the call-site. Also fixes some problems in Inliner - make sure type arguments to inline calls re fully defined - don't forget recursive calls in typeMap - don't forget positions in treeMap - drop dead code dealing with outer.
1 parent 1953180 commit e4427db

File tree

15 files changed

+41
-67
lines changed

15 files changed

+41
-67
lines changed

src/dotty/annotation/inline.scala

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/dotty/tools/dotc/config/ScalaSettings.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ class ScalaSettings extends Settings.SettingGroup {
186186
val Yexplainlowlevel = BooleanSetting("-Yexplain-lowlevel", "When explaining type errors, show types at a lower level.")
187187
val YnoDoubleBindings = BooleanSetting("-Yno-double-bindings", "Assert no namedtype is bound twice (should be enabled only if program is error-free).")
188188
val YshowVarBounds = BooleanSetting("-Yshow-var-bounds", "Print type variables with their bounds")
189+
val YnoInline = BooleanSetting("-Yno-inline", "Suppress inlining.")
189190

190191
val optimise = BooleanSetting("-optimise", "Generates faster bytecode by applying optimisations to the program") withAbbreviation "-optimize"
191192

src/dotty/tools/dotc/core/Decorators.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ object Decorators {
4242
*/
4343
implicit class ListDecorator[T](val xs: List[T]) extends AnyVal {
4444

45-
@inline final def mapconserve[U](f: T => U): List[U] = {
45+
final def mapconserve[U](f: T => U): List[U] = {
4646
@tailrec
4747
def loop(mapped: ListBuffer[U], unchanged: List[U], pending: List[T]): List[U] =
4848
if (pending.isEmpty) {

src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ class Definitions {
466466
def DeprecatedAnnot(implicit ctx: Context) = DeprecatedAnnotType.symbol.asClass
467467
lazy val ImplicitNotFoundAnnotType = ctx.requiredClassRef("scala.annotation.implicitNotFound")
468468
def ImplicitNotFoundAnnot(implicit ctx: Context) = ImplicitNotFoundAnnotType.symbol.asClass
469-
lazy val InlineAnnotType = ctx.requiredClassRef("dotty.annotation.inline")
469+
lazy val InlineAnnotType = ctx.requiredClassRef("scala.inline")
470470
def InlineAnnot(implicit ctx: Context) = InlineAnnotType.symbol.asClass
471471
lazy val InvariantBetweenAnnotType = ctx.requiredClassRef("dotty.annotation.internal.InvariantBetween")
472472
def InvariantBetweenAnnot(implicit ctx: Context) = InvariantBetweenAnnotType.symbol.asClass

src/dotty/tools/dotc/reporting/Reporter.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,16 +140,16 @@ trait Reporting { this: Context =>
140140
def debugwarn(msg: => String, pos: SourcePosition = NoSourcePosition): Unit =
141141
if (this.settings.debug.value) warning(msg, pos)
142142

143-
@dotty.annotation.inline
143+
@inline
144144
def debugTraceIndented[TD](question: => String, printer: Printers.Printer = Printers.default, show: Boolean = false)(op: => TD): TD =
145145
conditionalTraceIndented(this.settings.debugTrace.value, question, printer, show)(op)
146146

147-
@dotty.annotation.inline
147+
@inline
148148
def conditionalTraceIndented[TC](cond: Boolean, question: => String, printer: Printers.Printer = Printers.default, show: Boolean = false)(op: => TC): TC =
149149
if (cond) traceIndented[TC](question, printer, show)(op)
150150
else op
151151

152-
@dotty.annotation.inline
152+
@inline
153153
def traceIndented[T](question: => String, printer: Printers.Printer = Printers.default, show: Boolean = false)(op: => T): T =
154154
if (printer eq config.Printers.noPrinter) op
155155
else doTraceIndented[T](question, printer, show)(op)

src/dotty/tools/dotc/typer/Inliner.scala

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import Names.Name
1717
import SymDenotations.SymDenotation
1818
import Annotations.Annotation
1919
import transform.ExplicitOuter
20+
import Inferencing.fullyDefinedType
2021
import config.Printers.inlining
2122
import ErrorReporting.errorTree
2223
import util.{Property, SourceFile, NoSource}
@@ -36,13 +37,13 @@ object Inliner {
3637
def attachBody(inlineAnnot: Annotation, tree: => Tree)(implicit ctx: Context): Unit =
3738
inlineAnnot.tree.putAttachment(InlinedBody, new InlinedBody(tree))
3839

39-
def inlinedBody(sym: SymDenotation)(implicit ctx: Context): Tree =
40+
def inlinedBody(sym: SymDenotation)(implicit ctx: Context): Option[Tree] =
4041
sym.getAnnotation(defn.InlineAnnot).get.tree
41-
.attachment(InlinedBody).body
42+
.getAttachment(InlinedBody).map(_.body)
4243

4344
def inlineCall(tree: Tree, pt: Type)(implicit ctx: Context): Tree =
4445
if (enclosingInlineds.length < ctx.settings.xmaxInlines.value)
45-
new Inliner(tree, inlinedBody(tree.symbol)).inlined(pt)
46+
new Inliner(tree, inlinedBody(tree.symbol).get).inlined(pt)
4647
else errorTree(tree,
4748
i"""Maximal number of successive inlines (${ctx.settings.xmaxInlines.value}) exceeded,
4849
| Maybe this is caused by a recursive inline method?
@@ -86,6 +87,8 @@ class Inliner(call: tpd.Tree, rhs: tpd.Tree)(implicit ctx: Context) {
8687
private val (methPart, targs, argss) = decomposeCall(call)
8788
private val meth = methPart.symbol
8889

90+
for (targ <- targs) fullyDefinedType(targ.tpe, "inlined type argument", targ.pos)
91+
8992
private val prefix = methPart match {
9093
case Select(qual, _) => qual
9194
case _ => tpd.This(ctx.owner.enclosingClass.asClass)
@@ -156,19 +159,9 @@ class Inliner(call: tpd.Tree, rhs: tpd.Tree)(implicit ctx: Context) {
156159

157160
private object InlineTyper extends ReTyper {
158161
override def typedSelect(tree: untpd.Select, pt: Type)(implicit ctx: Context): Tree = {
159-
val acc = tree.symbol
160-
super.typedSelect(tree, pt) match {
161-
case res @ Select(qual, name) =>
162-
if (name.endsWith(nme.OUTER)) {
163-
val outerAcc = tree.symbol
164-
res.withType(qual.tpe.widen.normalizedPrefix)
165-
}
166-
else {
167-
ensureAccessible(res.tpe, qual.isInstanceOf[Super], tree.pos)
168-
res
169-
}
170-
case res => res
171-
}
162+
val res = super.typedSelect(tree, pt)
163+
ensureAccessible(res.tpe, tree.qualifier.isInstanceOf[untpd.Super], tree.pos)
164+
res
172165
}
173166
override def typedIf(tree: untpd.If, pt: Type)(implicit ctx: Context) = {
174167
val cond1 = typed(tree.cond, defn.BooleanType)
@@ -207,26 +200,27 @@ class Inliner(call: tpd.Tree, rhs: tpd.Tree)(implicit ctx: Context) {
207200
val typeMap = new TypeMap {
208201
def apply(t: Type) = t match {
209202
case t: ThisType => thisProxy.getOrElse(t, t)
210-
case t: TypeRef => paramProxy.getOrElse(t, t)
211-
case t: SingletonType => paramProxy.getOrElse(t, t)
203+
case t: TypeRef => paramProxy.getOrElse(t, mapOver(t))
204+
case t: SingletonType => paramProxy.getOrElse(t, mapOver(t))
212205
case t => mapOver(t)
213206
}
214207
}
215208

216-
def treeMap(tree: Tree) = tree match {
209+
def treeMap(tree: Tree) = {
210+
tree match {
217211
case _: This =>
218212
thisProxy.get(tree.tpe) match {
219-
case Some(t) => ref(t)
213+
case Some(t) => ref(t).withPos(tree.pos)
220214
case None => tree
221215
}
222216
case _: Ident =>
223217
paramProxy.get(tree.tpe) match {
224-
case Some(t: SingletonType) if tree.isTerm => singleton(t)
225-
case Some(t) if tree.isType => TypeTree(t)
218+
case Some(t: SingletonType) if tree.isTerm => singleton(t).withPos(tree.pos)
219+
case Some(t) if tree.isType => TypeTree(t).withPos(tree.pos)
226220
case None => tree
227221
}
228222
case _ => tree
229-
}
223+
}}
230224

231225
val inliner = new TreeTypeMap(typeMap, treeMap, meth :: Nil, ctx.owner :: Nil)
232226
val bindings = bindingsBuf.toList.map(_.withPos(call.pos))
@@ -235,7 +229,7 @@ class Inliner(call: tpd.Tree, rhs: tpd.Tree)(implicit ctx: Context) {
235229
val expansion1 = InlineTyper.typed(expansion, pt)(inlineContext(call))
236230
val result = tpd.Inlined(call, bindings, expansion1)
237231

238-
inlining.println(i"inlining $call\n --> \n$result")
232+
inlining.println(i"inlined $call\n --> \n$result")
239233
result
240234
}
241235
}

src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1757,7 +1757,9 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
17571757
}
17581758
else if (tree.tpe <:< pt)
17591759
if (tree.symbol.isInlineMethod &&
1760+
Inliner.inlinedBody(tree.symbol).isDefined &&
17601761
!ctx.owner.ownersIterator.exists(_.isInlineMethod) &&
1762+
!ctx.settings.YnoInline.value &&
17611763
!ctx.isAfterTyper)
17621764
adapt(Inliner.inlineCall(tree, pt), pt)
17631765
else if (ctx.typeComparer.GADTused && pt.isValueType)

src/dotty/tools/dotc/util/Stats.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import collection.mutable
2020
override def default(key: String): Int = 0
2121
}
2222

23-
@dotty.annotation.inline
23+
@inline
2424
def record(fn: String, n: Int = 1) =
2525
if (enabled) doRecord(fn, n)
2626

@@ -30,7 +30,7 @@ import collection.mutable
3030
hits(name) += n
3131
}
3232

33-
@dotty.annotation.inline
33+
@inline
3434
def track[T](fn: String)(op: => T) =
3535
if (enabled) doTrack(fn)(op) else op
3636

test/dotc/tests.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ class tests extends CompilerTest {
156156
.filter(_.nonEmpty)
157157
.toList
158158

159-
@Test def compileStdLib = compileList("compileStdLib", stdlibFiles, "-migration" :: scala2mode)
159+
@Test def compileStdLib = compileList("compileStdLib", stdlibFiles, "-migration" :: "-Yno-inline" :: scala2mode)
160160
@Test def compileMixed = compileLine(
161161
"""tests/pos/B.scala
162162
|./scala-scala/src/library/scala/collection/immutable/Seq.scala
@@ -274,7 +274,7 @@ class tests extends CompilerTest {
274274
"ClassOf.scala", "CollectEntryPoints.scala", "Constructors.scala", "CrossCastAnd.scala",
275275
"CtxLazy.scala", "ElimByName.scala", "ElimErasedValueType.scala", "ElimRepeated.scala",
276276
"ElimStaticThis.scala", "Erasure.scala", "ExpandPrivate.scala", "ExpandSAMs.scala",
277-
"ExplicitOuter.scala", "ExplicitSelf.scala", "ExtensionMethods.scala", "FirstTransform.scala",
277+
"ExplicitOuter.scala", "ExtensionMethods.scala", "FirstTransform.scala",
278278
"Flatten.scala", "FullParameterization.scala", "FunctionalInterfaces.scala", "GetClass.scala",
279279
"Getters.scala", "InterceptedMethods.scala", "LambdaLift.scala", "LiftTry.scala", "LinkScala2ImplClasses.scala",
280280
"MacroTransform.scala", "Memoize.scala", "Mixin.scala", "MixinOps.scala", "NonLocalReturns.scala",

tests/neg/inlineAccess.scala

Lines changed: 0 additions & 15 deletions
This file was deleted.

tests/neg/inlineAccess/C_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package p {
22
class C {
33
protected def f(): Unit = ()
44

5-
@dotty.annotation.inline
5+
@inline
66
def inl() = f() // error (when inlined): not accessible
77
}
88
}

tests/neg/power.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
object Test {
22

3-
@dotty.annotation.inline
3+
@inline
44
def power(x: Double, n: Int): Double =
55
if (n == 0) 1.0
66
else if (n == 1) x

tests/run/inline/inlines_1.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ object inlines {
55

66
final val monitored = false
77

8-
@dotty.annotation.inline
8+
@inline
99
def f(x: Int): Int = x * x
1010

1111
val hits = new mutable.HashMap[String, Int] {
@@ -21,7 +21,7 @@ object inlines {
2121

2222
@volatile private var stack: List[String] = Nil
2323

24-
@dotty.annotation.inline
24+
@inline
2525
def track[T](fn: String)(op: => T) =
2626
if (monitored) {
2727
stack = fn :: stack
@@ -34,9 +34,9 @@ object inlines {
3434
def f = "Outer.f"
3535
class Inner {
3636
val msg = " Inner"
37-
@dotty.annotation.inline def m = msg
38-
@dotty.annotation.inline def g = f
39-
@dotty.annotation.inline def h = f ++ m
37+
@inline def m = msg
38+
@inline def g = f
39+
@inline def h = f ++ m
4040
}
4141
val inner = new Inner
4242
}

tests/run/inlinePower/power_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package p
22

33
object pow {
44

5-
@dotty.annotation.inline
5+
@inline
66
def power(x: Double, n: Int): Double =
77
if (n == 0) 1.0
88
else if (n == 1) x

tests/run/inlinedAssign.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
object Test {
22

3-
@dotty.annotation.inline
3+
@inline
44
def swap[T](x: T, x_= : T => Unit, y: T, y_= : T => Unit) = {
55
val t = x
66
x_=(y)
@@ -10,8 +10,8 @@ object Test {
1010
def main(args: Array[String]) = {
1111
var x = 1
1212
var y = 2
13-
@dotty.annotation.inline def setX(z: Int) = x = z
14-
@dotty.annotation.inline def setY(z: Int) = y = z
13+
@inline def setX(z: Int) = x = z
14+
@inline def setY(z: Int) = y = z
1515
swap[Int](x, setX, y, setY)
1616
assert(x == 2 && y == 1)
1717
}

0 commit comments

Comments
 (0)