forked from scala/scala3
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Warn on inline given aliases with functions as RHS
```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 scala#16497
- Loading branch information
1 parent
758f3eb
commit 5bb693e
Showing
4 changed files
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
|
||
class Item(x: String) | ||
|
||
inline given a: Conversion[String, Item] = | ||
Item(_) // error | ||
|
||
inline given b: Conversion[String, Item] = | ||
(x => Item(x)) // error | ||
|
||
inline given c: Conversion[String, Item] = | ||
{ x => Item(x) } // error | ||
|
||
inline given d: Conversion[String, Item] with | ||
def apply(x: String) = Item(x) // ok | ||
|