Skip to content

Commit c0dcd9b

Browse files
committed
Fix #6190: eta-expand companion object if functions are expected
1 parent 1d84d3b commit c0dcd9b

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
@@ -679,15 +679,6 @@ object desugar {
679679
mods.is(Private) || (!mods.is(Protected) && mods.hasPrivateWithin)
680680
}
681681

682-
val companionParent =
683-
if (constrTparams.nonEmpty ||
684-
constrVparamss.length > 1 ||
685-
mods.is(Abstract) ||
686-
restrictedAccess ||
687-
isEnumCase) anyRef
688-
else
689-
// todo: also use anyRef if constructor has a dependent method type (or rule that out)!
690-
constrVparamss.foldRight(classTypeRef)((vparams, restpe) => Function(vparams map (_.tpt), restpe))
691682
def widenedCreatorExpr =
692683
widenDefs.foldLeft(creatorExpr)((rhs, meth) => Apply(Ident(meth.name), rhs :: Nil))
693684
val applyMeths =
@@ -713,7 +704,7 @@ object desugar {
713704
DefDef(methName, derivedTparams, (unapplyParam :: Nil) :: Nil, TypeTree(), unapplyRHS)
714705
.withMods(synthetic)
715706
}
716-
companionDefs(companionParent, applyMeths ::: unapplyMeth :: companionMembers)
707+
companionDefs(anyRef, applyMeths ::: unapplyMeth :: companionMembers)
717708
}
718709
else if (companionMembers.nonEmpty || companionDerived.nonEmpty || isEnum)
719710
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
@@ -2676,9 +2676,6 @@ class Typer extends Namer
26762676
* Examples for these cases are found in run/implicitFuns.scala and neg/i2006.scala.
26772677
*/
26782678
def adaptNoArgsUnappliedMethod(wtp: MethodType, functionExpected: Boolean, arity: Int): Tree = {
2679-
def isExpandableApply =
2680-
defn.isImplicitFunctionClass(tree.symbol.maybeOwner) && functionExpected
2681-
26822679
/** Is reference to this symbol `f` automatically expanded to `f()`? */
26832680
def isAutoApplied(sym: Symbol): Boolean =
26842681
sym.isConstructor ||
@@ -2695,7 +2692,7 @@ class Typer extends Namer
26952692
!tree.symbol.isConstructor &&
26962693
!tree.symbol.isAllOf(InlineMethod) &&
26972694
!ctx.mode.is(Mode.Pattern) &&
2698-
!(isSyntheticApply(tree) && !isExpandableApply)) {
2695+
!(isSyntheticApply(tree) && !functionExpected)) {
26992696
if (!defn.isFunctionType(pt))
27002697
pt match {
27012698
case SAMType(_) if !pt.classSymbol.hasAnnotation(defn.FunctionalInterfaceAnnot) =>
@@ -2720,10 +2717,11 @@ class Typer extends Namer
27202717
defn.isImplicitFunctionClass(underlying.classSymbol)
27212718
}
27222719

2723-
def adaptNoArgsOther(wtp: Type): Tree = {
2720+
def adaptNoArgsOther(wtp: Type, functionExpected: Boolean): Tree = {
27242721
ctx.typeComparer.GADTused = false
2725-
if (isImplicitFunctionRef(wtp) &&
2726-
!untpd.isContextualClosure(tree) &&
2722+
val implicitFun = isImplicitFunctionRef(wtp) && !untpd.isContextualClosure(tree)
2723+
def caseCompanion = functionExpected && tree.symbol.is(Module) && tree.symbol.companionClass.is(Case)
2724+
if ((implicitFun || caseCompanion) &&
27272725
!isApplyProto(pt) &&
27282726
pt != AssignProto &&
27292727
!ctx.mode.is(Mode.Pattern) &&
@@ -2839,7 +2837,7 @@ class Typer extends Namer
28392837
}
28402838
adaptNoArgsUnappliedMethod(wtp, funExpected, arity)
28412839
case _ =>
2842-
adaptNoArgsOther(wtp)
2840+
adaptNoArgsOther(wtp, functionExpected)
28432841
}
28442842
}
28452843

0 commit comments

Comments
 (0)