Skip to content

Convert List.apply into :: chains #18567

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/transform/BetaReduce.scala
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@ object BetaReduce:
case _ => None
tree match
case Apply(Select(fn, nme.apply), args) if defn.isFunctionNType(fn.tpe) =>
recur(fn, List(args)) match
recur(fn, args :: Nil) match
case Some(reduced) =>
seq(bindingsBuf.result(), reduced).withSpan(tree.span)
case None =>
tree
case Apply(TypeApply(Select(fn, nme.apply), targs), args) if fn.tpe.typeSymbol eq dotc.core.Symbols.defn.PolyFunctionClass =>
recur(fn, List(targs, args)) match
recur(fn, targs :: args :: Nil) match
case Some(reduced) =>
seq(bindingsBuf.result(), reduced).withSpan(tree.span)
case None =>
Expand Down
20 changes: 14 additions & 6 deletions compiler/src/dotty/tools/dotc/transform/CheckUnused.scala
Original file line number Diff line number Diff line change
Expand Up @@ -561,11 +561,19 @@ object CheckUnused:
else
Nil
val warnings =
List(sortedImp, sortedLocalDefs, sortedExplicitParams, sortedImplicitParams,
sortedPrivateDefs, sortedPatVars, unsetLocalDefs, unsetPrivateDefs).flatten.sortBy { s =>
val pos = s.pos.sourcePos
(pos.line, pos.column)
}
val unsorted = sortedImp ++
Copy link
Contributor

Choose a reason for hiding this comment

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

This has quadratic complexity. Use ::: instead of ++.

sortedLocalDefs ++
sortedExplicitParams ++
sortedImplicitParams ++
sortedPrivateDefs ++
sortedPatVars ++
unsetLocalDefs ++
unsetPrivateDefs

unsorted.sortBy { s =>
val pos = s.pos.sourcePos
(pos.line, pos.column)
}
UnusedResult(warnings.toSet)
end getUnused
//============================ HELPERS ====================================
Expand Down Expand Up @@ -705,7 +713,7 @@ object CheckUnused:
sym.everySymbol.exists(usedDef.apply)

private def everySymbol(using Context): List[Symbol] =
List(sym, sym.companionClass, sym.companionModule, sym.moduleClass).filter(_.exists)
(sym :: sym.companionClass :: sym.companionModule :: sym.moduleClass :: Nil).filter(_.exists)

/** A function is overriden. Either has `override flags` or parent has a matching member (type and name) */
private def isOverriden(using Context): Boolean =
Expand Down
10 changes: 5 additions & 5 deletions compiler/src/dotty/tools/dotc/transform/CompleteJavaEnums.scala
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ class CompleteJavaEnums extends MiniPhase with InfoTransformer { thisPhase =>
tp.derivedLambdaType(resType = addConstrParams(restpe))
case _ =>
tp.derivedLambdaType(
paramNames = tp.paramNames ++ List(nameParamName, ordinalParamName),
paramInfos = tp.paramInfos ++ List(defn.StringType, defn.IntType))
paramNames = tp.paramNames ++ (nameParamName :: ordinalParamName :: Nil),
paramInfos = tp.paramInfos ++ (defn.StringType :: defn.IntType :: Nil))
}
}

Expand All @@ -70,7 +70,7 @@ class CompleteJavaEnums extends MiniPhase with InfoTransformer { thisPhase =>
val flags = flag | Synthetic | (if isLocal then Private | Deferred else EmptyFlags)
val nameParam = newSymbol(owner, nameParamName, flags, defn.StringType, coord = owner.span)
val ordinalParam = newSymbol(owner, ordinalParamName, flags, defn.IntType, coord = owner.span)
List(ValDef(nameParam), ValDef(ordinalParam))
ValDef(nameParam) :: ValDef(ordinalParam) :: Nil
}

