Skip to content
Open
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
70 changes: 10 additions & 60 deletions compiler/src/dotty/tools/dotc/transform/PostTyper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,6 @@ class PostTyper extends MacroTransform with InfoTransformer { thisPhase =>

override def changesMembers: Boolean = true // the phase adds super accessors and synthetic members

/**
* Serializable and AbstractFunction1 are added for companion objects of case classes in scala2-library
*/
override def changesParents: Boolean =
if !initContextCalled then
throw new Exception("Calling changesParents before initContext, should call initContext first")
compilingScala2StdLib

override def transformPhase(using Context): Phase = thisPhase.next

def newTransformer(using Context): Transformer =
Expand All @@ -110,28 +102,16 @@ class PostTyper extends MacroTransform with InfoTransformer { thisPhase =>
SafeRefs.checker.traverse(unit.tpdTree)
super.run

/**
* Used to check that `changesParents` is called after `initContext`.
*
* This contract is easy to break and results in subtle bugs.
*/
private var initContextCalled = false

private var compilingScala2StdLib = false
override def initContext(ctx: FreshContext): Unit =
initContextCalled = true
compilingScala2StdLib = Feature.shouldBehaveAsScala2(using ctx)

/** The anonymous function symbols that need an explicified result type
* if their right hand side is also a closure. This is the case if
* the closure's type forms part of the type of a valdef or defdef
* that has a polymorphic closure type.
*/
private val closuresNeedingExplicify = mutable.Set.empty[Symbol]

val superAcc: SuperAccessors = new SuperAccessors(thisPhase)
val synthMbr: SyntheticMembers = new SyntheticMembers(thisPhase)
val beanProps: BeanProperties = new BeanProperties(thisPhase)
private val superAcc: SuperAccessors = new SuperAccessors(thisPhase)
private val synthMbr: SyntheticMembers = new SyntheticMembers(thisPhase)
private val beanProps: BeanProperties = new BeanProperties(thisPhase)

private def newPart(tree: Tree): Option[New] = methPart(tree) match {
case Select(nu: New, _) => Some(nu)
Expand Down Expand Up @@ -808,7 +788,7 @@ class PostTyper extends MacroTransform with InfoTransformer { thisPhase =>
if sym.isOpaqueAlias then
VarianceChecker.checkLambda(rhs, TypeBounds.upper(sym.opaqueAlias))
case _ =>
processMemberDef(super.transform(scala2LibPatch(tree)))
processMemberDef(super.transform(tree))
case tree: Bind =>
val sym = tree.symbol
if sym.isType && !sym.name.is(WildcardParamName) then
Expand Down Expand Up @@ -899,45 +879,15 @@ class PostTyper extends MacroTransform with InfoTransformer { thisPhase =>
private def annotateExperimentalCompanion(sym: Symbol)(using Context): Unit =
if sym.is(Module) then
ExperimentalAnnotation.copy(sym.companionClass).foreach(sym.addAnnotation)

// It needs to run at the phase of the postTyper --- otherwise, the test of the symbols will use
// the transformed denotation with added `Serializable` and `AbstractFunction1`.
private def scala2LibPatch(tree: TypeDef)(using Context) = atPhase(thisPhase):
val sym = tree.symbol
if compilingScala2StdLib && sym.is(ModuleClass) then
// Add Serializable to companion objects of serializable classes,
// and add AbstractFunction1 to companion objects of case classes with 1 parameter.
tree.rhs match
case impl: Template =>
var parents1 = impl.parents
val companionClass = sym.companionClass
if !sym.derivesFrom(defn.SerializableClass) && companionClass.derivesFrom(defn.SerializableClass) then
parents1 = parents1 :+ TypeTree(defn.SerializableType)
argTypeOfCaseClassThatNeedsAbstractFunction1(sym) match
case Some(args) if parents1.head.symbol.owner == defn.ObjectClass =>
parents1 = New(defn.AbstractFunctionClass(1).typeRef).select(nme.CONSTRUCTOR).appliedToTypes(args).ensureApplied :: parents1.tail
case _ =>
val impl1 = cpy.Template(impl)(parents = parents1)
cpy.TypeDef(tree)(rhs = impl1)
else tree
}

protected override def infoMayChange(sym: Symbol)(using Context): Boolean =
compilingScala2StdLib && sym.isAllOf(ModuleClass, butNot = Package)

def transformInfo(tp: Type, sym: Symbol)(using Context): Type = tp match
case info: ClassInfo =>
var parents1 = info.parents
val companionClass = sym.companionClass
if !sym.derivesFrom(defn.SerializableClass) && companionClass.derivesFrom(defn.SerializableClass) then
parents1 = parents1 :+ defn.SerializableType
argTypeOfCaseClassThatNeedsAbstractFunction1(sym) match
case Some(args) if parents1.head.typeSymbol == defn.ObjectClass =>
parents1 = defn.AbstractFunctionClass(1).typeRef.appliedTo(args) :: parents1.tail
case _ =>
if parents1 ne info.parents then info.derivedClassInfo(declaredParents = parents1)
else tp
case _ => tp
// This is a lie, superAcc and synthMbr make changes, but if this is `true` the compiler crashes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

[error] ## Exception when compiling 92 sources to /Users/solal/scala3/scaladoc/target/scala-3.11.0-RC1-bin-SNAPSHOT-nonbootstrapped/classes
[error] dotty.tools.dotc.core.UnpicklingError: Could not read definition method apply in /Users/solal/scala3/library/target/scala-library-bootstrapped/classes/scala/runtime/AbstractPartialFunction.tasty. Caused by the following exception:
[error] java.lang.AssertionError: assertion failed: Cyclic reference while unpickling definition at address 181 in unit <no file>

this does not sound fun to debug, and anyway compilingScala2StdLib was a constant false before so this isn't changing that

false

def transformInfo(tp: Type, sym: Symbol)(using Context): Type =
// The actual change is done by superAcc and synthMbr
tp

private def argTypeOfCaseClassThatNeedsAbstractFunction1(sym: Symbol)(using Context): Option[List[Type]] =
val companionClass = sym.companionClass
Expand Down
Loading