Skip to content

Fix/capturedvars #354

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 3 commits into from
Feb 10, 2015
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
8 changes: 2 additions & 6 deletions src/dotty/tools/dotc/transform/CapturedVars.scala
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,12 @@ class CapturedVars extends MiniPhase with IdentityDenotTransformer { thisTransfo
refMap.getOrElse(cls, refMap(defn.ObjectClass))
}

def capturedType(vble: Symbol)(implicit ctx: Context): Type = {
val oldInfo = vble.denot(ctx.withPhase(thisTransform)).info
refCls(oldInfo.classSymbol, vble.isVolatile).typeRef
}

override def prepareForValDef(vdef: ValDef)(implicit ctx: Context) = {
val sym = vdef.symbol
if (captured contains sym) {
val newd = sym.denot(ctx.withPhase(thisTransform)).copySymDenotation(
info = refCls(sym.info.classSymbol, sym.hasAnnotation(defn.VolatileAnnot)).typeRef)
info = refCls(sym.info.classSymbol, sym.hasAnnotation(defn.VolatileAnnot)).typeRef,
initFlags = sym.flags &~ Mutable)
newd.removeAnnotation(defn.VolatileAnnot)
newd.installAfter(thisTransform)
}
Expand Down
7 changes: 1 addition & 6 deletions src/dotty/tools/dotc/util/SourceFile.scala
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,7 @@ case class SourceFile(file: AbstractFile, content: Array[Char]) {
* Lines are numbered from 0
*/
def offsetToLine(offset: Int): Int = {
val lines = lineIndices
def findLine(lo: Int, hi: Int, mid: Int): Int =
if (offset < lines(mid)) findLine(lo, mid - 1, (lo + mid - 1) / 2)
else if (offset >= lines(mid + 1)) findLine(mid + 1, hi, (mid + 1 + hi) / 2)
else mid
lastLine = findLine(0, lines.length, lastLine)
lastLine = Util.bestFit(lineIndices, offset, lastLine)
lastLine
}

Expand Down
34 changes: 34 additions & 0 deletions src/dotty/tools/dotc/util/Util.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package dotty.tools.dotc.util
import reflect.ClassTag

object Util {

/** The index `i` in `candidates.indices` such that `candidates(i) <= x` and
* `candidates(i)` is closest to `x`, determined by binary search, or -1
* if `x < candidates(0)`.
* @param hint If between 0 and `candidates.length` use this
* as the first search point, otherwise use
* `candidates.length/2`.
* @pre candidates is sorted
*/
def bestFit(candidates: Array[Int], x: Int, hint: Int = -1): Int = {
def recur(lo: Int, hi: Int, mid: Int): Int =
if (x < candidates(mid))
recur(lo, mid - 1, (lo + mid - 1) / 2)
else if (mid + 1 < candidates.length && x >= candidates(mid + 1))
recur(mid + 1, hi, (mid + 1 + hi) / 2)
Copy link
Member

Choose a reason for hiding this comment

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

This is prone to overflow (http://googleresearch.blogspot.com.au/2006/06/extra-extra-read-all-about-it-nearly.html).

Of course we're unlikely to deal with a source file long enough to hit the problem :)

else mid
val initMid =
if (0 <= hint && hint < candidates.length) hint
else candidates.length / 2
if (candidates.isEmpty || x < candidates(0)) -1
else recur(0, candidates.length, initMid)
}

/** An array twice the size of given array, with existing elements copied over */
def dble[T: ClassTag](arr: Array[T]) = {
val arr1 = new Array[T](arr.length * 2)
Array.copy(arr, 0, arr1, 0, arr.length)
arr1
}
}