Skip to content

Commit 98e2f26

Browse files
committed
Use @uncheckedBounds to avoid introducing refchecks errors
... 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
1 parent 8371f48 commit 98e2f26

File tree

8 files changed

+96
-16
lines changed

8 files changed

+96
-16
lines changed

build.sbt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
scalaVersion := "2.10.2"
1+
scalaVersion := "2.10.3-RC1"
2+
3+
organization := "org.typesafe.async" // TODO new org name under scala-lang.
24

35
// Uncomment to test with a locally built copy of Scala.
46
// scalaHome := Some(file("/code/scala2/build/pack"))
57

6-
organization := "org.typesafe.async" // TODO new org name under scala-lang.
7-
88
name := "scala-async"
99

1010
version := "1.0.0-SNAPSHOT"

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ private[async] trait AnfTransform {
119119
}
120120

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

@@ -145,7 +145,7 @@ private[async] trait AnfTransform {
145145
}
146146

147147
private def defineVal(prefix: String, lhs: Tree, pos: Position): ValDef = {
148-
val sym = currOwner.newTermSymbol(name.fresh(prefix), pos, SYNTHETIC).setInfo(lhs.tpe)
148+
val sym = currOwner.newTermSymbol(name.fresh(prefix), pos, SYNTHETIC).setInfo(uncheckedBounds(lhs.tpe))
149149
changeOwner(lhs, currentOwner, sym)
150150
ValDef(sym, changeOwner(lhs, currentOwner, sym)).setType(NoType).setPos(pos)
151151
}

