Skip to content

Refine hasCommonParent criterion in OverridingPairs #11741

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 17, 2021

Conversation

odersky
Copy link
Contributor

@odersky odersky commented Mar 15, 2021

Fixes #11719

hasCommonParent is used to avoid re-visiting overriding pairs that were
already accounted for in a parent. But that only works if the base types
of the classes forming an overriding pair are the same for the parent and
the class itself. Otherwise we might miss an overriding relationship.

Fixes scala#11719

`hasCommonParent` is used to avoid re-visiting overriding pairs that were
already accounted for in a parent. But that only works if the base types
of the classes forming an overriding pair are the same for the parent and
the class itself. Otherwise we might miss an overriding relationship.
@odersky odersky requested a review from smarter March 15, 2021 09:59
@@ -80,6 +81,10 @@ class ElimErasedValueType extends MiniPhase with InfoTransformer { thisPhase =>
private def checkNoClashes(root: Symbol)(using Context) = {
val opc = atPhase(thisPhase) {
new OverridingPairs.Cursor(root) {
override def isSubParent(parent: Symbol, bc: Symbol)(using Context) =
// Need to compute suparents before erasure to not filter out parents
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// Need to compute suparents before erasure to not filter out parents
// Need to compute subparents before erasure to not filter out parents

*/
protected def isSubParent(parent: Symbol, bc: Symbol)(using Context) =
bc.typeParams.isEmpty
|| self.baseType(parent).baseType(bc) == self.baseType(bc)
Copy link
Member

Choose a reason for hiding this comment

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

While this fixes the immediate problem, it's worth noting that Scala 2 completely removed the subparent logic in scala/scala@cc55bd9 because:

subclassing on traits does not imply one's linearisation is contained in the other's

The testcase from that commit is:

trait IO {
  def c(x: Int): Int = ???
}
trait SO extends IO {
  override final def c(x: Int): Int = ???
}
trait SOIO extends IO {
  override def c(x: Int): Int = ???
}
trait SOSO extends SOIO with SO
abstract class AS extends SO
class L extends AS with SOSO // error expected: c definined in SOIO overrides final method c in SO 

Which currently compiles in Dotty, even though we end up overriding a final def with a non-final def. We already have an issue open to keep track of this (#7551) so we don't necessarily need to handle this now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK. We should evaluate the performance implications of subParents before removing them. Also bridge generation currently relies on the check being present, otherwise you would get duplicate bridges.

Copy link
Member

Choose a reason for hiding this comment

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

Also bridge generation currently relies on the check being present, otherwise you would get duplicate bridges.

Indeed, but Scala 2 had the same issue so scala/scala@cc55bd9 also takes care of that:

// Skip nextEntry if the (non-trait) class in `parents` is a subclass of the owners of both low and high.
// this means we'll come back to this entry when we consider `nonTraitParent`, and the linearisation order will be the same
// (this only works for non-trait classes, since subclassing on traits does not imply one's linearisation is contained in the other's)
// This is not just an optimization -- bridge generation relies on visiting each such class only once.
if (!isMatch || (nonTraitParent.isNonBottomSubClass(low.owner) && nonTraitParent.isNonBottomSubClass(high.owner)))

@smarter smarter assigned odersky and unassigned smarter Mar 15, 2021
@odersky odersky merged commit 2075e4c into scala:master Mar 17, 2021
@odersky odersky deleted the fix-11719 branch March 17, 2021 13:15
@Kordyjan Kordyjan added this to the 3.0.0 milestone Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Missing override check leads to ClassCastException
3 participants