Skip to content

Fix #2117: bug in typechecking super prefix with invalid enclosing class #2118

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 20, 2017
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
40 changes: 21 additions & 19 deletions compiler/src/dotty/tools/dotc/typer/TypeAssigner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -291,26 +291,28 @@ trait TypeAssigner {

def assignType(tree: untpd.Super, qual: Tree, inConstrCall: Boolean, mixinClass: Symbol = NoSymbol)(implicit ctx: Context) = {
val mix = tree.mix
val qtype @ ThisType(_) = qual.tpe
val cls = qtype.cls

def findMixinSuper(site: Type): Type = site.parents filter (_.name == mix.name) match {
case p :: Nil =>
p
case Nil =>
errorType(SuperQualMustBeParent(mix, cls), tree.pos)
case p :: q :: _ =>
errorType("ambiguous parent class qualifier", tree.pos)
qual.tpe match {
case err: ErrorType => untpd.cpy.Super(tree)(qual, mix).withType(err)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is this necessary, or can I just do tree.withType(err)?
In general, when I should I be using the tree copier?

Copy link
Contributor

Choose a reason for hiding this comment

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

Tree copier provides structural sharing and is used when you think that the tree might stay the same after your changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks. In this case, would it be ok/better to do tree.withType(err)?

case qtype @ ThisType(_) =>
val cls = qtype.cls
def findMixinSuper(site: Type): Type = site.parents filter (_.name == mix.name) match {
case p :: Nil =>
p
case Nil =>
errorType(SuperQualMustBeParent(mix, cls), tree.pos)
case p :: q :: _ =>
errorType("ambiguous parent class qualifier", tree.pos)
}
val owntype =
if (mixinClass.exists) mixinClass.typeRef
else if (!mix.isEmpty) findMixinSuper(cls.info)
else if (inConstrCall || ctx.erasedTypes) cls.info.firstParent
else {
val ps = cls.classInfo.parentsWithArgs
if (ps.isEmpty) defn.AnyType else ps.reduceLeft((x: Type, y: Type) => x & y)
}
tree.withType(SuperType(cls.thisType, owntype))
}
val owntype =
if (mixinClass.exists) mixinClass.typeRef
else if (!mix.isEmpty) findMixinSuper(cls.info)
else if (inConstrCall || ctx.erasedTypes) cls.info.firstParent
else {
val ps = cls.classInfo.parentsWithArgs
if (ps.isEmpty) defn.AnyType else ps.reduceLeft((x: Type, y: Type) => x & y)
}
tree.withType(SuperType(cls.thisType, owntype))
}

def assignType(tree: untpd.Apply, fn: Tree, args: List[Tree])(implicit ctx: Context) = {
Expand Down
3 changes: 3 additions & 0 deletions tests/neg/i2117.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class A {
C.super.foo() // error: C isn't an enclosing class
}