Skip to content

Refactor Reflection reporting #9864

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
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
18 changes: 10 additions & 8 deletions compiler/src/dotty/tools/dotc/quoted/QuoteContextImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2574,17 +2574,19 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext:
case cu => ""
end Source

def error(msg: => String, pos: Position): Unit =
dotc.report.error(msg, pos)
object Reporting extends ReportingModule:
def error(msg: => String, pos: Position): Unit =
dotc.report.error(msg, pos)

def error(msg: => String, sourceFile: SourceFile, start: Int, end: Int): Unit =
dotc.report.error(msg, dotc.util.SourcePosition(sourceFile, dotc.util.Spans.Span(start, end)))
def error(msg: => String, sourceFile: SourceFile, start: Int, end: Int): Unit =
dotc.report.error(msg, dotc.util.SourcePosition(sourceFile, dotc.util.Spans.Span(start, end)))

def warning(msg: => String, pos: Position): Unit =
dotc.report.warning(msg, pos)
def warning(msg: => String, pos: Position): Unit =
dotc.report.warning(msg, pos)

def warning(msg: => String, sourceFile: SourceFile, start: Int, end: Int): Unit =
dotc.report.error(msg, dotc.util.SourcePosition(sourceFile, dotc.util.Spans.Span(start, end)))
def warning(msg: => String, sourceFile: SourceFile, start: Int, end: Int): Unit =
dotc.report.error(msg, dotc.util.SourcePosition(sourceFile, dotc.util.Spans.Span(start, end)))
end Reporting

type Documentation = dotc.core.Comments.Comment

Expand Down
8 changes: 4 additions & 4 deletions library/src-bootstrapped/scala/quoted/report.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ object report:

/** Report an error at the position of the macro expansion */
def error(msg: => String)(using qctx: QuoteContext): Unit =
qctx.tasty.error(msg, qctx.tasty.rootPosition)
qctx.tasty.Reporting.error(msg, qctx.tasty.rootPosition)

/** Report an error at the on the position of `expr` */
def error(msg: => String, expr: Expr[Any])(using qctx: QuoteContext): Unit =
qctx.tasty.error(msg, expr.unseal.pos)
qctx.tasty.Reporting.error(msg, expr.unseal.pos)

