Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implicit parameters should warn at call site in >= 3.7 #22441

Merged
merged 3 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/ast/Desugar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,7 @@ object desugar {
val nu = vparamss.foldLeft(makeNew(classTypeRef)) { (nu, vparams) =>
val app = Apply(nu, vparams.map(refOfDef))
vparams match {
case vparam :: _ if vparam.mods.is(Given) || vparam.name.is(ContextBoundParamName) =>
case vparam :: _ if vparam.mods.isOneOf(GivenOrImplicit) || vparam.name.is(ContextBoundParamName) =>
app.setApplyKind(ApplyKind.Using)
case _ => app
}
Expand Down
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
15 changes: 15 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,19 @@ 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
|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)
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
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ class DottyLanguageServer extends LanguageServer
private def inProjectsSeeing(baseDriver: InteractiveDriver,
definitions: List[SourceTree],
symbols: List[Symbol]): List[(InteractiveDriver, Context, List[Symbol])] = {
val projects = projectsSeeing(definitions)(baseDriver.currentCtx)
val projects = projectsSeeing(definitions)(using baseDriver.currentCtx)
projects.toList.map { config =>
val remoteDriver = drivers(config)
val ctx = remoteDriver.currentCtx
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ trait WorksheetService { thisServer: DottyLanguageServer =>
val sendMessage = (pos: SourcePosition, msg: String) =>
client.publishOutput(WorksheetRunOutput(params.textDocument, range(pos).get, msg))

runWorksheet(driver, uri, sendMessage, cancelChecker)(driver.currentCtx)
runWorksheet(driver, uri, sendMessage, cancelChecker)(using driver.currentCtx)
cancelChecker.checkCanceled()
WorksheetRunResult(success = true)
} catch {
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
7 changes: 7 additions & 0 deletions tests/neg/i22440.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- 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.
| To disable the warning, please use the following option:
| "-Wconf:msg=Implicit parameters should be provided with a `using` clause:s"
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
4 changes: 4 additions & 0 deletions tests/pos/i22440.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//> using options "-Wconf:msg=Implicit parameters should be provided with a `using` clause:s"

def foo(implicit x: Int) = x
val _ = foo(1) // warn
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)
7 changes: 7 additions & 0 deletions tests/warn/i22440.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- 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.
| To disable the warning, please use the following option:
| "-Wconf:msg=Implicit parameters should be provided with a `using` clause:s"
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
Loading