From 971c5becbb8be443409c7711f93edb74185fbeba Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Mon, 18 Mar 2024 08:32:40 +0100 Subject: [PATCH] Avoid eta-reduction of `(..., f: T => R, ...) => f.apply(..)` into `f` Fixes #19962 [Cherry-picked e59335f8d1f6382998d2d74730a33eefce2d7e53] --- compiler/src/dotty/tools/dotc/transform/EtaReduce.scala | 1 + tests/pos/i19962.scala | 4 ++++ 2 files changed, 5 insertions(+) create mode 100644 tests/pos/i19962.scala diff --git a/compiler/src/dotty/tools/dotc/transform/EtaReduce.scala b/compiler/src/dotty/tools/dotc/transform/EtaReduce.scala index a8565d008f46..2b0c49644b09 100644 --- a/compiler/src/dotty/tools/dotc/transform/EtaReduce.scala +++ b/compiler/src/dotty/tools/dotc/transform/EtaReduce.scala @@ -43,6 +43,7 @@ class EtaReduce extends MiniPhase: arg.isInstanceOf[Ident] && arg.symbol == param.symbol) && isPurePath(fn) && fn.tpe <:< tree.tpe + && !(fn.symbol.is(Flags.Param) && fn.symbol.owner == mdef.symbol) // Do not eta-educe `(..., f: T => R, ...) => f.apply(..)` into `f` && defn.isFunctionClass(fn.tpe.widen.typeSymbol) => report.log(i"eta reducing $tree --> $fn") fn diff --git a/tests/pos/i19962.scala b/tests/pos/i19962.scala new file mode 100644 index 000000000000..66e538a04d5f --- /dev/null +++ b/tests/pos/i19962.scala @@ -0,0 +1,4 @@ +def selfie0: (AnyRef => AnyRef) => AnyRef = (f:AnyRef => AnyRef) => f(f) +def selfie1: Any = (f: Any => Any) => f(f) +def selfie2: Any = (f: (Any, Any) => Any, g: (Any, Any) => Any) => f(f, g) +def selfie3: Any = (f: (Any, Any) => Any, g: (Any, Any) => Any) => g(f, g)