/** Report an error at the position of the macro expansion and throws a StopQuotedContext */
def throwError(msg: => String)(using qctx: QuoteContext): Nothing = {
Expand All @@ -23,11 +23,11 @@ object report:

/** Report a warning */
def warning(msg: => String)(using qctx: QuoteContext): Unit =
qctx.tasty.warning(msg, qctx.tasty.rootPosition)
qctx.tasty.Reporting.warning(msg, qctx.tasty.rootPosition)

/** Report a warning at the on the position of `expr` */
def warning(msg: => String, expr: Expr[_])(using qctx: QuoteContext): Unit =
qctx.tasty.warning(msg, expr.unseal.pos)
qctx.tasty.Reporting.warning(msg, expr.unseal.pos)

/** Throwable used to stop the expansion of a macro after an error was reported */
class StopQuotedContext extends Throwable
Expand Down
25 changes: 16 additions & 9 deletions library/src/scala/tasty/Reflection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3238,19 +3238,26 @@ trait Reflection { reflection =>
// REPORTING //
///////////////

// TODO: these should not be top level
val Reporting: ReportingModule

/** Emits an error message */
def error(msg: => String, pos: Position): Unit
/** Module containg error and waring reporiting.
*
* Also see scala.quoted.report
*/
trait ReportingModule { self: Reporting.type =>
/** Emits an error message */
def error(msg: => String, pos: Position): Unit

/** Emits an error at a specific range of a file */
def error(msg: => String, source: SourceFile, start: Int, end: Int): Unit
/** Emits an error at a specific range of a file */
def error(msg: => String, source: SourceFile, start: Int, end: Int): Unit

/** Emits an error message */
def warning(msg: => String, pos: Position): Unit
/** Emits an error message */
def warning(msg: => String, pos: Position): Unit

/** Emits a warning at a specific range of a file */
def warning(msg: => String, source: SourceFile, start: Int, end: Int): Unit
}

/** Emits a warning at a specific range of a file */
def warning(msg: => String, source: SourceFile, start: Int, end: Int): Unit

///////////////////
// DOCUMENTATION //
Expand Down
6 changes: 3 additions & 3 deletions tests/neg-macros/delegate-match-1/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ private def fImpl(using qctx: QuoteContext): Expr[Unit] = {
case x: ImplicitSearchSuccess =>
'{}
case x: DivergingImplicit => '{}
error("DivergingImplicit\n" + x.explanation, rootPosition)
Reporting.error("DivergingImplicit\n" + x.explanation, rootPosition)
'{}
case x: NoMatchingImplicits =>
error("NoMatchingImplicits\n" + x.explanation, rootPosition)
Reporting.error("NoMatchingImplicits\n" + x.explanation, rootPosition)
'{}
case x: AmbiguousImplicits =>
error("AmbiguousImplicits\n" + x.explanation, rootPosition)
Reporting.error("AmbiguousImplicits\n" + x.explanation, rootPosition)
'{}
}
}
Expand Down
6 changes: 3 additions & 3 deletions tests/neg-macros/delegate-match-2/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ private def fImpl (using qctx: QuoteContext) : Expr[Unit] = {
case x: ImplicitSearchSuccess =>
'{}
case x: DivergingImplicit => '{}
error("DivergingImplicit\n" + x.explanation, rootPosition)
Reporting.error("DivergingImplicit\n" + x.explanation, rootPosition)
'{}
case x: NoMatchingImplicits =>
error("NoMatchingImplicits\n" + x.explanation, rootPosition)
Reporting.error("NoMatchingImplicits\n" + x.explanation, rootPosition)
'{}
case x: AmbiguousImplicits =>
error("AmbiguousImplicits\n" + x.explanation, rootPosition)
Reporting.error("AmbiguousImplicits\n" + x.explanation, rootPosition)
'{}
}
}
Expand Down
6 changes: 3 additions & 3 deletions tests/neg-macros/delegate-match-3/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ private def fImpl(using qctx: QuoteContext) : Expr[Unit] = {
case x: ImplicitSearchSuccess =>
'{}
case x: DivergingImplicit => '{}
error("DivergingImplicit\n" + x.explanation, rootPosition)
Reporting.error("DivergingImplicit\n" + x.explanation, rootPosition)
'{}
case x: NoMatchingImplicits =>
error("NoMatchingImplicits\n" + x.explanation, rootPosition)
Reporting.error("NoMatchingImplicits\n" + x.explanation, rootPosition)
'{}
case x: AmbiguousImplicits =>
error("AmbiguousImplicits\n" + x.explanation, rootPosition)
Reporting.error("AmbiguousImplicits\n" + x.explanation, rootPosition)
'{}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/neg-macros/i6432/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ object Macro {
sc match {
case '{ StringContext(${Varargs(parts)}: _*) } =>
for (part @ Const(s) <- parts)
error(s, part.unseal.pos)
Reporting.error(s, part.unseal.pos)
}
'{}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/neg-macros/i6432b/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ object Macro {
sc match {
case '{ StringContext(${Varargs(parts)}: _*) } =>
for (part @ Const(s) <- parts)
error(s, part.unseal.pos)
Reporting.error(s, part.unseal.pos)
}
'{}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/neg-macros/i9801/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ def impl(prog: Expr[Double])(using QuoteContext) : Expr[Double] =
triggerStackOverflow(0)
} catch {
case e =>
qctx.tasty.error(e.getMessage, prog.unseal.pos)
qctx.tasty.Reporting.error(e.getMessage, prog.unseal.pos)
'{ 42.0 }
}
2 changes: 1 addition & 1 deletion tests/neg-macros/macros-in-same-project-6/Foo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ object Foo {

def aMacroImplementation(using qctx: QuoteContext) : Expr[Unit] = {
import qctx.tasty._
error("some error", rootPosition)
Reporting.error("some error", rootPosition)
throw new NoClassDefFoundError("Bar$")
}
}
2 changes: 1 addition & 1 deletion tests/neg-macros/tasty-macro-error/quoted_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ object Macros {

def impl(x: Expr[Any])(using qctx: QuoteContext) : Expr[Unit] = {
import qctx.tasty._
error("here is the the argument is " + x.unseal.underlyingArgument.show, x.unseal.underlyingArgument.pos)
Reporting.error("here is the the argument is " + x.unseal.underlyingArgument.show, x.unseal.underlyingArgument.pos)
'{}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/neg-macros/tasty-macro-positions/quoted_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ object Macros {
def impl(x: Expr[Any])(using qctx: QuoteContext) : Expr[Unit] = {
import qctx.tasty._
val pos = x.unseal.underlyingArgument.pos
error("here is the the argument is " + x.unseal.underlyingArgument.show, pos)
error("here (+5) is the the argument is " + x.unseal.underlyingArgument.show, pos.sourceFile, pos.start + 5, pos.end + 5)
Reporting.error("here is the the argument is " + x.unseal.underlyingArgument.show, pos)
Reporting.error("here (+5) is the the argument is " + x.unseal.underlyingArgument.show, pos.sourceFile, pos.start + 5, pos.end + 5)
'{}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ object FIntepolator {

def apply(strCtxExpr: Expr[StringContext], argsExpr: Expr[Seq[Any]])(using qctx: QuoteContext) : Expr[String] = {
import qctx.tasty._
error("there are no parts", strCtxExpr.unseal.underlyingArgument.pos)
Reporting.error("there are no parts", strCtxExpr.unseal.underlyingArgument.pos)
'{ ($strCtxExpr).s($argsExpr: _*) }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ object Macro {
object FIntepolator {
def apply(strCtxExpr: Expr[StringContext], argsExpr: Expr[Seq[Any]])(using qctx: QuoteContext) : Expr[String] = {
import qctx.tasty._
error("there are no args", argsExpr.unseal.underlyingArgument.pos)
Reporting.error("there are no args", argsExpr.unseal.underlyingArgument.pos)
'{ ($strCtxExpr).s($argsExpr: _*) }
}

Expand Down
2 changes: 1 addition & 1 deletion tests/run-macros/quote-matcher-symantics-1/quoted_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ object Macros {

case _ =>
import qctx.tasty._
error("Expected explicit DSL", e.unseal.pos)
Reporting.error("Expected explicit DSL", e.unseal.pos)
'{ ??? }

}
Expand Down
4 changes: 2 additions & 2 deletions tests/run-macros/quote-matcher-symantics-2/quoted_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ object Macros {

case _ =>
import qctx.tasty._
error("Expected explicit DSL " + e.show, e.unseal.pos)
Reporting.error("Expected explicit DSL " + e.show, e.unseal.pos)
???
}

Expand All @@ -46,7 +46,7 @@ object Macros {
)
case _ =>
import qctx.tasty._
error("Expected explicit DSL => DSL " + e.show, e.unseal.pos)
Reporting.error("Expected explicit DSL => DSL " + e.show, e.unseal.pos)
???
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ object Macro {
val reporter = new Reporter {
def errorOnPart(msg: String, partIdx: Int): Unit = {
import qctx.tasty._
error(msg, parts(partIdx).unseal.pos)
Reporting.error(msg, parts(partIdx).unseal.pos)
}
}
fooCore(parts, args, reporter)
Expand Down