diff --git a/compiler/src/dotty/tools/dotc/reporting/Message.scala b/compiler/src/dotty/tools/dotc/reporting/Message.scala index 1ac5c6ecf407..7a9a42f812ce 100644 --- a/compiler/src/dotty/tools/dotc/reporting/Message.scala +++ b/compiler/src/dotty/tools/dotc/reporting/Message.scala @@ -424,12 +424,17 @@ trait NoDisambiguation extends Message: withoutDisambiguation() /** The fallback `Message` containing no explanation and having no `kind` */ -final class NoExplanation(msgFn: Context ?=> String)(using Context) extends Message(ErrorMessageID.NoExplanationID) { +final class NoExplanation(msgFn: Context ?=> String, actions: List[CodeAction] = List.empty)(using Context) extends Message(ErrorMessageID.NoExplanationID) { def msg(using Context): String = msgFn def explain(using Context): String = "" val kind: MessageKind = MessageKind.NoKind + override def actions(using Context): List[CodeAction] = actions + override def toString(): String = msg + + def withActions(actions: CodeAction*): NoExplanation = + new NoExplanation(msgFn, actions.toList) } /** The extractor for `NoExplanation` can be used to check whether any error diff --git a/compiler/src/dotty/tools/dotc/typer/Migrations.scala b/compiler/src/dotty/tools/dotc/typer/Migrations.scala index acf9e7668917..5f9fc928e7d6 100644 --- a/compiler/src/dotty/tools/dotc/typer/Migrations.scala +++ b/compiler/src/dotty/tools/dotc/typer/Migrations.scala @@ -21,6 +21,8 @@ import NameKinds.ContextBoundParamName import rewrites.Rewrites.patch import util.Spans.Span import rewrites.Rewrites +import dotty.tools.dotc.rewrites.Rewrites.ActionPatch +import dotty.tools.dotc.util.SourcePosition /** A utility trait containing source-dependent deprecation messages * and migrations. @@ -139,14 +141,23 @@ trait Migrations: val rewriteMsg = if hasParentheses then Message.rewriteNotice("This code", mversion.patchFrom) - else - "" - report.errorOrMigrationWarning( + else "" + val message = em"""Implicit parameters should be provided with a `using` clause.$rewriteMsg - |To disable the warning, please use the following option: + |To disable the warning, please use the following option: | "-Wconf:msg=Implicit parameters should be provided with a `using` clause:s" - |""", - pt.args.head.srcPos, mversion) + |""" + val codeAction = CodeAction( + title = "Add `using` clause", + description = None, + patches = List(ActionPatch(pt.args.head.startPos.sourcePos, "using ")) + ) + val withActions = message.withActions(codeAction) + report.errorOrMigrationWarning( + withActions, + pt.args.head.srcPos, + mversion + ) if hasParentheses && mversion.needsPatch then patch(Span(pt.args.head.span.start), "using ") end implicitParams diff --git a/compiler/test/dotty/tools/dotc/reporting/CodeActionTest.scala b/compiler/test/dotty/tools/dotc/reporting/CodeActionTest.scala index 870da08dcfba..ffc6762cc8c7 100644 --- a/compiler/test/dotty/tools/dotc/reporting/CodeActionTest.scala +++ b/compiler/test/dotty/tools/dotc/reporting/CodeActionTest.scala @@ -54,6 +54,19 @@ class CodeActionTest extends DottyTest: |""".stripMargin ) + @Test def addUsingClause = + checkCodeAction( + """|object Test: + | def foo(implicit a: Int) = a + | foo(123) + |""".stripMargin, + "Add `using` clause", + """|object Test: + | def foo(implicit a: Int) = a + | foo(using 123) + |""".stripMargin + ) + @Test def insertMissingCases = checkCodeAction( code = diff --git a/tests/neg/i22440.check b/tests/neg/i22440.check index 699d70f343c3..f5df4916db87 100644 --- a/tests/neg/i22440.check +++ b/tests/neg/i22440.check @@ -3,5 +3,5 @@ | ^ | Implicit parameters should be provided with a `using` clause. | This code can be rewritten automatically under -rewrite -source 3.7-migration. - | To disable the warning, please use the following option: + | To disable the warning, please use the following option: | "-Wconf:msg=Implicit parameters should be provided with a `using` clause:s" diff --git a/tests/warn/i22440.check b/tests/warn/i22440.check index eaa357661a59..018a31e57044 100644 --- a/tests/warn/i22440.check +++ b/tests/warn/i22440.check @@ -3,5 +3,5 @@ | ^ | Implicit parameters should be provided with a `using` clause. | This code can be rewritten automatically under -rewrite -source 3.7-migration. - | To disable the warning, please use the following option: + | To disable the warning, please use the following option: | "-Wconf:msg=Implicit parameters should be provided with a `using` clause:s" diff --git a/tests/warn/i22731.check b/tests/warn/i22731.check index f7b25eb3b384..a4701ed8a076 100644 --- a/tests/warn/i22731.check +++ b/tests/warn/i22731.check @@ -3,11 +3,11 @@ | ^^^^^^^^ | Implicit parameters should be provided with a `using` clause. | This code can be rewritten automatically under -rewrite -source 3.7-migration. - | To disable the warning, please use the following option: + | To disable the warning, please use the following option: | "-Wconf:msg=Implicit parameters should be provided with a `using` clause:s" -- Warning: tests/warn/i22731.scala:7:6 -------------------------------------------------------------------------------- 7 | foo { () => 43 } // warn | ^^^^^^^^^^^^ | Implicit parameters should be provided with a `using` clause. - | To disable the warning, please use the following option: + | To disable the warning, please use the following option: | "-Wconf:msg=Implicit parameters should be provided with a `using` clause:s"