Skip to content
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

Fix Mirror.Product for type lambdas #14003

Merged
merged 1 commit into from
Dec 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 7 additions & 0 deletions compiler/src/dotty/tools/dotc/core/TypeOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -829,4 +829,11 @@ object TypeOps:
def nestedPairs(ts: List[Type])(using Context): Type =
ts.foldRight(defn.EmptyTupleModule.termRef: Type)(defn.PairClass.typeRef.appliedTo(_, _))

class StripTypeVarsMap(using Context) extends TypeMap:
def apply(tp: Type) = mapOver(tp).stripTypeVar

/** Apply [[Type.stripTypeVar]] recursively. */
def stripTypeVars(tp: Type)(using Context): Type =
new StripTypeVarsMap().apply(tp)

end TypeOps
17 changes: 4 additions & 13 deletions compiler/src/dotty/tools/dotc/typer/Synthesizer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -249,21 +249,12 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
val cls = mirroredType.classSymbol
val accessors = cls.caseAccessors.filterNot(_.isAllOf(PrivateLocal))
val elemLabels = accessors.map(acc => ConstantType(Constant(acc.name.toString)))
val nestedPairs = TypeOps.nestedPairs(accessors.map(mirroredType.resultType.memberInfo(_).widenExpr))
val (monoType, elemsType) = mirroredType match
case mirroredType: HKTypeLambda =>
def accessorType(acc: Symbol) =
if cls.typeParams.hasSameLengthAs(mirroredType.paramRefs) then
acc.info.subst(cls.typeParams, mirroredType.paramRefs)
else
acc.info
val elems =
mirroredType.derivedLambdaType(
resType = TypeOps.nestedPairs(accessors.map(accessorType))
)
(mkMirroredMonoType(mirroredType), elems)
(mkMirroredMonoType(mirroredType), mirroredType.derivedLambdaType(resType = nestedPairs))
case _ =>
val elems = TypeOps.nestedPairs(accessors.map(mirroredType.memberInfo(_).widenExpr))
(mirroredType, elems)
(mirroredType, nestedPairs)
val elemsLabels = TypeOps.nestedPairs(elemLabels)
checkRefinement(formal, tpnme.MirroredElemTypes, elemsType, span)
checkRefinement(formal, tpnme.MirroredElemLabels, elemsLabels, span)
Expand Down Expand Up @@ -344,7 +335,7 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
(using Context): Tree =
if checkFormal(formal) then
formal.member(tpnme.MirroredType).info match
case TypeBounds(mirroredType, _) => synth(mirroredType.stripTypeVar, formal, span)
case TypeBounds(mirroredType, _) => synth(TypeOps.stripTypeVars(mirroredType), formal, span)
case other => EmptyTree
else EmptyTree

Expand Down
31 changes: 31 additions & 0 deletions tests/pos/i13859.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import scala.deriving.*

object Test:
type Kind1[C, O[_]] = C {
type MirroredType[X] = O[X]
type MirroredMonoType = O[Any]
type MirroredElemTypes[_] <: Tuple
}

type Kind2[C, O[_, _]] = C {
type MirroredType[X, Y] = O[X, Y]
type MirroredMonoType = O[Any, Any]
type MirroredElemTypes[_, _] <: Tuple
}

type Test[X] = (X, Boolean)
type Swap[X, Y] = (Y, X)

locally {
val x = summon[Kind1[Mirror.Product, Test]]
x: Mirror.Product {
type MirroredElemTypes[X] = (X, Boolean)
}
}

locally {
val x = summon[Kind2[Mirror.Product, Swap]]
x: Mirror.Product {
type MirroredElemTypes[X, Y] = (Y, X)
}
}