Skip to content

Commit 7b39421

Browse files
authored
Implicit parameters should warn at call site in >= 3.7 (#22441)
2 parents 4cb43b6 + b53c855 commit 7b39421

File tree

16 files changed

+59
-5
lines changed

16 files changed

+59
-5
lines changed

Diff for: compiler/src/dotty/tools/dotc/ast/Desugar.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -867,7 +867,7 @@ object desugar {
867867
val nu = vparamss.foldLeft(makeNew(classTypeRef)) { (nu, vparams) =>
868868
val app = Apply(nu, vparams.map(refOfDef))
869869
vparams match {
870-
case vparam :: _ if vparam.mods.is(Given) || vparam.name.is(ContextBoundParamName) =>
870+
case vparam :: _ if vparam.mods.isOneOf(GivenOrImplicit) || vparam.name.is(ContextBoundParamName) =>
871871
app.setApplyKind(ApplyKind.Using)
872872
case _ => app
873873
}

Diff for: compiler/src/dotty/tools/dotc/config/MigrationVersion.scala

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ enum MigrationVersion(val warnFrom: SourceVersion, val errorFrom: SourceVersion)
3232
case ParameterEnclosedByParenthesis extends MigrationVersion(future, future)
3333
case XmlLiteral extends MigrationVersion(future, future)
3434
case GivenSyntax extends MigrationVersion(future, never)
35+
case ImplicitParamsWithoutUsing extends MigrationVersion(`3.7`, future)
3536

3637
require(warnFrom.ordinal <= errorFrom.ordinal)
3738

Diff for: compiler/src/dotty/tools/dotc/typer/Migrations.scala

+15
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,19 @@ trait Migrations:
126126
patch(Span(pt.args.head.span.start), "using ")
127127
end contextBoundParams
128128

129+
/** Report implicit parameter lists and rewrite implicit parameter list to contextual params */
130+
def implicitParams(tree: Tree, tp: MethodOrPoly, pt: FunProto)(using Context): Unit =
131+
val mversion = mv.ImplicitParamsWithoutUsing
132+
if tp.companion == ImplicitMethodType && pt.applyKind != ApplyKind.Using && pt.args.nonEmpty then
133+
val rewriteMsg = Message.rewriteNotice("This code", mversion.patchFrom)
134+
report.errorOrMigrationWarning(
135+
em"""Implicit parameters should be provided with a `using` clause.$rewriteMsg
136+
|To disable the warning, please use the following option:
137+
| "-Wconf:msg=Implicit parameters should be provided with a `using` clause:s"
138+
|""",
139+
pt.args.head.srcPos, mversion)
140+
if mversion.needsPatch then
141+
patch(Span(pt.args.head.span.start), "using ")
142+
end implicitParams
143+
129144
end Migrations

Diff for: compiler/src/dotty/tools/dotc/typer/Typer.scala

+1
Original file line numberDiff line numberDiff line change
@@ -4164,6 +4164,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
41644164
def methodStr = methPart(tree).symbol.showLocated
41654165
if matchingApply(wtp, pt) then
41664166
migrate(contextBoundParams(tree, wtp, pt))
4167+
migrate(implicitParams(tree, wtp, pt))
41674168
if needsTupledDual(wtp, pt) then adapt(tree, pt.tupledDual, locked)
41684169
else tree
41694170
else if wtp.isContextualMethod then

Diff for: compiler/test/dotty/tools/dotc/CompilationTests.scala

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ class CompilationTests {
8383
compileFile("tests/rewrites/ambiguous-named-tuple-assignment.scala", defaultOptions.and("-rewrite", "-source:3.6-migration")),
8484
compileFile("tests/rewrites/i21382.scala", defaultOptions.and("-indent", "-rewrite")),
8585
compileFile("tests/rewrites/unused.scala", defaultOptions.and("-rewrite", "-Wunused:all")),
86+
compileFile("tests/rewrites/i22440.scala", defaultOptions.and("-rewrite"))
8687
).checkRewrites()
8788
}
8889

Diff for: language-server/src/dotty/tools/languageserver/DottyLanguageServer.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ class DottyLanguageServer extends LanguageServer
611611
private def inProjectsSeeing(baseDriver: InteractiveDriver,
612612
definitions: List[SourceTree],
613613
symbols: List[Symbol]): List[(InteractiveDriver, Context, List[Symbol])] = {
614-
val projects = projectsSeeing(definitions)(baseDriver.currentCtx)
614+
val projects = projectsSeeing(definitions)(using baseDriver.currentCtx)
615615
projects.toList.map { config =>
616616
val remoteDriver = drivers(config)
617617
val ctx = remoteDriver.currentCtx

Diff for: language-server/src/dotty/tools/languageserver/worksheet/WorksheetService.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ trait WorksheetService { thisServer: DottyLanguageServer =>
2424
val sendMessage = (pos: SourcePosition, msg: String) =>
2525
client.publishOutput(WorksheetRunOutput(params.textDocument, range(pos).get, msg))
2626

27-
runWorksheet(driver, uri, sendMessage, cancelChecker)(driver.currentCtx)
27+
runWorksheet(driver, uri, sendMessage, cancelChecker)(using driver.currentCtx)
2828
cancelChecker.checkCanceled()
2929
WorksheetRunResult(success = true)
3030
} catch {

Diff for: scaladoc/src/dotty/tools/scaladoc/api.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ object Signature:
143143
case class LinkToType(signature: Signature, dri: DRI, kind: Kind)
144144

145145
case class HierarchyGraph(edges: Seq[(LinkToType, LinkToType)], sealedNodes: Set[LinkToType] = Set.empty):
146-
def vertecies: Seq[LinkToType] = edges.flatten((a, b) => Seq(a, b)).distinct
146+
def vertecies: Seq[LinkToType] = edges.flatten(using (a, b) => Seq(a, b)).distinct
147147
def verteciesWithId: Map[LinkToType, Int] = vertecies.zipWithIndex.toMap
148148
def +(edge: (LinkToType, LinkToType)): HierarchyGraph = this ++ Seq(edge)
149149
def ++(edges: Seq[(LinkToType, LinkToType)]): HierarchyGraph =

Diff for: staging/src/scala/quoted/staging/QuoteDriver.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ private class QuoteDriver(appClassloader: ClassLoader) extends Driver:
4343

4444
val compiledExpr =
4545
try
46-
new QuoteCompiler().newRun(ctx).compileExpr(exprBuilder)
46+
new QuoteCompiler().newRun(using ctx).compileExpr(exprBuilder)
4747
catch case ex: dotty.tools.FatalError =>
4848
val enrichedMessage =
4949
s"""An unhandled exception was thrown in the staging compiler.

Diff for: tests/neg/i22440.check

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- Error: tests/neg/i22440.scala:4:12 ----------------------------------------------------------------------------------
2+
4 |val _ = foo(1) // error
3+
| ^
4+
| Implicit parameters should be provided with a `using` clause.
5+
| This code can be rewritten automatically under -rewrite -source 3.7-migration.
6+
| To disable the warning, please use the following option:
7+
| "-Wconf:msg=Implicit parameters should be provided with a `using` clause:s"

Diff for: tests/neg/i22440.scala

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
//> using options -source future
2+
3+
def foo(implicit x: Int) = x
4+
val _ = foo(1) // error

Diff for: tests/pos/i22440.scala

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
//> using options "-Wconf:msg=Implicit parameters should be provided with a `using` clause:s"
2+
3+
def foo(implicit x: Int) = x
4+
val _ = foo(1) // warn

Diff for: tests/rewrites/i22440.check

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//> using options -source 3.7-migration
2+
3+
def foo(implicit x: Int) = ()
4+
val _ = foo(using 1)
5+
val _ = foo (using 1)

Diff for: tests/rewrites/i22440.scala

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//> using options -source 3.7-migration
2+
3+
def foo(implicit x: Int) = ()
4+
val _ = foo(1)
5+
val _ = foo (1)

Diff for: tests/warn/i22440.check

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- Warning: tests/warn/i22440.scala:4:12 -------------------------------------------------------------------------------
2+
4 |val _ = foo(1) // warn
3+
| ^
4+
| Implicit parameters should be provided with a `using` clause.
5+
| This code can be rewritten automatically under -rewrite -source 3.7-migration.
6+
| To disable the warning, please use the following option:
7+
| "-Wconf:msg=Implicit parameters should be provided with a `using` clause:s"

Diff for: tests/warn/i22440.scala

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
//> using options -source 3.7
2+
3+
def foo(implicit x: Int) = x
4+
val _ = foo(1) // warn

0 commit comments

Comments
 (0)