-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add missing parents to companions of case classes in stdlib-bootstrapped #18153
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -549,14 +549,19 @@ class PostTyper extends MacroTransform with InfoTransformer { thisPhase => | |
|
||
private def scala2LibPatch(tree: TypeDef)(using Context) = | ||
val sym = tree.symbol | ||
if compilingScala2StdLib | ||
&& sym.is(ModuleClass) && !sym.derivesFrom(defn.SerializableClass) | ||
&& sym.companionClass.derivesFrom(defn.SerializableClass) | ||
then | ||
// Add Serializable to companion objects of serializable classes | ||
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 => | ||
val parents1 = impl.parents :+ TypeTree(defn.SerializableType) | ||
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 | ||
|
@@ -567,10 +572,31 @@ class PostTyper extends MacroTransform with InfoTransformer { thisPhase => | |
|
||
def transformInfo(tp: Type, sym: Symbol)(using Context): Type = tp match | ||
case info: ClassInfo => | ||
if !sym.derivesFrom(defn.SerializableClass) | ||
&& sym.companionClass.derivesFrom(defn.SerializableClass) | ||
then | ||
info.derivedClassInfo(declaredParents = info.parents :+ defn.SerializableType) | ||
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 | ||
|
||
private def argTypeOfCaseClassThatNeedsAbstractFunction1(sym: Symbol)(using Context): Option[List[Type]] = | ||
val companionClass = sym.companionClass | ||
if companionClass.is(CaseClass) | ||
&& !companionClass.primaryConstructor.is(Private) | ||
&& !companionClass.primaryConstructor.info.isVarArgsMethod | ||
then | ||
sym.info.decl(nme.apply).info match | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if it is overloaded? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. None of the cases in the library are overloaded. This would need to be disambiguated if a new |
||
case info: MethodType => | ||
info.paramInfos match | ||
case arg :: Nil => | ||
Some(arg :: info.resultType :: Nil) | ||
case args => None | ||
case _ => None | ||
else | ||
None | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why only those with 1 argument? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(TASTy) MiMa did not find cases with 2 or more arguments in the Scala 2 library.