Skip to content

Commit 94d7413

Browse files
committed
Merge pull request #28 from scala/topic/cleanups
Collection of clean-ups
2 parents 0c5a1ea + 0bdace2 commit 94d7413

File tree

6 files changed

+30
-40
lines changed

6 files changed

+30
-40
lines changed

src/main/scala/scala/async/internal/AnfTransform.scala

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,12 @@ private[async] trait AnfTransform {
127127
private object trace {
128128
private var indent = -1
129129

130-
def indentString = " " * indent
130+
private def indentString = " " * indent
131131

132132
def apply[T](args: Any)(t: => T): T = {
133133
def prefix = mode.toString.toLowerCase
134134
indent += 1
135-
def oneLine(s: Any) = s.toString.replaceAll( """\n""", "\\\\n").take(127)
135+
def oneLine(s: Any) = s.toString.replaceAll("""\n""", "\\\\n").take(127)
136136
try {
137137
AsyncUtils.trace(s"${indentString}$prefix(${oneLine(args)})")
138138
val result = t
@@ -173,7 +173,7 @@ private[async] trait AnfTransform {
173173
stats :+ treeCopy.Typed(tree, expr1, tpt)
174174

175175
case treeInfo.Applied(fun, targs, argss) if argss.nonEmpty =>
176-
// we an assume that no await call appears in a by-name argument position,
176+
// we can assume that no await call appears in a by-name argument position,
177177
// this has already been checked.
178178
val funStats :+ simpleFun = linearize.transformToList(fun)
179179
val (argStatss, argExprss): (List[List[List[Tree]]], List[List[Tree]]) =
@@ -221,14 +221,7 @@ private[async] trait AnfTransform {
221221
val condStats :+ condExpr = linearize.transformToList(cond)
222222
val thenBlock = linearize.transformToBlock(thenp)
223223
val elseBlock = linearize.transformToBlock(elsep)
224-
// Typechecking with `condExpr` as the condition fails if the condition
225-
// contains an await. `ifTree.setType(tree.tpe)` also fails; it seems
226-
// we rely on this call to `typeCheck` descending into the branches.
227-
// But, we can get away with typechecking a throwaway `If` tree with the
228-
// original scrutinee and the new branches, and setting that type on
229-
// the real `If` tree.
230-
val iff = treeCopy.If(tree, condExpr, thenBlock, elseBlock)
231-
condStats :+ iff
224+
condStats :+ treeCopy.If(tree, condExpr, thenBlock, elseBlock)
232225

233226
case Match(scrut, cases) =>
234227
val scrutStats :+ scrutExpr = linearize.transformToList(scrut)
@@ -248,9 +241,7 @@ private[async] trait AnfTransform {
248241
val newBlock = treeCopy.Block(b, valDefs ++ stats1, expr1)
249242
treeCopy.CaseDef(tree, pat, guard, newBlock)
250243
}
251-
// Refer to comments the translation of `If` above.
252-
val typedMatch = treeCopy.Match(tree, scrutExpr, caseDefs)
253-
scrutStats :+ typedMatch
244+
scrutStats :+ treeCopy.Match(tree, scrutExpr, caseDefs)
254245

255246
case LabelDef(name, params, rhs) =>
256247
List(LabelDef(name, params, Block(linearize.transformToList(rhs), Literal(Constant(())))).setSymbol(tree.symbol))

src/main/scala/scala/async/internal/AsyncBase.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ abstract class AsyncBase {
5252
execContext.tree.asInstanceOf[asyncMacro.global.Tree],
5353
fallbackEnabled)(implicitly[c.WeakTypeTag[T]].asInstanceOf[asyncMacro.global.WeakTypeTag[T]]).asInstanceOf[Tree]
5454

55+
// Mark range positions for synthetic code as transparent to allow some wiggle room for overlapping ranges
5556
for (t <- code)
5657
t.pos = t.pos.makeTransparent
5758

src/main/scala/scala/async/internal/AsyncMacro.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import scala.tools.nsc.transform.TypingTransformers
66
object AsyncMacro {
77
def apply(c: reflect.macros.Context, futureSystem0: FutureSystem): AsyncMacro = {
88
import language.reflectiveCalls
9-
val powerContext = c.asInstanceOf[c.type {val universe: Global; val callsiteTyper: universe.analyzer.Typer}]
9+
val powerContext = c.asInstanceOf[c.type { val universe: Global; val callsiteTyper: universe.analyzer.Typer }]
1010
new AsyncMacro {
1111
val global: powerContext.universe.type = powerContext.universe
1212
val callSiteTyper: global.analyzer.Typer = powerContext.callsiteTyper
@@ -26,7 +26,7 @@ private[async] trait AsyncMacro
2626
val callSiteTyper: global.analyzer.Typer
2727
val macroApplication: global.Tree
2828

29-
def macroPos = macroApplication.pos.makeTransparent
29+
lazy val macroPos = macroApplication.pos.makeTransparent
3030
def atMacroPos(t: global.Tree) = global.atPos(macroPos)(t)
3131

3232
}

src/main/scala/scala/async/internal/ExprBuilder.scala

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import collection.mutable
99
import language.existentials
1010
import scala.reflect.api.Universe
1111
import scala.reflect.api
12-
import scala.Some
1312

1413
trait ExprBuilder {
1514
builder: AsyncMacro =>
@@ -290,7 +289,8 @@ trait ExprBuilder {
290289
case class SymLookup(stateMachineClass: Symbol, applyTrParam: Symbol) {
291290
def stateMachineMember(name: TermName): Symbol =
292291
stateMachineClass.info.member(name)
293-
def memberRef(name: TermName) = gen.mkAttributedRef(stateMachineMember(name))
292+
def memberRef(name: TermName): Tree =
293+
gen.mkAttributedRef(stateMachineMember(name))
294294
}
295295

296296
def buildAsyncBlock(block: Block, symLookup: SymLookup): AsyncBlock = {
@@ -324,17 +324,17 @@ trait ExprBuilder {
324324

325325
/**
326326
* def resume(): Unit = {
327-
* try {
328-
* state match {
329-
* case 0 => {
330-
* f11 = exprReturningFuture
331-
* f11.onComplete(onCompleteHandler)(context)
332-
* }
333-
* ...
334-
* }
335-
* } catch {
336-
* case NonFatal(t) => result.failure(t)
337-
* }
327+
* try {
328+
* state match {
329+
* case 0 => {
330+
* f11 = exprReturningFuture
331+
* f11.onComplete(onCompleteHandler)(context)
332+
* }
333+
* ...
334+
* }
335+
* } catch {
336+
* case NonFatal(t) => result.failure(t)
337+
* }
338338
* }
339339
*/
340340
def resumeFunTree[T]: DefDef =
@@ -352,16 +352,17 @@ trait ExprBuilder {
352352
}), literalUnit))), EmptyTree))
353353

354354
/**
355-
* // assumes tr: Try[Any] is in scope.
356-
* //
355+
* assumes tr: Try[Any] is in scope.
356+
*
357357
* state match {
358-
* case 0 => {
359-
* x11 = tr.get.asInstanceOf[Double];
360-
* state = 1;
361-
* resume()
358+
* case 0 =>
359+
* x11 = tr.get.asInstanceOf[Double]
360+
* state = 1
361+
* resume()
362362
* }
363363
*/
364-
def onCompleteHandler[T: WeakTypeTag]: Tree = Match(symLookup.memberRef(name.state), initStates.flatMap(_.mkOnCompleteHandler[T]).toList)
364+
def onCompleteHandler[T: WeakTypeTag]: Tree =
365+
Match(symLookup.memberRef(name.state), initStates.flatMap(_.mkOnCompleteHandler[T]).toList)
365366
}
366367
}
367368

src/main/scala/scala/async/internal/FutureSystem.scala

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
package scala.async.internal
55

66
import scala.language.higherKinds
7-
8-
import scala.reflect.macros.Context
97
import scala.reflect.internal.SymbolTable
108

119
/**
@@ -54,7 +52,7 @@ trait FutureSystem {
5452
def spawn(tree: Tree, execContext: Tree): Tree =
5553
future(Expr[Unit](tree))(Expr[ExecContext](execContext)).tree
5654

57-
// TODO Why is this needed?
55+
// This is only needed in `AsyncBaseWithCPSFallback` and should be removed once CPS fall-back support is dropped.
5856
def castTo[A: WeakTypeTag](future: Expr[Fut[Any]]): Expr[Fut[A]]
5957
}
6058

src/test/scala/scala/async/TreeInterrogation.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ object TreeInterrogation extends App {
7272
import scala.async.Async._
7373
val tree = tb.parse(
7474
""" import _root_.scala.async.internal.AsyncId.{async, await}
75-
| import reflect.runtime.universe._
7675
| async {
7776
| implicit def view(a: Int): String = ""
7877
| await(0).length

0 commit comments

Comments
 (0)