Skip to content

Avoid assigning null to vars of derived value type #91

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 1 commit into from
Sep 30, 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
11 changes: 5 additions & 6 deletions src/main/scala/scala/async/internal/AnfTransform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private[async] trait AnfTransform {
expr match {
case Apply(fun, args) if isAwait(fun) =>
val valDef = defineVal(name.await, expr, tree.pos)
stats :+ valDef :+ gen.mkAttributedStableRef(valDef.symbol).setType(tree.tpe).setPos(tree.pos)
stats :+ valDef :+ atPos(tree.pos)(gen.mkAttributedStableRef(valDef.symbol)).setType(tree.tpe)

case If(cond, thenp, elsep) =>
// if type of if-else is Unit don't introduce assignment,
Expand All @@ -90,7 +90,7 @@ private[async] trait AnfTransform {
}
}
val ifWithAssign = treeCopy.If(tree, cond, branchWithAssign(thenp), branchWithAssign(elsep)).setType(definitions.UnitTpe)
stats :+ varDef :+ ifWithAssign :+ gen.mkAttributedStableRef(varDef.symbol).setType(tree.tpe).setPos(tree.pos)
stats :+ varDef :+ ifWithAssign :+ atPos(tree.pos)(gen.mkAttributedStableRef(varDef.symbol).setType(tree.tpe))
}
case LabelDef(name, params, rhs) =>
statsExprUnit
Expand All @@ -115,7 +115,7 @@ private[async] trait AnfTransform {
}
val matchWithAssign = treeCopy.Match(tree, scrut, casesWithAssign).setType(definitions.UnitTpe)
require(matchWithAssign.tpe != null, matchWithAssign)
stats :+ varDef :+ matchWithAssign :+ gen.mkAttributedStableRef(varDef.symbol).setPos(tree.pos).setType(tree.tpe)
stats :+ varDef :+ matchWithAssign :+ atPos(tree.pos)(gen.mkAttributedStableRef(varDef.symbol))
}
case _ =>
stats :+ expr
Expand All @@ -124,7 +124,7 @@ private[async] trait AnfTransform {

private def defineVar(prefix: String, tp: Type, pos: Position): ValDef = {
val sym = currOwner.newTermSymbol(name.fresh(prefix), pos, MUTABLE | SYNTHETIC).setInfo(uncheckedBounds(tp))
ValDef(sym, gen.mkZero(uncheckedBounds(tp))).setType(NoType).setPos(pos)
ValDef(sym, mkZero(uncheckedBounds(tp))).setType(NoType).setPos(pos)
}
}

Expand All @@ -150,7 +150,6 @@ private[async] trait AnfTransform {

private def defineVal(prefix: String, lhs: Tree, pos: Position): ValDef = {
val sym = currOwner.newTermSymbol(name.fresh(prefix), pos, SYNTHETIC).setInfo(uncheckedBounds(lhs.tpe))
changeOwner(lhs, currentOwner, sym)
ValDef(sym, changeOwner(lhs, currentOwner, sym)).setType(NoType).setPos(pos)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice.

}

Expand Down Expand Up @@ -213,7 +212,7 @@ private[async] trait AnfTransform {

/** The depth of the nested applies: e.g. Apply(Apply(Apply(_, _), _), _)
* has depth 3. Continues through type applications (without counting them.)
*/
*/
def applyDepth: Int = {
def loop(tree: Tree): Int = tree match {
case Apply(fn, _) => 1 + loop(fn)
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/scala/async/internal/AsyncTransform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ trait AsyncTransform {
List(
asyncBase.nullOut(global)(Expr[String](Literal(Constant(fieldSym.name.toString))), Expr[Any](Ident(fieldSym))).tree
),
Assign(gen.mkAttributedStableRef(fieldSym.owner.thisType, fieldSym), gen.mkZero(fieldSym.info))
Assign(gen.mkAttributedStableRef(fieldSym.owner.thisType, fieldSym), mkZero(fieldSym.info))
)
}
val asyncState = asyncBlock.asyncStates.find(_.state == state).get
Expand Down
13 changes: 13 additions & 0 deletions src/main/scala/scala/async/internal/TransformUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,19 @@ private[async] trait TransformUtils {
}
}

def mkZero(tp: Type): Tree = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can add a comment with q"new C[$..targs](${mkZero(wrappedType)}). Could be helpful in the future.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hopefully this can just turn into a quasiquote when I merge it to master. If not, I'll comment it.

if (tp.typeSymbol.isDerivedValueClass) {
val argZero = mkZero(tp.memberType(tp.typeSymbol.derivedValueClassUnbox).resultType)
val target: Tree = gen.mkAttributedSelect(
typer.typedPos(macroPos)(
New(TypeTree(tp.baseType(tp.typeSymbol)))), tp.typeSymbol.primaryConstructor)
val zero = gen.mkMethodCall(target, argZero :: Nil)
gen.mkCast(zero, tp)
} else {
gen.mkZero(tp)
}
}

// =====================================
// Copy/Pasted from Scala 2.10.3. See SI-7694.
private lazy val UncheckedBoundsClass = {
Expand Down
64 changes: 64 additions & 0 deletions src/test/scala/scala/async/run/toughtype/ToughType.scala
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,72 @@ class ToughTypeSpec {
case `e` =>
}
}

@Test def ticket83ValueClass() {
import scala.async.Async._
import scala.concurrent._, duration._, ExecutionContext.Implicits.global
val f = async {
val uid = new IntWrapper("foo")
await(Future(uid))
}
val result = Await.result(f, 5.seconds)
result mustEqual (new IntWrapper("foo"))
}

@Test def ticket86NestedValueClass() {
import ExecutionContext.Implicits.global

val f = async {
val a = Future.successful(new IntWrapper("42"))
await(await(a).plusStr)
}
val result = Await.result(f, 5.seconds)
result mustEqual "42!"
}

@Test def ticket86MatchedValueClass(): Unit = {
import ExecutionContext.Implicits.global

def doAThing(param: IntWrapper) = Future(None)

val fut = async {
Option(new IntWrapper("value!")) match {
case Some(valueHolder) =>
await(doAThing(valueHolder))
case None =>
None
}
}

val result = Await.result(fut, 5.seconds)
result mustBe None
}

@Test def ticket86MatchedParameterizedValueClass(): Unit = {
import ExecutionContext.Implicits.global

def doAThing(param: ParamWrapper[String]) = Future(None)

val fut = async {
Option(new ParamWrapper("value!")) match {
case Some(valueHolder) =>
await(doAThing(valueHolder))
case None =>
None
}
}

val result = Await.result(fut, 5.seconds)
result mustBe None
}
}

class IntWrapper(val value: String) extends AnyVal {
def plusStr = Future.successful(value + "!")
}
class ParamWrapper[T](val value: T) extends AnyVal


trait A

trait B
Expand Down