Skip to content

Commit 52c9fa8

Browse files
committed
chore: implicit parameters should warn at call site
1 parent 81e057a commit 52c9fa8

File tree

10 files changed

+43
-0
lines changed

10 files changed

+43
-0
lines changed

compiler/src/dotty/tools/dotc/config/MigrationVersion.scala

Lines changed: 1 addition & 0 deletions
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

compiler/src/dotty/tools/dotc/typer/Migrations.scala

Lines changed: 12 additions & 0 deletions
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

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 1 addition & 0 deletions
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

compiler/test/dotty/tools/dotc/CompilationTests.scala

Lines changed: 1 addition & 0 deletions
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

tests/neg/i22440.check

Lines changed: 5 additions & 0 deletions
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.

tests/neg/i22440.scala

Lines changed: 4 additions & 0 deletions
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

tests/rewrites/i22440.check

Lines changed: 5 additions & 0 deletions
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)

tests/rewrites/i22440.scala

Lines changed: 5 additions & 0 deletions
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)

tests/warn/i22440.check

Lines changed: 5 additions & 0 deletions
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.

tests/warn/i22440.scala

Lines changed: 4 additions & 0 deletions
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)