Skip to content

Fix #8874: Strip opaque types when checking self type conformance #9223

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
Jun 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
11 changes: 11 additions & 0 deletions compiler/src/dotty/tools/dotc/transform/SymUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,15 @@ class SymUtils(val self: Symbol) extends AnyVal {
&& self.owner.is(Module)
&& self.owner.linkedClass.is(Case)
&& self.owner.linkedClass.isDeclaredInfix

/** The declared self type of this class, as seen from `site`, stripping
* all refinements for opaque types.
*/
def declaredSelfTypeAsSeenFrom(site: Type)(using Context) =
def (tp: Type).stripOpaques: Type = tp match
case RefinedType(parent, name, _) if self.info.decl(name).symbol.isOpaqueAlias =>
parent.stripOpaques
case _ =>
tp
self.asClass.givenSelfType.stripOpaques.asSeenFrom(site, self)
}
16 changes: 4 additions & 12 deletions compiler/src/dotty/tools/dotc/typer/Checking.scala
Original file line number Diff line number Diff line change
Expand Up @@ -169,27 +169,19 @@ object Checking {
* and that the instance conforms to the self type of the created class.
*/
def checkInstantiable(tp: Type, posd: Positioned)(using Context): Unit =
tp.underlyingClassRef(refinementOK = false) match {
tp.underlyingClassRef(refinementOK = false) match
case tref: TypeRef =>
val cls = tref.symbol
if (cls.isOneOf(AbstractOrTrait))
ctx.error(CantInstantiateAbstractClassOrTrait(cls, isTrait = cls.is(Trait)), posd.sourcePos)
if (!cls.is(Module)) {
def (tp: Type).stripOpaques: Type = tp match
case RefinedType(parent, name, _) if cls.info.decl(name).symbol.isOpaqueAlias =>
parent.stripOpaques
case _ =>
tp
if !cls.is(Module) then
// Create a synthetic singleton type instance, and check whether
// it conforms to the self type of the class as seen from that instance.
val stp = SkolemType(tp)
val selfType =
cls.asClass.givenSelfType.stripOpaques.asSeenFrom(stp, cls)
if (selfType.exists && !(stp <:< selfType))
val selfType = cls.declaredSelfTypeAsSeenFrom(stp)
if selfType.exists && !(stp <:< selfType) then
ctx.error(DoesNotConformToSelfTypeCantBeInstantiated(tp, selfType), posd.sourcePos)
}
case _ =>
}

/** Check that type `tp` is realizable. */
def checkRealizable(tp: Type, posd: Positioned, what: String = "path")(using Context): Unit = {
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/RefChecks.scala
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ object RefChecks {
private def checkParents(cls: Symbol)(using Context): Unit = cls.info match {
case cinfo: ClassInfo =>
def checkSelfConforms(other: ClassSymbol, category: String, relation: String) = {
val otherSelf = other.givenSelfType.asSeenFrom(cls.thisType, other.classSymbol)
if (otherSelf.exists && !(cinfo.selfType <:< otherSelf))
val otherSelf = other.declaredSelfTypeAsSeenFrom(cls.thisType)
if otherSelf.exists && !(cinfo.selfType <:< otherSelf) then
ctx.error(DoesNotConformToSelfType(category, cinfo.selfType, cls, otherSelf, relation, other.classSymbol),
cls.sourcePos)
}
Expand Down
6 changes: 6 additions & 0 deletions tests/pos/i8874.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
trait Abc:
opaque type Log = Double

class AbcClass extends Abc

val v = new AbcClass