Skip to content

Commit 49b8bac

Browse files
committed
small optimisation
1 parent 1e1eb8c commit 49b8bac

File tree

2 files changed

+12
-22
lines changed

2 files changed

+12
-22
lines changed

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,12 @@ object Types {
4949
implicit def eqType: CanEqual[Type, Type] = CanEqual.derived
5050

5151
object GenericTupleType:
52-
def unapply(tp: Type)(using Context): Option[List[Type]] = tp match
53-
case tp @ AppliedType(r: TypeRef, _) if r.symbol == defn.PairClass && tp.tupleArity(relaxEmptyTuple = true) > 0 =>
54-
Some(tp.tupleElementTypes)
55-
case AppliedType(r: TypeRef, args) if defn.isTupleClass(r.symbol) =>
56-
Some(args)
57-
case _ =>
58-
None
52+
def unapply(tp: AppliedType)(using Context): Option[List[Type]] =
53+
tp.tycon match
54+
case TypeRef(_, cls: ClassSymbol) if tp.tupleArity(relaxEmptyTuple = true) > 0 => // avoid type aliases
55+
Some(tp.tupleElementTypes)
56+
case _ =>
57+
None
5958

6059
/** Main class representing types.
6160
*

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

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -298,13 +298,6 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
298298
n == m
299299
case _ => this.asClass eq that.asClass // class equality otherwise
300300

301-
def isSub(that: MirrorSource)(using Context): Boolean = (this.arity, that.arity) match
302-
case (n, m) if n > 0 || m > 0 =>
303-
// we shortcut when at least one was a tuple.
304-
// This protects us from comparing classes for two TupleXXL with different arities.
305-
n == m
306-
case _ => this.asClass isSubClass that.asClass
307-
308301
def asClass(using Context): Symbol = this match
309302
case ClassSymbol(cls) => cls
310303
case GenericTuple(arity, _) =>
@@ -374,9 +367,11 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
374367
val mirroredClass = msrc.asClass
375368
val accessors = mirroredClass.caseAccessors.filterNot(_.isAllOf(PrivateLocal))
376369
val elemLabels = accessors.map(acc => ConstantType(Constant(acc.name.toString)))
377-
val nestedPairs = msrc match
378-
case MirrorSource.GenericTuple(_, args) => TypeOps.nestedPairs(args)
379-
case _ => TypeOps.nestedPairs(accessors.map(mirroredType.resultType.memberInfo(_).widenExpr))
370+
val (arity, nestedPairs) = msrc match
371+
case MirrorSource.GenericTuple(arity, args) =>
372+
(Some(arity), TypeOps.nestedPairs(args))
373+
case MirrorSource.ClassSymbol(cls) =>
374+
(None, TypeOps.nestedPairs(accessors.map(mirroredType.resultType.memberInfo(_).widenExpr)))
380375
val (monoType, elemsType) = mirroredType match
381376
case mirroredType: HKTypeLambda =>
382377
(mkMirroredMonoType(mirroredType), mirroredType.derivedLambdaType(resType = nestedPairs))
@@ -391,11 +386,7 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
391386
.refinedWith(tpnme.MirroredElemLabels, TypeAlias(elemsLabels))
392387
val mirrorRef =
393388
if mirroredClass.useCompanionAsProductMirror then companionPath(mirroredType, span)
394-
else
395-
val arity = msrc match
396-
case MirrorSource.GenericTuple(arity, _) => Some(arity)
397-
case _ => None
398-
anonymousMirror(monoType, ExtendsProductMirror, arity, span)
389+
else anonymousMirror(monoType, ExtendsProductMirror, arity, span)
399390
withNoErrors(mirrorRef.cast(mirrorType))
400391
end makeProductMirror
401392

0 commit comments

Comments
 (0)