/** Add arguments `args` to the parent constructor application in `parents` that invokes
Expand Down Expand Up @@ -173,9 +173,9 @@ class CompleteJavaEnums extends MiniPhase with InfoTransformer { thisPhase =>
ref(cls.owner.paramSymss.head.find(_.name == name).get)
val args =
if cls.owner.isAllOf(EnumCase) then
List(Literal(Constant(cls.owner.name.toString)), Literal(Constant(ordinalFor(cls.owner))))
Literal(Constant(cls.owner.name.toString)) :: Literal(Constant(ordinalFor(cls.owner))) :: Nil
else
List(creatorParamRef(nme.nameDollar), creatorParamRef(nme.ordinalDollar_))
creatorParamRef(nme.nameDollar) :: creatorParamRef(nme.ordinalDollar_) :: Nil
cpy.Template(templ)(
parents = addEnumConstrArgs(cls.owner.owner.linkedClass, templ.parents, args),
)
Expand Down
10 changes: 5 additions & 5 deletions compiler/src/dotty/tools/dotc/transform/ExpandSAMs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ class ExpandSAMs extends MiniPhase:
val tpe1 = collectAndStripRefinements(tpe)
val Seq(samDenot) = tpe1.possibleSamMethods
cpy.Block(tree)(stats,
AnonClass(List(tpe1),
List(samDenot.symbol.asTerm.name -> fn.symbol.asTerm),
AnonClass(tpe1 :: Nil,
samDenot.symbol.asTerm.name -> fn.symbol.asTerm :: Nil,
refinements.toList
)
)
Expand Down Expand Up @@ -134,9 +134,9 @@ class ExpandSAMs extends MiniPhase:
val pfRHS = partialFunRHS(anon.rhs)
val anonSym = anon.symbol
val anonTpe = anon.tpe.widen
val parents = List(
defn.AbstractPartialFunctionClass.typeRef.appliedTo(anonTpe.firstParamTypes.head, anonTpe.resultType),
defn.SerializableType)
val parents =
defn.AbstractPartialFunctionClass.typeRef.appliedTo(anonTpe.firstParamTypes.head, anonTpe.resultType) ::
defn.SerializableType :: Nil

AnonClass(anonSym.owner, parents, tree.span) { pfSym =>
def overrideSym(sym: Symbol) = sym.copy(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ class ExtensionMethods extends MiniPhase with DenotTransformer with FullParamete
val underlying = valueErasure(underlyingOfValueClass(valueClass))
val evt = ErasedValueType(valueClass.typeRef, underlying)
val u2evtSym = newSymbol(moduleSym, nme.U2EVT, Synthetic | Method,
MethodType(List(nme.x_0), List(underlying), evt))
MethodType(nme.x_0 :: Nil, underlying :: Nil, evt))
val evt2uSym = newSymbol(moduleSym, nme.EVT2U, Synthetic | Method,
MethodType(List(nme.x_0), List(evt), underlying))
MethodType(nme.x_0 :: Nil, evt :: Nil, underlying))
enterInModuleClass(u2evtSym)
enterInModuleClass(evt2uSym)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class FunctionXXLForwarders extends MiniPhase with IdentityDenotTransformer {
var idx = -1
val argss = receiver.tpe.widenDealias.paramInfoss.map(_.map { param =>
idx += 1
argsApply.appliedToTermArgs(List(Literal(Constant(idx)))).cast(param)
argsApply.appliedToTermArgs(Literal(Constant(idx)) :: Nil).cast(param)
})
ref(receiver.symbol).appliedToArgss(argss).cast(defn.ObjectType)
}
Expand All @@ -51,8 +51,8 @@ class FunctionXXLForwarders extends MiniPhase with IdentityDenotTransformer {
ddef.symbol.allOverriddenSymbols.exists(sym => defn.isXXLFunctionClass(sym.owner))
}
yield {
val xsType = defn.ArrayType.appliedTo(List(defn.ObjectType))
val methType = MethodType(List(nme.args))(_ => List(xsType), _ => defn.ObjectType)
val xsType = defn.ArrayType.appliedTo(defn.ObjectType :: Nil)
val methType = MethodType(nme.args :: Nil)(_ => xsType :: Nil, _ => defn.ObjectType)
val meth = newSymbol(ddef.symbol.owner, nme.apply, Synthetic | Method, methType)
DefDef(meth, paramss => forwarderRhs(ddef, paramss.head.head))
}
Expand Down
23 changes: 11 additions & 12 deletions compiler/src/dotty/tools/dotc/transform/Instrumentation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,17 @@ class Instrumentation extends MiniPhase { thisPhase =>
override def isEnabled(using Context) =
ctx.settings.Yinstrument.value

private val collectionNamesOfInterest = List(
"map", "flatMap", "filter", "filterNot", "withFilter", "collect", "flatten", "foldLeft", "foldRight", "take",
"reverse", "zip", "++", ":::", ":+", "distinct", "dropRight", "takeRight", "groupBy", "groupMap", "init", "inits",
"interect", "mkString", "partition", "reverse_:::", "scanLeft", "scanRight",
"sortBy", "sortWith", "sorted", "span", "splitAt", "takeWhile", "transpose", "unzip", "unzip3",
"updated", "zipAll", "zipWithIndex",
"mapConserve", "mapconserve", "filterConserve", "zipWithConserve", "mapWithIndexConserve"
)

private val namesOfInterest = collectionNamesOfInterest ++ List(
"::", "+=", "toString", "newArray", "box", "toCharArray", "termName", "typeName",
"slice", "staticRef", "requiredClass")
private val collectionNamesOfInterest =
"map" :: "flatMap" :: "filter" :: "filterNot" :: "withFilter" :: "collect" :: "flatten" :: "foldLeft" :: "foldRight" :: "take" ::
"reverse" :: "zip" :: "++" :: ":::" :: ":+" :: "distinct" :: "dropRight" :: "takeRight" :: "groupBy" :: "groupMap" :: "init" :: "inits" ::
"interect" :: "mkString" :: "partition" :: "reverse_:::" :: "scanLeft" :: "scanRight" ::
"sortBy" :: "sortWith" :: "sorted" :: "span" :: "splitAt" :: "takeWhile" :: "transpose" :: "unzip" :: "unzip3" ::
"updated" :: "zipAll" :: "zipWithIndex" ::
"mapConserve" :: "mapconserve" :: "filterConserve" :: "zipWithConserve" :: "mapWithIndexConserve" :: Nil

private val namesOfInterest = collectionNamesOfInterest ++ (
"::" :: "+=" :: "toString" :: "newArray" :: "box" :: "toCharArray" :: "termName" :: "typeName" ::
"slice" :: "staticRef" :: "requiredClass" :: Nil)

private var namesToRecord: Set[Name] = _
private var collectionNamesToRecord: Set[Name] = _
Expand Down
12 changes: 6 additions & 6 deletions compiler/src/dotty/tools/dotc/transform/LazyVals.scala
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ class LazyVals extends MiniPhase with IdentityDenotTransformer {
Block(stats.init, stats.last),
unitLiteral
)
DefDef(sym.asTerm, Block(List(init), targetRef.ensureApplied))
DefDef(sym.asTerm, Block(init :: Nil, targetRef.ensureApplied))
}

/** Create thread-unsafe lazy accessor for not-nullable types equivalent to such code
Expand All @@ -249,7 +249,7 @@ class LazyVals extends MiniPhase with IdentityDenotTransformer {
Block(stats.init, stats.last),
unitLiteral
)
DefDef(sym.asTerm, Block(List(init), targetRef.ensureApplied))
DefDef(sym.asTerm, Block(init :: Nil, targetRef.ensureApplied))
}

def transformMemberDefThreadUnsafe(x: ValOrDefDef)(using Context): Thicket = {
Expand Down Expand Up @@ -567,13 +567,13 @@ class LazyVals extends MiniPhase with IdentityDenotTransformer {
CaseDef(
Bind(caseSymbol, ref(caseSymbol)),
EmptyTree,
Block(List(triggerRetry), Throw(ref(caseSymbol)))
Block(triggerRetry :: Nil, Throw(ref(caseSymbol)))
)
}

val initialize = If(
casFlag.appliedTo(thiz, offset, flagRef, computeState, fieldId),
Try(compute, List(retryCase), EmptyTree),
Try(compute, retryCase :: Nil, EmptyTree),
unitLiteral
)

Expand All @@ -587,7 +587,7 @@ class LazyVals extends MiniPhase with IdentityDenotTransformer {
)
)

val loop = WhileDo(EmptyTree, Block(List(flagDef, stateDef), condition))
val loop = WhileDo(EmptyTree, Block(flagDef :: stateDef :: Nil, condition))
DefDef(methodSymbol, loop)
}

Expand Down Expand Up @@ -634,7 +634,7 @@ class LazyVals extends MiniPhase with IdentityDenotTransformer {
flag = ValDef(flagSymbol, Literal(Constant(0L)))
val fieldTree = thizClass.select(lazyNme.RLazyVals.getDeclaredField).appliedTo(Literal(Constant(flagName.toString)))
val offsetTree = ValDef(offsetSymbol.nn, getOffsetStatic.appliedTo(fieldTree))
appendOffsetDefs += (claz -> new OffsetInfo(List(offsetTree), ord))
appendOffsetDefs += (claz -> new OffsetInfo(offsetTree :: Nil, ord))
}

val containerName = LazyLocalName.fresh(x.name.asTermName)
Expand Down
10 changes: 5 additions & 5 deletions compiler/src/dotty/tools/dotc/transform/MacroAnnotations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ class MacroAnnotations:
*/
def expandAnnotations(tree: MemberDef)(using Context): List[DefTree] =
if !hasMacroAnnotation(tree.symbol) then
List(tree)
tree :: Nil
else if tree.symbol.is(Module) && !tree.symbol.isClass then
// only class is transformed
List(tree)
tree :: Nil
else if tree.symbol.isType && !tree.symbol.isClass then
report.error("macro annotations are not supported on type", tree)
List(tree)
tree :: Nil
else
debug.println(i"Expanding macro annotations of:\n$tree")

