Skip to content

Commit

Permalink
Admit parametric aliases of classes in parent typing
Browse files Browse the repository at this point in the history
When typing parent types as constructors with implicitly added `()` arguments,
also admit alias types that become classes after eta-collapsing.

Fixes #18623
  • Loading branch information
odersky committed Nov 5, 2023
1 parent ef97ee2 commit 257a10c
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 14 deletions.
15 changes: 10 additions & 5 deletions compiler/src/dotty/tools/dotc/core/TypeApplications.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ object TypeApplications {

def apply(tycon: Type)(using Context): Type =
assert(tycon.typeParams.nonEmpty, tycon)
tycon.EtaExpand(tycon.typeParamSymbols)
tycon.etaExpand(tycon.typeParamSymbols)

/** Test that the parameter bounds in a hk type lambda `[X1,...,Xn] => C[X1, ..., Xn]`
* contain the bounds of the type parameters of `C`. This is necessary to be able to
Expand Down Expand Up @@ -71,7 +71,7 @@ object TypeApplications {
*/
def EtaExpandIfHK(tparams: List[TypeParamInfo], args: List[Type])(using Context): List[Type] =
if (tparams.isEmpty) args
else args.zipWithConserve(tparams)((arg, tparam) => arg.EtaExpandIfHK(tparam.paramInfoOrCompleter))
else args.zipWithConserve(tparams)((arg, tparam) => arg.etaExpandIfHK(tparam.paramInfoOrCompleter))

/** A type map that tries to reduce (part of) the result type of the type lambda `tycon`
* with the given `args`(some of which are wildcard arguments represented by type bounds).
Expand Down Expand Up @@ -245,7 +245,7 @@ class TypeApplications(val self: Type) extends AnyVal {
def topType(using Context): Type =
if self.hasSimpleKind then
defn.AnyType
else EtaExpand(self.typeParams) match
else etaExpand(self.typeParams) match
case tp: HKTypeLambda =>
tp.derivedLambdaType(resType = tp.resultType.topType)
case _ =>
Expand Down Expand Up @@ -302,7 +302,7 @@ class TypeApplications(val self: Type) extends AnyVal {
/** Convert a type constructor `TC` which has type parameters `X1, ..., Xn`
* to `[X1, ..., Xn] -> TC[X1, ..., Xn]`.
*/
def EtaExpand(tparams: List[TypeParamInfo])(using Context): Type =
def etaExpand(tparams: List[TypeParamInfo])(using Context): Type =
HKTypeLambda.fromParams(tparams, self.appliedTo(tparams.map(_.paramRef)))
//.ensuring(res => res.EtaReduce =:= self, s"res = $res, core = ${res.EtaReduce}, self = $self, hc = ${res.hashCode}")

Expand All @@ -311,7 +311,7 @@ class TypeApplications(val self: Type) extends AnyVal {
if (isLambdaSub) self else EtaExpansion(self)

/** Eta expand if `self` is a (non-lambda) class reference and `bound` is a higher-kinded type */
def EtaExpandIfHK(bound: Type)(using Context): Type = {
def etaExpandIfHK(bound: Type)(using Context): Type = {
val hkParams = bound.hkTypeParams
if (hkParams.isEmpty) self
else self match {
Expand All @@ -321,6 +321,11 @@ class TypeApplications(val self: Type) extends AnyVal {
}
}

/** Maps [Ts] => C[Ts] to C */
def etaCollapse(using Context): Type = self match
case EtaExpansion(classType) => classType
case _ => self

/** The type representing
*
* T[U1, ..., Un]
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/core/TypeComparer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,7 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
case _ =>
val tparams1 = tp1.typeParams
if (tparams1.nonEmpty)
return recur(tp1.EtaExpand(tparams1), tp2) || fourthTry
return recur(tp1.etaExpand(tparams1), tp2) || fourthTry
tp2 match {
case EtaExpansion(tycon2: TypeRef) if tycon2.symbol.isClass && tycon2.symbol.is(JavaDefined) =>
recur(tp1, tycon2) || fourthTry
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,7 @@ class Scala2Unpickler(bytes: Array[Byte], classRoot: ClassDenotation, moduleClas
}
else if args.nonEmpty then
tycon.safeAppliedTo(EtaExpandIfHK(sym.typeParams, args.map(translateTempPoly)))
else if (sym.typeParams.nonEmpty) tycon.EtaExpand(sym.typeParams)
else if (sym.typeParams.nonEmpty) tycon.etaExpand(sym.typeParams)
else tycon
case TYPEBOUNDStpe =>
val lo = readTypeRef()
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/Deriving.scala
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ trait Deriving {
// case (a) ... see description above
val derivedParams = clsParams.dropRight(instanceArity)
val instanceType =
if (instanceArity == clsArity) clsType.EtaExpand(clsParams)
if (instanceArity == clsArity) clsType.etaExpand(clsParams)
else {
val derivedParamTypes = derivedParams.map(_.typeRef)

Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/Namer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1197,7 +1197,7 @@ class Namer { typer: Typer =>
val forwarderName = checkNoConflict(alias.toTypeName, isPrivate = false, span)
var target = pathType.select(sym)
if target.typeParams.nonEmpty then
target = target.EtaExpand(target.typeParams)
target = target.etaExpand(target.typeParams)
newSymbol(
cls, forwarderName,
Exported | Final,
Expand Down Expand Up @@ -1518,7 +1518,7 @@ class Namer { typer: Typer =>

def typedParentType(tree: untpd.Tree): tpd.Tree =
val parentTpt = typer.typedType(parent, AnyTypeConstructorProto)
val ptpe = parentTpt.tpe
val ptpe = parentTpt.tpe.dealias.etaCollapse
if ptpe.typeParams.nonEmpty
&& ptpe.underlyingClassRef(refinementOK = false).exists
then
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/RefChecks.scala
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ object RefChecks {
*/
def checkOverride(checkSubType: (Type, Type) => Context ?=> Boolean, member: Symbol, other: Symbol): Unit =
def memberTp(self: Type) =
if (member.isClass) TypeAlias(member.typeRef.EtaExpand(member.typeParams))
if (member.isClass) TypeAlias(member.typeRef.etaExpand(member.typeParams))
else self.memberInfo(member)
def otherTp(self: Type) =
self.memberInfo(other)
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4306,7 +4306,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
AppliedType(tree.tpe, tp.typeParams.map(Function.const(TypeBounds.empty)))
else
// Eta-expand higher-kinded type
tree.tpe.EtaExpand(tp.typeParamSymbols)
tree.tpe.etaExpand(tp.typeParamSymbols)
tree.withType(tp1)
}
if (ctx.mode.is(Mode.Pattern) || ctx.mode.isQuotedPattern || tree1.tpe <:< pt) tree1
Expand Down
4 changes: 2 additions & 2 deletions tests/neg/i4557.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ object O {
type S0[X, Y] = C1[X, Y]
type S1 = C1[Int] // error

class D0 extends T0 // error
class D0 extends T0 // was error, now ok
class D1 extends T0[Int]
class D2 extends T0[String, Int] // error

class E0 extends S0 // error
class E0 extends S0 // was error, now ok
class E1 extends S0[Int] // error
class E2 extends S0[String, Int]
}
15 changes: 15 additions & 0 deletions tests/pos/i18623.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
final abstract class ForcedRecompilationToken[T]
object ForcedRecompilationToken {
implicit def default: ForcedRecompilationToken["abc"] = null
}

class GoodNoParens[T](implicit ev: ForcedRecompilationToken[T])
type BadNoParens[T] = GoodNoParens[T]

// error
object A extends BadNoParens

// ok
object B extends BadNoParens()
object C extends GoodNoParens

20 changes: 20 additions & 0 deletions tests/pos/i18623a.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
final abstract class ForcedRecompilationToken[T]
object ForcedRecompilationToken {
implicit def default: ForcedRecompilationToken["abc"] = null
}

object x {
class GoodNoParens[T](implicit ev: ForcedRecompilationToken[T])
}
export x.GoodNoParens as BadNoParens

// error
object A extends BadNoParens

// ok
object B extends BadNoParens()
object C extends x.GoodNoParens

object App extends App {
println("compiled")
}

0 comments on commit 257a10c

Please sign in to comment.