Skip to content

Merge 2.10.x to master #84

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jul 21, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/main/scala/scala/async/internal/AsyncTransform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,10 @@ trait AsyncTransform {
case ValDef(_, _, _, rhs) if liftedSyms(tree.symbol) =>
api.atOwner(api.currentOwner) {
val fieldSym = tree.symbol
val set = Assign(gen.mkAttributedStableRef(thisType(fieldSym.owner.asClass), fieldSym), api.recur(rhs))
set.changeOwner(tree.symbol, api.currentOwner)
api.typecheck(atPos(tree.pos)(set))
val lhs = atPos(tree.pos) {
gen.mkAttributedStableRef(thisType(fieldSym.owner.asClass), fieldSym)
}
treeCopy.Assign(tree, lhs, api.recur(rhs)).setType(definitions.UnitTpe).changeOwner(fieldSym, api.currentOwner)
}
case _: DefTree if liftedSyms(tree.symbol) =>
EmptyTree
Expand Down
3 changes: 1 addition & 2 deletions src/main/scala/scala/async/internal/Lifter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,7 @@ trait Lifter {
sym.setFlag(MUTABLE | STABLE | PRIVATE | LOCAL)
sym.setName(name.fresh(sym.name.toTermName))
sym.setInfo(deconst(sym.info))
val zeroRhs = atPos(t.pos)(gen.mkZero(vd.symbol.info))
treeCopy.ValDef(vd, Modifiers(sym.flags), sym.name, TypeTree(tpe(sym)).setPos(t.pos), zeroRhs)
treeCopy.ValDef(vd, Modifiers(sym.flags), sym.name, TypeTree(tpe(sym)).setPos(t.pos), EmptyTree)
case dd@DefDef(_, _, tparams, vparamss, tpt, rhs) =>
sym.setName(this.name.fresh(sym.name.toTermName))
sym.setFlag(PRIVATE | LOCAL)
Expand Down
71 changes: 43 additions & 28 deletions src/main/scala/scala/async/internal/LiveVariables.scala
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,22 @@ trait LiveVariables {

/** Tests if `state1` is a predecessor of `state2`.
*/
def isPred(state1: Int, state2: Int, seen: Set[Int] = Set()): Boolean =
if (seen(state1)) false // breaks cycles in the CFG
else cfg get state1 match {
case Some(nextStates) =>
nextStates.contains(state2) || nextStates.exists(isPred(_, state2, seen + state1))
case None =>
false
}
def isPred(state1: Int, state2: Int): Boolean = {
val seen = scala.collection.mutable.HashSet[Int]()

def isPred0(state1: Int, state2: Int): Boolean =
if(state1 == state2) false
else if (seen(state1)) false // breaks cycles in the CFG
else cfg get state1 match {
case Some(nextStates) =>
seen += state1
nextStates.contains(state2) || nextStates.exists(isPred0(_, state2))
case None =>
false
}

isPred0(state1, state2)
}

val finalState = asyncStates.find(as => !asyncStates.exists(other => isPred(as.state, other.state))).get

Expand Down Expand Up @@ -162,12 +170,10 @@ trait LiveVariables {
LVexit = LVexit + (finalState.state -> noNull)

var currStates = List(finalState) // start at final state
var pred = List[AsyncState]() // current predecessor states
var hasChanged = true // if something has changed we need to continue iterating
var captured: Set[Symbol] = Set()

while (hasChanged) {
hasChanged = false
while (!currStates.isEmpty) {
var entryChanged: List[AsyncState] = Nil

for (cs <- currStates) {
val LVentryOld = LVentry(cs.state)
Expand All @@ -176,44 +182,53 @@ trait LiveVariables {
val LVentryNew = LVexit(cs.state) ++ referenced.used
if (!LVentryNew.sameElements(LVentryOld)) {
LVentry = LVentry + (cs.state -> LVentryNew)
hasChanged = true
entryChanged ::= cs
}
}

pred = currStates.flatMap(cs => asyncStates.filter(_.nextStates.contains(cs.state)))
val pred = entryChanged.flatMap(cs => asyncStates.filter(_.nextStates.contains(cs.state)))
var exitChanged: List[AsyncState] = Nil

for (p <- pred) {
val LVexitOld = LVexit(p.state)
val LVexitNew = p.nextStates.flatMap(succ => LVentry(succ)).toSet
if (!LVexitNew.sameElements(LVexitOld)) {
LVexit = LVexit + (p.state -> LVexitNew)
hasChanged = true
exitChanged ::= p
}
}

currStates = pred
currStates = exitChanged
}

for (as <- asyncStates) {
AsyncUtils.vprintln(s"LVentry at state #${as.state}: ${LVentry(as.state).mkString(", ")}")
AsyncUtils.vprintln(s"LVexit at state #${as.state}: ${LVexit(as.state).mkString(", ")}")
}

def lastUsagesOf(field: Tree, at: AsyncState, avoid: Set[AsyncState]): Set[Int] =
if (avoid(at)) Set()
else if (captured(field.symbol)) {
Set()
}
else LVentry get at.state match {
case Some(fields) if fields.exists(_ == field.symbol) =>
Set(at.state)
case _ =>
val preds = asyncStates.filter(_.nextStates.contains(at.state)).toSet
preds.flatMap(p => lastUsagesOf(field, p, avoid + at))
def lastUsagesOf(field: Tree, at: AsyncState): Set[Int] = {
val avoid = scala.collection.mutable.HashSet[AsyncState]()

def lastUsagesOf0(field: Tree, at: AsyncState): Set[Int] = {
if (avoid(at)) Set()
else if (captured(field.symbol)) {
Set()
}
else LVentry get at.state match {
case Some(fields) if fields.exists(_ == field.symbol) =>
Set(at.state)
case _ =>
avoid += at
val preds = asyncStates.filter(_.nextStates.contains(at.state)).toSet
preds.flatMap(p => lastUsagesOf0(field, p))
}
}

lastUsagesOf0(field, at)
}

val lastUsages: Map[Tree, Set[Int]] =
liftables.map(fld => (fld -> lastUsagesOf(fld, finalState, Set()))).toMap
liftables.map(fld => (fld -> lastUsagesOf(fld, finalState))).toMap

for ((fld, lastStates) <- lastUsages)
AsyncUtils.vprintln(s"field ${fld.symbol.name} is last used in states ${lastStates.mkString(", ")}")
Expand Down
8 changes: 7 additions & 1 deletion src/main/scala/scala/async/internal/TransformUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,17 @@ private[async] trait TransformUtils {
case ld: LabelDef => ld.symbol
}.toSet
t.exists {
case rt: RefTree => !(labelDefs contains rt.symbol)
case rt: RefTree => rt.symbol != null && isLabel(rt.symbol) && !(labelDefs contains rt.symbol)
case _ => false
}
}

private def isLabel(sym: Symbol): Boolean = {
val LABEL = 1L << 17 // not in the public reflection API.
(internal.flags(sym).asInstanceOf[Long] & LABEL) != 0L
}


/** Map a list of arguments to:
* - A list of argument Trees
* - A list of auxillary results.
Expand Down
77 changes: 77 additions & 0 deletions src/test/scala/scala/async/run/ifelse1/IfElse1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,75 @@ class TestIfElse1Class {
}
z
}

def pred: Future[Boolean] = async(true)

def m5: Future[Boolean] = async {
if(if(if(if(if(if(if(if(if(if(if(if(if(if(if(if(if(if(if(if(if(await(pred))
await(pred)
else
false)
await(pred)
else
false)
await(pred)
else
false)
await(pred)
else
false)
await(pred)
else
false)
await(pred)
else
false)
await(pred)
else
false)
await(pred)
else
false)
await(pred)
else
false)
await(pred)
else
false)
await(pred)
else
false)
await(pred)
else
false)
await(pred)
else
false)
await(pred)
else
false)
await(pred)
else
false)
await(pred)
else
false)
await(pred)
else
false)
await(pred)
else
false)
await(pred)
else
false)
await(pred)
else
false)
await(pred)
else
false
}
}

class IfElse1Spec {
Expand Down Expand Up @@ -124,4 +193,12 @@ class IfElse1Spec {
val res = Await.result(fut, 2 seconds)
res mustBe (14)
}

@Test
def `await in deeply-nested if-else conditions`() {
val o = new TestIfElse1Class
val fut = o.m5
val res = Await.result(fut, 2 seconds)
res mustBe true
}
}
63 changes: 63 additions & 0 deletions src/test/scala/scala/async/run/ifelse4/IfElse4.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (C) 2012-2014 Typesafe Inc. <http://www.typesafe.com>
*/

package scala.async
package run
package ifelse4

import language.{reflectiveCalls, postfixOps}
import scala.concurrent.{Future, ExecutionContext, future, Await}
import scala.concurrent.duration._
import scala.async.Async.{async, await}
import org.junit.Test


class TestIfElse4Class {

import ExecutionContext.Implicits.global

class F[A]
class S[A](val id: String)
trait P

case class K(f: F[_])

def result[A](f: F[A]) = async {
new S[A with P]("foo")
}

def run(k: K) = async {
val res = await(result(k.f))
// these triggered a crash with mismatched existential skolems
// found : S#10272[_$1#10308 with String#137] where type _$1#10308
// required: S#10272[_$1#10311 with String#137] forSome { type _$1#10311 }

// This variation of the crash could be avoided by fixing the over-eager
// generation of states in `If` nodes, which was caused by a bug in label
// detection code.
if(true) {
identity(res)
}

// This variation remained after the aforementioned fix, however.
// It was fixed by manually typing the `Assign(liftedField, rhs)` AST,
// which is how we avoid these problems through the rest of the ANF transform.
if(true) {
identity(res)
await(result(k.f))
}
res
}
}

class IfElse4Spec {

@Test
def `await result with complex type containing skolem`() {
val o = new TestIfElse4Class
val fut = o.run(new o.K(null))
val res = Await.result(fut, 2 seconds)
res.id mustBe ("foo")
}
}
16 changes: 16 additions & 0 deletions src/test/scala/scala/async/run/toughtype/ToughType.scala
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,22 @@ class ToughTypeSpec {
}(SomeExecutionContext)
}
}

}

@Test def ticket66Nothing() {
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
val e = new Exception()
val f: Future[Nothing] = Future.failed(e)
val f1 = async {
await(f)
}
try {
Await.result(f1, 5.seconds)
} catch {
case `e` =>
}
}
}

Expand Down