Skip to content

Commit 879483d

Browse files
authored
Merge pull request scala#7841 from som-snytt/topic/clean-patmat
Reduce or eliminate postfixOps
2 parents da9e140 + 1c2ec05 commit 879483d

33 files changed

+69
-111
lines changed

src/compiler/scala/tools/nsc/Global.scala

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import transform.patmat.PatternMatching
3636
import transform._
3737
import backend.{JavaPlatform, ScalaPrimitives}
3838
import backend.jvm.{BackendStats, GenBCode}
39-
import scala.language.postfixOps
4039
import scala.tools.nsc.ast.{TreeGen => AstTreeGen}
4140
import scala.tools.nsc.classpath._
4241
import scala.tools.nsc.profile.Profiler
@@ -756,7 +755,7 @@ class Global(var currentSettings: Settings, reporter0: LegacyReporter)
756755
private def phaseHelp(title: String, elliptically: Boolean, describe: SubComponent => String): String = {
757756
val Limit = 16 // phase names should not be absurdly long
758757
val MaxCol = 80 // because some of us edit on green screens
759-
val maxName = phaseNames map (_.length) max
758+
val maxName = phaseNames.map(_.length).max
760759
val width = maxName min Limit
761760
val maxDesc = MaxCol - (width + 6) // descriptions not novels
762761
val fmt = if (settings.verbose || !elliptically) s"%${maxName}s %2s %s%n"
@@ -806,13 +805,12 @@ class Global(var currentSettings: Settings, reporter0: LegacyReporter)
806805
/** Returns List of (phase, value) pairs, including only those
807806
* where the value compares unequal to the previous phase's value.
808807
*/
809-
def afterEachPhase[T](op: => T): List[(Phase, T)] = { // used in tests
808+
def afterEachPhase[T](op: => T): List[(Phase, T)] = // used in tests
810809
phaseDescriptors.map(_.ownPhase).filterNot(_ eq NoPhase).foldLeft(List[(Phase, T)]()) { (res, ph) =>
811810
val value = exitingPhase(ph)(op)
812811
if (res.nonEmpty && res.head._2 == value) res
813812
else ((ph, value)) :: res
814-
} reverse
815-
}
813+
}.reverse
816814

817815
// ------------ REPL utilities ---------------------------------
818816

@@ -1420,7 +1418,7 @@ class Global(var currentSettings: Settings, reporter0: LegacyReporter)
14201418
case -1 => mkName(str)
14211419
case idx =>
14221420
val phasePart = str drop (idx + 1)
1423-
settings.Yshow.tryToSetColon(phasePart split ',' toList)
1421+
settings.Yshow.tryToSetColon(phasePart.split(',').toList)
14241422
mkName(str take idx)
14251423
}
14261424
}
@@ -1699,7 +1697,7 @@ class Global(var currentSettings: Settings, reporter0: LegacyReporter)
16991697
val syms = findMemberFromRoot(fullName) match {
17001698
// The name as given was not found, so we'll sift through every symbol in
17011699
// the run looking for plausible matches.
1702-
case NoSymbol => phased(currentRun.symSource.keys map (sym => findNamedMember(fullName, sym)) filterNot (_ == NoSymbol) toList)
1700+
case NoSymbol => phased(currentRun.symSource.keys.map(findNamedMember(fullName, _)).filterNot(_ == NoSymbol).toList)
17031701
// The name as given matched, so show only that.
17041702
case sym => List(sym)
17051703
}

src/compiler/scala/tools/nsc/Main.scala

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@
1313
package scala.tools
1414
package nsc
1515

