Skip to content

Commit

Permalink
Remove empty argument lists for classes with only context bounds (#21513
Browse files Browse the repository at this point in the history
)

Closes #21418
  • Loading branch information
WojciechMazur authored Sep 3, 2024
2 parents f774497 + 40c083d commit 5730f91
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
6 changes: 6 additions & 0 deletions compiler/src/dotty/tools/dotc/typer/Migrations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ trait Migrations:
em"""Context bounds will map to context parameters.
|A `using` clause is needed to pass explicit arguments to them.$rewriteMsg""",
tree.srcPos, mversion)
tree match
case Apply(ta @ TypeApply(Select(New(_), _), _), Nil) =>
// Remove empty arguments for calls to new that may precede the context bound.
// They are no longer necessary.
patch(Span(ta.span.end, pt.args.head.span.start - 1), "")
case _ => ()
if mversion.needsPatch && pt.args.nonEmpty then
patch(Span(pt.args.head.span.start), "using ")
end contextBoundParams
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 @@ -77,6 +77,7 @@ class CompilationTests {
compileFile("tests/rewrites/i17399.scala", unindentOptions.and("-rewrite")),
compileFile("tests/rewrites/i20002.scala", defaultOptions.and("-indent", "-rewrite")),
compileDir("tests/rewrites/annotation-named-pararamters", defaultOptions.and("-rewrite", "-source:3.6-migration")),
compileFile("tests/rewrites/i21418.scala", unindentOptions.and("-rewrite", "-source:3.5-migration")),
).checkRewrites()
}

Expand Down
26 changes: 26 additions & 0 deletions tests/rewrites/i21418.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
trait Effect[F[_]]
class Countdown[F[_]: Effect]
class Countdown1[F[_]: Effect](howMany: Int)
class Countdown2[F[_]: Effect, F2[_]: Effect]

def foo[F[_]: Effect]() =
"foo"

@main def Test = {
val a = new Countdown[Option](using ???)
Countdown[Option](using ???)
val b = Countdown[Option](using ???)
new Countdown[Option](using ???)
val c = Countdown[List](using ???)
new Countdown2[List, Option](using ???, ???)
new Countdown2[List, Option] (using ???, ???)
Countdown2[List, Option](using ???, ???)
Countdown2[List, Option] (using ???, ???)
new Countdown1[Option](10)(using ???)
new Array[Int](10)
new scala.collection.immutable.HashSet[Int]
new scala.collection.immutable.HashSet[Int]()
new scala.collection.immutable.HashSet[Int] ()
foo()(using ???)
foo() (using ???)
}
26 changes: 26 additions & 0 deletions tests/rewrites/i21418.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
trait Effect[F[_]]
class Countdown[F[_]: Effect]
class Countdown1[F[_]: Effect](howMany: Int)
class Countdown2[F[_]: Effect, F2[_]: Effect]

def foo[F[_]: Effect]() =
"foo"

@main def Test = {
val a = new Countdown[Option]()(???)
Countdown[Option]()(???)
val b = Countdown[Option]()(???)
new Countdown[Option] ()(???)
val c = Countdown[List] () (???)
new Countdown2[List, Option] () (???, ???)
new Countdown2[List, Option] (using ???, ???)
Countdown2[List, Option] () (???, ???)
Countdown2[List, Option] (using ???, ???)
new Countdown1[Option](10)(???)
new Array[Int](10)
new scala.collection.immutable.HashSet[Int]
new scala.collection.immutable.HashSet[Int]()
new scala.collection.immutable.HashSet[Int] ()
foo()(???)
foo() (???)
}

0 comments on commit 5730f91

Please sign in to comment.