Skip to content

Commit 76a0b29

Browse files
authored
Merge pull request #15270 from dwijnand/same-length
2 parents 6508dfa + b199749 commit 76a0b29

File tree

5 files changed

+8
-17
lines changed

5 files changed

+8
-17
lines changed

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

+3
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ object Decorators {
184184
loop(xs, xs, 0)
185185
end mapWithIndexConserve
186186

187+
/** True if two lists have the same length. Since calling length on linear sequences
188+
* is Θ(n), it is an inadvisable way to test length equality. This method is Θ(n min m).
189+
*/
187190
final def hasSameLengthAs[U](ys: List[U]): Boolean = {
188191
@tailrec def loop(xs: List[T], ys: List[U]): Boolean =
189192
if (xs.isEmpty) ys.isEmpty

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ object Denotations {
551551
case _ => NoType
552552
case tp1: PolyType =>
553553
tp2 match
554-
case tp2: PolyType if sameLength(tp1.paramNames, tp2.paramNames) =>
554+
case tp2: PolyType if tp1.paramNames.hasSameLengthAs(tp2.paramNames) =>
555555
val resType = infoMeet(tp1.resType, tp2.resType.subst(tp2, tp1), safeIntersection)
556556
if resType.exists then
557557
tp1.derivedLambdaType(

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1943,7 +1943,7 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
19431943
case tp1: PolyType =>
19441944
tp2.widen match {
19451945
case tp2: PolyType =>
1946-
sameLength(tp1.paramNames, tp2.paramNames) &&
1946+
tp1.paramNames.hasSameLengthAs(tp2.paramNames) &&
19471947
matchesType(tp1.resultType, tp2.resultType.subst(tp2, tp1), relaxed)
19481948
case _ =>
19491949
false

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ trait TypeAssigner {
286286
def assignType(tree: untpd.Apply, fn: Tree, args: List[Tree])(using Context): Apply = {
287287
val ownType = fn.tpe.widen match {
288288
case fntpe: MethodType =>
289-
if (sameLength(fntpe.paramInfos, args) || ctx.phase.prev.relaxedTyping)
289+
if (fntpe.paramInfos.hasSameLengthAs(args) || ctx.phase.prev.relaxedTyping)
290290
safeSubstMethodParams(fntpe, args.tpes)
291291
else
292292
errorType(i"wrong number of arguments at ${ctx.phase.prev} for $fntpe: ${fn.tpe}, expected: ${fntpe.paramInfos.length}, found: ${args.length}", tree.srcPos)
@@ -358,7 +358,7 @@ trait TypeAssigner {
358358
if tp eq pt then pt.newLikeThis(pt.paramNames, pt.paramInfos, pt.resType)
359359
else tp)
360360
val argTypes = args.tpes.mapConserve(ensureFresh)
361-
if (sameLength(argTypes, paramNames)) pt.instantiate(argTypes)
361+
if (argTypes.hasSameLengthAs(paramNames)) pt.instantiate(argTypes)
362362
else wrongNumberOfTypeArgs(fn.tpe, pt.typeParams, args, tree.srcPos)
363363
}
364364
}
@@ -474,7 +474,7 @@ trait TypeAssigner {
474474
assert(!hasNamedArg(args) || ctx.reporter.errorsReported, tree)
475475
val tparams = tycon.tpe.typeParams
476476
val ownType =
477-
if (sameLength(tparams, args))
477+
if (tparams.hasSameLengthAs(args))
478478
if (tycon.symbol == defn.andType) AndType(args(0).tpe, args(1).tpe)
479479
else if (tycon.symbol == defn.orType) OrType(args(0).tpe, args(1).tpe, soft = false)
480480
else tycon.tpe.appliedTo(args.tpes)

compiler/src/dotty/tools/package.scala

-12
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,6 @@ package object tools {
88

99
val ListOfNil: List[Nil.type] = Nil :: Nil
1010

11-
/** True if two lists have the same length. Since calling length on linear sequences
12-
* is O(n), it is an inadvisable way to test length equality.
13-
*/
14-
final def sameLength[T](xs: List[T], ys: List[T]): Boolean = xs match {
15-
case _ :: xs1 =>
16-
ys match {
17-
case _ :: ys1 => sameLength(xs1, ys1)
18-
case _ => false
19-
}
20-
case _ => ys.isEmpty
21-
}
22-
2311
/** Throws an `UnsupportedOperationException` with the given method name. */
2412
def unsupported(methodName: String): Nothing =
2513
throw new UnsupportedOperationException(methodName)

0 commit comments

Comments
 (0)