16-
import scala.language.postfixOps
17-
1816
/** The main class for NSC, a compiler for the programming
1917
* language Scala.
2018
*/
2119
class MainClass extends Driver with EvalLoop {
2220
def resident(compiler: Global): Unit = loop { line =>
23-
val command = new CompilerCommand(line split "\\s+" toList, new Settings(scalacError))
21+
val command = new CompilerCommand(line.split("\\s+").toList, new Settings(scalacError))
2422
compiler.reporter.reset()
2523
new compiler.Run() compile command.files
2624
}

src/compiler/scala/tools/nsc/PhaseAssembly.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
package scala.tools.nsc
1414

1515
import scala.collection.mutable
16-
import scala.language.postfixOps
1716

1817
/** Converts an unordered morass of components into an order that
1918
* satisfies their mutual constraints.
@@ -99,7 +98,7 @@ trait PhaseAssembly {
9998
* names are sorted alphabetical at each level, into the compiler phase list
10099
*/
101100
def compilerPhaseList(): List[SubComponent] =
102-
nodes.values.toList filter (_.level > 0) sortBy (x => (x.level, x.phasename)) flatMap (_.phaseobj) flatten
101+
nodes.values.toList.filter(_.level > 0).sortBy(x => (x.level, x.phasename)).flatMap(_.phaseobj).flatten
103102

104103
/* Test if there are cycles in the graph, assign levels to the nodes
105104
* and collapse hard links into nodes

src/compiler/scala/tools/nsc/ast/NodePrinters.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ package ast
1515

1616
import java.lang.System.{lineSeparator => EOL}
1717
import symtab.Flags._
18-
import scala.language.postfixOps
1918
import scala.reflect.internal.util.ListOfNil
2019

2120
/** The object `nodePrinter` converts the internal tree
@@ -67,8 +66,8 @@ abstract class NodePrinters {
6766
def showAttributes(tree: Tree): String = {
6867
if (infolevel == InfoLevel.Quiet) ""
6968
else {
70-
try { List(showSymbol(tree), showType(tree)) filterNot (_ == "") mkString ", " trim }
71-
catch { case ex: Throwable => "sym= <error> " + ex.getMessage }
69+
try List(showSymbol(tree), showType(tree)).filterNot(_ == "").mkString(", ").trim
70+
catch { case ex: Throwable => s"sym= <error> ${ex.getMessage}" }
7271
}
7372
}
7473
}

src/compiler/scala/tools/nsc/ast/TreeDSL.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,10 @@ trait TreeDSL {
128128
}
129129
class TryStart(body: Tree, catches: List[CaseDef], fin: Tree) {
130130
def CATCH(xs: CaseDef*) = new TryStart(body, xs.toList, fin)
131-
def ENDTRY = Try(body, catches, fin)
131+
def FINALLY(end: END.type) = Try(body, catches, fin)
132+
def FINALLY(fin1: Tree) = Try(body, catches, fin1)
132133
}
134+
object END
133135

134136
def CASE(pat: Tree): CaseStart = new CaseStart(pat, EmptyTree)
135137
def DEFAULT: CaseStart = new CaseStart(Ident(nme.WILDCARD), EmptyTree)

src/compiler/scala/tools/nsc/ast/TreeGen.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ package ast
1515

1616
import scala.collection.mutable.ListBuffer
1717
import symtab.Flags._
18-
import scala.language.postfixOps
1918
import scala.reflect.internal.util.FreshNameCreator
2019
import scala.reflect.internal.util.ListOfNil
2120

@@ -106,7 +105,7 @@ abstract class TreeGen extends scala.reflect.internal.TreeGen with TreeDSL {
106105
def mkAppliedTypeForCase(clazz: Symbol): Tree = {
107106
val numParams = clazz.typeParams.size
108107
if (clazz.typeParams.isEmpty) Ident(clazz)
109-
else AppliedTypeTree(Ident(clazz), 1 to numParams map (_ => Bind(tpnme.WILDCARD, EmptyTree)) toList)
108+
else AppliedTypeTree(Ident(clazz), (1 to numParams).map(_ => Bind(tpnme.WILDCARD, EmptyTree)).toList)
110109
}
111110
def mkBindForCase(patVar: Symbol, clazz: Symbol, targs: List[Type]): Tree = {
112111
Bind(patVar, Typed(Ident(nme.WILDCARD),

src/compiler/scala/tools/nsc/ast/parser/Scanners.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import Tokens._
2020
import scala.annotation.{switch, tailrec}
2121
import scala.collection.mutable, mutable.{ListBuffer, ArrayBuffer}
2222
import scala.tools.nsc.ast.parser.xml.Utility.isNameStart
23-
import scala.language.postfixOps
2423

2524
/** See Parsers.scala / ParsersCommon for some explanation of ScannersCommon.
2625
*/
@@ -1379,7 +1378,7 @@ trait Scanners extends ScannersCommon {
13791378
/** The source code with braces and line starts annotated with [NN] showing the index */
13801379
private def markedSource = {
13811380
val code = unit.source.content
1382-
val braces = code.indices filter (idx => "{}\n" contains code(idx)) toSet
1381+
val braces = code.indices.filter(idx => "{}\n" contains code(idx)).toSet
13831382
val mapped = code.indices map (idx => if (braces(idx)) s"${code(idx)}[$idx]" else "" + code(idx))
13841383
mapped.mkString("")
13851384
}

src/compiler/scala/tools/nsc/io/Jar.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
package scala.tools.nsc
1414
package io
1515

16-
import scala.language.postfixOps
1716
import java.io.{DataOutputStream, InputStream, OutputStream}
1817
import java.util.jar._
1918

@@ -51,9 +50,9 @@ class Jar(file: File) extends AbstractIterable[JarEntry] {
5150
def mainClass = manifest map (f => f(Name.MAIN_CLASS))
5251
/** The manifest-defined classpath String if available. */
5352
def classPathString: Option[String] =
54-
for (m <- manifest ; cp <- m.attrs get Name.CLASS_PATH) yield cp
53+
for (m <- manifest ; cp <- m.attrs.get(Name.CLASS_PATH)) yield cp
5554
def classPathElements: List[String] = classPathString match {
56-
case Some(s) => s split "\\s+" toList
55+
case Some(s) => s.split("\\s+").toList
5756
case _ => Nil
5857
}
5958

src/compiler/scala/tools/nsc/symtab/SymbolTrackers.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ package scala.tools.nsc
1414
package symtab
1515

1616
import scala.language.implicitConversions
17-
import scala.language.postfixOps
1817

1918
/** Printing the symbol graph (for those symbols attached to an AST node)
2019
* after each phase.
@@ -176,8 +175,8 @@ trait SymbolTrackers {
176175
val change = Change(added, removed, prevMap, owners, flags)
177176

178177
prevMap = currentMap
179-
prevOwners = current map (s => (s, s.owner)) toMap;
180-
prevFlags = current map (s => (s, (s.flags & flagsMask))) toMap;
178+
prevOwners = current.map(s => (s, s.owner)).toMap
179+
prevFlags = current.map(s => (s, (s.flags & flagsMask))).toMap
181180
history = change :: history
182181
}
183182
def show(label: String): String = {

src/compiler/scala/tools/nsc/transform/CleanUp.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package transform
1616
import symtab._
1717
import Flags._
1818
import scala.collection._
19-
import scala.language.postfixOps
2019

2120
abstract class CleanUp extends Statics with Transform with ast.TreeDSL {
2221
import global._
@@ -269,7 +268,7 @@ abstract class CleanUp extends Statics with Transform with ast.TreeDSL {
269268
def catchBody = Throw(Apply(Select(Ident(invokeExc), nme.getCause), Nil))
270269

271270
// try { method.invoke } catch { case e: InvocationTargetExceptionClass => throw e.getCause() }
272-
fixResult(TRY (invocation) CATCH { CASE (catchVar) ==> catchBody } ENDTRY)
271+
fixResult(TRY (invocation) CATCH { CASE (catchVar) ==> catchBody } FINALLY END)
273272
}
274273

275274
/* A possible primitive method call, represented by methods in BoxesRunTime. */

src/compiler/scala/tools/nsc/transform/patmat/Logic.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,14 @@
1313
package scala
1414
package tools.nsc.transform.patmat
1515

16-
import scala.language.postfixOps
1716
import scala.collection.mutable
1817
import scala.collection.immutable.ArraySeq
1918
import scala.reflect.internal.util.{HashSet, NoPosition, Position, StatisticsStatics}
2019

2120
trait Logic extends Debugging {
2221
import global.statistics
2322

24-
private def max(xs: Seq[Int]) = if (xs isEmpty) 0 else xs max
23+
private def max(xs: Seq[Int]) = if (xs.isEmpty) 0 else xs.max
2524
private def alignedColumns(cols: Seq[Any]): Seq[String] = {
2625
def toString(x: Any) = if (x == null) "" else x.toString
2726
if (cols.isEmpty || cols.tails.isEmpty) cols map toString

src/compiler/scala/tools/nsc/transform/patmat/MatchCodeGen.scala

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212

1313
package scala.tools.nsc.transform.patmat
1414

15-
import scala.language.postfixOps
16-
1715
import scala.tools.nsc.symtab.Flags.SYNTHETIC
1816
import scala.reflect.internal.util.Position
1917

@@ -128,11 +126,11 @@ trait MatchCodeGen extends Interface {
128126
// must compute catchAll after caseLabels (side-effects nextCase)
129127
// catchAll.isEmpty iff no synthetic default case needed (the (last) user-defined case is a default)
130128
// if the last user-defined case is a default, it will never jump to the next case; it will go immediately to matchEnd
131-
val catchAllDef = matchFailGen map { matchFailGen =>
129+
val catchAllDef = matchFailGen.map { matchFailGen =>
132130
val scrutRef = scrutSym.fold(EmptyTree: Tree)(REF) // for alternatives
133131

134-
LabelDef(_currCase, Nil, matchEnd APPLY (matchFailGen(scrutRef)))
135-
} toList // at most 1 element
132+
LabelDef(_currCase, Nil, matchEnd APPLY matchFailGen(scrutRef))
133+
}.toList // at most 1 element
136134

137135
// scrutSym == NoSymbol when generating an alternatives matcher
138136
val scrutDef = scrutSym.fold(List[Tree]())(ValDef(_, scrut) :: Nil) // for alternatives

src/compiler/scala/tools/nsc/transform/patmat/MatchOptimization.scala

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212

1313
package scala.tools.nsc.transform.patmat
1414

15-
import scala.language.postfixOps
16-
1715
import scala.tools.nsc.symtab.Flags.MUTABLE
1816
import scala.collection.mutable
1917
import scala.reflect.internal.util.Position
@@ -62,7 +60,7 @@ trait MatchOptimization extends MatchTreeMaking with MatchAnalysis {
6260
if (conds(False)) false // stop when we encounter a definite "no" or a "not sure"
6361
else {
6462
val nonTrivial = conds - True
65-
if (nonTrivial nonEmpty) {
63+
if (!nonTrivial.isEmpty) {
6664
tested ++= nonTrivial
6765

6866
// is there an earlier test that checks our condition and whose dependencies are implied by ours?
@@ -468,7 +466,7 @@ trait MatchOptimization extends MatchTreeMaking with MatchAnalysis {
468466
// a switch with duplicate cases yields a verify error,
469467
// and a switch with duplicate cases and guards cannot soundly be rewritten to an unguarded switch
470468
// (even though the verify error would disappear, the behaviour would change)
471-
val allReachable = unreachableCase(caseDefsWithGuards) map (cd => reportUnreachable(cd.body.pos)) isEmpty
469+
val allReachable = unreachableCase(caseDefsWithGuards).map(cd => reportUnreachable(cd.body.pos)).isEmpty
472470

473471
if (!allReachable) Nil
474472
else if (noGuards(caseDefsWithGuards)) {
@@ -536,7 +534,7 @@ trait MatchOptimization extends MatchTreeMaking with MatchAnalysis {
536534
// TODO: if patterns allow switch but the type of the scrutinee doesn't, cast (type-test) the scrutinee to the corresponding switchable type and switch on the result
537535
if (regularSwitchMaker.switchableTpe(dealiasWiden(scrutSym.tpe))) {
538536
val caseDefsWithDefault = regularSwitchMaker(cases map {c => (scrutSym, c)}, pt)
539-
if (caseDefsWithDefault isEmpty) None // not worth emitting a switch.
537+
if (caseDefsWithDefault.isEmpty) None // not worth emitting a switch.
540538
else {
541539
// match on scrutSym -- converted to an int if necessary -- not on scrut directly (to avoid duplicating scrut)
542540
val scrutToInt: Tree =
@@ -586,7 +584,7 @@ trait MatchOptimization extends MatchTreeMaking with MatchAnalysis {
586584
// TODO: drop null checks
587585
override def emitTypeSwitch(bindersAndCases: List[(Symbol, List[TreeMaker])], pt: Type): Option[List[CaseDef]] = {
588586
val caseDefsWithDefault = typeSwitchMaker(bindersAndCases, pt)
589-
if (caseDefsWithDefault isEmpty) None
587+
if (caseDefsWithDefault.isEmpty) None
590588
else Some(caseDefsWithDefault)
591589
}
592590
}

src/compiler/scala/tools/nsc/transform/patmat/MatchTranslation.scala

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@
1212

1313
package scala.tools.nsc.transform.patmat
1414

15-
import scala.language.postfixOps
1615
import scala.reflect.internal.util.StatisticsStatics
1716

18-
1917
/** Translate typed Trees that represent pattern matches into the patternmatching IR, defined by TreeMakers.
2018
*/
2119
trait MatchTranslation {
@@ -30,8 +28,6 @@ trait MatchTranslation {
3028
private def setVarInfo(sym: Symbol, info: Type) =
3129
sym setInfo debug.patmatResult(s"changing ${sym.defString} to")(repeatedToSeq(info))
3230

33-
private def hasSym(t: Tree) = t.symbol != null && t.symbol != NoSymbol
34-
3531
trait MatchTranslator extends TreeMakers with TreeMakerWarnings {
3632
import typer.context
3733
def selectorPos: Position
@@ -62,8 +58,8 @@ trait MatchTranslation {
6258

6359
object SymbolBound {
6460
def unapply(tree: Tree): Option[(Symbol, Tree)] = tree match {
65-
case Bind(_, expr) if hasSym(tree) => Some(tree.symbol -> expr)
66-
case _ => None
61+
case Bind(_, expr) if tree.hasExistingSymbol => Some(tree.symbol -> expr)
62+
case _ => None
6763
}
6864
}
6965

@@ -408,7 +404,7 @@ trait MatchTranslation {
408404
}
409405

410406
// never store these in local variables (for PreserveSubPatBinders)
411-
lazy val ignoredSubPatBinders: Set[Symbol] = subPatBinders zip args collect { case (b, PatternBoundToUnderscore()) => b } toSet
407+
lazy val ignoredSubPatBinders: Set[Symbol] = (subPatBinders zip args).collect { case (b, PatternBoundToUnderscore()) => b }.toSet
412408

413409
// there are `productArity` non-seq elements in the tuple.
414410
protected def firstIndexingBinder = productArity

src/compiler/scala/tools/nsc/transform/patmat/MatchTreeMaking.scala

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212

1313
package scala.tools.nsc.transform.patmat
1414

15-
import scala.language.postfixOps
16-
1715
import scala.tools.nsc.symtab.Flags.{SYNTHETIC, ARTIFACT}
1816
import scala.collection.mutable
1917
import scala.reflect.internal.util.Position
@@ -632,7 +630,7 @@ trait MatchTreeMaking extends MatchCodeGen with Debugging {
632630
emitSwitch(scrut, scrutSym, casesNoSubstOnly, pt, matchFailGenOverride, unchecked = suppression.suppressExhaustive).getOrElse{
633631
if (requireSwitch) reporter.warning(scrut.pos, "could not emit switch for @switch annotated match")
634632

635-
if (casesNoSubstOnly nonEmpty) {
633+
if (!casesNoSubstOnly.isEmpty) {
636634
// before optimizing, check casesNoSubstOnly for presence of a default case,
637635
// since DCE will eliminate trivial cases like `case _ =>`, even if they're the last one
638636
// exhaustivity and reachability must be checked before optimization as well
@@ -652,7 +650,7 @@ trait MatchTreeMaking extends MatchCodeGen with Debugging {
652650

653651
val matchRes = codegen.matcher(scrut, scrutSym, pt)(cases map combineExtractors, synthCatchAll)
654652

655-
if (toHoist isEmpty) matchRes else Block(toHoist, matchRes)
653+
if (toHoist.isEmpty) matchRes else Block(toHoist, matchRes)
656654
} else {
657655
codegen.matcher(scrut, scrutSym, pt)(Nil, matchFailGen)
658656
}

src/compiler/scala/tools/nsc/transform/patmat/PatternMatching.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ package scala.tools.nsc.transform.patmat
1515
import scala.collection.mutable.ListBuffer
1616
import scala.tools.nsc.Global
1717
import scala.tools.nsc.ast
18-
import scala.language.postfixOps
1918
import scala.tools.nsc.transform.TypingTransformers
2019
import scala.tools.nsc.transform.Transform
2120
import scala.reflect.internal.util.Statistics
@@ -207,7 +206,7 @@ trait Interface extends ast.TreeDSL {
207206
def apply(from: Symbol, to: Tree): Substitution = new Substitution(from :: Nil, to :: Nil)
208207
// requires sameLength(from, to)
209208
def apply(from: List[Symbol], to: List[Tree]): Substitution =
210-
if (from nonEmpty) new Substitution(from, to) else EmptySubstitution
209+
if (from.isEmpty) EmptySubstitution else new Substitution(from, to)
211210
}
212211

213212
class Substitution(val from: List[Symbol], val to: List[Tree]) {

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ package scala.tools.nsc
1414
package typechecker
1515

1616
import Checkability._
17-
import scala.language.postfixOps
1817

1918
/** On pattern matcher checkability:
2019
*
@@ -292,7 +291,7 @@ trait Checkable {
292291
case TypeRef(_, NothingClass | NullClass | AnyValClass, _) => false
293292
case RefinedType(_, decls) if !decls.isEmpty => false
294293
case RefinedType(parents, _) => parents forall isCheckable
295-
case p => new CheckabilityChecker(AnyTpe, p) isCheckable
294+
case p => new CheckabilityChecker(AnyTpe, p).isCheckable
296295
})
297296
)
298297

0 commit comments

Comments
 (0)