Skip to content
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

inline method stops inlining function when called naming the parameter #9466

Closed
rcano opened this issue Jul 29, 2020 · 3 comments · Fixed by #9469
Closed

inline method stops inlining function when called naming the parameter #9466

rcano opened this issue Jul 29, 2020 · 3 comments · Fixed by #9469

Comments

@rcano
Copy link

rcano commented Jul 29, 2020

Minimized code

object inlinefail:
  inline def i(inline f: Int => Boolean): String =
    if f(34) then "a"
    else "b"

  def test: String = i(f = _ == 34)

Output

disassemblied (via CFR) test method:

    public String test() {
        return BoxesRunTime.unboxToBoolean((Object)((JFunction1.mcZI.sp & Serializable)(JFunction1.mcZI.sp & Serializable)_$1 -> _$1 == 34).apply((Object)BoxesRunTime.boxToInteger((int)34))) ? "a" : "b";
    }

Expectation

Bytecode should be the same as for the code

  def test: String = i(_ == 34) //not naming the parameter

which yields

    public String test() {
        int _$1 = 34;
        return _$1 == 34 ? "a" : "b";
    }
@bishabosha
Copy link
Member

i would have thought this could be constant folded to "a"

@nicolasstucki
Copy link
Contributor

nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Jul 30, 2020
Redefine `quoted.Expr.betaRduce` to not rely on complex type level computations.
Changed the signature as follows
```diff
- def betaReduce[F, Args <: Tuple, R, G](f: Expr[F])(using tf: TupledFunction[F, Args => R], tg: TupledFunction[G, TupleOfExpr[Args] => Expr[R]], qctx: QuoteContext): G = ...
+ def betaReduce[T](expr: Expr[T])(using qctx: QuoteContext): Option[Expr[T]] = ...
```

Improvements
* Simpler API that covers all kind of functions at once (normal/given/erased)
* Better error message for ill-typed `betaRduce` calls
* Adds the possiblility of knowing if the beta-reeduction suceeded
* Use `transform.BetaReduce`
* Improve `transform.BetaReduce` to handle `Inlined` trees and constant argumets
* Fixes scala#9466

Drawback
* Need for slightly loneger code (previous interface could be implented on top of this one)
@nicolasstucki nicolasstucki linked a pull request Jul 30, 2020 that will close this issue
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Jul 30, 2020
Redefine `quoted.Expr.betaRduce` to not rely on complex type level computations.
Changed the signature as follows
```diff
- def betaReduce[F, Args <: Tuple, R, G](f: Expr[F])(using tf: TupledFunction[F, Args => R], tg: TupledFunction[G, TupleOfExpr[Args] => Expr[R]], qctx: QuoteContext): G = ...
+ def betaReduce[T](expr: Expr[T])(using qctx: QuoteContext): Option[Expr[T]] = ...
```

Improvements
* Simpler API that covers all kind of functions at once (normal/given/erased)
* Better error message for ill-typed `betaRduce` calls
* Adds the possiblility of knowing if the beta-reeduction suceeded
* Use `transform.BetaReduce`
* Improve `transform.BetaReduce` to handle `Inlined` trees and constant argumets
* Fixes scala#9466

Drawback
* Need for slightly loneger code (previous interface could be implented on top of this one)
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Jul 30, 2020
Redefine `quoted.Expr.betaRduce` to not rely on complex type level computations.
Changed the signature as follows
```diff
- def betaReduce[F, Args <: Tuple, R, G](f: Expr[F])(using tf: TupledFunction[F, Args => R], tg: TupledFunction[G, TupleOfExpr[Args] => Expr[R]], qctx: QuoteContext): G = ...
+ def betaReduce[T](expr: Expr[T])(using qctx: QuoteContext): Expr[T] = ...
```

Improvements
* Simpler API that covers all kind of functions at once (normal/given/erased)
* Better error message for ill-typed `betaRduce` calls
* Adds the possiblility of knowing if the beta-reeduction suceeded
* Use `transform.BetaReduce`
* Improve `transform.BetaReduce` to handle `Inlined` trees and constant argumets
* Fixes scala#9466

Drawback
* Need for slightly loneger code (previous interface could be implented on top of this one)
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Jul 30, 2020
Redefine `quoted.Expr.betaRduce` to not rely on complex type level computations.
Changed the signature as follows
```diff
- def betaReduce[F, Args <: Tuple, R, G](f: Expr[F])(using tf: TupledFunction[F, Args => R], tg: TupledFunction[G, TupleOfExpr[Args] => Expr[R]], qctx: QuoteContext): G = ...
+ def betaReduce[T](expr: Expr[T])(using qctx: QuoteContext): Expr[T] = ...
```

Improvements
* Simpler API that covers all kind of functions at once (normal/given/erased)
* Better error message for ill-typed `betaRduce` calls
* Adds the possiblility of knowing if the beta-reeduction suceeded
* Use `transform.BetaReduce`
* Improve `transform.BetaReduce` to handle `Inlined` trees and constant argumets
* Fixes scala#9466

Drawback
* Need for slightly loneger code (previous interface could be implented on top of this one)
@rcano
Copy link
Author

rcano commented Jul 30, 2020

i would have thought this could be constant folded to "a"

it doesn't because the if is not an inlined if. In the snippet that is on purpose because I wanted to show that it wasn't inlining the lambda when naming the parameter

nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Jul 30, 2020
Redefine `quoted.Expr.betaRduce` to not rely on complex type level computations.
Changed the signature as follows
```diff
- def betaReduce[F, Args <: Tuple, R, G](f: Expr[F])(using tf: TupledFunction[F, Args => R], tg: TupledFunction[G, TupleOfExpr[Args] => Expr[R]], qctx: QuoteContext): G = ...
+ def betaReduce[T](expr: Expr[T])(using qctx: QuoteContext): Expr[T] = ...
```

Improvements
* Simpler API that covers all kind of functions at once (normal/given/erased)
* Better error message for ill-typed `betaRduce` calls
* Adds the possiblility of knowing if the beta-reeduction suceeded
* Use `transform.BetaReduce`
* Improve `transform.BetaReduce` to handle `Inlined` trees and constant argumets
* Fixes scala#9466

Drawback
* Need for slightly loneger code (previous interface could be implented on top of this one)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants