Skip to content

Commit

Permalink
chore: implicit parameters should warn at call site
Browse files Browse the repository at this point in the history
  • Loading branch information
hamzaremmal committed Jan 29, 2025
1 parent 81e057a commit 8b1be6d
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ enum MigrationVersion(val warnFrom: SourceVersion, val errorFrom: SourceVersion)
case ParameterEnclosedByParenthesis extends MigrationVersion(future, future)
case XmlLiteral extends MigrationVersion(future, future)
case GivenSyntax extends MigrationVersion(future, never)
case ImplicitParamsWithoutUsing extends MigrationVersion(`3.7`, future)

require(warnFrom.ordinal <= errorFrom.ordinal)

Expand Down
12 changes: 12 additions & 0 deletions compiler/src/dotty/tools/dotc/typer/Migrations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,16 @@ trait Migrations:
patch(Span(pt.args.head.span.start), "using ")
end contextBoundParams

/** Report implicit parameter lists and rewrite implicit parameter list to contextual params */
def implicitParams(tree: Tree, tp: MethodOrPoly, pt: FunProto)(using Context): Unit =
val mversion = mv.ImplicitParamsWithoutUsing
if tp.companion == ImplicitMethodType && pt.applyKind != ApplyKind.Using && pt.args.nonEmpty then
val rewriteMsg = Message.rewriteNotice("This code", mversion.patchFrom)
report.errorOrMigrationWarning(
em"Implicit parameters should be provided with a `using` clause.$rewriteMsg",
pt.args.head.srcPos, mversion)
if mversion.needsPatch then
patch(Span(pt.args.head.span.start), "using ")
end implicitParams

end Migrations
1 change: 1 addition & 0 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4164,6 +4164,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
def methodStr = methPart(tree).symbol.showLocated
if matchingApply(wtp, pt) then
migrate(contextBoundParams(tree, wtp, pt))
migrate(implicitParams(tree, wtp, pt))
if needsTupledDual(wtp, pt) then adapt(tree, pt.tupledDual, locked)
else tree
else if wtp.isContextualMethod then
Expand Down
1 change: 1 addition & 0 deletions compiler/test/dotty/tools/dotc/CompilationTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class CompilationTests {
compileFile("tests/rewrites/ambiguous-named-tuple-assignment.scala", defaultOptions.and("-rewrite", "-source:3.6-migration")),
compileFile("tests/rewrites/i21382.scala", defaultOptions.and("-indent", "-rewrite")),
compileFile("tests/rewrites/unused.scala", defaultOptions.and("-rewrite", "-Wunused:all")),
compileFile("tests/rewrites/i22440.scala", defaultOptions.and("-rewrite"))
).checkRewrites()
}

Expand Down
2 changes: 1 addition & 1 deletion scaladoc/src/dotty/tools/scaladoc/api.scala
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ object Signature:
case class LinkToType(signature: Signature, dri: DRI, kind: Kind)

case class HierarchyGraph(edges: Seq[(LinkToType, LinkToType)], sealedNodes: Set[LinkToType] = Set.empty):
def vertecies: Seq[LinkToType] = edges.flatten((a, b) => Seq(a, b)).distinct
def vertecies: Seq[LinkToType] = edges.flatten(using (a, b) => Seq(a, b)).distinct
def verteciesWithId: Map[LinkToType, Int] = vertecies.zipWithIndex.toMap
def +(edge: (LinkToType, LinkToType)): HierarchyGraph = this ++ Seq(edge)
def ++(edges: Seq[(LinkToType, LinkToType)]): HierarchyGraph =
Expand Down
2 changes: 1 addition & 1 deletion staging/src/scala/quoted/staging/QuoteDriver.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private class QuoteDriver(appClassloader: ClassLoader) extends Driver:

val compiledExpr =
try
new QuoteCompiler().newRun(ctx).compileExpr(exprBuilder)
new QuoteCompiler().newRun(using ctx).compileExpr(exprBuilder)
catch case ex: dotty.tools.FatalError =>
val enrichedMessage =
s"""An unhandled exception was thrown in the staging compiler.
Expand Down
5 changes: 5 additions & 0 deletions tests/neg/i22440.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- Error: tests/neg/i22440.scala:4:12 ----------------------------------------------------------------------------------
4 |val _ = foo(1) // error
| ^
| Implicit parameters should be provided with a `using` clause.
| This code can be rewritten automatically under -rewrite -source 3.7-migration.
4 changes: 4 additions & 0 deletions tests/neg/i22440.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//> using options -source future

def foo(implicit x: Int) = x
val _ = foo(1) // error
5 changes: 5 additions & 0 deletions tests/rewrites/i22440.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//> using options -source 3.7-migration

def foo(implicit x: Int) = ()
val _ = foo(using 1)
val _ = foo (using 1)
5 changes: 5 additions & 0 deletions tests/rewrites/i22440.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//> using options -source 3.7-migration

def foo(implicit x: Int) = ()
val _ = foo(1)
val _ = foo (1)
5 changes: 5 additions & 0 deletions tests/warn/i22440.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- Warning: tests/warn/i22440.scala:4:12 -------------------------------------------------------------------------------
4 |val _ = foo(1) // warn
| ^
| Implicit parameters should be provided with a `using` clause.
| This code can be rewritten automatically under -rewrite -source 3.7-migration.
4 changes: 4 additions & 0 deletions tests/warn/i22440.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//> using options -source 3.7

def foo(implicit x: Int) = x
val _ = foo(1) // warn

0 comments on commit 8b1be6d

Please sign in to comment.