Skip to content

Commit 3ff2a75

Browse files
committed
Fix scala#6190: eta-expand companion object if functions are expected
1 parent fa3c006 commit 3ff2a75

File tree

2 files changed

+7
-18
lines changed

2 files changed

+7
-18
lines changed

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

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -680,15 +680,6 @@ object desugar {
680680
mods.is(Private) || (!mods.is(Protected) && mods.hasPrivateWithin)
681681
}
682682

683-
val companionParent =
684-
if (constrTparams.nonEmpty ||
685-
constrVparamss.length > 1 ||
686-
mods.is(Abstract) ||
687-
restrictedAccess ||
688-
isEnumCase) anyRef
689-
else
690-
// todo: also use anyRef if constructor has a dependent method type (or rule that out)!
691-
constrVparamss.foldRight(classTypeRef)((vparams, restpe) => Function(vparams map (_.tpt), restpe))
692683
def widenedCreatorExpr =
693684
widenDefs.foldLeft(creatorExpr)((rhs, meth) => Apply(Ident(meth.name), rhs :: Nil))
694685
val applyMeths =
@@ -714,7 +705,7 @@ object desugar {
714705
DefDef(methName, derivedTparams, (unapplyParam :: Nil) :: Nil, TypeTree(), unapplyRHS)
715706
.withMods(synthetic)
716707
}
717-
companionDefs(companionParent, applyMeths ::: unapplyMeth :: companionMembers)
708+
companionDefs(anyRef, applyMeths ::: unapplyMeth :: companionMembers)
718709
}
719710
else if (companionMembers.nonEmpty || companionDerived.nonEmpty || isEnum)
720711
companionDefs(anyRef, companionMembers)

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2680,9 +2680,6 @@ class Typer extends Namer
26802680
* Examples for these cases are found in run/implicitFuns.scala and neg/i2006.scala.
26812681
*/
26822682
def adaptNoArgsUnappliedMethod(wtp: MethodType, functionExpected: Boolean, arity: Int): Tree = {
2683-
def isExpandableApply =
2684-
defn.isImplicitFunctionClass(tree.symbol.maybeOwner) && functionExpected
2685-
26862683
/** Is reference to this symbol `f` automatically expanded to `f()`? */
26872684
def isAutoApplied(sym: Symbol): Boolean =
26882685
sym.isConstructor ||
@@ -2699,7 +2696,7 @@ class Typer extends Namer
26992696
!tree.symbol.isConstructor &&
27002697
!tree.symbol.isAllOf(InlineMethod) &&
27012698
!ctx.mode.is(Mode.Pattern) &&
2702-
!(isSyntheticApply(tree) && !isExpandableApply)) {
2699+
!(isSyntheticApply(tree) && !functionExpected)) {
27032700
if (!defn.isFunctionType(pt))
27042701
pt match {
27052702
case SAMType(_) if !pt.classSymbol.hasAnnotation(defn.FunctionalInterfaceAnnot) =>
@@ -2724,10 +2721,11 @@ class Typer extends Namer
27242721
defn.isImplicitFunctionClass(underlying.classSymbol)
27252722
}
27262723

2727-
def adaptNoArgsOther(wtp: Type): Tree = {
2724+
def adaptNoArgsOther(wtp: Type, functionExpected: Boolean): Tree = {
27282725
ctx.typeComparer.GADTused = false
2729-
if (isImplicitFunctionRef(wtp) &&
2730-
!untpd.isContextualClosure(tree) &&
2726+
val implicitFun = isImplicitFunctionRef(wtp) && !untpd.isContextualClosure(tree)
2727+
def caseCompanion = functionExpected && tree.symbol.is(Module) && tree.symbol.companionClass.is(Case)
2728+
if ((implicitFun || caseCompanion) &&
27312729
!isApplyProto(pt) &&
27322730
pt != AssignProto &&
27332731
!ctx.mode.is(Mode.Pattern) &&
@@ -2843,7 +2841,7 @@ class Typer extends Namer
28432841
}
28442842
adaptNoArgsUnappliedMethod(wtp, funExpected, arity)
28452843
case _ =>
2846-
adaptNoArgsOther(wtp)
2844+
adaptNoArgsOther(wtp, functionExpected)
28472845
}
28482846
}
28492847

0 commit comments

Comments
 (0)