Skip to content

Commit

Permalink
Allow eta-expansion of inline defs (#18249)
Browse files Browse the repository at this point in the history
The fact that this wasn't allowed before seems to be a bug and not an
intentional restriction: the check was originally introduced in
a019a4e for "typelevel methods" which
are only vaguely related to today's inline methods.
  • Loading branch information
nicolasstucki committed Jul 19, 2023
2 parents e1e8549 + a7bae7a commit b21241b
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 4 deletions.
2 changes: 0 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3972,12 +3972,10 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer

// Reasons NOT to eta expand:
// - we reference a constructor
// - we reference a typelevel method
// - we are in a pattern
// - the current tree is a synthetic apply which is not expandable (eta-expasion would simply undo that)
if arity >= 0
&& !tree.symbol.isConstructor
&& !tree.symbol.isAllOf(InlineMethod)
&& !ctx.mode.is(Mode.Pattern)
&& !(isSyntheticApply(tree) && !functionExpected)
then
Expand Down
2 changes: 1 addition & 1 deletion tests/neg/i12207.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ extension [T](t: T) inline def pi[P <: Tuple](using P): T = ???
inline def env[P <: Tuple, T](op: P ?=> T): P ?=> T = op

@main def Test =
env { pi[String] } // error // error
env { pi[String] } // error
2 changes: 1 addition & 1 deletion tests/neg/i7459.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ object Foo {
inline def summon[T](x: T): T = x match {
case t: T => t
}
println(summon) // error
println(summon)
}

import scala.deriving.*
Expand Down
9 changes: 9 additions & 0 deletions tests/pos/inline-eta.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Foo(x: Int)

object A:
inline def bar(x: Int): Int = x
val g1 = bar
val g2: Int => Int = bar

def foo(xs: List[Int]) =
xs.map(Foo.apply) // use the `inline def apply` constructor proxy

0 comments on commit b21241b

Please sign in to comment.