Skip to content

Warn on inline given aliases with functions as RHS #16499

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

Merged
merged 2 commits into from
Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ enum ErrorMessageID(val isActive: Boolean = true) extends java.lang.Enum[ErrorMe
case MissingArgumentID // errorNumer 171
case MissingImplicitArgumentID // errorNumber 172
case CannotBeAccessedID // errorNumber 173
case InlineGivenShouldNotBeFunctionID // errorNumber 174

def errorNumber = ordinal - 1

Expand Down
20 changes: 20 additions & 0 deletions compiler/src/dotty/tools/dotc/reporting/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2769,4 +2769,24 @@ extends ReferenceMsg(CannotBeAccessedID):
i"$whatCanNot be accessed as a member of $pre$where.$whyNot"
def explain(using Context) = ""

class InlineGivenShouldNotBeFunction()(using Context)
extends SyntaxMsg(InlineGivenShouldNotBeFunctionID):
def msg(using Context) =
i"""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."""
def explain(using Context) =
i"""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
|
| given Conversion[A, B] with
| inline def apply(x: A) = x.toB
"""

4 changes: 4 additions & 0 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2374,6 +2374,10 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
if sym.isInlineMethod then
if StagingContext.level > 0 then
report.error("inline def cannot be within quotes", sym.sourcePos)
if sym.is(Given)
&& untpd.stripBlock(untpd.unsplice(ddef.rhs)).isInstanceOf[untpd.Function]
then
report.warning(InlineGivenShouldNotBeFunction(), ddef.rhs.srcPos)
val rhsToInline = PrepareInlineable.wrapRHS(ddef, tpt1, rhs1)
PrepareInlineable.registerInlineInfo(sym, rhsToInline)

Expand Down
15 changes: 15 additions & 0 deletions tests/neg-custom-args/fatal-warnings/inline-givens.scala
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