Skip to content

Commit afe1e88

Browse files
authored
Merge pull request #5795 from dotty-staging/fix-#5794
Fix #5794: eta-expansion triggered properly
2 parents 3c62b96 + ec0768e commit afe1e88

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

compiler/src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,15 +1121,19 @@ class Definitions {
11211121
/** Is `tp` (an alias) of either a scala.FunctionN or a scala.ImplicitFunctionN
11221122
* instance?
11231123
*/
1124-
def isNonDepFunctionType(tp: Type)(implicit ctx: Context): Boolean = {
1124+
def isNonRefinedFunction(tp: Type)(implicit ctx: Context): Boolean = {
11251125
val arity = functionArity(tp)
11261126
val sym = tp.dealias.typeSymbol
1127-
arity >= 0 && isFunctionClass(sym) && tp.isRef(FunctionType(arity, sym.name.isImplicitFunction, sym.name.isErasedFunction).typeSymbol)
1127+
1128+
arity >= 0 &&
1129+
isFunctionClass(sym) &&
1130+
tp.isRef(FunctionType(arity, sym.name.isImplicitFunction, sym.name.isErasedFunction).typeSymbol) &&
1131+
!tp.isInstanceOf[RefinedType]
11281132
}
11291133

11301134
/** Is `tp` a representation of a (possibly depenent) function type or an alias of such? */
11311135
def isFunctionType(tp: Type)(implicit ctx: Context): Boolean =
1132-
isNonDepFunctionType(tp.dropDependentRefinement)
1136+
isNonRefinedFunction(tp.dropDependentRefinement)
11331137

11341138
// Specialized type parameters defined for scala.Function{0,1,2}.
11351139
lazy val Function1SpecializedParamTypes: collection.Set[TypeRef] =
@@ -1169,7 +1173,7 @@ class Definitions {
11691173
false
11701174
})
11711175

1172-
def functionArity(tp: Type)(implicit ctx: Context): Int = tp.dealias.argInfos.length - 1
1176+
def functionArity(tp: Type)(implicit ctx: Context): Int = tp.dropDependentRefinement.dealias.argInfos.length - 1
11731177

11741178
/** Return underlying immplicit function type (i.e. instance of an ImplicitFunctionN class)
11751179
* or NoType if none exists. The following types are considered as underlying types:

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,7 @@ class Typer extends Namer
756756
case _ => untpd.TypeTree(tp)
757757
}
758758
pt.stripTypeVar.dealias.followSyntheticOpaque match {
759-
case pt1 if defn.isNonDepFunctionType(pt1) =>
759+
case pt1 if defn.isNonRefinedFunction(pt1) =>
760760
// if expected parameter type(s) are wildcards, approximate from below.
761761
// if expected result type is a wildcard, approximate from above.
762762
// this can type the greatest set of admissible closures.
@@ -2528,7 +2528,7 @@ class Typer extends Namer
25282528
ctx.warning(ex"${tree.symbol} is eta-expanded even though $pt does not have the @FunctionalInterface annotation.", tree.sourcePos)
25292529
case _ =>
25302530
}
2531-
simplify(typed(etaExpand(tree, wtp, arity), pt), pt, locked)
2531+
simplify(typed(etaExpand(tree, wtp, arity), pt), pt, locked)
25322532
} else if (wtp.paramInfos.isEmpty && isAutoApplied(tree.symbol))
25332533
readaptSimplified(tpd.Apply(tree, Nil))
25342534
else if (wtp.isImplicitMethod)

tests/pos/i5794.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
object Test {
2+
trait Entry { type Key; val key: Key }
3+
def extractKey(e: Entry): e.Key = e.key
4+
val extractor: (e: Entry) => e.Key = extractKey
5+
}

0 commit comments

Comments
 (0)