From f1db208c426f42414a57c180dbe68090161787cb Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Wed, 16 Aug 2023 11:57:00 +0200 Subject: [PATCH 1/2] Improve non-static macro implementation error message 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](https://github.com/scala/improvement-proposals/pull/58) would introduce a way to not generate this accessor. Fixes #15413 --- compiler/src/dotty/tools/dotc/transform/Splicer.scala | 8 ++++++++ tests/neg-macros/i15413.check | 6 ++++++ tests/neg-macros/i15413.scala | 7 +++++++ 3 files changed, 21 insertions(+) create mode 100644 tests/neg-macros/i15413.check create mode 100644 tests/neg-macros/i15413.scala diff --git a/compiler/src/dotty/tools/dotc/transform/Splicer.scala b/compiler/src/dotty/tools/dotc/transform/Splicer.scala index c870336995f0..07f6b823bf8f 100644 --- a/compiler/src/dotty/tools/dotc/transform/Splicer.scala +++ b/compiler/src/dotty/tools/dotc/transform/Splicer.scala @@ -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 { @@ -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. diff --git a/tests/neg-macros/i15413.check b/tests/neg-macros/i15413.check new file mode 100644 index 000000000000..56f587eb2fb4 --- /dev/null +++ b/tests/neg-macros/i15413.check @@ -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 diff --git a/tests/neg-macros/i15413.scala b/tests/neg-macros/i15413.scala new file mode 100644 index 000000000000..186ba60f3d25 --- /dev/null +++ b/tests/neg-macros/i15413.scala @@ -0,0 +1,7 @@ +import scala.quoted.* + +class Macro: + inline def foo = ${ Macro.fooImpl } // error + +object Macro: + private def fooImpl(using Quotes) = '{} From 546ecf946ba21228b3bbb4a5334ad416dfddcfdd Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Thu, 17 Aug 2023 20:23:58 +0200 Subject: [PATCH 2/2] Update compiler/src/dotty/tools/dotc/transform/Splicer.scala Co-authored-by: Jan Chyb <48855024+jchyb@users.noreply.github.com> --- compiler/src/dotty/tools/dotc/transform/Splicer.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/src/dotty/tools/dotc/transform/Splicer.scala b/compiler/src/dotty/tools/dotc/transform/Splicer.scala index 07f6b823bf8f..46a8571b4f30 100644 --- a/compiler/src/dotty/tools/dotc/transform/Splicer.scala +++ b/compiler/src/dotty/tools/dotc/transform/Splicer.scala @@ -216,7 +216,7 @@ object Splicer { 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 + // TODO suggest use of @binaryAPI once we have the annotation report.error( i"""Macro implementation is not statically accessible. |