Skip to content

Fix #5794: eta-expansion triggered properly #5795

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

Merged
merged 1 commit into from
Jan 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions compiler/src/dotty/tools/dotc/core/Definitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1121,15 +1121,19 @@ class Definitions {
/** Is `tp` (an alias) of either a scala.FunctionN or a scala.ImplicitFunctionN
* instance?
*/
def isNonDepFunctionType(tp: Type)(implicit ctx: Context): Boolean = {
def isNonRefinedFunction(tp: Type)(implicit ctx: Context): Boolean = {
val arity = functionArity(tp)
val sym = tp.dealias.typeSymbol
arity >= 0 && isFunctionClass(sym) && tp.isRef(FunctionType(arity, sym.name.isImplicitFunction, sym.name.isErasedFunction).typeSymbol)

arity >= 0 &&
isFunctionClass(sym) &&
tp.isRef(FunctionType(arity, sym.name.isImplicitFunction, sym.name.isErasedFunction).typeSymbol) &&
!tp.isInstanceOf[RefinedType]
}

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

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

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

/** Return underlying immplicit function type (i.e. instance of an ImplicitFunctionN class)
* or NoType if none exists. The following types are considered as underlying types:
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@ class Typer extends Namer
case _ => untpd.TypeTree(tp)
}
pt.stripTypeVar.dealias.followSyntheticOpaque match {
case pt1 if defn.isNonDepFunctionType(pt1) =>
case pt1 if defn.isNonRefinedFunction(pt1) =>
// if expected parameter type(s) are wildcards, approximate from below.
// if expected result type is a wildcard, approximate from above.
// this can type the greatest set of admissible closures.
Expand Down Expand Up @@ -2528,7 +2528,7 @@ class Typer extends Namer
ctx.warning(ex"${tree.symbol} is eta-expanded even though $pt does not have the @FunctionalInterface annotation.", tree.sourcePos)
case _ =>
}
simplify(typed(etaExpand(tree, wtp, arity), pt), pt, locked)
simplify(typed(etaExpand(tree, wtp, arity), pt), pt, locked)
} else if (wtp.paramInfos.isEmpty && isAutoApplied(tree.symbol))
readaptSimplified(tpd.Apply(tree, Nil))
else if (wtp.isImplicitMethod)
Expand Down
5 changes: 5 additions & 0 deletions tests/pos/i5794.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
object Test {
trait Entry { type Key; val key: Key }
def extractKey(e: Entry): e.Key = e.key
val extractor: (e: Entry) => e.Key = extractKey
}