From 5315f08d7adb5ac66186e0070c0458b999bbb359 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Thu, 24 Sep 2020 10:23:38 +0200 Subject: [PATCH] Refactor Reflection error reporting --- community-build/community-projects/scalatest | 2 +- .../community-projects/xml-interpolator | 2 +- .../tools/dotc/quoted/QuoteContextImpl.scala | 18 +++++++------ .../scala/quoted/report.scala | 8 +++--- library/src/scala/tasty/Reflection.scala | 25 ++++++++++++------- .../neg-macros/delegate-match-1/Macro_1.scala | 6 ++--- .../neg-macros/delegate-match-2/Macro_1.scala | 6 ++--- .../neg-macros/delegate-match-3/Macro_1.scala | 6 ++--- tests/neg-macros/i6432/Macro_1.scala | 2 +- tests/neg-macros/i6432b/Macro_1.scala | 2 +- tests/neg-macros/i9801/Macro_1.scala | 2 +- .../macros-in-same-project-6/Foo.scala | 2 +- .../tasty-macro-error/quoted_1.scala | 2 +- .../tasty-macro-positions/quoted_1.scala | 4 +-- .../Macro_1.scala | 2 +- .../Macro_1.scala | 2 +- .../quote-matcher-symantics-1/quoted_1.scala | 2 +- .../quote-matcher-symantics-2/quoted_1.scala | 4 +-- .../Macros_1.scala | 2 +- 19 files changed, 54 insertions(+), 45 deletions(-) diff --git a/community-build/community-projects/scalatest b/community-build/community-projects/scalatest index e447cc132f61..70c08e1b6312 160000 --- a/community-build/community-projects/scalatest +++ b/community-build/community-projects/scalatest @@ -1 +1 @@ -Subproject commit e447cc132f61444ec97e50d9937a85b10c0d76e2 +Subproject commit 70c08e1b63125b149ea5e6f5a34d8a0bb3b78b9f diff --git a/community-build/community-projects/xml-interpolator b/community-build/community-projects/xml-interpolator index b744dff4553b..5b7836580ed0 160000 --- a/community-build/community-projects/xml-interpolator +++ b/community-build/community-projects/xml-interpolator @@ -1 +1 @@ -Subproject commit b744dff4553b7f7d9855bb9943a868fafc4db101 +Subproject commit 5b7836580ed0473bf6a91360a76e306a3e7b0b96 diff --git a/compiler/src/dotty/tools/dotc/quoted/QuoteContextImpl.scala b/compiler/src/dotty/tools/dotc/quoted/QuoteContextImpl.scala index 9990b0753394..490950b06ba5 100644 --- a/compiler/src/dotty/tools/dotc/quoted/QuoteContextImpl.scala +++ b/compiler/src/dotty/tools/dotc/quoted/QuoteContextImpl.scala @@ -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 diff --git a/library/src-bootstrapped/scala/quoted/report.scala b/library/src-bootstrapped/scala/quoted/report.scala index cb9d0445a838..a17883c61920 100644 --- a/library/src-bootstrapped/scala/quoted/report.scala +++ b/library/src-bootstrapped/scala/quoted/report.scala @@ -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 = { @@ -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 diff --git a/library/src/scala/tasty/Reflection.scala b/library/src/scala/tasty/Reflection.scala index 2cd36d1db6df..7c7762cb04ca 100644 --- a/library/src/scala/tasty/Reflection.scala +++ b/library/src/scala/tasty/Reflection.scala @@ -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 // diff --git a/tests/neg-macros/delegate-match-1/Macro_1.scala b/tests/neg-macros/delegate-match-1/Macro_1.scala index f6a006deb485..48f5b0618a03 100644 --- a/tests/neg-macros/delegate-match-1/Macro_1.scala +++ b/tests/neg-macros/delegate-match-1/Macro_1.scala @@ -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) '{} } } diff --git a/tests/neg-macros/delegate-match-2/Macro_1.scala b/tests/neg-macros/delegate-match-2/Macro_1.scala index f4226053a08e..2557380aaa58 100644 --- a/tests/neg-macros/delegate-match-2/Macro_1.scala +++ b/tests/neg-macros/delegate-match-2/Macro_1.scala @@ -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) '{} } } diff --git a/tests/neg-macros/delegate-match-3/Macro_1.scala b/tests/neg-macros/delegate-match-3/Macro_1.scala index 93a8d32a501c..b58baeb0168a 100644 --- a/tests/neg-macros/delegate-match-3/Macro_1.scala +++ b/tests/neg-macros/delegate-match-3/Macro_1.scala @@ -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) '{} } } diff --git a/tests/neg-macros/i6432/Macro_1.scala b/tests/neg-macros/i6432/Macro_1.scala index 3626779af30d..cf66125a81b0 100644 --- a/tests/neg-macros/i6432/Macro_1.scala +++ b/tests/neg-macros/i6432/Macro_1.scala @@ -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) } '{} } diff --git a/tests/neg-macros/i6432b/Macro_1.scala b/tests/neg-macros/i6432b/Macro_1.scala index 3626779af30d..cf66125a81b0 100644 --- a/tests/neg-macros/i6432b/Macro_1.scala +++ b/tests/neg-macros/i6432b/Macro_1.scala @@ -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) } '{} } diff --git a/tests/neg-macros/i9801/Macro_1.scala b/tests/neg-macros/i9801/Macro_1.scala index 5bf2106548f9..44c331aa7045 100644 --- a/tests/neg-macros/i9801/Macro_1.scala +++ b/tests/neg-macros/i9801/Macro_1.scala @@ -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 } } diff --git a/tests/neg-macros/macros-in-same-project-6/Foo.scala b/tests/neg-macros/macros-in-same-project-6/Foo.scala index 89c70b755215..38b94e381532 100644 --- a/tests/neg-macros/macros-in-same-project-6/Foo.scala +++ b/tests/neg-macros/macros-in-same-project-6/Foo.scala @@ -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$") } } diff --git a/tests/neg-macros/tasty-macro-error/quoted_1.scala b/tests/neg-macros/tasty-macro-error/quoted_1.scala index 613f9a4aa563..bcb05a2aa5df 100644 --- a/tests/neg-macros/tasty-macro-error/quoted_1.scala +++ b/tests/neg-macros/tasty-macro-error/quoted_1.scala @@ -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) '{} } diff --git a/tests/neg-macros/tasty-macro-positions/quoted_1.scala b/tests/neg-macros/tasty-macro-positions/quoted_1.scala index 4ded1b9c899c..18f6a2b16bf5 100644 --- a/tests/neg-macros/tasty-macro-positions/quoted_1.scala +++ b/tests/neg-macros/tasty-macro-positions/quoted_1.scala @@ -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) '{} } diff --git a/tests/neg-macros/tasty-string-interpolator-position-a/Macro_1.scala b/tests/neg-macros/tasty-string-interpolator-position-a/Macro_1.scala index 06556ce8e138..a5785bfb7523 100644 --- a/tests/neg-macros/tasty-string-interpolator-position-a/Macro_1.scala +++ b/tests/neg-macros/tasty-string-interpolator-position-a/Macro_1.scala @@ -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: _*) } } diff --git a/tests/neg-macros/tasty-string-interpolator-position-b/Macro_1.scala b/tests/neg-macros/tasty-string-interpolator-position-b/Macro_1.scala index 1d9e849c5564..efd5f1805614 100644 --- a/tests/neg-macros/tasty-string-interpolator-position-b/Macro_1.scala +++ b/tests/neg-macros/tasty-string-interpolator-position-b/Macro_1.scala @@ -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: _*) } } diff --git a/tests/run-macros/quote-matcher-symantics-1/quoted_1.scala b/tests/run-macros/quote-matcher-symantics-1/quoted_1.scala index 4bc796909386..71ab112438b3 100644 --- a/tests/run-macros/quote-matcher-symantics-1/quoted_1.scala +++ b/tests/run-macros/quote-matcher-symantics-1/quoted_1.scala @@ -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) '{ ??? } } diff --git a/tests/run-macros/quote-matcher-symantics-2/quoted_1.scala b/tests/run-macros/quote-matcher-symantics-2/quoted_1.scala index eb3d6865b609..3f74b10e9e4e 100644 --- a/tests/run-macros/quote-matcher-symantics-2/quoted_1.scala +++ b/tests/run-macros/quote-matcher-symantics-2/quoted_1.scala @@ -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) ??? } @@ -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) ??? } diff --git a/tests/run-macros/tasty-string-interpolation-reporter-test/Macros_1.scala b/tests/run-macros/tasty-string-interpolation-reporter-test/Macros_1.scala index 648146174459..5956230cd7d5 100644 --- a/tests/run-macros/tasty-string-interpolation-reporter-test/Macros_1.scala +++ b/tests/run-macros/tasty-string-interpolation-reporter-test/Macros_1.scala @@ -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)