Skip to content

Commit

Permalink
Improve non-static macro implementation error message
Browse files Browse the repository at this point in the history
If non-static inline accessor is generated we do not we can tell the
user why they cannot access the macro implementation this way.

Currently we do not have a clean way to fix this code, but in the future [SIP-58](scala/improvement-proposals#58)
would introduce a way to not generate this accessor.

Fixes #15413

[Cherry-picked f1db208]
  • Loading branch information
nicolasstucki authored and WojciechMazur committed Jun 19, 2024
1 parent feeddb7 commit 8dfbd6d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
8 changes: 8 additions & 0 deletions compiler/src/dotty/tools/dotc/transform/Splicer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import dotty.tools.dotc.quoted.{PickledQuotes, QuoteUtils}

import scala.quoted.Quotes
import scala.quoted.runtime.impl._
import dotty.tools.dotc.core.NameKinds

/** Utility class to splice quoted expressions */
object Splicer {
Expand Down Expand Up @@ -214,6 +215,13 @@ object Splicer {
report.error("Macro cannot be implemented with an `inline` method", fn.srcPos)
args.flatten.foreach(checkIfValidArgument)

case Call(fn, args) if fn.symbol.name.is(NameKinds.InlineAccessorName) =>
// TODO suggest use of @binaryAPI one we have the annotation
report.error(
i"""Macro implementation is not statically accessible.
|
|Non-static inline accessor was generated in ${fn.symbol.owner}
|""".stripMargin, tree.srcPos)
case _ =>
report.error(
"""Malformed macro.
Expand Down
6 changes: 6 additions & 0 deletions tests/neg-macros/i15413.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- Error: tests/neg-macros/i15413.scala:4:22 ---------------------------------------------------------------------------
4 | inline def foo = ${ Macro.fooImpl } // error
| ^^^^^^^^^^^^^
| Macro implementation is not statically accessible.
|
| Non-static inline accessor was generated in class Macro
7 changes: 7 additions & 0 deletions tests/neg-macros/i15413.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import scala.quoted.*

class Macro:
inline def foo = ${ Macro.fooImpl } // error

object Macro:
private def fooImpl(using Quotes) = '{}

0 comments on commit 8dfbd6d

Please sign in to comment.