Skip to content

Commit 3c4148a

Browse files
committed
chore: implicit parameters should warn at call site
1 parent f7e5df5 commit 3c4148a

File tree

10 files changed

+43
-0
lines changed

10 files changed

+43
-0
lines changed

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

+12
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,16 @@ 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+
pt.args.head.srcPos, mversion)
137+
if mversion.needsPatch then
138+
patch(Span(pt.args.head.span.start), "using ")
139+
end implicitParams
140+
129141
end Migrations

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

+1
Original file line numberDiff line numberDiff line change
@@ -4159,6 +4159,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
41594159
def methodStr = methPart(tree).symbol.showLocated
41604160
if matchingApply(wtp, pt) then
41614161
migrate(contextBoundParams(tree, wtp, pt))
4162+
migrate(implicitParams(tree, wtp, pt))
41624163
if needsTupledDual(wtp, pt) then adapt(tree, pt.tupledDual, locked)
41634164
else tree
41644165
else if wtp.isContextualMethod then

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

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ class CompilationTests {
8282
compileFile("tests/rewrites/infix-named-args.scala", defaultOptions.and("-rewrite", "-source:3.6-migration")),
8383
compileFile("tests/rewrites/ambigious-named-tuple-assignment.scala", defaultOptions.and("-rewrite", "-source:3.6-migration")),
8484
compileFile("tests/rewrites/i21382.scala", defaultOptions.and("-indent", "-rewrite")),
85+
compileFile("tests/rewrites/i22440.scala", defaultOptions.and("-rewrite"))
8586
).checkRewrites()
8687
}
8788

Diff for: tests/neg/i22440.check

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
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.

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/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

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
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.

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)