From cb3a4ef6e78fbaafd112920167dde5b195f26cb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=8BAndrzej=20Ressel?= Date: Wed, 16 Feb 2022 10:21:28 +0100 Subject: [PATCH 1/2] Set context when reporting summonInline errors Backport of #14405 --- .../src/dotty/tools/dotc/typer/Inliner.scala | 16 +++++++++------- tests/neg-macros/i13406/Qux_1.scala | 4 ++++ tests/neg-macros/i13406/Test_2.scala | 8 ++++++++ 3 files changed, 21 insertions(+), 7 deletions(-) create mode 100644 tests/neg-macros/i13406/Qux_1.scala create mode 100644 tests/neg-macros/i13406/Test_2.scala diff --git a/compiler/src/dotty/tools/dotc/typer/Inliner.scala b/compiler/src/dotty/tools/dotc/typer/Inliner.scala index 722e3abfec85..0bae47ceff0a 100644 --- a/compiler/src/dotty/tools/dotc/typer/Inliner.scala +++ b/compiler/src/dotty/tools/dotc/typer/Inliner.scala @@ -849,13 +849,15 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(using Context) { def searchImplicit(tpt: Tree) = val evTyper = new Typer(ctx.nestingLevel + 1) val evCtx = ctx.fresh.setTyper(evTyper) - val evidence = evTyper.inferImplicitArg(tpt.tpe, tpt.span)(using evCtx) - evidence.tpe match - case fail: Implicits.SearchFailureType => - val msg = evTyper.missingArgMsg(evidence, tpt.tpe, "") - errorTree(tpt, em"$msg") - case _ => - evidence + inContext(evCtx) { + val evidence = evTyper.inferImplicitArg(tpt.tpe, tpt.span) + evidence.tpe match + case fail: Implicits.SearchFailureType => + val msg = evTyper.missingArgMsg(evidence, tpt.tpe, "") + errorTree(call, em"$msg") + case _ => + evidence + } return searchImplicit(callTypeArgs.head) } diff --git a/tests/neg-macros/i13406/Qux_1.scala b/tests/neg-macros/i13406/Qux_1.scala new file mode 100644 index 000000000000..f68c92de6612 --- /dev/null +++ b/tests/neg-macros/i13406/Qux_1.scala @@ -0,0 +1,4 @@ +// Qux_1.scala +sealed trait Qux[T] // anonymous mirror because no companion +object QuxImpl: + case class Foo[A](a: A) extends Qux[A] diff --git a/tests/neg-macros/i13406/Test_2.scala b/tests/neg-macros/i13406/Test_2.scala new file mode 100644 index 000000000000..f2cb0a3f05ee --- /dev/null +++ b/tests/neg-macros/i13406/Test_2.scala @@ -0,0 +1,8 @@ +// Test_2.scala +trait Bar + +inline given derivedReducible(using scala.deriving.Mirror.SumOf[Qux[_]]): Bar = + scala.compiletime.summonInline[Bar] + ??? + +def test = derivedReducible // error From f15746a22dfa129e049cd6156b7e1813c1b4f56b Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Mon, 21 Feb 2022 15:53:38 +0100 Subject: [PATCH 2/2] Followup #14405 Make same fix in another part of the code. --- .../src/dotty/tools/dotc/typer/Inliner.scala | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/typer/Inliner.scala b/compiler/src/dotty/tools/dotc/typer/Inliner.scala index 0bae47ceff0a..6dfdc6167bb3 100644 --- a/compiler/src/dotty/tools/dotc/typer/Inliner.scala +++ b/compiler/src/dotty/tools/dotc/typer/Inliner.scala @@ -1320,17 +1320,19 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(using Context) { def searchImplicit(sym: TermSymbol, tpt: Tree) = { val evTyper = new Typer(ctx.nestingLevel + 1) val evCtx = ctx.fresh.setTyper(evTyper) - val evidence = evTyper.inferImplicitArg(tpt.tpe, tpt.span)(using evCtx) - evidence.tpe match { - case fail: Implicits.AmbiguousImplicits => - report.error(evTyper.missingArgMsg(evidence, tpt.tpe, ""), tpt.srcPos) - true // hard error: return true to stop implicit search here - case fail: Implicits.SearchFailureType => - false - case _ => - //inlining.println(i"inferred implicit $sym: ${sym.info} with $evidence: ${evidence.tpe.widen}, ${evCtx.gadt.constraint}, ${evCtx.typerState.constraint}") - newTermBinding(sym, evidence) - true + inContext(evCtx) { + val evidence = evTyper.inferImplicitArg(tpt.tpe, tpt.span) + evidence.tpe match { + case fail: Implicits.AmbiguousImplicits => + report.error(evTyper.missingArgMsg(evidence, tpt.tpe, ""), tpt.srcPos) + true // hard error: return true to stop implicit search here + case fail: Implicits.SearchFailureType => + false + case _ => + //inlining.println(i"inferred implicit $sym: ${sym.info} with $evidence: ${evidence.tpe.widen}, ${evCtx.gadt.constraint}, ${evCtx.typerState.constraint}") + newTermBinding(sym, evidence) + true + } } }