Skip to content

Commit d8555fd

Browse files
committed
Add nestedExists extension method
Add nestedExists extension method for List[List[T]] exists and optimize it as well as nestedMap.
1 parent 880d304 commit d8555fd

File tree

6 files changed

+12
-8
lines changed

6 files changed

+12
-8
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -596,9 +596,9 @@ object desugar {
596596
(ordinalMethLit(ordinal) :: enumLabelLit(className.toString) :: Nil, scaffolding)
597597
else (Nil, Nil)
598598
def copyMeths = {
599-
val hasRepeatedParam = constrVparamss.exists(_.exists {
599+
val hasRepeatedParam = constrVparamss.nestedExists {
600600
case ValDef(_, tpt, _) => isRepeated(tpt)
601-
})
601+
}
602602
if (mods.is(Abstract) || hasRepeatedParam) Nil // cannot have default arguments for repeated parameters, hence copy method is not issued
603603
else {
604604
def copyDefault(vparam: ValDef) =

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ object DesugarEnums {
297297
case parent => parent.isType && typeHasRef(parent)
298298
}
299299

300-
vparamss.exists(_.exists(valDefHasRef)) || parents.exists(parentHasRef)
300+
vparamss.nestedExists(valDefHasRef) || parents.exists(parentHasRef)
301301
}
302302

303303
/** A pair consisting of

compiler/src/dotty/tools/dotc/core/Decorators.scala

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,16 @@ object Decorators {
195195
}
196196

197197
extension [T, U](xss: List[List[T]])
198-
def nestedMap(f: T => U): List[List[U]] =
199-
xss.map(_.map(f))
198+
def nestedMap(f: T => U): List[List[U]] = xss match
199+
case xs :: xss1 => xs.map(f) :: xss1.nestedMap(f)
200+
case nil => Nil
200201
def nestedMapConserve(f: T => U): List[List[U]] =
201202
xss.mapconserve(_.mapconserve(f))
202203
def nestedZipWithConserve(yss: List[List[U]])(f: (T, U) => T): List[List[T]] =
203204
xss.zipWithConserve(yss)((xs, ys) => xs.zipWithConserve(ys)(f))
205+
def nestedExists(p: T => Boolean): Boolean = xss match
206+
case xs :: xss1 => xs.exists(p) || xss1.nestedExists(p)
207+
case nil => false
204208
end extension
205209

206210
extension (text: Text)

compiler/src/dotty/tools/dotc/core/SymDenotations.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -899,7 +899,7 @@ object SymDenotations {
899899
else if is(NoDefaultParams) then false
900900
else
901901
val result =
902-
rawParamss.exists(_.exists(_.is(HasDefault)))
902+
rawParamss.nestedExists(_.is(HasDefault))
903903
|| allOverriddenSymbols.exists(_.hasDefaultParams)
904904
setFlag(if result then HasDefaultParams else NoDefaultParams)
905905
result

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3856,7 +3856,7 @@ object Parsers {
38563856
val problem = tree match
38573857
case tree: MemberDef if !(tree.mods.flags & ModifierFlags).isEmpty =>
38583858
i"refinement cannot be ${(tree.mods.flags & ModifierFlags).flagStrings().mkString("`", "`, `", "`")}"
3859-
case tree: DefDef if tree.vparamss.exists(_.exists(!_.rhs.isEmpty)) =>
3859+
case tree: DefDef if tree.vparamss.nestedExists(!_.rhs.isEmpty) =>
38603860
i"refinement cannot have default arguments"
38613861
case tree: ValOrDefDef =>
38623862
if tree.rhs.isEmpty then ""

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ class ElimRepeated extends MiniPhase with InfoTransformer { thisPhase =>
237237

238238
/** Is there a repeated parameter in some parameter list? */
239239
private def hasRepeatedParams(sym: Symbol)(using Context): Boolean =
240-
sym.info.paramInfoss.flatten.exists(_.isRepeatedParam)
240+
sym.info.paramInfoss.nestedExists(_.isRepeatedParam)
241241

242242
/** Is this the type of a method that has a repeated parameter type as
243243
* its last parameter in the last parameter list?

0 commit comments

Comments
 (0)