src/main/scala/scala/async/internal/AsyncId.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ object IdentityFutureSystem extends FutureSystem {
4141
def execContextType: Type = weakTypeOf[Unit]
4242

4343
def createProm[A: WeakTypeTag]: Expr[Prom[A]] = reify {
44-
new Prom()
44+
new Prom[A]()
4545
}
4646

4747
def promiseToFuture[A: WeakTypeTag](prom: Expr[Prom[A]]) = reify {

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ trait AsyncTransform {
66
import global._
77

88
def asyncTransform[T](body: Tree, execContext: Tree, cpsFallbackEnabled: Boolean)
9-
(implicit resultType: WeakTypeTag[T]): Tree = {
9+
(resultType: WeakTypeTag[T]): Tree = {
10+
11+
// We annotate the type of the whole expression as `T @uncheckedBounds` so as not to introduce
12+
// warnings about non-conformant LUBs. See SI-7694
13+
// This implicit propatages the annotated type in the type tag.
14+
implicit val uncheckedBoundsResultTag: WeakTypeTag[T] = WeakTypeTag[T](rootMirror, FixedMirrorTypeCreator(rootMirror, uncheckedBounds(resultType.tpe)))
1015

1116
reportUnsupportedAwaits(body, report = !cpsFallbackEnabled)
1217

@@ -22,12 +27,12 @@ trait AsyncTransform {
2227
DefDef(NoMods, name.apply, Nil, applyVParamss, TypeTree(definitions.UnitTpe), Literal(Constant(())))
2328
}
2429

25-
val stateMachineType = applied("scala.async.StateMachine", List(futureSystemOps.promType[T], futureSystemOps.execContextType))
30+
val stateMachineType = applied("scala.async.StateMachine", List(futureSystemOps.promType[T](uncheckedBoundsResultTag), futureSystemOps.execContextType))
2631

2732
val stateMachine: ClassDef = {
2833
val body: List[Tree] = {
2934
val stateVar = ValDef(Modifiers(Flag.MUTABLE | Flag.PRIVATE | Flag.LOCAL), name.state, TypeTree(definitions.IntTpe), Literal(Constant(0)))
30-
val result = ValDef(NoMods, name.result, TypeTree(futureSystemOps.promType[T]), futureSystemOps.createProm[T].tree)
35+
val result = ValDef(NoMods, name.result, TypeTree(futureSystemOps.promType[T](uncheckedBoundsResultTag)), futureSystemOps.createProm[T](uncheckedBoundsResultTag).tree)
3136
val execContextValDef = ValDef(NoMods, name.execContext, TypeTree(), execContext)
3237

3338
val apply0DefDef: DefDef = {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ trait ExprBuilder {
284284

285285
def onCompleteHandler[T: WeakTypeTag]: Tree
286286

287-
def resumeFunTree[T]: DefDef
287+
def resumeFunTree[T: WeakTypeTag]: DefDef
288288
}
289289

290290
case class SymLookup(stateMachineClass: Symbol, applyTrParam: Symbol) {
@@ -303,12 +303,12 @@ trait ExprBuilder {
303303
new AsyncBlock {
304304
def asyncStates = blockBuilder.asyncStates.toList
305305

306-
def mkCombinedHandlerCases[T]: List[CaseDef] = {
306+
def mkCombinedHandlerCases[T: WeakTypeTag]: List[CaseDef] = {
307307
val caseForLastState: CaseDef = {
308308
val lastState = asyncStates.last
309309
val lastStateBody = Expr[T](lastState.body)
310310
val rhs = futureSystemOps.completeProm(
311-
Expr[futureSystem.Prom[T]](symLookup.memberRef(name.result)), reify(scala.util.Success(lastStateBody.splice)))
311+
Expr[futureSystem.Prom[T]](symLookup.memberRef(name.result)), reify(scala.util.Success[T](lastStateBody.splice)))
312312
mkHandlerCase(lastState.state, rhs.tree)
313313
}
314314
asyncStates.toList match {
@@ -337,7 +337,7 @@ trait ExprBuilder {
337337
* }
338338
* }
339339
*/
340-
def resumeFunTree[T]: DefDef =
340+
def resumeFunTree[T: WeakTypeTag]: DefDef =
341341
DefDef(Modifiers(), name.resume, Nil, List(Nil), Ident(definitions.UnitClass),
342342
Try(
343343
Match(symLookup.memberRef(name.state), mkCombinedHandlerCases[T]),

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,19 @@ private[async] trait TransformUtils {
244244
// Attributed version of `TreeGen#mkCastPreservingAnnotations`
245245
def mkAttributedCastPreservingAnnotations(tree: Tree, tp: Type): Tree = {
246246
atPos(tree.pos) {
247-
val casted = gen.mkAttributedCast(tree, tp.withoutAnnotations.dealias)
247+
val casted = gen.mkAttributedCast(tree, uncheckedBounds(tp.withoutAnnotations).dealias)
248248
Typed(casted, TypeTree(tp)).setType(tp)
249249
}
250250
}
251+
252+
// =====================================
253+
// Copy/Pasted from Scala 2.10.3. See SI-7694.
254+
private lazy val UncheckedBoundsClass = {
255+
global.rootMirror.getClassIfDefined("scala.reflect.internal.annotations.uncheckedBounds")
256+
}
257+
final def uncheckedBounds(tp: Type): Type = {
258+
if (tp.typeArgs.isEmpty || UncheckedBoundsClass == NoSymbol) tp
259+
else tp.withAnnotation(AnnotationInfo marker UncheckedBoundsClass.tpe)
260+
}
261+
// =====================================
251262
}

src/test/scala/scala/async/run/toughtype/ToughType.scala

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import language.{reflectiveCalls, postfixOps}
1010
import scala.concurrent._
1111
import scala.concurrent.duration._
1212
import scala.async.Async._
13-
import org.junit.Test
13+
import org.junit.{Assert, Test}
1414
import scala.async.internal.AsyncId
1515

1616

@@ -136,3 +136,10 @@ class ToughTypeSpec {
136136
foo
137137
}
138138
}
139+
140+
trait A
141+
trait B
142+
143+
trait L[A2, B2 <: A2] {
144+
def bar(a: Any, b: Any) = 0
145+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package scala.async
2+
package run
3+
package uncheckedBounds
4+
5+
import org.junit.{Test, Assert}
6+
import scala.async.TreeInterrogation
7+
8+
class UncheckedBoundsSpec {
9+
@Test def insufficientLub_SI_7694() {
10+
suppressingFailureBefore2_10_3 {
11+
eval( s"""
12+
object Test {
13+
import _root_.scala.async.run.toughtype._
14+
import _root_.scala.async.internal.AsyncId.{async, await}
15+
async {
16+
(if (true) await(null: L[A, A]) else await(null: L[B, B]))
17+
}
18+
}
19+
""", compileOptions = s"-cp ${toolboxClasspath} ")
20+
}
21+
}
22+
23+
@Test def insufficientLub_SI_7694_ScalaConcurrent() {
24+
suppressingFailureBefore2_10_3 {
25+
eval( s"""
26+
object Test {
27+
import _root_.scala.async.run.toughtype._
28+
import _root_.scala.async.Async.{async, await}
29+
import scala.concurrent._
30+
import scala.concurrent.ExecutionContext.Implicits.global
31+
async {
32+
(if (true) await(null: Future[L[A, A]]) else await(null: Future[L[B, B]]))
33+
}
34+
}
35+
""", compileOptions = s"-cp ${toolboxClasspath} ")
36+
}
37+
}
38+
39+
private def suppressingFailureBefore2_10_3(body: => Any) {
40+
try {
41+
body
42+
} catch {
43+
case x: Throwable =>
44+
// @uncheckedBounds was only introduced in 2.10.3/ 2.11.0-M5, so avoid reporting this test failure in those cases.
45+
scala.util.Properties.versionNumberString match {
46+
case "2.10.0" | "2.10.1" | "2.10.2" | "2.11.0-M4" => // ignore, the @uncheckedBounds doesn't exist yet
47+
case _ =>
48+
val annotationExists =
49+
reflect.runtime.currentMirror.staticClass("scala.reflect.internal.annotations.uncheckedBounds") == reflect.runtime.universe.NoSymbol
50+
if (annotationExists)
51+
Assert.fail("@uncheckedBounds not found in scala-reflect.jar")
52+
else
53+
Assert.fail(s"@uncheckedBounds exists, but it didn't prevent this failure: $x")
54+
}
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)