Expand All @@ -63,7 +63,7 @@ class MacroAnnotations:
case ex: scala.quoted.runtime.StopMacroExpansion =>
if !ctx.reporter.hasErrors then
report.error("Macro expansion was aborted by the macro without any errors reported. Macros should issue errors to end-users when aborting a macro expansion with StopMacroExpansion.", annot.tree)
List(tree)
tree :: Nil
case Interpreter.MissingClassDefinedInCurrentRun(sym) =>
Interpreter.suspendOnMissing(sym, annot.tree)
case NonFatal(ex) =>
Expand All @@ -75,7 +75,7 @@ class MacroAnnotations:
| ${stack.mkString("\n ")}
|"""
report.error(msg, annot.tree)
List(tree)
tree :: Nil
case _ =>
throw ex0
transformedTrees.span(_.symbol != tree.symbol) match
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/transform/Mixin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ class Mixin extends MiniPhase with SymTransformer { thisPhase =>
else if (getter.is(Lazy, butNot = Module))
transformFollowing(superRef(getter).appliedToNone)
else if (getter.is(Module))
New(getter.info.resultType, List(This(cls)))
New(getter.info.resultType, This(cls) :: Nil)
else
Underscore(getter.info.resultType)
// transformFollowing call is needed to make memoize & lazy vals run
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/transform/MixinOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class MixinOps(cls: ClassSymbol, thisPhase: DenotTransformer)(using Context) {
val superCls: Symbol = cls.superClass
val mixins: List[ClassSymbol] = cls.mixins

lazy val JUnit4Annotations: List[Symbol] = List("Test", "Ignore", "Before", "After", "BeforeClass", "AfterClass").
lazy val JUnit4Annotations: List[Symbol] = ("Test" :: "Ignore" :: "Before" :: "After" :: "BeforeClass" :: "AfterClass" :: Nil).
map(n => getClassIfDefined("org.junit." + n)).
filter(_.exists)

Expand Down
7 changes: 3 additions & 4 deletions compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1042,10 +1042,9 @@ object PatternMatcher {
case _ =>
end checkSwitch

val optimizations: List[(String, Plan => Plan)] = List(
"mergeTests" -> mergeTests,
"inlineVars" -> inlineVars
)
val optimizations: List[(String, Plan => Plan)] =
"mergeTests" -> mergeTests ::
"inlineVars" -> inlineVars :: Nil

/** Translate pattern match to sequence of tests. */
def translateMatch(tree: Match): Tree = {
Expand Down
10 changes: 5 additions & 5 deletions compiler/src/dotty/tools/dotc/transform/PickleQuotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ object PickleQuotes {
val typeName = body.tpe.typeSymbol.name
val literalValue =
if lit.const.tag == Constants.NullTag || lit.const.tag == Constants.UnitTag then Nil
else List(body)
else body :: Nil
val constModule = lit.const.tag match
case Constants.BooleanTag => defn. Quotes_reflect_BooleanConstant
case Constants.ByteTag => defn. Quotes_reflect_ByteConstant
Expand Down Expand Up @@ -321,8 +321,8 @@ object PickleQuotes {
else
Lambda(
MethodType(
List(nme.idx, nme.contents, nme.quotes).map(name => UniqueName.fresh(name).toTermName),
List(defn.IntType, defn.SeqType.appliedTo(defn.AnyType), defn.QuotesClass.typeRef),
(nme.idx :: nme.contents :: nme.quotes :: Nil).map(name => UniqueName.fresh(name).toTermName),
defn.IntType :: defn.SeqType.appliedTo(defn.AnyType) :: defn.QuotesClass.typeRef :: Nil,
defn.QuotedExprClass.typeRef.appliedTo(defn.AnyType)),
args =>
val cases = holeContents.zipWithIndex.map { case (splice, idx) =>
Expand Down Expand Up @@ -352,8 +352,8 @@ object PickleQuotes {
if quote.isTypeQuote then defn.QuoteUnpickler_unpickleTypeV2
else defn.QuoteUnpickler_unpickleExprV2
val unpickleArgs =
if quote.isTypeQuote then List(pickledQuoteStrings, types)
else List(pickledQuoteStrings, types, termHoles)
if quote.isTypeQuote then pickledQuoteStrings :: types :: Nil
else pickledQuoteStrings :: types :: termHoles :: Nil
quotes
.asInstance(defn.QuoteUnpicklerClass.typeRef)
.select(unpickleMeth).appliedToType(bodyType)
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/transform/SelectStatic.scala
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class SelectStatic extends MiniPhase with IdentityDenotTransformer {
val tree1 =
if isStaticRef && !tree.qualifier.symbol.isAllOf(JavaModule) && !tree.qualifier.isType then
if isStaticOwnerRef(tree.qualifier) then ref(sym)
else Block(List(tree.qualifier), ref(sym))
else Block(tree.qualifier :: Nil, ref(sym))
else tree

normalize(tree1)
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/transform/Splicing.scala
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ class Splicing extends MacroTransform:
val bindingsTypes = bindings.map(_.termRef.widenTermRefExpr)
val methType = MethodType(bindingsTypes, newTree.tpe)
val meth = newSymbol(spliceOwner, nme.ANON_FUN, Synthetic | Method, methType)
val ddef = DefDef(meth, List(bindings), newTree.tpe, newTree.changeOwner(ctx.owner, meth))
val ddef = DefDef(meth, bindings :: Nil, newTree.tpe, newTree.changeOwner(ctx.owner, meth))
val fnType = defn.FunctionType(bindings.size, isContextual = false).appliedTo(bindingsTypes :+ newTree.tpe)
val closure = Block(ddef :: Nil, Closure(Nil, ref(meth), TypeTree(fnType)))
tpd.Hole(true, holeIdx, refs, closure, tpe)
Expand Down Expand Up @@ -348,7 +348,7 @@ class Splicing extends MacroTransform:
private def spliced(tpe: Type)(body: Context ?=> Tree)(using Context): Tree =
val exprTpe = defn.QuotedExprClass.typeRef.appliedTo(tpe)
val closure =
val methTpe = ContextualMethodType(List(defn.QuotesClass.typeRef), exprTpe)
val methTpe = ContextualMethodType(defn.QuotesClass.typeRef :: Nil, exprTpe)
val meth = newSymbol(ctx.owner, nme.ANON_FUN, Synthetic | Method, methTpe)
Closure(meth, argss => {
withCurrentQuote(argss.head.head) {
Expand Down
Loading