Skip to content

Commit bc51a19

Browse files
committed
AsyncMacro.global is gone
1 parent ac7ba71 commit bc51a19

File tree

9 files changed

+76
-53
lines changed

9 files changed

+76
-53
lines changed

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55

66
package scala.async.internal
77

8-
import scala.tools.nsc.Global
98
import scala.Predef._
9+
import scala.reflect.internal.util.Collections.map2
1010

1111
private[async] trait AnfTransform {
1212
self: AsyncMacro =>
1313

14-
import c.universe.{gen => _, _}
14+
import c.universe._
1515
import Flag._
1616
import c.internal._
1717
import decorators._
@@ -61,7 +61,7 @@ private[async] trait AnfTransform {
6161
} else {
6262
val varDef = defineVar(name.ifRes, expr.tpe, tree.pos)
6363
def branchWithAssign(orig: Tree) = api.typecheck(atPos(orig.pos) {
64-
def cast(t: Tree) = mkAttributedCastPreservingAnnotations(t, varDef.symbol.tpe)
64+
def cast(t: Tree) = mkAttributedCastPreservingAnnotations(t, tpe(varDef.symbol))
6565
orig match {
6666
case Block(thenStats, thenExpr) => Block(thenStats, Assign(Ident(varDef.symbol), cast(thenExpr)))
6767
case _ => Assign(Ident(varDef.symbol), cast(orig))
@@ -82,7 +82,7 @@ private[async] trait AnfTransform {
8282
else {
8383
val varDef = defineVar(name.matchRes, expr.tpe, tree.pos)
8484
def typedAssign(lhs: Tree) =
85-
api.typecheck(atPos(lhs.pos)(Assign(Ident(varDef.symbol), mkAttributedCastPreservingAnnotations(lhs, varDef.symbol.tpe))))
85+
api.typecheck(atPos(lhs.pos)(Assign(Ident(varDef.symbol), mkAttributedCastPreservingAnnotations(lhs, tpe(varDef.symbol)))))
8686
val casesWithAssign = cases map {
8787
case cd@CaseDef(pat, guard, body) =>
8888
val newBody = body match {
@@ -102,7 +102,7 @@ private[async] trait AnfTransform {
102102

103103
private def defineVar(prefix: String, tp: Type, pos: Position): ValDef = {
104104
val sym = api.currentOwner.newTermSymbol(name.fresh(prefix), pos, MUTABLE | SYNTHETIC).setInfo(uncheckedBounds(tp))
105-
ValDef(sym, gen.mkZero(uncheckedBounds(tp))).setType(NoType).setPos(pos)
105+
valDef(sym, gen.mkZero(uncheckedBounds(tp))).setType(NoType).setPos(pos)
106106
}
107107
}
108108

@@ -129,7 +129,7 @@ private[async] trait AnfTransform {
129129
def defineVal(prefix: String, lhs: Tree, pos: Position): ValDef = {
130130
val sym = api.currentOwner.newTermSymbol(name.fresh(prefix), pos, SYNTHETIC).setInfo(uncheckedBounds(lhs.tpe))
131131
lhs.changeOwner(api.currentOwner, sym)
132-
ValDef(sym, lhs.changeOwner(api.currentOwner, sym)).setType(NoType).setPos(pos)
132+
valDef(sym, lhs.changeOwner(api.currentOwner, sym)).setType(NoType).setPos(pos)
133133
}
134134

135135
object anf {
@@ -161,7 +161,7 @@ private[async] trait AnfTransform {
161161
val stats :+ expr1 = linearize.transformToList(expr)
162162
stats :+ treeCopy.Typed(tree, expr1, tpt)
163163

164-
case treeInfo.Applied(fun, targs, argss) if argss.nonEmpty =>
164+
case q"$fun[..$targs](...$argss)" if argss.nonEmpty =>
165165
// we can assume that no await call appears in a by-name argument position,
166166
// this has already been checked.
167167
val funStats :+ simpleFun = linearize.transformToList(fun)
@@ -271,7 +271,7 @@ private[async] trait AnfTransform {
271271
case _: ValDef | _: DefDef | _: Function | _: ClassDef | _: TypeDef =>
272272
api.atOwner(tree.symbol)(anfLinearize(tree))
273273
case _: ModuleDef =>
274-
api.atOwner(tree.symbol.moduleClass orElse tree.symbol)(anfLinearize(tree))
274+
api.atOwner(tree.symbol.asModule.moduleClass orElse tree.symbol)(anfLinearize(tree))
275275
case _ =>
276276
anfLinearize(tree)
277277
}

src/main/scala/scala/async/internal/AsyncAnalysis.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ trait AsyncAnalysis {
2828
var hasUnsupportedAwaits = false
2929

3030
override def nestedClass(classDef: ClassDef) {
31-
val kind = if (classDef.symbol.isTrait) "trait" else "class"
31+
val kind = if (classDef.symbol.asClass.isTrait) "trait" else "class"
3232
reportUnsupportedAwait(classDef, s"nested ${kind}")
3333
}
3434

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,7 @@ abstract class AsyncBase {
4545
import c.universe._, c.internal._, decorators._
4646
val asyncMacro = AsyncMacro(c, self)
4747

48-
val code = asyncMacro.asyncTransform[T](
49-
body.tree.asInstanceOf[asyncMacro.global.Tree],
50-
execContext.tree.asInstanceOf[asyncMacro.global.Tree]
51-
)(implicitly[c.WeakTypeTag[T]].asInstanceOf[asyncMacro.global.WeakTypeTag[T]]).asInstanceOf[Tree]
48+
val code = asyncMacro.asyncTransform[T](body.tree, execContext.tree)(c.weakTypeTag[T])
5249
AsyncUtils.vprintln(s"async state machine transform expands to:\n ${code}")
5350

5451
// Mark range positions for synthetic code as transparent to allow some wiggle room for overlapping ranges
Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
package scala.async.internal
22

3-
import scala.tools.nsc.Global
4-
import scala.tools.nsc.transform.TypingTransformers
5-
63
object AsyncMacro {
7-
def apply(c0: reflect.macros.Context, base: AsyncBase): AsyncMacro = {
4+
def apply(c0: reflect.macros.Context, base: AsyncBase): AsyncMacro { val c: c0.type } = {
85
import language.reflectiveCalls
9-
val powerContext = c0.asInstanceOf[c0.type { val universe: Global; val callsiteTyper: universe.analyzer.Typer }]
106
new AsyncMacro { self =>
11-
val c: scala.reflect.macros.Context { val universe: global.type } = c0.asInstanceOf[scala.reflect.macros.Context { val universe: global.type }]
12-
val global: powerContext.universe.type = powerContext.universe
7+
val c: c0.type = c0
138
// This member is required by `AsyncTransform`:
14-
val asyncBase: AsyncBase = base
9+
val asyncBase: AsyncBase = base
1510
// These members are required by `ExprBuilder`:
16-
val futureSystem: FutureSystem = base.futureSystem
11+
val futureSystem: FutureSystem = base.futureSystem
1712
val futureSystemOps: futureSystem.Ops {val c: self.c.type} = futureSystem.mkOps(c)
1813
}
1914
}
@@ -23,9 +18,8 @@ private[async] trait AsyncMacro
2318
extends AnfTransform with TransformUtils with Lifter
2419
with ExprBuilder with AsyncTransform with AsyncAnalysis with LiveVariables {
2520

26-
val c: scala.reflect.macros.Context { val universe: global.type }
27-
val global: Global
21+
val c: scala.reflect.macros.Context
2822

2923
lazy val macroPos = c.macroApplication.pos.makeTransparent
30-
def atMacroPos(t: global.Tree) = c.universe.atPos(macroPos)(t)
24+
def atMacroPos(t: c.Tree) = c.universe.atPos(macroPos)(t)
3125
}

src/main/scala/scala/async/internal/AsyncTransform.scala

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package scala.async.internal
33
trait AsyncTransform {
44
self: AsyncMacro =>
55

6-
import c.universe.{gen => _, _}
6+
import c.universe._
77
import c.internal._
88
import decorators._
99

@@ -15,7 +15,7 @@ trait AsyncTransform {
1515
// We annotate the type of the whole expression as `T @uncheckedBounds` so as not to introduce
1616
// warnings about non-conformant LUBs. See SI-7694
1717
// This implicit propagates the annotated type in the type tag.
18-
implicit val uncheckedBoundsResultTag: WeakTypeTag[T] = WeakTypeTag[T](rootMirror, FixedMirrorTypeCreator(rootMirror, uncheckedBounds(resultType.tpe)))
18+
implicit val uncheckedBoundsResultTag: WeakTypeTag[T] = c.WeakTypeTag[T](uncheckedBounds(resultType.tpe))
1919

2020
reportUnsupportedAwaits(body)
2121

@@ -82,7 +82,7 @@ trait AsyncTransform {
8282
List(
8383
asyncBase.nullOut(c.universe)(c.Expr[String](Literal(Constant(fieldSym.name.toString))), c.Expr[Any](Ident(fieldSym))).tree
8484
),
85-
Assign(gen.mkAttributedStableRef(fieldSym.owner.thisType, fieldSym), gen.mkZero(fieldSym.info))
85+
Assign(gen.mkAttributedStableRef(thisType(fieldSym.owner), fieldSym), gen.mkZero(fieldSym.info))
8686
)
8787
}
8888
val asyncState = asyncBlock.asyncStates.find(_.state == state).get
@@ -143,9 +143,9 @@ trait AsyncTransform {
143143
liftedSyms.foreach {
144144
sym =>
145145
if (sym != null) {
146-
sym.owner = stateMachineClass
146+
sym.setOwner(stateMachineClass)
147147
if (sym.isModule)
148-
sym.moduleClass.owner = stateMachineClass
148+
sym.asModule.moduleClass.setOwner(stateMachineClass)
149149
}
150150
}
151151
// Replace the ValDefs in the splicee with Assigns to the corresponding lifted
@@ -158,7 +158,7 @@ trait AsyncTransform {
158158
case ValDef(_, _, _, rhs) if liftedSyms(tree.symbol) =>
159159
api.atOwner(api.currentOwner) {
160160
val fieldSym = tree.symbol
161-
val set = Assign(gen.mkAttributedStableRef(fieldSym.owner.thisType, fieldSym), api.recur(rhs))
161+
val set = Assign(gen.mkAttributedStableRef(thisType(fieldSym.owner.asClass), fieldSym), api.recur(rhs))
162162
set.changeOwner(tree.symbol, api.currentOwner)
163163
api.typecheck(atPos(tree.pos)(set))
164164
}
@@ -167,7 +167,7 @@ trait AsyncTransform {
167167
case Ident(name) if liftedSyms(tree.symbol) =>
168168
val fieldSym = tree.symbol
169169
atPos(tree.pos) {
170-
gen.mkAttributedStableRef(fieldSym.owner.thisType, fieldSym).setType(tree.tpe)
170+
gen.mkAttributedStableRef(thisType(fieldSym.owner.asClass), fieldSym).setType(tree.tpe)
171171
}
172172
case _ =>
173173
api.default(tree)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ trait ExprBuilder {
1515

1616
import c.universe._
1717
import defn._
18+
import c.internal._
1819

1920
val futureSystem: FutureSystem
2021
val futureSystemOps: futureSystem.Ops { val c: builder.c.type }

src/main/scala/scala/async/internal/Lifter.scala

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ trait Lifter {
44
self: AsyncMacro =>
55
import c.universe._
66
import Flag._
7+
import c.internal._
8+
import decorators._
79

810
/**
911
* Identify which DefTrees are used (including transitively) which are declared
@@ -88,7 +90,7 @@ trait Lifter {
8890

8991
// Only mark transitive references of defs, modules and classes. The RHS of lifted vals/vars
9092
// stays in its original location, so things that it refers to need not be lifted.
91-
if (!(sym.isVal || sym.isVar))
93+
if (!(sym.isTerm && (sym.asTerm.isVal || sym.asTerm.isVar)))
9294
defSymToReferenced(sym).foreach(sym2 => markForLift(sym2))
9395
}
9496
}
@@ -111,35 +113,35 @@ trait Lifter {
111113
val treeLifted = t match {
112114
case vd@ValDef(_, _, tpt, rhs) =>
113115
sym.setFlag(MUTABLE | STABLE | PRIVATE | LOCAL)
114-
sym.name = name.fresh(sym.name.toTermName)
115-
sym.modifyInfo(_.deconst)
116+
sym.setName(name.fresh(sym.name.toTermName))
117+
sym.setInfo(deconst(sym.info))
116118
val zeroRhs = atPos(t.pos)(gen.mkZero(vd.symbol.info))
117-
treeCopy.ValDef(vd, Modifiers(sym.flags), sym.name, TypeTree(sym.tpe).setPos(t.pos), zeroRhs)
119+
treeCopy.ValDef(vd, Modifiers(sym.flags), sym.name, TypeTree(tpe(sym)).setPos(t.pos), zeroRhs)
118120
case dd@DefDef(_, _, tparams, vparamss, tpt, rhs) =>
119-
sym.name = this.name.fresh(sym.name.toTermName)
121+
sym.setName(this.name.fresh(sym.name.toTermName))
120122
sym.setFlag(PRIVATE | LOCAL)
121123
// Was `DefDef(sym, rhs)`, but this ran afoul of `ToughTypeSpec.nestedMethodWithInconsistencyTreeAndInfoParamSymbols`
122124
// due to the handling of type parameter skolems in `thisMethodType` in `Namers`
123125
treeCopy.DefDef(dd, Modifiers(sym.flags), sym.name, tparams, vparamss, tpt, rhs)
124126
case cd@ClassDef(_, _, tparams, impl) =>
125-
sym.name = newTypeName(name.fresh(sym.name.toString).toString)
127+
sym.setName(newTypeName(name.fresh(sym.name.toString).toString))
126128
companionship.companionOf(cd.symbol) match {
127129
case NoSymbol =>
128130
case moduleSymbol =>
129-
moduleSymbol.name = sym.name.toTermName
130-
moduleSymbol.moduleClass.name = moduleSymbol.name.toTypeName
131+
moduleSymbol.setName(sym.name.toTermName)
132+
moduleSymbol.asModule.moduleClass.setName(moduleSymbol.name.toTypeName)
131133
}
132134
treeCopy.ClassDef(cd, Modifiers(sym.flags), sym.name, tparams, impl)
133135
case md@ModuleDef(_, _, impl) =>
134136
companionship.companionOf(md.symbol) match {
135137
case NoSymbol =>
136-
sym.name = name.fresh(sym.name.toTermName)
137-
sym.moduleClass.name = sym.name.toTypeName
138+
sym.setName(name.fresh(sym.name.toTermName))
139+
sym.asModule.moduleClass.setName(sym.name.toTypeName)
138140
case classSymbol => // will be renamed by `case ClassDef` above.
139141
}
140142
treeCopy.ModuleDef(md, Modifiers(sym.flags), sym.name, impl)
141143
case td@TypeDef(_, _, tparams, rhs) =>
142-
sym.name = newTypeName(name.fresh(sym.name.toString).toString)
144+
sym.setName(newTypeName(name.fresh(sym.name.toString).toString))
143145
treeCopy.TypeDef(td, Modifiers(sym.flags), sym.name, tparams, rhs)
144146
}
145147
atPos(t.pos)(treeLifted)

src/main/scala/scala/async/internal/LiveVariables.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ trait LiveVariables {
5555

5656
// determine which fields should be live also at the end (will not be nulled out)
5757
val noNull: Set[Symbol] = liftedSyms.filter { sym =>
58-
sym.tpe.typeSymbol.isPrimitiveValueClass || liftables.exists { tree =>
58+
tpe(sym).typeSymbol.asClass.isPrimitive || liftables.exists { tree =>
5959
!liftedSyms.contains(tree.symbol) && tree.exists(_.symbol == sym)
6060
}
6161
}

src/main/scala/scala/async/internal/TransformUtils.scala

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@ package scala.async.internal
55

66
import scala.reflect.macros.Context
77
import reflect.ClassTag
8+
import scala.collection.immutable.ListMap
89

910
/**
1011
* Utilities used in both `ExprBuilder` and `AnfTransform`.
1112
*/
1213
private[async] trait TransformUtils {
1314
self: AsyncMacro =>
1415

15-
import c.universe.{gen => _, _}
16+
import c.universe._
1617
import c.internal._
18+
import decorators._
1719

1820
object name {
1921
val resume = newTermName("resume")
@@ -51,7 +53,7 @@ private[async] trait TransformUtils {
5153
if (Boolean_ShortCircuits contains fun.symbol) (i, j) => true
5254
else {
5355
val paramss = fun.tpe.paramss
54-
val byNamess = paramss.map(_.map(_.isByNameParam))
56+
val byNamess = paramss.map(_.map(_.asTerm.isByNameParam))
5557
(i, j) => util.Try(byNamess(i)(j)).getOrElse(false)
5658
}
5759
}
@@ -86,10 +88,6 @@ private[async] trait TransformUtils {
8688
val Async_await = asyncBase.awaitMethod(c.universe)(c.macroApplication.symbol).ensuring(_ != NoSymbol)
8789
}
8890

89-
def isSafeToInline(tree: Tree) = {
90-
treeInfo.isExprSafeToInline(tree)
91-
}
92-
9391
// `while(await(x))` ... or `do { await(x); ... } while(...)` contain an `If` that loops;
9492
// we must break that `If` into states so that it convert the label jump into a state machine
9593
// transition
@@ -192,7 +190,7 @@ private[async] trait TransformUtils {
192190
case dd: DefDef => nestedMethod(dd)
193191
case fun: Function => function(fun)
194192
case m@Match(EmptyTree, _) => patMatFunction(m) // Pattern matching anonymous function under -Xoldpatmat of after `restorePatternMatchingFunctions`
195-
case treeInfo.Applied(fun, targs, argss) if argss.nonEmpty =>
193+
case q"$fun[..$targs](...$argss)" if argss.nonEmpty =>
196194
val isInByName = isByName(fun)
197195
for ((args, i) <- argss.zipWithIndex) {
198196
for ((arg, j) <- args.zipWithIndex) {
@@ -219,11 +217,42 @@ private[async] trait TransformUtils {
219217
// Attributed version of `TreeGen#mkCastPreservingAnnotations`
220218
def mkAttributedCastPreservingAnnotations(tree: Tree, tp: Type): Tree = {
221219
atPos(tree.pos) {
222-
val casted = c.typecheck(gen.mkCast(tree, uncheckedBounds(tp.withoutAnnotations).dealias))
220+
val casted = c.typecheck(gen.mkCast(tree, uncheckedBounds(withoutAnnotations(tp)).dealias))
223221
Typed(casted, TypeTree(tp)).setType(tp)
224222
}
225223
}
226224

225+
def deconst(tp: Type): Type = tp match {
226+
case AnnotatedType(anns, underlying) => annotatedType(anns, deconst(underlying))
227+
case ExistentialType(quants, underlying) => existentialType(quants, deconst(underlying))
228+
case ConstantType(value) => deconst(value.tpe)
229+
case _ => tp
230+
}
231+
232+
def withAnnotation(tp: Type, ann: Annotation): Type = withAnnotations(tp, List(ann))
233+
234+
def withAnnotations(tp: Type, anns: List[Annotation]): Type = tp match {
235+
case AnnotatedType(existingAnns, underlying) => annotatedType(anns ::: existingAnns, underlying)
236+
case ExistentialType(quants, underlying) => existentialType(quants, withAnnotations(underlying, anns))
237+
case _ => annotatedType(anns, tp)
238+
}
239+
240+
def withoutAnnotations(tp: Type): Type = tp match {
241+
case AnnotatedType(anns, underlying) => withoutAnnotations(underlying)
242+
case ExistentialType(quants, underlying) => existentialType(quants, withoutAnnotations(underlying))
243+
case _ => tp
244+
}
245+
246+
def tpe(sym: Symbol): Type = {
247+
if (sym.isType) sym.asType.toType
248+
else sym.info
249+
}
250+
251+
def thisType(sym: Symbol): Type = {
252+
if (sym.isClass) sym.asClass.thisPrefix
253+
else NoPrefix
254+
}
255+
227256
// =====================================
228257
// Copy/Pasted from Scala 2.10.3. See SI-7694.
229258
private lazy val UncheckedBoundsClass = {
@@ -232,7 +261,7 @@ private[async] trait TransformUtils {
232261
}
233262
final def uncheckedBounds(tp: Type): Type = {
234263
if (tp.typeArgs.isEmpty || UncheckedBoundsClass == NoSymbol) tp
235-
else tp.withAnnotation(AnnotationInfo marker UncheckedBoundsClass.tpe)
264+
else withAnnotation(tp, Annotation(UncheckedBoundsClass.asType.toType, Nil, ListMap()))
236265
}
237266
// =====================================
238267
}

0 commit comments

Comments
 (0)