Skip to content

Disallow inline given aliases with functions as RHS #16498

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

Closed
wants to merge 2 commits into from

Conversation

odersky
Copy link
Contributor

@odersky odersky commented Dec 10, 2022

    inline given a: Conversion[String, Item] = Item(_)  // error

will now produce this error:

5 |  inline given a: Conversion[String, Item] = Item(_)  // error
  |                                             ^^^^^^^
  |inline given alias cannot have a function value as right-hand side.
  |Either drop `inline` or rewrite the given with an explicit `apply` method.
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | A function value on the right-hand side of an inline given alias would expand to
  | an anonymous class. Each application of the inline given would then create a
  | fresh copy of that class, which can increase code size in surprising ways.
  | For that reason, functions are disallowed as right hand sides of inline given aliases.
  | You should either drop `inline` or rewrite to an explicit `apply` method. E.g.
  |
  |     inline given Conversion[A, B] = x => x.toB // error
  |
  | should be re-formulated as
  |
  |     inline given Conversion[A, B] with
  |       def apply(x: A) = x.toB
  |

Fixes #16497
Alternative to #16499

```scala
    inline given a: Conversion[String, Item] = Item(_)  // error
```
will now produce this error:
```
5 |  inline given a: Conversion[String, Item] = Item(_)  // error
  |                                             ^^^^^^^
  |inline given alias cannot have a function value as right-hand side.
  |Either drop `inline` or rewrite the given with an explicit `apply` method.
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | A function value on the right-hand side of an inline given alias would expand to
  | an anonymous class. Each application of the inline given would then create a
  | fresh copy of that class, which can increase code size in surprising ways.
  | For that reason, functions are disallowed as right hand sides of inline given aliases.
  | You should either drop `inline` or rewrite to an explicit `apply` method. E.g.
  |
  |     inline given Conversion[A, B] = x => x.toB // error
  |
  | should be re-formulated as
  |
  |     inline given Conversion[A, B] with
  |       def apply(x: A) = x.toB
  |
```
Fixes scala#16497
@odersky
Copy link
Contributor Author

odersky commented Dec 11, 2022

I am going to close this in favor of #16499

@odersky odersky closed this Dec 11, 2022
odersky added a commit that referenced this pull request Dec 15, 2022
```scala
    inline given a: Conversion[String, Item] = Item(_)
```
will now produce this warning:
```
 5 |  inline given a: Conversion[String, Item] = Item(_)
   |                                             ^^^^^^^
   |An inline given alias with a function value as right-hand side can significantly increase
   |generated code size. You should either drop the `inline` or rewrite the given with an
   |explicit `apply` method.
   |----------------------------------------------------------------------------
   | Explanation (enabled by `-explain`)
   |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   | A function value on the right-hand side of an inline given alias expands to
   | an anonymous class. Each application of the inline given will then create a
   | fresh copy of that class, which can increase code size in surprising ways.
   | For that reason, functions are discouraged as right hand sides of inline given aliases.
   | You should either drop `inline` or rewrite to an explicit `apply` method. E.g.
   |
   |     inline given Conversion[A, B] = x => x.toB
   |
   | should be re-formulated as
   |
   |     inline given Conversion[A, B] with
   |       def apply(x: A) = x.toB
   |
```
Fixes #16497
Alternative to #16498
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Inline SAM conversions can generate lots of code
1 participant