Skip to content

Fix #8573: Fix higher-kinded bounded opaque types #8580

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

Merged
merged 1 commit into from
Mar 22, 2020
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
14 changes: 8 additions & 6 deletions compiler/src/dotty/tools/dotc/core/SymDenotations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -432,11 +432,13 @@ object SymDenotations {
* self type of the enclosing class.
* Otherwise return `info`
*
* @param info Is assumed to be a (lambda-abstracted) right hand side TypeAlias
* of the opaque type definition.
* @param rhs The right hand side tree of the type definition
* @param info Is assumed to be a (lambda-abstracted) right hand side TypeAlias
* of the opaque type definition.
* @param rhs The right hand side tree of the type definition
* @param tparams The type parameters with which the right-hand side bounds should be abstracted
*
*/
def opaqueToBounds(info: Type, rhs: tpd.Tree)(using Context): Type =
def opaqueToBounds(info: Type, rhs: tpd.Tree, tparams: List[TypeParamInfo])(using Context): Type =

def setAlias(tp: Type) =
def recur(self: Type): Unit = self match
Expand All @@ -449,7 +451,7 @@ object SymDenotations {
end setAlias

def bounds(t: tpd.Tree): TypeBounds = t match
case LambdaTypeTree(_, body) =>
case LambdaTypeTree(tparams, body) =>
bounds(body)
case TypeBoundsTree(lo, hi, alias) =>
assert(!alias.isEmpty)
Expand All @@ -460,7 +462,7 @@ object SymDenotations {
info match
case TypeAlias(alias) if isOpaqueAlias && owner.isClass =>
setAlias(alias)
HKTypeLambda.boundsFromParams(alias.typeParams, bounds(rhs))
HKTypeLambda.boundsFromParams(tparams, bounds(rhs))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious, we have to use tparams instead of alias.typeParams due to the following test in boundsFromParams?

      def isOpaqueAlias = params match
        case (param: Symbol) :: _ => param.owner.is(Opaque)
        case _ => false

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alias.typeParams is a list of TypeParamRefs, which are different from the symbols on rhs. That caused the problem.

case _ =>
info
end opaqueToBounds
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,7 @@ class TreeUnpickler(reader: TastyReader,
case _: TypeBounds | _: ClassInfo => checkNonCyclic(sym, rhs.tpe, reportErrors = false)
case _ => rhs.tpe.toBounds
},
rhs)
rhs, rhs.tpe.typeParams)
if sym.isOpaqueAlias then sym.typeRef.recomputeDenot() // make sure we see the new bounds from now on
sym.resetFlag(Provisional)
TypeDef(rhs)
Expand Down
8 changes: 7 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Namer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -997,10 +997,16 @@ class Namer { typer: Typer =>
val rhs1 = typedAheadType(rhs)
val rhsBodyType: TypeBounds = rhs1.tpe.toBounds
val unsafeInfo = if (isDerived) rhsBodyType else abstracted(rhsBodyType)

def opaqueToBounds(info: Type): Type =
if sym.isOpaqueAlias && tparamSyms.isEmpty && info.typeParams.nonEmpty then
ctx.error(em"opaque type alias must be fully applied", rhs.sourcePos)
sym.opaqueToBounds(info, rhs1, tparamSyms)

if (isDerived) sym.info = unsafeInfo
else {
sym.info = NoCompleter
sym.info = sym.opaqueToBounds(checkNonCyclic(sym, unsafeInfo, reportErrors = true), rhs1)
sym.info = opaqueToBounds(checkNonCyclic(sym, unsafeInfo, reportErrors = true))
}
if sym.isOpaqueAlias then sym.typeRef.recomputeDenot() // make sure we see the new bounds from now on
sym.resetFlag(Provisional)
Expand Down
8 changes: 4 additions & 4 deletions tests/neg/i6225.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
object O1 {
object O1 { // error: cannot be instantiated
type A[X] = X
opaque type T = A
opaque type T = A // error: opaque type alias must be fully applied
}

object O2 {
opaque type A[X] = X
object A {
opaque type T = A
object A { // error: cannot be instantiated
opaque type T = A // error: opaque type alias must be fully applied
}
}

Expand Down
3 changes: 3 additions & 0 deletions tests/pos/i8537.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
val f: Int => String = _ => "b"
var g: Int => String = (_) => "b" // workaround
var h: Int => String = _ => "b" // end of statement expected but '=>' found
7 changes: 7 additions & 0 deletions tests/pos/i8573.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
object Example1 {
opaque type Foo[A] <: A = A
}

object Example2 {
opaque type Foo[A] >: A = A
}