Skip to content

Commit 1a3976f

Browse files
committed
Revert pull request scala#720 (CPS: enable return expressions in CPS code if they are in tail position)
Reverts commit 0ada070. Reverts commit 51c92f0. Reverts commit cdfbe8e. Reverts commit 796024c.
1 parent 161b583 commit 1a3976f

File tree

13 files changed

+3
-208
lines changed

13 files changed

+3
-208
lines changed

src/compiler/scala/tools/nsc/typechecker/Modes.scala

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,6 @@ trait Modes {
8686
*/
8787
final val TYPEPATmode = 0x10000
8888

89-
/** RETmode is set when we are typing a return expression.
90-
*/
91-
final val RETmode = 0x20000
92-
9389
final private val StickyModes = EXPRmode | PATTERNmode | TYPEmode | ALTmode
9490

9591
final def onlyStickyModes(mode: Int) =

src/compiler/scala/tools/nsc/typechecker/Typers.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4024,8 +4024,7 @@ trait Typers extends Modes with Adaptations with Tags {
40244024
ReturnWithoutTypeError(tree, enclMethod.owner)
40254025
} else {
40264026
context.enclMethod.returnsSeen = true
4027-
val expr1: Tree = typed(expr, EXPRmode | BYVALmode | RETmode, restpt.tpe)
4028-
4027+
val expr1: Tree = typed(expr, EXPRmode | BYVALmode, restpt.tpe)
40294028
// Warn about returning a value if no value can be returned.
40304029
if (restpt.tpe.typeSymbol == UnitClass) {
40314030
// The typing in expr1 says expr is Unit (it has already been coerced if

src/continuations/plugin/scala/tools/selectivecps/CPSAnnotationChecker.scala

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,6 @@ abstract class CPSAnnotationChecker extends CPSUtils with Modes {
171171
vprintln("yes we can!! (byval)")
172172
return true
173173
}
174-
} else if ((mode & global.analyzer.RETmode) != 0) {
175-
vprintln("yes we can!! (return)")
176-
return true
177174
}
178175
}
179176
false
@@ -187,7 +184,6 @@ abstract class CPSAnnotationChecker extends CPSUtils with Modes {
187184
val patMode = (mode & global.analyzer.PATTERNmode) != 0
188185
val exprMode = (mode & global.analyzer.EXPRmode) != 0
189186
val byValMode = (mode & global.analyzer.BYVALmode) != 0
190-
val retMode = (mode & global.analyzer.RETmode) != 0
191187

192188
val annotsTree = cpsParamAnnotation(tree.tpe)
193189
val annotsExpected = cpsParamAnnotation(pt)
@@ -214,12 +210,6 @@ abstract class CPSAnnotationChecker extends CPSUtils with Modes {
214210
val res = tree modifyType addMinusMarker
215211
vprintln("adapted annotations (by val) of " + tree + " to " + res.tpe)
216212
res
217-
} else if (retMode && !hasPlusMarker(tree.tpe) && annotsTree.isEmpty && annotsExpected.nonEmpty) {
218-
// add a marker annotation that will make tree.tpe behave as pt, subtyping wise
219-
// tree will look like having no annotation
220-
val res = tree modifyType (_ withAnnotations List(newPlusMarker()))
221-
vprintln("adapted annotations (return) of " + tree + " to " + res.tpe)
222-
res
223213
} else tree
224214
}
225215

@@ -476,11 +466,6 @@ abstract class CPSAnnotationChecker extends CPSUtils with Modes {
476466
}
477467
tpe
478468

479-
case ret @ Return(expr) =>
480-
if (hasPlusMarker(expr.tpe))
481-
ret setType expr.tpe
482-
ret.tpe
483-
484469
case _ =>
485470
tpe
486471
}

src/continuations/plugin/scala/tools/selectivecps/CPSUtils.scala

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
package scala.tools.selectivecps
44

55
import scala.tools.nsc.Global
6-
import scala.collection.mutable.ListBuffer
76

