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
Closed
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 InlineGivenCannotBeFunctionID // errorNumber 174

def errorNumber = ordinal - 1

Expand Down
19 changes: 19 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,23 @@ extends ReferenceMsg(CannotBeAccessedID):
i"$whatCanNot be accessed as a member of $pre$where.$whyNot"
def explain(using Context) = ""

class InlineGivenCannotBeFunction()(using Context)
extends SyntaxMsg(InlineGivenCannotBeFunctionID):
def msg(using Context) =
i"""inline given alias cannot have a function value as right-hand side.
|Either drop `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 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
"""

2 changes: 2 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,8 @@ 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) && ddef.rhs.isInstanceOf[untpd.Function] then
report.error(InlineGivenCannotBeFunction(), ddef.rhs.srcPos)
val rhsToInline = PrepareInlineable.wrapRHS(ddef, tpt1, rhs1)
PrepareInlineable.registerInlineInfo(sym, rhsToInline)

Expand Down
4 changes: 2 additions & 2 deletions tests/neg-macros/i11483/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ trait CpsMonad[F[_]]:
@compileTimeOnly("await should be inside async block")
def await[F[_],T](f:F[T])(using am:CpsMonad[F]):T = ???

inline given conversion[F[_],T](using CpsMonad[F]): Conversion[F[T],T] =
x => await(x)
inline given conversion[F[_],T](using CpsMonad[F]): Conversion[F[T],T] with
inline def apply(x: F[T]) = await(x)


object X {
Expand Down
9 changes: 9 additions & 0 deletions tests/neg/inline-givens.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

class Item(x: String)

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

inline given b: Conversion[String, Item] with
def apply(x: String) = Item(x)

4 changes: 2 additions & 2 deletions tests/pos/i13900.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import scala.annotation.targetName
opaque type Inlined[T] = T
object Inlined:
extension [T](inlined: Inlined[T]) def value: T = inlined
inline given fromValue[T <: Singleton]: Conversion[T, Inlined[T]] =
value => value
inline given fromValue[T <: Singleton]: Conversion[T, Inlined[T]] with
def apply(value: T) = value
@targetName("fromValueWide")
given fromValue[Wide]: Conversion[Wide, Inlined[Wide]] = value => value

Expand Down
3 changes: 2 additions & 1 deletion tests/pos/i8276.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
object NOTHING

inline given [A]: Conversion[NOTHING.type, Option[A]] = _ => None
inline given [A]: Conversion[NOTHING.type, Option[A]] with
def apply(x: NOTHING.type) = None

def apply[A](p: Vector[A], o: Option[A] = NOTHING): Unit = ???

Expand Down