Skip to content

Commit a4599a5

Browse files
committed
Fix scala#6190: eta-expand companion object if functions are expected
1 parent 121ced4 commit a4599a5

File tree

2 files changed

+15
-31
lines changed

2 files changed

+15
-31
lines changed

compiler/src/dotty/tools/dotc/ast/Desugar.scala

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -676,28 +676,6 @@ object desugar {
676676
mods.is(Private) || (!mods.is(Protected) && mods.hasPrivateWithin)
677677
}
678678

679-
/** Does one of the parameter's types (in the first param clause)
680-
* mention a preceding parameter?
681-
*/
682-
def isParamDependent = constrVparamss match
683-
case vparams :: _ =>
684-
val paramNames = vparams.map(_.name).toSet
685-
vparams.exists(_.tpt.existsSubTree {
686-
case Ident(name: TermName) => paramNames.contains(name)
687-
case _ => false
688-
})
689-
case _ => false
690-
691-
val companionParent =
692-
if constrTparams.nonEmpty
693-
|| constrVparamss.length > 1
694-
|| mods.is(Abstract)
695-
|| restrictedAccess
696-
|| isParamDependent
697-
|| isEnumCase
698-
then anyRef
699-
else
700-
constrVparamss.foldRight(classTypeRef)((vparams, restpe) => Function(vparams map (_.tpt), restpe))
701679
val applyMeths =
702680
if (mods.is(Abstract)) Nil
703681
else {
@@ -727,7 +705,7 @@ object desugar {
727705
val toStringMeth =
728706
DefDef(nme.toString_, Nil, Nil, TypeTree(), Literal(Constant(className.toString))).withMods(Modifiers(Override | Synthetic))
729707

730-
companionDefs(companionParent, applyMeths ::: unapplyMeth :: toStringMeth :: companionMembers)
708+
companionDefs(anyRef, applyMeths ::: unapplyMeth :: toStringMeth :: companionMembers)
731709
}
732710
else if (companionMembers.nonEmpty || companionDerived.nonEmpty || isEnum)
733711
companionDefs(anyRef, companionMembers)

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3222,9 +3222,6 @@ class Typer extends Namer
32223222
* Examples for these cases are found in run/implicitFuns.scala and neg/i2006.scala.
32233223
*/
32243224
def adaptNoArgsUnappliedMethod(wtp: MethodType, functionExpected: Boolean, arity: Int): Tree = {
3225-
def isExpandableApply =
3226-
defn.isContextFunctionClass(tree.symbol.maybeOwner) && functionExpected
3227-
32283225
/** Is reference to this symbol `f` automatically expanded to `f()`? */
32293226
def isAutoApplied(sym: Symbol): Boolean =
32303227
sym.isConstructor
@@ -3241,7 +3238,7 @@ class Typer extends Namer
32413238
!tree.symbol.isConstructor &&
32423239
!tree.symbol.isAllOf(InlineMethod) &&
32433240
!ctx.mode.is(Mode.Pattern) &&
3244-
!(isSyntheticApply(tree) && !isExpandableApply)) {
3241+
!(isSyntheticApply(tree) && !functionExpected)) {
32453242
if (!defn.isFunctionType(pt))
32463243
pt match {
32473244
case SAMType(_) if !pt.classSymbol.hasAnnotation(defn.FunctionalInterfaceAnnot) =>
@@ -3266,9 +3263,18 @@ class Typer extends Namer
32663263
defn.isContextFunctionClass(underlying.classSymbol)
32673264
}
32683265

3269-
def adaptNoArgsOther(wtp: Type): Tree = {
3270-
if (isContextFunctionRef(wtp) &&
3271-
!untpd.isContextualClosure(tree) &&
3266+
def adaptNoArgsOther(wtp: Type, functionExpected: Boolean): Tree = {
3267+
val implicitFun = isContextFunctionRef(wtp) && !untpd.isContextualClosure(tree)
3268+
def caseCompanion =
3269+
functionExpected &&
3270+
tree.symbol.is(Module) &&
3271+
tree.symbol.companionClass.is(Case) &&
3272+
!tree.tpe.widen.classSymbol.asClass.classParents.exists(defn.isFunctionType(_)) && {
3273+
report.warning("The method `apply` is inserted. The auto insertion will be deprecated, please write `" + tree.show + ".apply` explicitly.", tree.sourcePos)
3274+
true
3275+
}
3276+
3277+
if ((implicitFun || caseCompanion) &&
32723278
!isApplyProto(pt) &&
32733279
pt != AssignProto &&
32743280
!ctx.mode.is(Mode.Pattern) &&
@@ -3390,7 +3396,7 @@ class Typer extends Namer
33903396
}
33913397
adaptNoArgsUnappliedMethod(wtp, funExpected, arity)
33923398
case _ =>
3393-
adaptNoArgsOther(wtp)
3399+
adaptNoArgsOther(wtp, functionExpected)
33943400
}
33953401
}
33963402

0 commit comments

Comments
 (0)