87
trait CPSUtils {
98
val global: Global
@@ -136,43 +135,4 @@ trait CPSUtils {
136135
case _ => None
137136
}
138137
}
139-
140-
def isTailReturn(retExpr: Tree, body: Tree): Boolean = {
141-
val removed = ListBuffer[Tree]()
142-
removeTailReturn(body, removed)
143-
removed contains retExpr
144-
}
145-
146-
def removeTailReturn(tree: Tree, removed: ListBuffer[Tree]): Tree = tree match {
147-
case Block(stms, r @ Return(expr)) =>
148-
removed += r
149-
treeCopy.Block(tree, stms, expr)
150-
151-
case Block(stms, expr) =>
152-
treeCopy.Block(tree, stms, removeTailReturn(expr, removed))
153-
154-
case If(cond, r1 @ Return(thenExpr), r2 @ Return(elseExpr)) =>
155-
removed ++= Seq(r1, r2)
156-
treeCopy.If(tree, cond, removeTailReturn(thenExpr, removed), removeTailReturn(elseExpr, removed))
157-
158-
case If(cond, thenExpr, elseExpr) =>
159-
treeCopy.If(tree, cond, removeTailReturn(thenExpr, removed), removeTailReturn(elseExpr, removed))
160-
161-
case Try(block, catches, finalizer) =>
162-
treeCopy.Try(tree,
163-
removeTailReturn(block, removed),
164-
(catches map (t => removeTailReturn(t, removed))).asInstanceOf[List[CaseDef]],
165-
removeTailReturn(finalizer, removed))
166-
167-
case CaseDef(pat, guard, r @ Return(expr)) =>
168-
removed += r
169-
treeCopy.CaseDef(tree, pat, guard, expr)
170-
171-
case CaseDef(pat, guard, body) =>
172-
treeCopy.CaseDef(tree, pat, guard, removeTailReturn(body, removed))
173-
174-
case _ =>
175-
tree
176-
}
177-
178138
}

src/continuations/plugin/scala/tools/selectivecps/SelectiveANFTransform.scala

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import scala.tools.nsc.plugins._
99

1010
import scala.tools.nsc.ast._
1111

12-
import scala.collection.mutable.ListBuffer
13-
1412
/**
1513
* In methods marked @cps, explicitly name results of calls to other @cps methods
1614
*/
@@ -48,20 +46,10 @@ abstract class SelectiveANFTransform extends PluginComponent with Transform with
4846
// this would cause infinite recursion. But we could remove the
4947
// ValDef case here.
5048

51-
case dd @ DefDef(mods, name, tparams, vparamss, tpt, rhs0) =>
49+
case dd @ DefDef(mods, name, tparams, vparamss, tpt, rhs) =>
5250
debuglog("transforming " + dd.symbol)
5351

5452
atOwner(dd.symbol) {
55-
val tailReturns = ListBuffer[Tree]()
56-
val rhs = removeTailReturn(rhs0, tailReturns)
57-
// throw an error if there is a Return tree which is not in tail position
58-
rhs0 foreach {
59-
case r @ Return(_) =>
60-
if (!tailReturns.contains(r))
61-
unit.error(r.pos, "return expressions in CPS code must be in tail position")
62-
case _ => /* do nothing */
63-
}
64-
6553
val rhs1 = transExpr(rhs, None, getExternalAnswerTypeAnn(tpt.tpe))
6654

6755
debuglog("result "+rhs1)
@@ -165,6 +153,7 @@ abstract class SelectiveANFTransform extends PluginComponent with Transform with
165153
}
166154
}
167155

156+
168157
def transExpr(tree: Tree, cpsA: CPSInfo, cpsR: CPSInfo): Tree = {
169158
transTailValue(tree, cpsA, cpsR) match {
170159
case (Nil, b) => b

test/files/continuations-neg/ts-1681-nontail-return.check

Lines changed: 0 additions & 4 deletions
This file was deleted.

test/files/continuations-neg/ts-1681-nontail-return.scala

Lines changed: 0 additions & 18 deletions
This file was deleted.

test/files/continuations-run/ts-1681-2.check

Lines changed: 0 additions & 5 deletions
This file was deleted.

test/files/continuations-run/ts-1681-2.scala

Lines changed: 0 additions & 44 deletions
This file was deleted.

test/files/continuations-run/ts-1681-3.check

Lines changed: 0 additions & 4 deletions
This file was deleted.

test/files/continuations-run/ts-1681-3.scala

Lines changed: 0 additions & 27 deletions
This file was deleted.

test/files/continuations-run/ts-1681.check

Lines changed: 0 additions & 3 deletions
This file was deleted.

test/files/continuations-run/ts-1681.scala

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)