Skip to content

Fix insertion of using in applications with trailing lambda syntax #22937

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

Merged
merged 2 commits into from
Apr 12, 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
14 changes: 12 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/Migrations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,24 @@ trait Migrations:
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)
// The application can only be rewritten if it uses parentheses syntax.
// See issue #22927 and related tests.
val hasParentheses =
ctx.source.content
.slice(tree.span.end, pt.args.head.span.start)
.exists(_ == '(')
val rewriteMsg =
if hasParentheses then
Message.rewriteNotice("This code", mversion.patchFrom)
else
""
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
if hasParentheses && mversion.needsPatch then
patch(Span(pt.args.head.span.start), "using ")
end implicitParams

Expand Down
4 changes: 3 additions & 1 deletion compiler/test/dotty/tools/dotc/CompilationTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ 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"))
compileFile("tests/rewrites/i22440.scala", defaultOptions.and("-rewrite")),
compileFile("tests/rewrites/i22731.scala", defaultOptions.and("-rewrite", "-source:3.7-migration")),
compileFile("tests/rewrites/i22731b.scala", defaultOptions.and("-rewrite", "-source:3.7-migration")),
).checkRewrites()
}

Expand Down
11 changes: 11 additions & 0 deletions tests/rewrites/i22731.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Input(x: String)

trait Decoder[T]

object Decoder:
inline def apply[T](implicit f: () => Unit): Decoder[T] = ???

object Input:
given Decoder[Input] = Decoder { () =>
Input("")
}
13 changes: 13 additions & 0 deletions tests/rewrites/i22731.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Input(x: String)

trait Decoder[T]

object Decoder {
inline def apply[T](implicit f: () => Unit): Decoder[T] = ???
}

object Input {
given Decoder[Input] = Decoder { () =>
Input("")
}
}
50 changes: 50 additions & 0 deletions tests/rewrites/i22731b.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
def foo(implicit f: () => Unit): Unit = ???
def bar(a: Int)(implicit f: () => Unit): Unit = ???

@main def main =
// `using` can automatically be added when the application is done with parentheses
foo ( using () => 43 )
foo ( using () =>
val x = 42
43
)
foo( using () =>
val x = 42
43
)
foo (using () =>
val x = 42
43
)
bar(1) ( using () =>
val x = 42
43 )

// `using` cannot automatically be added when the application is done with trailing lambda syntax
foo { () => 43 }
foo { () => val x = 42; 43 }
foo{ () => val x = 42; 43 }
foo {() => val x = 42; 43}
bar(1) { () =>
val x = 42
43 }
foo: () =>
43
foo : () =>
val x = 42
43
foo :() =>
val x = 42
43
foo
: () =>
val x = 42
43
foo
:
() =>
val x = 42
43
bar(1) : () =>
val x = 42
43
50 changes: 50 additions & 0 deletions tests/rewrites/i22731b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
def foo(implicit f: () => Unit): Unit = ???
def bar(a: Int)(implicit f: () => Unit): Unit = ???

@main def main =
// `using` can automatically be added when the application is done with parentheses
foo ( () => 43 )
foo ( () =>
val x = 42
43
)
foo( () =>
val x = 42
43
)
foo (() =>
val x = 42
43
)
bar(1) ( () =>
val x = 42
43 )

// `using` cannot automatically be added when the application is done with trailing lambda syntax
foo { () => 43 }
foo { () => val x = 42; 43 }
foo{ () => val x = 42; 43 }
foo {() => val x = 42; 43}
bar(1) { () =>
val x = 42
43 }
foo: () =>
43
foo : () =>
val x = 42
43
foo :() =>
val x = 42
43
foo
: () =>
val x = 42
43
foo
:
() =>
val x = 42
43
bar(1) : () =>
val x = 42
43
13 changes: 13 additions & 0 deletions tests/warn/i22731.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- Warning: tests/warn/i22731.scala:5:11 -------------------------------------------------------------------------------
5 | foo ( () => 43 ) // 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"
-- Warning: tests/warn/i22731.scala:7:6 --------------------------------------------------------------------------------
7 | foo { () => 43 } // warn
| ^^^^^^^^^^^^
| Implicit parameters should be provided with a `using` clause.
| To disable the warning, please use the following option:
| "-Wconf:msg=Implicit parameters should be provided with a `using` clause:s"
7 changes: 7 additions & 0 deletions tests/warn/i22731.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def foo(implicit f: () => Unit): Unit = ???

@main def main =
// `using` can automatically be added when the application is done with parentheses
foo ( () => 43 ) // warn
// `using` cannot automatically be added when the application is done with trailing lambda syntax
foo { () => 43 } // warn