From db67331359cf83f8114b879c6f2981bd70b998b6 Mon Sep 17 00:00:00 2001 From: Jan Chyb Date: Mon, 30 Sep 2024 17:20:43 +0200 Subject: [PATCH] Do not warn about expected missing positions in quotes reflect.Symbols.pos --- .../src/scala/quoted/runtime/impl/QuotesImpl.scala | 4 +++- tests/neg-macros/i19842-a.check | 2 +- tests/neg-macros/i19842-b.check | 2 +- tests/pos-macros/i21672/Macro_1.scala | 10 ++++++++++ tests/pos-macros/i21672/Test_2.scala | 3 +++ 5 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 tests/pos-macros/i21672/Macro_1.scala create mode 100644 tests/pos-macros/i21672/Test_2.scala diff --git a/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala b/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala index ce8d19aae46a..fad89ee8d8a2 100644 --- a/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala +++ b/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala @@ -25,6 +25,7 @@ import scala.quoted.runtime.impl.printers.* import scala.reflect.TypeTest import dotty.tools.dotc.core.NameKinds.ExceptionBinderName import dotty.tools.dotc.transform.TreeChecker +import dotty.tools.dotc.util.NoSource object QuotesImpl { @@ -2688,9 +2689,10 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler if self.exists then val symPos = self.sourcePos if symPos.exists then Some(symPos) - else + else if self.source.exists then if xCheckMacro then report.warning(s"Missing symbol position (defaulting to position 0): $self\nThis is a compiler bug. Please report it.") Some(self.source.atSpan(dotc.util.Spans.Span(0))) + else None else None def docstring: Option[String] = diff --git a/tests/neg-macros/i19842-a.check b/tests/neg-macros/i19842-a.check index 30b295cd05a5..a5d4cec547d8 100644 --- a/tests/neg-macros/i19842-a.check +++ b/tests/neg-macros/i19842-a.check @@ -9,8 +9,8 @@ | | at scala.runtime.Scala3RunTime$.assertFailed(Scala3RunTime.scala:8) | at dotty.tools.dotc.transform.TreeChecker$.checkParents(TreeChecker.scala:210) + | at scala.quoted.runtime.impl.QuotesImpl$reflect$ClassDef$.module(QuotesImpl.scala:258) | at scala.quoted.runtime.impl.QuotesImpl$reflect$ClassDef$.module(QuotesImpl.scala:257) - | at scala.quoted.runtime.impl.QuotesImpl$reflect$ClassDef$.module(QuotesImpl.scala:256) | at Macros$.makeSerializer(Macro.scala:25) | |--------------------------------------------------------------------------------------------------------------------- diff --git a/tests/neg-macros/i19842-b.check b/tests/neg-macros/i19842-b.check index d84d916acb66..38595a2531fe 100644 --- a/tests/neg-macros/i19842-b.check +++ b/tests/neg-macros/i19842-b.check @@ -9,8 +9,8 @@ | | at scala.runtime.Scala3RunTime$.assertFailed(Scala3RunTime.scala:8) | at dotty.tools.dotc.transform.TreeChecker$.checkParents(TreeChecker.scala:210) + | at scala.quoted.runtime.impl.QuotesImpl$reflect$ClassDef$.module(QuotesImpl.scala:258) | at scala.quoted.runtime.impl.QuotesImpl$reflect$ClassDef$.module(QuotesImpl.scala:257) - | at scala.quoted.runtime.impl.QuotesImpl$reflect$ClassDef$.module(QuotesImpl.scala:256) | at Macros$.makeSerializer(Macro.scala:27) | |--------------------------------------------------------------------------------------------------------------------- diff --git a/tests/pos-macros/i21672/Macro_1.scala b/tests/pos-macros/i21672/Macro_1.scala new file mode 100644 index 000000000000..2e17631d6cf4 --- /dev/null +++ b/tests/pos-macros/i21672/Macro_1.scala @@ -0,0 +1,10 @@ +object Repro { + inline def apply(): Unit = ${ applyImpl } + + import scala.quoted.* + def applyImpl(using q: Quotes): Expr[Unit] = { + import q.reflect.* + report.info(TypeRepr.of[Some[String]].typeSymbol.pos.toString) + '{ () } + } +} diff --git a/tests/pos-macros/i21672/Test_2.scala b/tests/pos-macros/i21672/Test_2.scala new file mode 100644 index 000000000000..b164962100af --- /dev/null +++ b/tests/pos-macros/i21672/Test_2.scala @@ -0,0 +1,3 @@ +//> using options -Xfatal-warnings +object Test: + Repro()