Skip to content

Commit 03555e8

Browse files
committed
Avoid some boxings of vars
1 parent 10359a5 commit 03555e8

File tree

5 files changed

+45
-34
lines changed

5 files changed

+45
-34
lines changed

compiler/src/dotty/tools/dotc/ast/Positioned.scala

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,17 @@ abstract class Positioned(implicit @constructorOnly src: SourceFile) extends Src
154154
}
155155
}
156156

157+
private class LastPosRef:
158+
var positioned: Positioned | Null = null
159+
var span = NoSpan
160+
157161
/** Check that all positioned items in this tree satisfy the following conditions:
158162
* - Parent spans contain child spans
159163
* - If item is a non-empty tree, it has a position
160164
*/
161165
def checkPos(nonOverlapping: Boolean)(using Context): Unit = try {
162166
import untpd._
163-
var lastPositioned: Positioned | Null = null
164-
var lastSpan = NoSpan
167+
val last = LastPosRef()
165168
def check(p: Any): Unit = p match {
166169
case p: Positioned =>
167170
assert(span contains p.span,
@@ -181,19 +184,19 @@ abstract class Positioned(implicit @constructorOnly src: SourceFile) extends Src
181184
case _: XMLBlock =>
182185
// FIXME: Trees generated by the XML parser do not satisfy `checkPos`
183186
case _: WildcardFunction
184-
if lastPositioned.isInstanceOf[ValDef] && !p.isInstanceOf[ValDef] =>
187+
if last.positioned.isInstanceOf[ValDef] && !p.isInstanceOf[ValDef] =>
185188
// ignore transition from last wildcard parameter to body
186189
case _ =>
187-
assert(!lastSpan.exists || !p.span.exists || lastSpan.end <= p.span.start,
190+
assert(!last.span.exists || !p.span.exists || last.span.end <= p.span.start,
188191
i"""position error, child positions overlap or in wrong order
189192
|parent = $this
190-
|1st child = $lastPositioned
191-
|1st child span = $lastSpan
193+
|1st child = ${last.positioned}
194+
|1st child span = ${last.span}
192195
|2nd child = $p
193196
|2nd child span = ${p.span}""".stripMargin)
194197
}
195-
lastPositioned = p
196-
lastSpan = p.span
198+
last.positioned = p
199+
last.span = p.span
197200
p.checkPos(nonOverlapping)
198201
case m: untpd.Modifiers =>
199202
m.annotations.foreach(check)

compiler/src/dotty/tools/dotc/ast/TreeInfo.scala

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -830,10 +830,12 @@ trait TypedTreeInfo extends TreeInfo[Type] { self: Trees.Instance[Type] =>
830830

831831
/** The symbols defined locally in a statement list */
832832
def localSyms(stats: List[Tree])(using Context): List[Symbol] =
833-
val locals = new mutable.ListBuffer[Symbol]
834-
for stat <- stats do
835-
if stat.isDef && stat.symbol.exists then locals += stat.symbol
836-
locals.toList
833+
if stats.isEmpty then Nil
834+
else
835+
val locals = new mutable.ListBuffer[Symbol]
836+
for stat <- stats do
837+
if stat.isDef && stat.symbol.exists then locals += stat.symbol
838+
locals.toList
837839

838840
/** If `tree` is a DefTree, the symbol defined by it, otherwise NoSymbol */
839841
def definedSym(tree: Tree)(using Context): Symbol =

compiler/src/dotty/tools/dotc/parsing/Scanners.scala

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ object Scanners {
553553

554554
// If nextWidth is an indentation level not yet seen by enclosing indentation
555555
// region, invoke `handler`.
556-
def handleNewIndentWidth(r: Region, handler: Indented => Unit): Unit = r match
556+
inline def handleNewIndentWidth(r: Region, inline handler: Indented => Unit): Unit = r match
557557
case r @ Indented(curWidth, prefix, outer)
558558
if curWidth < nextWidth && !r.otherIndentWidths.contains(nextWidth) && nextWidth != lastWidth =>
559559
handler(r)
@@ -571,7 +571,7 @@ object Scanners {
571571
* they start with `(`, `[` or `{`, or the last statement ends in a `return`.
572572
* The Scala 2 rules apply under source `3.0-migration` or under `-no-indent`.
573573
*/
574-
def isContinuing =
574+
inline def isContinuing =
575575
lastWidth < nextWidth
576576
&& (openParensTokens.contains(token) || lastToken == RETURN)
577577
&& !pastBlankLine
@@ -608,10 +608,11 @@ object Scanners {
608608
case r: Indented =>
609609
insert(OUTDENT, offset)
610610
handleNewIndentWidth(r.enclosing, ir =>
611+
val lw = lastWidth
611612
errorButContinue(
612613
em"""The start of this line does not match any of the previous indentation widths.
613614
|Indentation width of current line : $nextWidth
614-
|This falls between previous widths: ${ir.width} and $lastWidth"""))
615+
|This falls between previous widths: ${ir.width} and $lw"""))
615616
case r =>
616617
if skipping then
617618
if r.enclosing.isClosedByUndentAt(nextWidth) then
@@ -627,7 +628,8 @@ object Scanners {
627628
else if lastToken == SELFARROW then
628629
currentRegion.knownWidth = nextWidth
629630
else if (lastWidth != nextWidth)
630-
errorButContinue(spaceTabMismatchMsg(lastWidth, nextWidth))
631+
val lw = lastWidth
632+
errorButContinue(spaceTabMismatchMsg(lw, nextWidth))
631633
if token != OUTDENT then
632634
handleNewIndentWidth(currentRegion, _.otherIndentWidths += nextWidth)
633635
if next.token == EMPTY then

compiler/src/dotty/tools/dotc/transform/Erasure.scala

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -698,18 +698,20 @@ object Erasure {
698698
return tree.asInstanceOf[Tree] // we are re-typing a primitive array op
699699

700700
val owner = mapOwner(origSym)
701-
var sym = if (owner eq origSym.maybeOwner) origSym else owner.info.decl(tree.name).symbol
702-
if !sym.exists then
703-
// We fail the sym.exists test for pos/i15158.scala, where we pass an infinitely
704-
// recurring match type to an overloaded constructor. An equivalent test
705-
// with regular apply methods succeeds. It's at present unclear whether
706-
// - the program should be rejected, or
707-
// - there is another fix.
708-
// Therefore, we apply the fix to use the pre-erasure symbol, but only
709-
// for constructors, in order not to mask other possible bugs that would
710-
// trigger the assert(sym.exists, ...) below.
711-
val prevSym = tree.symbol(using preErasureCtx)
712-
if prevSym.isConstructor then sym = prevSym
701+
val sym =
702+
(if (owner eq origSym.maybeOwner) origSym else owner.info.decl(tree.name).symbol)
703+
.orElse {
704+
// We fail the sym.exists test for pos/i15158.scala, where we pass an infinitely
705+
// recurring match type to an overloaded constructor. An equivalent test
706+
// with regular apply methods succeeds. It's at present unclear whether
707+
// - the program should be rejected, or
708+
// - there is another fix.
709+
// Therefore, we apply the fix to use the pre-erasure symbol, but only
710+
// for constructors, in order not to mask other possible bugs that would
711+
// trigger the assert(sym.exists, ...) below.
712+
val prevSym = tree.symbol(using preErasureCtx)
713+
if prevSym.isConstructor then prevSym else NoSymbol
714+
}
713715

714716
assert(sym.exists, i"no owner from $owner/${origSym.showLocated} in $tree")
715717

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -244,15 +244,17 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
244244
imp.importSym.info match
245245
case ImportType(expr) =>
246246
val pre = expr.tpe
247-
var denot = pre.memberBasedOnFlags(name, required, excluded)
247+
val denot0 = pre.memberBasedOnFlags(name, required, excluded)
248248
.accessibleFrom(pre)(using refctx)
249249
// Pass refctx so that any errors are reported in the context of the
250250
// reference instead of the context of the import scope
251-
if denot.exists then
252-
if checkBounds then
253-
denot = denot.filterWithPredicate { mbr =>
254-
mbr.matchesImportBound(if mbr.symbol.is(Given) then imp.givenBound else imp.wildcardBound)
255-
}
251+
if denot0.exists then
252+
val denot =
253+
if checkBounds then
254+
denot0.filterWithPredicate { mbr =>
255+
mbr.matchesImportBound(if mbr.symbol.is(Given) then imp.givenBound else imp.wildcardBound)
256+
}
257+
else denot0
256258
def isScalaJsPseudoUnion =
257259
denot.name == tpnme.raw.BAR && ctx.settings.scalajs.value && denot.symbol == JSDefinitions.jsdefn.PseudoUnionClass
258260
// Just like Scala2Unpickler reinterprets Scala.js pseudo-unions

0 commit comments

Comments
 (0)