aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/scala/async/internal/TransformUtils.scala
Commit message (Collapse)AuthorAgeFilesLines
* drop support for Scala 2.11 as of 0.10.0Seth Tisue2018-05-011-10/+7
| | | | also upgrade Scala 2.12.4 -> 2.12.6
* copyright 2018 LightbendSeth Tisue2018-02-061-1/+1
|
* Avoid NPE with import treesJason Zaugg2017-11-271-1/+1
|
* Workaround ill-scoped exist. skolem refs emited by patmatJason Zaugg2017-10-161-0/+40
| | | | | | | | | | | | | | | | | | | e.g `val x2 = Foo[$1] with Bar = boundValue` is rewritten to `val x2 = (Foo[$1] @uncheckedBounds) with Bar = boundValue` This is to have refchecks turn a blind eye to the type argument that doesn't conform the to type parameter bounds. For regular compilation, without the async transform between patmat and refchecks, bound conformance is disabled with: https://github.com/scala/scala/blob/v2.11.7/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala#L1743 Using the `uncheckedBounds` annotation is a newer, more inclusive way of acheiving the same thing: https://github.com/scala/scala/blob/v2.11.7/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala#L1677
* Fix ANF transform for corner case in late transformsJason Zaugg2017-09-291-0/+21
| | | | | Unfortunately I wasn't able to extract a test case, but the patch has been tested to fix a problem on a real world code base.
* Allow result field name to be externally specifiedJason Zaugg2017-09-271-2/+2
|
* Support future systems that perform external failure handlingJason Zaugg2017-09-271-0/+2
|
* Allow future system to enable more name fresheningJason Zaugg2017-09-271-13/+30
|
* Merge pull request #160 from retronym/topic/2.12Jason Zaugg2016-09-081-0/+8
|\ | | | | Compatibility with Scala 2.12.0-RC1
| * Compatibility with Scala 2.12.0-RC1Jason Zaugg2016-09-081-0/+8
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | - method local lazy vals are now encoded as a single ValDef rather than a ValDef + DefDef pair. We need to treat ValDef-s with the LAZY flag in the same way as we used to treat the DefDef. - Rename one of the symbols `ANF,anf` in the same scope to avoid generating anonymous class names that differ only in case. The compiler warned about this one. - When patching the LabelDefs to have a `Unit` result type, propagate this other LabelDefs conclude with a jump to that label. Not sure why, but without this we now hit an error in the backend about the nonsensical attempt to emit a coercion from void to int. - Use crossScalaVersions in the build and update the Scala versions tested in CI.
* | Rework extension point for checking for already-completed futuresJason Zaugg2016-03-031-0/+1
|/ | | | | | | | | | | | The current extension point assumes that if a future is in the completed state, a subsequent call to get the already completed value will succeed. While this assumption holds for scala.concurrent.Future, it might not hold for a future system that has semantics like a weak reference. This commit uses a single call `getCompleted` to query the state and get the already completed value. This returns null if the value is not available.
* Various fixes to late expansionJason Zaugg2016-01-191-3/+9
| | | | | | | | | | | | | | | | | | - Detect cross-state symbol references where the RefTree is nested in a LabelDef. Failure to do so led to ill-scoped local variable references which sometimes manifest as VerifyErrors. - Emit a default case in the Match intended to be a tableswitch. We have to do this ourselves if we expand after pattern matcher - Cleanup generated code to avoid redundant blocks - Avoid unnecessary `matchRes` temporary variable for unit-typed pattern matches - Fix the trace level logging in the ANF transform to restore indented output. - Emit `{ state = nextState; ... }` rather than `try { ... } finally { state = nextState }` in state handlers. This simplifies generated code and has the same meaning, as the code in the state machine isn't reentrant and can't observe the "early" transition of the state.
* Avoid spurious "illegal await" error in IDE with nestingJason Zaugg2015-10-091-6/+16
| | | | | | | | | | | | | | | | | | | The presentation compiler runs with `-Ymacro-expand:discard`, which retains the macro expandee in the typechecked trees, rather than substituting in the expansion. This mode was motivated as a means to keep IDE functionality working (e.g. completion, navigation, refactoring) inside macro applications. However, if one has nested async macro applications, as reported in the IDE ticket: https://www.assembla.com/spaces/scala-ide/tickets/1002561 ... the expansion of the outer async application was reporting await calls enclosed by the inner async application. This change tweaks the traversers used for this analysis to stop whenever it sees an async.
* Small cleanups after code reviewJason Zaugg2015-09-231-8/+27
| | | | | | | | - More internal docs - Be more frugal with the `NoAwait` attachment, for some AST node types this is implied. - Just use `x`, rather than what was effectively `x.reverseMap(identity).reverse`
* Enable a compiler plugin to use the async transform after patmatJason Zaugg2015-09-221-2/+124
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Currently, the async transformation is performed during the typer phase, like all other macros. We have to levy a few artificial restrictions on whern an async boundary may be: for instance we don't support await within a pattern guard. A more natural home for the transform would be after patterns have been translated. The test case in this commit shows how to use the async transform from a custom compiler phase after patmat. The remainder of the commit updates the implementation to handle the new tree shapes. For states that correspond to a label definition, we use `-symbol.id` as the state ID. This made it easier to emit the forward jumps to when processing the label application before we had seen the label definition. I've also made the transformation more efficient in the way it checks whether a given tree encloses an `await` call: we traverse the input tree at the start of the macro, and decorate it with tree attachments containig the answer to this question. Even after the ANF and state machine transforms introduce new layers of synthetic trees, the `containsAwait` code need only traverse shallowly through those trees to find a child that has the cached answer from the original traversal. I had to special case the ANF transform for expressions that always lead to a label jump: we avoids trying to push an assignment to a result variable into `if (cond) jump1() else jump2()`, in trees of the form: ``` % cat sandbox/jump.scala class Test { def test = { (null: Any) match { case _: String => "" case _ => "" } } } % qscalac -Xprint:patmat -Xprint-types sandbox/jump.scala def test: String = { case <synthetic> val x1: Any = (null{Null(null)}: Any){Any}; case5(){ if (x1.isInstanceOf{[T0]=> Boolean}[String]{Boolean}) matchEnd4{(x: String)String}(""{String("")}){String} else case6{()String}(){String}{String} }{String}; case6(){ matchEnd4{(x: String)String}(""{String("")}){String} }{String}; matchEnd4(x: String){ x{String} }{String} }{String} ```
* Avoid masking user exception with ??? for Nothing typed expressionsJason Zaugg2015-07-271-0/+1
| | | | | | | | | | | | | | | | | | | | | | Code like: val x = if (cond) throw new A else throw new B Was being transformed to: val ifRes = ??? if (cond) ifRes = throw new A else ifRes = throw new B val x = ifRes by way of the use of `gen.mkZero` which throws `???` if the requested type is `Nothing` This commit special cases `Nothing` typed expressions in a similar manner to `Unit` type expressions. The example above is now translated to: if (cond) throw new A else throw new B val x = throw new IllegalStateException() Fixes #120
* Fix compiler crash with value class in result positionJason Zaugg2015-07-061-2/+1
| | | | | | | We were leaking untyped trees out of the macro, which crashed in refchecks. This commit proactively typechecks the tree returned by `mkZero`.
* Merge remote-tracking branch 'origin/2.10.x' into ↵Jason Zaugg2014-12-181-4/+1
|\ | | | | | | | | | | | | | | | | merge/2.10.x-to-master-20141219 Conflicts: src/main/scala/scala/async/internal/AsyncTransform.scala src/main/scala/scala/async/internal/ExprBuilder.scala src/test/scala/scala/async/TreeInterrogation.scala
| * Avoid unbounded stack consumption for synchronous control flowJason Zaugg2014-12-151-4/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Previously, as sequence of state transitions that did not pass through an asynchrous boundary incurred stack frames. The trivial loop in the enclosed test case would then overflow the stack. This commit merges the `resume` and `apply(tr: Try[Any])` methods into a `apply`. It changes the body of this method to be an infinite loop with returns at the terminal points in the state machine (or at a terminal failure.) To allow merging of these previously separate matches, states that contain an await are now allocated two state ids: one for the setup code that calls `onComplete`, and one for the code in the continuation that records the result and advances the state machine. Fixes #93
* | Fix regression around await of non-class typeGene Novark2014-12-091-4/+5
| | | | | | | | E.g. type param, abstrat type.
* | Additional tests and comments around mkZero for value classesJason Zaugg2014-10-011-1/+11
| |
* | Merge branch 'ticket/86-mkZero' into merge/2.10.x-to-master-20140930Jason Zaugg2014-10-011-0/+16
|\| | | | | | | | | | | | | Conflicts: src/main/scala/scala/async/internal/AnfTransform.scala src/main/scala/scala/async/internal/AsyncTransform.scala src/test/scala/scala/async/run/toughtype/ToughType.scala
| * Avoid assigning null to vars of derived value typeJason Zaugg2014-09-291-0/+13
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | `TreeGen#mkZero` returns `q"null"` for derived value classes. ``` scala> class V(val a: String) extends AnyVal defined class V scala> showRaw(gen.mkZero(typeOf[V])) res0: String = Literal(Constant(null)) ``` We use this API in async to generate the initial value for ANF-lifted temporary variables. However, this leads to NPEs, as after posterasure, we call the unbox method on a null reference: ``` % cat sandbox/Macro.scala; scalac-hash v2.10.4 sandbox/Macro.scala; scala-hash v2.10.4 -e 'val x = Macros.myMacro' import scala.reflect.macros.Context import scala.language.experimental.macros object Macros { def macroImpl(c: Context): c.Expr[C] = { import c.universe._ val e1 = c.Expr[C](Literal(Constant(null)).setType(typeOf[C])) reify(e1.splice.asInstanceOf[C @annotation.unchecked.uncheckedVariance]) } def myMacro: C = macro macroImpl } class C(val a: String) extends AnyVal java.lang.NullPointerException at Main$$anon$1.<init>(scalacmd4059893593754060829.scala:1) at Main$.main(scalacmd4059893593754060829.scala:1) at Main.main(scalacmd4059893593754060829.scala) ``` This commit installs a custom version of `mkZero` that instead returns `q"new C[$..targs](${mkZero(wrappedType)})`. Thanks to @ewiner for pinpointing the problem.
* | Merge remote-tracking branch 'origin/2.10.x' into ↵Jason Zaugg2014-07-211-1/+7
|\| | | | | | | | | | | | | | | merge/2.10.x-to-master-20140721 Conflicts: src/main/scala/scala/async/internal/AsyncTransform.scala src/main/scala/scala/async/internal/Lifter.scala
| * Fix regression around type skolems and if exprs.Jason Zaugg2014-07-181-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | If we start with: async({ val res = await[S[_$1 with String]](s); if (true) await[Int](0) res }) Typechecking the application (before macro expansion) yields (where the trees are printed in the form `expr{tpe}`): async[S[_$1#5738 with String#137]]({ val res: S[_$1#5490 with String] forSome { type _$1#5490 } = await[S[_$1#5487 with String]]( s{S[_$1#5487 with String]} ){S[_$1#5487 with String]}; if (true) await(0) else () res{S[_$1#5738 with String]} }{S[_$1#5738 with String]}){S[_$1#5738 with String]} Note that the type of the second last line contains a skolemized symbol `_$1#5738` of the existential `_$1#5490`. This is created by this case in `Typer#adapt`: case et @ ExistentialType(_, _) if ((mode & (EXPRmode | LHSmode)) == EXPRmode) => adapt(tree setType et.skolemizeExistential(context.owner, tree), mode, pt, original) Our ANF rewrites part of this code to: <synthetic> val await$1: S[_$1#5487 with String] = await[S[_$1#5487 with String]](awaitable$1); val res: S[_$1#5490 with String] forSome { type _$1 } = await$1; And later, the state machine transformation splits the last line into a blank field and an assignment. Typechecking the `Assign` node led to the an type error. This commit manually attributes the types to the `Assign` node so as to avoid these problem. It also reigns in an overeager rewriting of `If` nodes in the ANF transform, which was due to a bug in the label detection logic introduced in 4fc5463538. Thanks to @gnovark for yet another devilish test case and analysis of the problem with label detection. I worked on a more principled fix on: https://github.com/retronym/async/compare/ticket/79-2?expand=1 in which I try to use `repackExistential` to convert skolemized types to existentials for use as the types of synthetic vals introduced by the ANF transform. This ran into a deeper problem with existential subtyping in the compiler itself though.
* | Update to Scala 2.11.0-RC4, adapting to change in quasiquotesJason Zaugg2014-04-051-0/+68
| | | | | | | | | | | | | | | | Namely: https://github.com/scala/scala/pull/3656 I can't find a way to express a QQ that matches an constructor invocation *and* lets me bind a reference to the `New` tree. So I've dropped down to a borrowed version of `TreeInfo#Applied`.
* | AsyncMacro.global is goneEugene Burmako2014-02-151-9/+38
| |
* | AsyncMacro.macroApplication is goneEugene Burmako2014-02-151-1/+1
| |
* | callSiteTyper and TypingTransformers are goneEugene Burmako2014-02-151-9/+0
| |
* | migrates transformAt to typingTransformEugene Burmako2014-02-151-9/+5
| |
* | replaces mkAttributedCastEugene Burmako2014-02-151-2/+3
| |
* | gets rid of home-grown changeOwnerEugene Burmako2014-02-151-18/+1
| |
* | eliminates all usages of global in TransformUtilsEugene Burmako2014-02-151-2/+3
| |
* | removes "import global._" and "def Expr" in TransformUtilsEugene Burmako2014-02-151-4/+2
| |
* | currentUnit.freshName => c.freshName (leads to less precise tests...)Eugene Burmako2014-02-151-2/+2
| |
* | abort => c.abortEugene Burmako2014-02-151-3/+0
|/
* Unhardcode use of scala.util.TryJason Zaugg2014-01-281-4/+0
| | | | In favour of the type defined by the active FutureSystem.
* Update copyright years.Jason Zaugg2014-01-141-1/+1
| | | | 2013 must have been unlucky.
* Fix crashers in do/while and while(await(..))Jason Zaugg2013-11-221-0/+13
| | | | | | | | | | | | | | | | | | | | | | | | | | | The new tree shapes handled for do/while look like: // type checked async({ val b = false; doWhile$1(){ await(()); if (b) doWhile$1() else () }; () }) We had to change ExprBuilder to create states for the if/else that concludes the doWhile body, and also loosen the assertion that the label jump must be the last thing we see. We also have to look for more than just `containsAwait` when deciding whether an `If` needs to be transformed into states; it might also contain a jump to the enclosing label that is on the other side of an `await`, and hence needs to be a state transition instead.
* Fix a NPE in the presentation compilerJason Zaugg2013-11-201-0/+1
| | | | | | | | | | | | | | | | | | | | | | | | | We were using a TypingTransformer and we called `atOwner` before we had called `transform`. This meant that `currTree` was null, which was observed when that was passed to `Context#make`. IDE ticket: https://scala-ide-portfolio.assembla.com/spaces/scala-ide/tickets/1001971#/activity/ticket: Stack trace: exception during macro expansion: java.lang.NullPointerException at scala.tools.nsc.interactive.ContextTrees$class.addContext(ContextTrees.scala:78) at scala.tools.nsc.interactive.Global.addContext(Global.scala:28) at scala.tools.nsc.interactive.Global.registerContext(Global.scala:268) at scala.tools.nsc.typechecker.Contexts$Context.make(Contexts.scala:295) at scala.tools.nsc.typechecker.Contexts$Context.make0(Contexts.scala:320) at scala.tools.nsc.typechecker.Contexts$Context.make(Contexts.scala:327) at scala.tools.nsc.typechecker.Typers$Typer.atOwner(Typers.scala:5662) at scala.tools.nsc.transform.TypingTransformers$TypingTransformer.atOwner(TypingTransformers.scala:33) at scala.tools.nsc.transform.TypingTransformers$TypingTransformer.atOwner(TypingTransformers.scala:28) at scala.async.internal.AsyncTransform$class.fixup$1(AsyncTransform.scala:191)
* Don't aggressively null out captured varsJason Zaugg2013-11-121-1/+1
| | | | | Once they escape, we leave the references in the state machines fields untouched.
* Minimize the public APIJason Zaugg2013-11-071-2/+1
| | | | | | | | | | - Remove the CPS fallback version of async. That was not intended to be part of 1.0. - Lookup the await method beside the macro, rather than requiring all calls to go to AsyncBase.await. - Create a minimal version of Async that just contains await/async and delegates to the macro implementation in internal._ - Add scaladoc.
* Use @uncheckedBounds to avoid introducing refchecks errorsJason Zaugg2013-08-221-1/+12
| | | | | | | | | ... in code that would otherwise have smuggled through these slack LUBs in the types of trees but never in a TypeTree. More details in SI-7694. Fixes #29
* Preserve ApplyImplicitView / ApplyImplicitArgs in AnfTransform.Jason Zaugg2013-07-251-1/+1
|
* Move implementation details to scala.async.internal._.Jason Zaugg2013-07-071-0/+251
If we intend to keep CPS fallback around for any length of time it should probably move there too.